Class: PacketGen::Header::IP
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/ip.rb
Overview
A IP header consists of:
-
a first byte (#u8 of StructFu::Int8 type) composed of:
-
a total length (#length, StructFu::Int16 type),
-
a ID (#id,
Int16type), -
a #frag worg (
Int16) composed of:-
a 13-bit #fragment_offset field,
-
a Time-to-Live (#ttl) field (
Int8), -
a #protocol field (
Int8), -
a #checksum field (
Int16), -
a destination IP ddress (#dst,
Addrtype), -
and a #body (StructFu::String type).
Create a IP header
# standalone
ip = PacketGen::Header::IP.new
# in a packet
pkt = PacketGen.gen('IP')
# access to IP header
pkt.ip # => PacketGen::Header::IP
IP attributes
ip.u8 = 0x45
# the same as
ip.version = 4
ip.ihl = 5
ip.length = 0x43
ip.id = 0x1234
ip.frag = 0x2031
# the same as:
ip.flag_mf = true
ip.fragment_offset = 0x31
ip.flag_rsv? # => Boolean
ip.flag_df? # => Boolean
ip.flag_mf? # => Boolean
ip.ttl = 0x40
ip.protocol = 6
ip.checksum = 0xffff
ip.src = '127.0.0.1'
ip.src # => "127.0.0.1"
ip[:src] # => PacketGen::Header::IP::Addr
ip.dst = '127.0.0.2'
ip.body.read 'this is a body'
Defined Under Namespace
Classes: Addr
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#checksum ⇒ Integer
Getter for checksum attribute.
-
#dst ⇒ String
(also: #destination)
Get IP destination address.
-
#flag_df ⇒ Boolean
Don’t Fragment flag.
-
#flag_mf ⇒ Boolena
More Fragment flags.
-
#flag_rsv ⇒ Boolean
Reserved bit from flags.
-
#frag ⇒ Integer
Getter for frag attribute.
-
#fragment_offset ⇒ Integer
13-bit fragment offset.
-
#id ⇒ Integer
Getter for id attribute.
-
#ihl ⇒ Integer
4-bit IP header length attribute.
-
#length ⇒ Integer
Getter for length attribute.
-
#protocol ⇒ Integer
Getter for protocol attribute.
-
#src ⇒ String
(also: #source)
Get IP source address.
-
#tos ⇒ Integer
Getter for TOS attribute.
-
#ttl ⇒ Integer
Getter for ttl attribute.
-
#u8 ⇒ Object
Returns the value of attribute u8.
-
#version ⇒ Integer
4-bit version attribute.
Instance Method Summary collapse
- #calc_checksum ⇒ Integer
-
#calc_length ⇒ Integer
Compute length and set
lengthfield. -
#initialize(options = {}) ⇒ IP
constructor
A new instance of IP.
- #inspect ⇒ String
-
#pseudo_header_checksum ⇒ Integer
Get IP part of pseudo header checksum.
-
#read(str) ⇒ self
Read a IP header from a string.
-
#to_w(iface = nil) ⇒ void
Send IP packet on wire.
Methods included from HeaderClassMethods
bind_header, define_bit_fields_on, known_headers
Methods included from HeaderMethods
#header_id, #ip_header, #packet, #packet=, #protocol_name
Methods included from StructFu
#clone, #set_endianness, #sz, #to_s, #typecast
Methods inherited from Struct
Constructor Details
#initialize(options = {}) ⇒ IP
Returns a new instance of IP.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/packetgen/header/ip.rb', line 145 def initialize(={}) super Int8.new((([:version] || 4) << 4) | ([:ihl] || 5)), Int8.new([:tos] || 0), Int16.new([:length] || 20), Int16.new([:id] || rand(65535)), Int16.new([:frag] || 0), Int8.new([:ttl] || 64), Int8.new([:protocol]), Int16.new([:checksum] || 0), Addr.new.from_human([:src] || '127.0.0.1'), Addr.new.from_human([:dst] || '127.0.0.1'), StructFu::String.new.read([:body]) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def body @body end |
#checksum ⇒ Integer
Getter for checksum attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def checksum @checksum end |
#dst ⇒ String Also known as: destination
Get IP destination address
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def dst @dst end |
#flag_df ⇒ Boolean
Returns Don’t Fragment flag.
173 |
# File 'lib/packetgen/header/ip.rb', line 173 define_bit_fields_on :frag, :flag_rsv, :flag_df, :flag_mf, :fragment_offset, 13 |
#flag_mf ⇒ Boolena
Returns More Fragment flags.
173 |
# File 'lib/packetgen/header/ip.rb', line 173 define_bit_fields_on :frag, :flag_rsv, :flag_df, :flag_mf, :fragment_offset, 13 |
#flag_rsv ⇒ Boolean
Returns reserved bit from flags.
173 |
# File 'lib/packetgen/header/ip.rb', line 173 define_bit_fields_on :frag, :flag_rsv, :flag_df, :flag_mf, :fragment_offset, 13 |
#frag ⇒ Integer
Getter for frag attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def frag @frag end |
#fragment_offset ⇒ Integer
Returns 13-bit fragment offset.
173 |
# File 'lib/packetgen/header/ip.rb', line 173 define_bit_fields_on :frag, :flag_rsv, :flag_df, :flag_mf, :fragment_offset, 13 |
#id ⇒ Integer
Getter for id attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def id @id end |
#ihl ⇒ Integer
Returns 4-bit IP header length attribute.
163 |
# File 'lib/packetgen/header/ip.rb', line 163 define_bit_fields_on :u8, :version, 4, :ihl, 4 |
#length ⇒ Integer
Getter for length attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def length @length end |
#protocol ⇒ Integer
Getter for protocol attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def protocol @protocol end |
#src ⇒ String Also known as: source
Get IP source address
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def src @src end |
#tos ⇒ Integer
Getter for TOS attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def tos @tos end |
#ttl ⇒ Integer
Getter for ttl attribute
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def ttl @ttl end |
#u8 ⇒ Object
Returns the value of attribute u8
61 62 63 |
# File 'lib/packetgen/header/ip.rb', line 61 def u8 @u8 end |
#version ⇒ Integer
Returns 4-bit version attribute.
163 |
# File 'lib/packetgen/header/ip.rb', line 163 define_bit_fields_on :u8, :version, 4, :ihl, 4 |
Instance Method Details
#calc_checksum ⇒ Integer
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/packetgen/header/ip.rb', line 198 def calc_checksum checksum = (self[:u8].to_i << 8) | self.tos checksum += self.length checksum += self.id checksum += self.frag checksum += (self.ttl << 8) | self.protocol checksum += (self[:src].to_i >> 16) checksum += (self[:src].to_i & 0xffff) checksum += self[:dst].to_i >> 16 checksum += self[:dst].to_i & 0xffff checksum = (checksum & 0xffff) + (checksum >> 16) checksum = ~(checksum % 0xffff ) & 0xffff self[:checksum].value = (checksum == 0) ? 0xffff : checksum end |
#calc_length ⇒ Integer
Compute length and set length field
215 216 217 |
# File 'lib/packetgen/header/ip.rb', line 215 def calc_length self[:length].value = self.sz end |
#inspect ⇒ String
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/packetgen/header/ip.rb', line 360 def inspect str = Inspect.dashed_line(self.class, 2) shift = Inspect.shift_level(2) to_h.each do |attr, value| next if attr == :body str << Inspect.inspect_attribute(attr, value, 2) if attr == :u8 str << shift + Inspect::INSPECT_FMT_ATTR % ['', 'version', version] str << shift + Inspect::INSPECT_FMT_ATTR % ['', 'ihl', ihl] elsif attr == :frag flags = flag_rsv? ? %w(RSV) : [] flags << 'DF' if flag_df? flags << 'MF' if flag_mf? flags_str = flags.empty? ? 'none' : flags.join(',') str << shift + Inspect::INSPECT_FMT_ATTR % ['', 'flags', flags_str] foff = Inspect.int_dec_hex(fragment_offset, 4) str << shift + Inspect::INSPECT_FMT_ATTR % ['', 'frag_offset', foff] end end str end |
#pseudo_header_checksum ⇒ Integer
Get IP part of pseudo header checksum.
342 343 344 345 |
# File 'lib/packetgen/header/ip.rb', line 342 def pseudo_header_checksum checksum = self[:src].to_i + self[:dst].to_i (checksum >> 16) + (checksum & 0xffff) end |
#read(str) ⇒ self
Read a IP header from a string
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/packetgen/header/ip.rb', line 178 def read(str) return self if str.nil? raise ParseError, 'string too short for IP' if str.size < self.sz force_binary str self[:u8].read str[0, 1] self[:tos].read str[1, 1] self[:length].read str[2, 2] self[:id].read str[4, 2] self[:frag].read str[6, 2] self[:ttl].read str[8, 1] self[:protocol].read str[9, 1] self[:checksum].read str[10, 2] self[:src].read str[12, 4] self[:dst].read str[16, 4] self[:body].read str[20..-1] self 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 fields are set by kernel, so bad IP packets cannot be sent this way. To do so, use Eth#to_w.
353 354 355 356 357 |
# File 'lib/packetgen/header/ip.rb', line 353 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 end |