Class: PacketFu::Octets

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/protos/ip/header.rb

Overview

Octets implements the addressing scheme for IP.

Header Definition

Int32 :ip_addr

Constant Summary collapse

IPV4_RE =
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ Octets

Returns a new instance of Octets.



14
15
16
17
# File 'lib/packetfu/protos/ip/header.rb', line 14

def initialize(args={})
  super(
  Int32.new(args[:ip_addr]))
end

Instance Attribute Details

#ip_addrObject

Returns the value of attribute ip_addr

Returns:

  • (Object)

    the current value of ip_addr



10
11
12
# File 'lib/packetfu/protos/ip/header.rb', line 10

def ip_addr
  @ip_addr
end

Instance Method Details

#o1Object

Returns the value for the first octet



77
78
79
# File 'lib/packetfu/protos/ip/header.rb', line 77

def o1
  (self.to_i >> 24) & 0xff
end

#o2Object

Returns the value for the second octet



82
83
84
# File 'lib/packetfu/protos/ip/header.rb', line 82

def o2
  (self.to_i >> 16) & 0xff
end

#o3Object

Returns the value for the third octet



87
88
89
# File 'lib/packetfu/protos/ip/header.rb', line 87

def o3
  (self.to_i >> 8) & 0xff
end

#o4Object

Returns the value for the fourth octet



92
93
94
# File 'lib/packetfu/protos/ip/header.rb', line 92

def o4
  self.to_i & 0xff
end

#octetsObject

Returns the IP address as 4 octets



66
67
68
69
70
71
72
73
74
# File 'lib/packetfu/protos/ip/header.rb', line 66

def octets
  addr = self.to_i
  [
    ((addr >> 24) & 0xff),
    ((addr >> 16) & 0xff),
    ((addr >> 8) & 0xff),
    (addr & 0xff)
  ]
end

#read(str) ⇒ Object

Reads a string to populate the object.



25
26
27
28
29
30
# File 'lib/packetfu/protos/ip/header.rb', line 25

def read(str)
  force_binary(str)
  return self if str.nil?
  self[:ip_addr].read str[0,4]
  self
end

#read_quad(str) ⇒ Object

Set the IP Address by reading a dotted-quad address.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/packetfu/protos/ip/header.rb', line 45

def read_quad(str)
  match = IPV4_RE.match(str)
  if match.nil?
    raise ArgumentError.new("str is not a valid IPV4 address")
  end
    a = match[1].to_i
    b = match[2].to_i
    c = match[3].to_i
    d = match[4].to_i
  unless (a >= 0 && a <= 255 &&
          b >= 0 && b <= 255 &&
          c >= 0 && c <= 255 &&
          d >= 0 && d <= 255)
    raise ArgumentError.new("str is not a valid IPV4 address")
  end

  self[:ip_addr].value = (a<<24) + (b<<16) + (c<<8) + d
  self
end

#to_iObject

Returns an address in numerical format.



40
41
42
# File 'lib/packetfu/protos/ip/header.rb', line 40

def to_i
  self[:ip_addr].to_i
end

#to_sObject

Returns the object in string form.



20
21
22
# File 'lib/packetfu/protos/ip/header.rb', line 20

def to_s
  [self[:ip_addr].to_i].pack("N")
end

#to_xObject

Returns an address in dotted-quad format.



33
34
35
36
37
# File 'lib/packetfu/protos/ip/header.rb', line 33

def to_x
  # This could be slightly faster if we reproduced the code in
  # 'octets()' and didn't have to map to strings.
  self.octets.map(&:to_s).join('.')
end