Class: FFI::Packets::Ip::Hdr

Inherits:
Struct
  • Object
show all
Includes:
DRY::NetStructHelper
Defined in:
lib/ffi/packets/ip.rb

Overview

IP header, without options

field :v_hl,    :uint8,   :desc => 'v=vers(. & 0xf0) / '+
                                   'hl=hdr len(. & 0x0f)'
field :tos,     :uint8,   :desc => 'type of service'
field :len,     :uint16,  :desc => 'total length (incl header)'
field :id,      :uint16,  :desc => 'identification'
field :off,     :uint16,  :desc => 'fragment offset and flags'
field :ttl,     :uint8,   :desc => 'time to live'
field :proto,   :uint8,   :desc => 'protocol'
field :sum,     :uint16,  :desc => 'checksum'
field :src,     :uint32,  :desc => 'source address'
field :dst,     :uint32,  :desc => 'destination address'

Defined Under Namespace

Modules: Tos

Constant Summary collapse

Proto =

Alias to Ip::Proto

Ip::Proto

Instance Method Summary collapse

Instance Method Details

#dstObject

Returns the destination IP address from the header as an IPv4 address string.



114
115
116
# File 'lib/ffi/packets/ip.rb', line 114

def dst
  Util.ipv4_ltoa( Util.ntohl( self[:dst] ))
end

#dst=(val) ⇒ Object

Sets destination IP address in the header from an IPv4 address string or 32-bit number.



107
108
109
110
# File 'lib/ffi/packets/ip.rb', line 107

def dst=(val)
  val = Util.ipv4_atol(val) if val.kind_of? String
  self[:dst] = Util.htonl(val)
end

#hlObject

Returns the value of the hl field. This field is a 4-bit value occupying the lower 4 bits of the ‘v_hl’ field.

This value is the size of the IP header (including opts if present) in 32-bit words. The byte size maximum is 15*4 - 60 bytes.



72
73
74
# File 'lib/ffi/packets/ip.rb', line 72

def hl
  self[:v_hl] & 0x0f
end

#hl=(val) ⇒ Object

Sets the value of the hl field. This field is a 4-bit value occupying the lower 4 bits of the ‘v_hl’ field.

This value is the size of the IP header (including opts if present) in 32-bit words. The byte size maximum is 15*4 - 60 bytes.

Raises:

  • (ArgumentError)


61
62
63
64
65
# File 'lib/ffi/packets/ip.rb', line 61

def hl=(val)
  raise(ArgumentError, "value for header length too high") if val > 0xf
  self[:v_hl] &= 0xf0
  self[:v_hl] += val
end

#lookup_protoObject



90
# File 'lib/ffi/packets/ip.rb', line 90

def lookup_proto; Proto[ self.proto ]; end

#lookup_tosObject



85
# File 'lib/ffi/packets/ip.rb', line 85

def lookup_tos;  Tos[ self.tos ]; end

#set_fields(params = nil) ⇒ Object

Overrides set_fields to supply a default value for the ‘v_hl’ field.

v  = 4
hl = 5 # number of 32-bit words - 20 bytes (size of hdr without opts)


51
52
53
54
# File 'lib/ffi/packets/ip.rb', line 51

def set_fields(params=nil)
  params ||= {}
  super({:v_hl => 0x45}.merge(params))
end

#srcObject

Returns the source IP address as an IPv4 address string as an IPv4 address string.



101
102
103
# File 'lib/ffi/packets/ip.rb', line 101

def src
  Util.ipv4_ltoa( Util.ntohl( self[:src] ))
end

#src=(val) ⇒ Object

Sets source IP address in the header from an IPv4 address string or 32-bit number.



94
95
96
97
# File 'lib/ffi/packets/ip.rb', line 94

def src=(val)
  val = Util.ipv4_atol(val) if val.kind_of? String
  self[:src] = Util.htonl(val)
end