Class: PacketGen::Header::IP::Addr
- Includes:
- StructFu
- Defined in:
- lib/packetgen/header/ip.rb
Overview
IP address, as a group of 4 bytes
Constant Summary collapse
- IPV4_ADDR_REGEX =
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
Instance Attribute Summary collapse
-
#a1 ⇒ Object
Returns the value of attribute a1.
-
#a2 ⇒ Object
Returns the value of attribute a2.
-
#a3 ⇒ Object
Returns the value of attribute a3.
-
#a4 ⇒ Object
Returns the value of attribute a4.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Addr
constructor
A new instance of Addr.
-
#parse(str) ⇒ self
Parse a dotted address.
-
#read(str) ⇒ self
Read a Addr from a string.
-
#to_i ⇒ Integer
Addr as an integer.
-
#to_x ⇒ String
Addr in human readable form (dotted format).
Methods included from StructFu
#body=, #clone, #set_endianness, #sz, #to_s, #typecast
Methods inherited from Struct
Constructor Details
#initialize(options = {}) ⇒ Addr
Returns a new instance of Addr.
26 27 28 29 30 31 32 |
# File 'lib/packetgen/header/ip.rb', line 26 def initialize(={}) super Int8.new([:a1]), Int8.new([:a2]), Int8.new([:a3]), Int8.new([:a4]) end |
Instance Attribute Details
#a1 ⇒ Object
Returns the value of attribute a1
16 17 18 |
# File 'lib/packetgen/header/ip.rb', line 16 def a1 @a1 end |
#a2 ⇒ Object
Returns the value of attribute a2
16 17 18 |
# File 'lib/packetgen/header/ip.rb', line 16 def a2 @a2 end |
#a3 ⇒ Object
Returns the value of attribute a3
16 17 18 |
# File 'lib/packetgen/header/ip.rb', line 16 def a3 @a3 end |
#a4 ⇒ Object
Returns the value of attribute a4
16 17 18 |
# File 'lib/packetgen/header/ip.rb', line 16 def a4 @a4 end |
Instance Method Details
#parse(str) ⇒ self
Parse a dotted address
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/packetgen/header/ip.rb', line 37 def parse(str) return self if str.nil? m = str.match(IPV4_ADDR_REGEX) if m self[:a1].read m[1].to_i self[:a2].read m[2].to_i self[:a3].read m[3].to_i self[:a4].read m[4].to_i end self end |
#read(str) ⇒ self
Read a Addr from a string
52 53 54 55 56 57 58 59 |
# File 'lib/packetgen/header/ip.rb', line 52 def read(str) return self if str.nil? raise ParseError, 'string too short for Eth' if str.size < self.sz force_binary str [:a1, :a2, :a3, :a4].each_with_index do |byte, i| self[byte].read str[i, 1] end end |
#to_i ⇒ Integer
Addr as an integer
74 75 76 77 |
# File 'lib/packetgen/header/ip.rb', line 74 def to_i (self.a1 << 24) | (self.a2 << 16) | (self.a3 << 8) | self.a4 end |
#to_x ⇒ String
Addr in human readable form (dotted format)
68 69 70 |
# File 'lib/packetgen/header/ip.rb', line 68 def to_x members.map { |m| "#{self[m].to_i}" }.join('.') end |