Class: PacketGen::Header::IP::Addr
- Inherits:
-
BinStruct::Struct
- Object
- BinStruct::Struct
- PacketGen::Header::IP::Addr
- Includes:
- BinStruct::Structable
- Defined in:
- lib/packetgen/header/ip/addr.rb
Overview
IP address, as a group of 4 bytes
Constant Summary collapse
- IPV4_ADDR_REGEX =
Regex to match IPv4 addresses
/^(\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 second byte.
-
#a3 ⇒ Integer
IP address third byte.
-
#a4 ⇒ Integer
IP address fourth byte.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality.
-
#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 a 32-bit integer.
Instance Attribute Details
#a1 ⇒ Integer
Returns IP address first byte.
19 |
# File 'lib/packetgen/header/ip/addr.rb', line 19 define_attr :a1, BinStruct::Int8 |
#a2 ⇒ Integer
Returns IP address second byte.
22 |
# File 'lib/packetgen/header/ip/addr.rb', line 22 define_attr :a2, BinStruct::Int8 |
#a3 ⇒ Integer
Returns IP address third byte.
25 |
# File 'lib/packetgen/header/ip/addr.rb', line 25 define_attr :a3, BinStruct::Int8 |
#a4 ⇒ Integer
Returns IP address fourth byte.
28 |
# File 'lib/packetgen/header/ip/addr.rb', line 28 define_attr :a4, BinStruct::Int8 |
Instance Method Details
#==(other) ⇒ Boolean
Check equality. Equal is other has same class and all attributes are equal.
71 72 73 74 |
# File 'lib/packetgen/header/ip/addr.rb', line 71 def ==(other) other.is_a?(self.class) && attributes.all? { |attr| self[attr].value == other[attr].value } end |
#from_human(str) ⇒ self
Read a dotted address
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/packetgen/header/ip/addr.rb', line 36 def from_human(str) return self if str.nil? m = str.match(IPV4_ADDR_REGEX) if m self[:a1].from_human(m[1].to_i) self[:a2].from_human(m[2].to_i) self[:a3].from_human(m[3].to_i) self[:a4].from_human(m[4].to_i) end self end |
#mcast? ⇒ Boolean
Return true if this address is a multicast one
64 65 66 |
# File 'lib/packetgen/header/ip/addr.rb', line 64 def mcast? self.a1 >= 224 && self.a1 <= 239 end |
#to_human ⇒ String
Addr in human readable form (dotted format)
51 52 53 |
# File 'lib/packetgen/header/ip/addr.rb', line 51 def to_human attributes.map { |f| self[f].to_i.to_s }.join('.') end |
#to_i ⇒ Integer
Addr as a 32-bit integer
57 58 59 60 |
# File 'lib/packetgen/header/ip/addr.rb', line 57 def to_i (self.a1 << 24) | (self.a2 << 16) | (self.a3 << 8) | self.a4 end |