Class: PacketGen::Header::IP::Addr
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- PacketGen::Header::IP::Addr
- 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 ⇒ Integer
IP address first byte.
-
#a2 ⇒ Integer
IP address seconf byte.
-
#a3 ⇒ Integer
IP address third byte.
-
#a4 ⇒ Integer
IP address fourth byte.
Attributes inherited from Base
Instance Method Summary collapse
-
#from_human(str) ⇒ self
Read a dotted address.
-
#to_human ⇒ String
Addr in human readable form (dotted format).
-
#to_i ⇒ Integer
Addr as an integer.
Methods inherited from Base
bind_header, #header_id, inherited, #ip_header, known_headers, #parse?, #protocol_name
Methods inherited from Types::Fields
#[], #[]=, #body=, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, #force_binary, inherited, #initialize, #inspect, #read, #sz, #to_h, #to_s
Constructor Details
This class inherits a constructor from PacketGen::Types::Fields
Instance Attribute Details
#a1 ⇒ Integer
Returns IP address first byte.
68 |
# File 'lib/packetgen/header/ip.rb', line 68 define_field :a1, Types::Int8 |
#a2 ⇒ Integer
Returns IP address seconf byte.
71 |
# File 'lib/packetgen/header/ip.rb', line 71 define_field :a2, Types::Int8 |
#a3 ⇒ Integer
Returns IP address third byte.
74 |
# File 'lib/packetgen/header/ip.rb', line 74 define_field :a3, Types::Int8 |
#a4 ⇒ Integer
Returns IP address fourth byte.
77 |
# File 'lib/packetgen/header/ip.rb', line 77 define_field :a4, Types::Int8 |
Instance Method Details
#from_human(str) ⇒ self
Read a dotted address
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/packetgen/header/ip.rb', line 84 def from_human(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 |
#to_human ⇒ String
Addr in human readable form (dotted format)
98 99 100 |
# File 'lib/packetgen/header/ip.rb', line 98 def to_human fields.map { |f| "#{self[f].to_i}" }.join('.') end |
#to_i ⇒ Integer
Addr as an integer
104 105 106 107 |
# File 'lib/packetgen/header/ip.rb', line 104 def to_i (self.a1 << 24) | (self.a2 << 16) | (self.a3 << 8) | self.a4 end |