Class: PacketGen::Header::IP
- Defined in:
- lib/packetgen/header/ip.rb,
lib/packetgen/header/ip.rb,
lib/packetgen/header/ip/addr.rb,
lib/packetgen/header/ip/option.rb,
lib/packetgen/header/ip/options.rb
Overview
IP protocol (RFC 791)
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| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
A IP header consists of:
-
a first byte (#u8 of
BinStruct::Int8
type) composed of: -
a Type of Service field (#tos,
BinStruct::Int8
type), -
a total length (#length,
BinStruct::Int16
type), -
a ID (#id,
BinStruct::Int16
type), -
a #frag worg (
BinStruct::Int16
) composed of:-
a 13-bit #fragment_offset field,
-
a Time-to-Live (#ttl) field (
Int8
), -
a #protocol field (
BinStruct::Int8
), -
a #checksum field (
BinStruct::Int16
), -
and a #body (
BinStruct::String
type).
Defined Under Namespace
Classes: Addr, ArrayOfAddr, EOL, LSRR, NOP, Option, Options, RA, RR, SI, SSRR
Constant Summary collapse
- ETHERTYPE =
IP Ether type
0x0800
Instance Attribute Summary collapse
-
#body ⇒ BinStruct::String, Headerable
IP body.
-
#checksum ⇒ Integer
16-bit IP header checksum.
-
#dst ⇒ Addr
destination IP address.
-
#flag_df ⇒ Boolean
Don’t Fragment flag.
-
#flag_mf ⇒ Boolean
More Fragment flags.
-
#flag_rsv ⇒ Boolean
reserved bit from flags.
-
#frag ⇒ Integer
16-bit frag word.
-
#fragment_offset ⇒ Integer
13-bit fragment offset.
-
#id ⇒ Integer
16-bit ID.
-
#ihl ⇒ Integer
4-bit IP header length attribute, as 32-bit word count.
-
#length ⇒ Integer
16-bit IP total length, including this header.
-
#options ⇒ Options
IP options.
-
#protocol ⇒ Integer
8-bit upper protocol.
-
#src ⇒ Addr
source IP address.
-
#tos ⇒ Integer
8-bit Type of Service self.
-
#ttl ⇒ Integer
8-bit Time To Live.
-
#u8 ⇒ Integer
First byte of IP header.
-
#version ⇒ Integer
4-bit version attribute.
Class Method Summary collapse
-
.reduce_checksum(checksum) ⇒ Integer
Helper method to reduce an IP checksum.
-
.sum16(hdr) ⇒ Integer
Helper method to compute sum of 16-bit words.
Instance Method Summary collapse
-
#calc_checksum ⇒ Integer
Compute checksum and set
checksum
field. -
#calc_length ⇒ Integer
Compute and set
length
andihl
field. -
#parse? ⇒ Boolean
Check version field.
-
#pseudo_header_checksum ⇒ Integer
Get IP part of pseudo header checksum.
-
#reply! ⇒ self
Invert source and destination addresses.
-
#to_s ⇒ Object
Get binary string.
-
#to_w(_iface = nil) ⇒ void
Send IP packet on wire.
Methods inherited from Base
bind, calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header
Methods included from PacketGen::Headerable
#added_to_packet, included, #method_name, #packet, #packet=, #protocol_name, #read
Constructor Details
This class inherits a constructor from PacketGen::Header::Base
Instance Attribute Details
#body ⇒ BinStruct::String, Headerable
IP body
166 |
# File 'lib/packetgen/header/ip.rb', line 166 define_attr :body, BinStruct::String |
#checksum ⇒ Integer
16-bit IP header checksum
148 |
# File 'lib/packetgen/header/ip.rb', line 148 define_attr :checksum, BinStruct::Int16, default: 0 |
#dst ⇒ Addr
destination IP address
156 |
# File 'lib/packetgen/header/ip.rb', line 156 define_attr :dst, Addr, default: '127.0.0.1' |
#flag_df ⇒ Boolean
Don’t Fragment flag
136 |
# File 'lib/packetgen/header/ip.rb', line 136 define_bit_attr :frag, flag_rsv: 1, flag_df: 1, flag_mf: 1, fragment_offset: 13 |
#flag_mf ⇒ Boolean
More Fragment flags
136 |
# File 'lib/packetgen/header/ip.rb', line 136 define_bit_attr :frag, flag_rsv: 1, flag_df: 1, flag_mf: 1, fragment_offset: 13 |
#flag_rsv ⇒ Boolean
reserved bit from flags
136 |
# File 'lib/packetgen/header/ip.rb', line 136 define_bit_attr :frag, flag_rsv: 1, flag_df: 1, flag_mf: 1, fragment_offset: 13 |
#frag ⇒ Integer
16-bit frag word
136 |
# File 'lib/packetgen/header/ip.rb', line 136 define_bit_attr :frag, flag_rsv: 1, flag_df: 1, flag_mf: 1, fragment_offset: 13 |
#fragment_offset ⇒ Integer
13-bit fragment offset
136 |
# File 'lib/packetgen/header/ip.rb', line 136 define_bit_attr :frag, flag_rsv: 1, flag_df: 1, flag_mf: 1, fragment_offset: 13 |
#id ⇒ Integer
Returns 16-bit ID.
120 |
# File 'lib/packetgen/header/ip.rb', line 120 define_attr :id, BinStruct::Int16, default: ->(_) { rand(65_535) } |
#ihl ⇒ Integer
4-bit IP header length attribute, as 32-bit word count. Default to 5 (IP header without option).
110 |
# File 'lib/packetgen/header/ip.rb', line 110 define_bit_attr :u8, default: 0x45, version: 4, ihl: 4 |
#length ⇒ Integer
16-bit IP total length, including this header.
117 |
# File 'lib/packetgen/header/ip.rb', line 117 define_attr :length, BinStruct::Int16, default: 20 |
#options ⇒ Options
IP options
161 162 |
# File 'lib/packetgen/header/ip.rb', line 161 define_attr :options, Options, optional: ->(h) { h.ihl > 5 }, builder: ->(h, t) { t.new(length_from: -> { (h.ihl - 5) * 4 }) } |
#protocol ⇒ Integer
8-bit upper protocol
144 |
# File 'lib/packetgen/header/ip.rb', line 144 define_attr :protocol, BinStruct::Int8 |
#src ⇒ Addr
source IP address
152 |
# File 'lib/packetgen/header/ip.rb', line 152 define_attr :src, Addr, default: '127.0.0.1' |
#tos ⇒ Integer
Returns 8-bit Type of Service self.
113 |
# File 'lib/packetgen/header/ip.rb', line 113 define_attr :tos, BinStruct::Int8, default: 0 |
#ttl ⇒ Integer
8-bit Time To Live
140 |
# File 'lib/packetgen/header/ip.rb', line 140 define_attr :ttl, BinStruct::Int8, default: 64 |
#u8 ⇒ Integer
110 |
# File 'lib/packetgen/header/ip.rb', line 110 define_bit_attr :u8, default: 0x45, version: 4, ihl: 4 |
#version ⇒ Integer
4-bit version attribute
110 |
# File 'lib/packetgen/header/ip.rb', line 110 define_bit_attr :u8, default: 0x45, version: 4, ihl: 4 |
Class Method Details
.reduce_checksum(checksum) ⇒ Integer
Helper method to reduce an IP checksum. This method:
-
checks a checksum is not greater than 0xffff. If it is, reduces it.
-
inverts reduced checksum.
-
forces checksum to 0xffff if computed checksum is 0.
197 198 199 200 201 |
# File 'lib/packetgen/header/ip.rb', line 197 def self.reduce_checksum(checksum) checksum = (checksum & 0xffff) + (checksum >> 16) while checksum > 0xffff checksum = ~checksum & 0xffff checksum.zero? ? 0xffff : checksum end |
.sum16(hdr) ⇒ Integer
Helper method to compute sum of 16-bit words. Used to compute IP-style checksums.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/packetgen/header/ip.rb', line 173 def self.sum16(hdr) old_checksum = nil if hdr.respond_to?(:checksum) old_checksum = hdr.checksum hdr.checksum = 0 end data = hdr.to_s data << "\x00" if data.size.odd? sum = data.unpack('n*').sum hdr.checksum = old_checksum if old_checksum sum end |
Instance Method Details
#calc_checksum ⇒ Integer
Compute checksum and set checksum
field
205 206 207 208 209 210 211 212 |
# File 'lib/packetgen/header/ip.rb', line 205 def calc_checksum # Checksum is only on header, so cannot use IP.sum16, # which also calculates checksum on #body. nb_words = ihl * 2 self.checksum = 0 checksum = to_s.unpack("n#{nb_words}").sum self[:checksum].value = IP.reduce_checksum(checksum) end |
#calc_length ⇒ Integer
Compute and set length
and ihl
field
217 218 219 220 |
# File 'lib/packetgen/header/ip.rb', line 217 def calc_length Base.calculate_and_set_length(self) self.ihl = 5 + self[:options].sz / 4 end |
#parse? ⇒ Boolean
Check version field
245 246 247 |
# File 'lib/packetgen/header/ip.rb', line 245 def parse? (version == 4) && (ihl >= 5) end |
#pseudo_header_checksum ⇒ Integer
Get IP part of pseudo header checksum.
224 225 226 227 |
# File 'lib/packetgen/header/ip.rb', line 224 def pseudo_header_checksum checksum = self[:src].to_i + self[:dst].to_i (checksum >> 16) + (checksum & 0xffff) end |
#reply! ⇒ self
Invert source and destination addresses
259 260 261 262 |
# File 'lib/packetgen/header/ip.rb', line 259 def reply! self[:src], self[:dst] = self[:dst], self[:src] self end |
#to_s ⇒ Object
Get binary string. Fixup IHL if needed (IP header has options, and IHL was not set by user).
251 252 253 254 |
# File 'lib/packetgen/header/ip.rb', line 251 def to_s self.ihl = 5 + self[:options].sz / 4 if self.ihl == 5 super end |
#to_w(_iface = nil) ⇒ void
This method returns an undefined value.
Send IP packet on wire.
When sending packet at IP level, checksum
and length
attributes are set by kernel, so bad IP packets cannot be sent this way. To do so, use Eth#to_w.
235 236 237 238 239 240 |
# File 'lib/packetgen/header/ip.rb', line 235 def to_w(_iface=nil) sock = Socket.new(Socket::AF_INET, Socket::SOCK_RAW, Socket::IPPROTO_RAW) sockaddrin = Socket.sockaddr_in(0, dst) sock.send(to_s, 0, sockaddrin) sock.close end |