Class: PacketFu::Octets
- Includes:
- StructFu
- Defined in:
- lib/packetfu/protos/ip.rb
Overview
Octets implements the addressing scheme for IP.
Header Definition
Int8 :o1
Int8 :o2
Int8 :o3
Int8 :o4
Instance Attribute Summary collapse
-
#o1 ⇒ Object
Returns the value of attribute o1.
-
#o2 ⇒ Object
Returns the value of attribute o2.
-
#o3 ⇒ Object
Returns the value of attribute o3.
-
#o4 ⇒ Object
Returns the value of attribute o4.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Octets
constructor
A new instance of 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
#initialize(args = {}) ⇒ Octets
Returns a new instance of Octets.
15 16 17 18 19 20 21 |
# File 'lib/packetfu/protos/ip.rb', line 15 def initialize(args={}) super( Int8.new(args[:o1]), Int8.new(args[:o2]), Int8.new(args[:o3]), Int8.new(args[:o4])) end |
Instance Attribute Details
#o1 ⇒ Object
Returns the value of attribute o1
12 13 14 |
# File 'lib/packetfu/protos/ip.rb', line 12 def o1 @o1 end |
#o2 ⇒ Object
Returns the value of attribute o2
12 13 14 |
# File 'lib/packetfu/protos/ip.rb', line 12 def o2 @o2 end |
#o3 ⇒ Object
Returns the value of attribute o3
12 13 14 |
# File 'lib/packetfu/protos/ip.rb', line 12 def o3 @o3 end |
#o4 ⇒ Object
Returns the value of attribute o4
12 13 14 |
# File 'lib/packetfu/protos/ip.rb', line 12 def o4 @o4 end |
Instance Method Details
#read(str) ⇒ Object
Reads a string to populate the object.
29 30 31 32 33 34 35 36 37 |
# File 'lib/packetfu/protos/ip.rb', line 29 def read(str) force_binary(str) return self if str.nil? self[:o1].read str[0,1] self[:o2].read str[1,1] self[:o3].read str[2,1] self[:o4].read str[3,1] self end |
#read_quad(str) ⇒ Object
Set the IP Address by reading a dotted-quad address.
52 53 54 |
# File 'lib/packetfu/protos/ip.rb', line 52 def read_quad(str) read([IPAddr.new(str).to_i].pack("N")) end |
#to_i ⇒ Object
Returns an address in numerical format.
46 47 48 49 |
# File 'lib/packetfu/protos/ip.rb', line 46 def to_i ip_str = [o1, o2, o3, o4].map {|x| x.to_i.to_s}.join('.') IPAddr.new(ip_str).to_i end |
#to_s ⇒ Object
Returns the object in string form.
24 25 26 |
# File 'lib/packetfu/protos/ip.rb', line 24 def to_s self.to_a.map {|x| x.to_s}.join end |
#to_x ⇒ Object
Returns an address in dotted-quad format.
40 41 42 43 |
# File 'lib/packetfu/protos/ip.rb', line 40 def to_x ip_str = [o1, o2, o3, o4].map {|x| x.to_i.to_s}.join('.') IPAddr.new(ip_str).to_s end |