Class: PacketGen::Header::IP::Addr
- Inherits:
-
Types::Fields
- Object
- Types::Fields
- PacketGen::Header::IP::Addr
- Defined in:
- lib/packetgen/header/ip/addr.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.
Instance Method Summary collapse
-
#from_human(str) ⇒ self
Read a dotted address.
-
#mcast? ⇒ Boolean
Return true if this address is a multicast one.
-
#to_human ⇒ String
Addr in human readable form (dotted format).
-
#to_i ⇒ Integer
Addr as an integer.
Methods inherited from Types::Fields
#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #initialize, #inspect, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field
Constructor Details
This class inherits a constructor from PacketGen::Types::Fields
Instance Attribute Details
#a1 ⇒ Integer
Returns IP address first byte.
16 |
# File 'lib/packetgen/header/ip/addr.rb', line 16 define_field :a1, Types::Int8 |
#a2 ⇒ Integer
Returns IP address seconf byte.
19 |
# File 'lib/packetgen/header/ip/addr.rb', line 19 define_field :a2, Types::Int8 |
Instance Method Details
#from_human(str) ⇒ self
Read a dotted address
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/packetgen/header/ip/addr.rb', line 32 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 |
#mcast? ⇒ Boolean
Return true if this address is a multicast one
60 61 62 |
# File 'lib/packetgen/header/ip/addr.rb', line 60 def mcast? self.a1 >= 224 && self.a1 <= 239 end |
#to_human ⇒ String
Addr in human readable form (dotted format)
47 48 49 |
# File 'lib/packetgen/header/ip/addr.rb', line 47 def to_human fields.map { |f| self[f].to_i.to_s }.join('.') end |
#to_i ⇒ Integer
Addr as an integer
53 54 55 56 |
# File 'lib/packetgen/header/ip/addr.rb', line 53 def to_i (self.a1 << 24) | (self.a2 << 16) | (self.a3 << 8) | self.a4 end |