Class: PacketGen::Header::IPv6::Addr

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

Overview

IPv6 address, as a group of 8 2-byte words

Author:

  • Sylvain Daubert

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

1st 2-byte word of IPv6 address

Returns:

  • (Integer)

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

define_field :a1, Types::Int16

#a2Integer

2nd 2-byte word of IPv6 address

Returns:

  • (Integer)

26
# File 'lib/packetgen/header/ipv6/addr.rb', line 26

define_field :a2, Types::Int16

#a3Integer

3rd 2-byte word of IPv6 address

Returns:

  • (Integer)

30
# File 'lib/packetgen/header/ipv6/addr.rb', line 30

define_field :a3, Types::Int16

#a4Integer

4th 2-byte word of IPv6 address

Returns:

  • (Integer)

34
# File 'lib/packetgen/header/ipv6/addr.rb', line 34

define_field :a4, Types::Int16

#a5Integer

5th 2-byte word of IPv6 address

Returns:

  • (Integer)

38
# File 'lib/packetgen/header/ipv6/addr.rb', line 38

define_field :a5, Types::Int16

#a6Integer

6th 2-byte word of IPv6 address

Returns:

  • (Integer)

42
# File 'lib/packetgen/header/ipv6/addr.rb', line 42

define_field :a6, Types::Int16

#a7Integer

7th 2-byte word of IPv6 address

Returns:

  • (Integer)

46
# File 'lib/packetgen/header/ipv6/addr.rb', line 46

define_field :a7, Types::Int16

#a8Integer

8th 2-byte word of IPv6 address

Returns:

  • (Integer)

50
# File 'lib/packetgen/header/ipv6/addr.rb', line 50

define_field :a8, Types::Int16

Instance Method Details

#==(other) ⇒ Object


94
95
96
97
# File 'lib/packetgen/header/ipv6/addr.rb', line 94

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

#from_human(str) ⇒ self

Read a colon-delimited address

Parameters:

  • str (String)

Returns:

  • (self)

Raises:

  • (ArgumentError)

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/packetgen/header/ipv6/addr.rb', line 57

def from_human(str)
  return self if str.nil?

  addr = IPAddr.new(str)
  raise ArgumentError, 'string is not a IPv6 address' unless addr.ipv6?

  addri = addr.to_i
  self.a1 = addri >> 112
  self.a2 = addri >> 96 & 0xffff
  self.a3 = addri >> 80 & 0xffff
  self.a4 = addri >> 64 & 0xffff
  self.a5 = addri >> 48 & 0xffff
  self.a6 = addri >> 32 & 0xffff
  self.a7 = addri >> 16 & 0xffff
  self.a8 = addri & 0xffff
  self
end

#mcast?Boolean

Return true if this address is a multicast one

Returns:

  • (Boolean)

90
91
92
# File 'lib/packetgen/header/ipv6/addr.rb', line 90

def mcast?
  self.a1 & 0xff00 == 0xff00
end

#to_aArray<Integer>

Return an array of address 16-bit words

Returns:

  • (Array<Integer>)

84
85
86
# File 'lib/packetgen/header/ipv6/addr.rb', line 84

def to_a
  @fields.values
end

#to_humanString

Addr6 in human readable form (colon-delimited hex string)

Returns:

  • (String)

78
79
80
# File 'lib/packetgen/header/ipv6/addr.rb', line 78

def to_human
  IPAddr.new(to_a.map { |a| a.to_i.to_s(16) }.join(':')).to_s
end