Class: PacketFu::Octets
- Includes:
- StructFu
- Defined in:
- lib/packetfu/protos/ip/header.rb
Overview
Constant Summary collapse
- IPV4_RE =
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
Instance Attribute Summary collapse
-
#ip_addr ⇒ Object
Returns the value of attribute ip_addr.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Octets
constructor
A new instance of Octets.
-
#o1 ⇒ Object
Returns the value for the first octet.
-
#o2 ⇒ Object
Returns the value for the second octet.
-
#o3 ⇒ Object
Returns the value for the third octet.
-
#o4 ⇒ Object
Returns the value for the fourth octet.
-
#octets ⇒ Object
Returns the IP address as 4 octets.
-
#read(str) ⇒ Object
Reads a string to populate the object.
-
#read_quad(str) ⇒ Object
Set the IP Address by reading a dotted-quad address.
-
#to_i ⇒ Object
Returns an address in numerical format.
-
#to_s ⇒ Object
Returns the object in string form.
-
#to_x ⇒ Object
Returns an address in dotted-quad format.
Methods included from StructFu
#body=, #clone, #set_endianness, #sz, #typecast
Methods inherited from Struct
Constructor Details
Instance Attribute Details
#ip_addr ⇒ Object
Returns the value of attribute ip_addr
10 11 12 |
# File 'lib/packetfu/protos/ip/header.rb', line 10 def ip_addr @ip_addr end |
Instance Method Details
#o1 ⇒ Object
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 |
#o2 ⇒ Object
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 |
#o3 ⇒ Object
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 |
#o4 ⇒ Object
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 |
#octets ⇒ Object
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_i ⇒ Object
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_s ⇒ Object
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_x ⇒ Object
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 |