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

Inherits:
Base show all
Defined in:
lib/packetgen/header/ipv6.rb

Overview

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

Author:

  • Sylvain Daubert

Instance Attribute Summary collapse

Attributes inherited from Base

#packet

Instance Method Summary collapse

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

#a1Integer

1st 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a1, Types::Int16

#a2Integer

2nd 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a2, Types::Int16

#a3Integer

3rd 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a3, Types::Int16

#a4Integer

4th 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a4, Types::Int16

#a5Integer

5th 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a5, Types::Int16

#a6Integer

6th 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a6, Types::Int16

#a7Integer

7th 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a7, Types::Int16

#a8Integer

8th 2-byte word of IPv6 address

Returns:

  • (Integer)


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

define_field :a8, Types::Int16

Instance Method Details

#from_human(str) ⇒ self

Read a colon-delimited address

Parameters:

  • str (String)

Returns:

  • (self)

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/packetgen/header/ipv6.rb', line 88

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

#to_aArray<Integer>

Return an array of address 16-bit words

Returns:

  • (Array<Integer>)


112
113
114
# File 'lib/packetgen/header/ipv6.rb', line 112

def to_a
  @fields.values
end

#to_humanString

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

Returns:

  • (String)


106
107
108
# File 'lib/packetgen/header/ipv6.rb', line 106

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