Class: PacketGen::Header::IP::Addr

Inherits:
Types::Fields show all
Includes:
Types::Fieldable
Defined in:
lib/packetgen/header/ip/addr.rb

Overview

IP address, as a group of 4 bytes

Author:

  • Sylvain Daubert

Constant Summary collapse

IPV4_ADDR_REGEX =
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types::Fieldable

#format_inspect, #read, #sz, #to_s, #type_name

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

#a1Integer

Returns IP address first byte.

Returns:

  • (Integer)

    IP address first byte



19
# File 'lib/packetgen/header/ip/addr.rb', line 19

define_field :a1, Types::Int8

#a2Integer

Returns IP address seconf byte.

Returns:

  • (Integer)

    IP address seconf byte



22
# File 'lib/packetgen/header/ip/addr.rb', line 22

define_field :a2, Types::Int8

#a3Integer

Returns IP address third byte.

Returns:

  • (Integer)

    IP address third byte



25
# File 'lib/packetgen/header/ip/addr.rb', line 25

define_field :a3, Types::Int8

#a4Integer

Returns IP address fourth byte.

Returns:

  • (Integer)

    IP address fourth byte



28
# File 'lib/packetgen/header/ip/addr.rb', line 28

define_field :a4, Types::Int8

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
# File 'lib/packetgen/header/ip/addr.rb', line 67

def ==(other)
  other.is_a?(self.class) &&
    fields.all? { |attr| self[attr].value == other[attr].value }
end

#from_human(str) ⇒ self

Read a dotted address

Parameters:

  • str (String)

Returns:

  • (self)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/packetgen/header/ip/addr.rb', line 35

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

Returns:

  • (Boolean)


63
64
65
# File 'lib/packetgen/header/ip/addr.rb', line 63

def mcast?
  self.a1 >= 224 && self.a1 <= 239
end

#to_humanString

Addr in human readable form (dotted format)

Returns:

  • (String)


50
51
52
# File 'lib/packetgen/header/ip/addr.rb', line 50

def to_human
  fields.map { |f| self[f].to_i.to_s }.join('.')
end

#to_iInteger

Addr as an integer

Returns:

  • (Integer)


56
57
58
59
# File 'lib/packetgen/header/ip/addr.rb', line 56

def to_i
  (self.a1 << 24) | (self.a2 << 16) | (self.a3 << 8) |
    self.a4
end