class PacketFu::Octets
Octets implements the addressing scheme for IP.
Header Definition
Int32 :ip_addr
Constants
- IPV4_RE
Public Class Methods
new(args={})
click to toggle source
Calls superclass method
# File lib/packetfu/protos/ip/header.rb, line 13 def initialize(args={}) super( Int32.new(args[:ip_addr])) end
Public Instance Methods
o1()
click to toggle source
Returns the value for the first octet
# File lib/packetfu/protos/ip/header.rb, line 76 def o1 (self.to_i >> 24) & 0xff end
o2()
click to toggle source
Returns the value for the second octet
# File lib/packetfu/protos/ip/header.rb, line 81 def o2 (self.to_i >> 16) & 0xff end
o3()
click to toggle source
Returns the value for the third octet
# File lib/packetfu/protos/ip/header.rb, line 86 def o3 (self.to_i >> 8) & 0xff end
o4()
click to toggle source
Returns the value for the fourth octet
# File lib/packetfu/protos/ip/header.rb, line 91 def o4 self.to_i & 0xff end
octets()
click to toggle source
Returns the IP address as 4 octets
# File lib/packetfu/protos/ip/header.rb, line 65 def octets addr = self.to_i [ ((addr >> 24) & 0xff), ((addr >> 16) & 0xff), ((addr >> 8) & 0xff), (addr & 0xff) ] end
read(str)
click to toggle source
Reads a string to populate the object.
# File lib/packetfu/protos/ip/header.rb, line 24 def read(str) force_binary(str) return self if str.nil? self[:ip_addr].read str[0,4] self end
read_quad(str)
click to toggle source
Set the IP Address by reading a dotted-quad address.
# File lib/packetfu/protos/ip/header.rb, line 44 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()
click to toggle source
Returns an address in numerical format.
# File lib/packetfu/protos/ip/header.rb, line 39 def to_i self[:ip_addr].to_i end
to_s()
click to toggle source
Returns the object in string form.
# File lib/packetfu/protos/ip/header.rb, line 19 def to_s [self[:ip_addr].to_i].pack("N") end
to_x()
click to toggle source
Returns an address in dotted-quad format.
# File lib/packetfu/protos/ip/header.rb, line 32 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