Class: PacketGen::Header::OSPFv2
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- PacketGen::Header::OSPFv2
- Defined in:
- lib/packetgen/header/ospfv2.rb,
lib/packetgen/header/ospfv2/lsa.rb,
lib/packetgen/header/ospfv2/hello.rb,
lib/packetgen/header/ospfv2/ls_ack.rb,
lib/packetgen/header/ospfv2/ls_update.rb,
lib/packetgen/header/ospfv2/ls_request.rb,
lib/packetgen/header/ospfv2/lsa_header.rb,
lib/packetgen/header/ospfv2/db_description.rb
Overview
This class supports OSPFv2 (RFC 2328). A OSPFv2 header has the following format:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version # | Type | Packet length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Router ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Area ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | AuType |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Authentication |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Authentication |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
An OSPFv2 header consists of:
-
a #version field (Types::Int8),
-
a #type field (Types::Int8Enum),
-
a #length field (Types::Int16). The length includes the header,
-
a #router_id field (Types::Int32),
-
an #area_id field (Types::Int32),
-
a #checksum field (Types::Int16),
-
an #au_type field (Types::Int16Enum),
-
an #authentication field (Types::Int64),
-
and a #body (Types::String).
Create an OSPFv2 header
# standalone
ospf = PacketGen::Header::OSPFv2.new
# in a packet
pkt = PacketGen.gen('IP', src: source_ip).add('OSPFv2')
# make IP header correct for OSPF
pkt.ospfize
# or make it correct with specific destination address
pkt.ospfize(dst: :all_spf_routers)
# access to OSPF header
pkt.ospfv2 # => PacketGen::Header::OSPFv2
OSPFv2 attributes
ospf.version # => 2
ospf.type = 'LS_ACK' # or 5
ospf.length = 154
ospf.router_id = 0xc0a80001
ospf.area_id = 1
ospf.checksum = 0xabcd
ospf.au_type = 'NO_AUTH' # or 0
ospf.authentication = 0
OSPFv2 body
OSPFv2 #body should contain OSPF payload for given #type:
-
or LSAck.
Defined Under Namespace
Classes: ArrayOfExternal, ArrayOfLSA, ArrayOfLSR, ArrayOfLink, ArrayOfTosMetric, DbDescription, External, Hello, LSA, LSAASExternal, LSAHeader, LSANetwork, LSARouter, LSAck, LSR, LSRequest, LSUpdate, Link, TosMetric
Constant Summary collapse
- IP_PROTOCOL =
IP protocol number for OSPF
89
- TYPES =
OSPF packet types
{ 'HELLO' => 1, 'DB_DESCRIPTION' => 2, 'LS_REQUEST' => 3, 'LS_UPDATE' => 4, 'LS_ACK' => 5 }.freeze
- AU_TYPES =
Authentication types
{ 'NO_AUTH' => 0, 'PASSWORD' => 1, 'CRYPTO' => 2, 'CRYPTO_WITH_ESN' => 3 }.freeze
Instance Attribute Summary collapse
-
#area_id ⇒ Integer
32-bit area ID.
-
#au_type ⇒ Integer
16-bit authentication type.
-
#authentication ⇒ Integer
64-bit authentication data.
- #body ⇒ String, Base
-
#checksum ⇒ Integer
16-bit OSPF packet checksum.
-
#length ⇒ Integer
16-bit OSPF packet length.
-
#router_id ⇒ Integer
32-bit router ID.
-
#type ⇒ Integer
8-bit OSPF packet type.
-
#version ⇒ Integer
8-bit OSPF version.
Class Method Summary collapse
-
.define_options(hdr) ⇒ void
private
Helper class method to define an OSPFv2 options field.
Instance Method Summary collapse
- #added_to_packet(packet) ⇒ Object private
-
#calc_checksum ⇒ Integer
Compute checksum and set
checksum
field. -
#calc_length ⇒ Integer
Compute length and set
length
field. -
#human_au_type ⇒ String
Get human-readable AU type.
-
#human_type ⇒ String
Get human-readable type.
-
#ospfize(dst: nil) ⇒ void
Fixup IP header according to RFC 2328: * set TOS field to 0xc0, * optionally set destination address, * set TTL to 1 if destination is a mcast address.
Methods inherited from Base
bind, calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header
Methods included from PacketGen::Headerable
included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read
Methods inherited from Types::Fields
#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #initialize, #inspect, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field
Constructor Details
This class inherits a constructor from PacketGen::Header::Base
Instance Attribute Details
#area_id ⇒ Integer
32-bit area ID
113 |
# File 'lib/packetgen/header/ospfv2.rb', line 113 define_field :area_id, Types::Int32 |
#au_type ⇒ Integer
16-bit authentication type. Types are defined in AU_TYPES.
121 |
# File 'lib/packetgen/header/ospfv2.rb', line 121 define_field :au_type, Types::Int16Enum, enum: AU_TYPES |
#authentication ⇒ Integer
64-bit authentication data
125 |
# File 'lib/packetgen/header/ospfv2.rb', line 125 define_field :authentication, Types::Int64 |
#body ⇒ String, Base
128 |
# File 'lib/packetgen/header/ospfv2.rb', line 128 define_field :body, Types::String |
#checksum ⇒ Integer
16-bit OSPF packet checksum
117 |
# File 'lib/packetgen/header/ospfv2.rb', line 117 define_field :checksum, Types::Int16 |
#length ⇒ Integer
16-bit OSPF packet length
105 |
# File 'lib/packetgen/header/ospfv2.rb', line 105 define_field :length, Types::Int16 |
#router_id ⇒ Integer
32-bit router ID
109 |
# File 'lib/packetgen/header/ospfv2.rb', line 109 define_field :router_id, Types::Int32 |
Class Method Details
.define_options(hdr) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Helper class method to define an OSPFv2 options field.
160 161 162 163 164 |
# File 'lib/packetgen/header/ospfv2.rb', line 160 def self.(hdr) hdr.define_field :options, Types::Int8 hdr.define_bit_fields_on :options, :dn_opt, :o_opt, :dc_opt, :l_opt, :n_opt, :mc_opt, :e_opt, :mt_opt end |
Instance Method Details
#added_to_packet(packet) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method is used internally by PacketGen and should not be directly called
169 170 171 172 |
# File 'lib/packetgen/header/ospfv2.rb', line 169 def added_to_packet(packet) ospf_idx = packet.headers.size packet.instance_eval "def ospfize(**kwargs) @headers[#{ospf_idx}].ospfize(**kwargs); end" # def ospfize(**kwargs) @headers[2].ospfize(**kwargs); end end |
#calc_checksum ⇒ Integer
Compute checksum and set checksum
field
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/packetgen/header/ospfv2.rb', line 176 def calc_checksum # #authentication field is not used in checksum calculation, # so force it to 0 before checksumming saved_auth = self.authentication self.authentication = 0 sum = IP.sum16(self) self.checksum = IP.reduce_checksum(sum) # Restore #authentication value self.authentication = saved_auth self.checksum end |
#calc_length ⇒ Integer
Compute length and set length
field
205 206 207 |
# File 'lib/packetgen/header/ospfv2.rb', line 205 def calc_length self[:length].value = Base.calculate_and_set_length(self) end |
#human_au_type ⇒ String
Get human-readable AU type
199 200 201 |
# File 'lib/packetgen/header/ospfv2.rb', line 199 def human_au_type self[:au_type].to_human end |
#human_type ⇒ String
Get human-readable type
193 194 195 |
# File 'lib/packetgen/header/ospfv2.rb', line 193 def human_type self[:type].to_human end |
#ospfize(dst: nil) ⇒ void
This method returns an undefined value.
Fixup IP header according to RFC 2328:
-
set TOS field to 0xc0,
-
optionally set destination address,
-
set TTL to 1 if destination is a mcast address.
This method may be called as:
# first way
pkt.ospfv2.ospfize
# second way
pkt.ospfize
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/packetgen/header/ospfv2.rb', line 222 def ospfize(dst: nil) ip = ip_header(self) ip.tos = 0xc0 dst = case dst when :all_spf_routers '224.0.0.5' when :all_d_routers '224.0.0.6' else dst end ip.dst = dst unless dst.nil? ip.ttl = 1 if ip[:dst].mcast? end |