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

Inherits:
Struct
  • Object
show all
Includes:
StructFu
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

Instance Method Summary collapse

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #to_s, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(options = {}) ⇒ Addr

Returns a new instance of Addr.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :a1 (Integer)
  • :a2 (Integer)
  • :a3 (Integer)
  • :a4 (Integer)
  • :a5 (Integer)
  • :a6 (Integer)
  • :a7 (Integer)
  • :a8 (Integer)


29
30
31
32
33
34
35
36
37
38
# File 'lib/packetgen/header/ipv6.rb', line 29

def initialize(options={})
  super Int16.new(options[:a1]),
        Int16.new(options[:a2]),
        Int16.new(options[:a3]),
        Int16.new(options[:a4]),
        Int16.new(options[:a5]),
        Int16.new(options[:a6]),
        Int16.new(options[:a7]),
        Int16.new(options[:a8])
end

Instance Attribute Details

#a1Object

Returns the value of attribute a1

Returns:

  • (Object)

    the current value of a1



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a1
  @a1
end

#a2Object

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a2
  @a2
end

#a3Object

Returns the value of attribute a3

Returns:

  • (Object)

    the current value of a3



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a3
  @a3
end

#a4Object

Returns the value of attribute a4

Returns:

  • (Object)

    the current value of a4



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a4
  @a4
end

#a5Object

Returns the value of attribute a5

Returns:

  • (Object)

    the current value of a5



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a5
  @a5
end

#a6Object

Returns the value of attribute a6

Returns:

  • (Object)

    the current value of a6



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a6
  @a6
end

#a7Object

Returns the value of attribute a7

Returns:

  • (Object)

    the current value of a7



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a7
  @a7
end

#a8Object

Returns the value of attribute a8

Returns:

  • (Object)

    the current value of a8



17
18
19
# File 'lib/packetgen/header/ipv6.rb', line 17

def a8
  @a8
end

Instance Method Details

#parse(str) ⇒ self

Parse a colon-delimited address

Parameters:

Returns:

  • (self)

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/packetgen/header/ipv6.rb', line 43

def parse(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

#read(str) ⇒ self

Read a Addr6 from a binary string

Parameters:

Returns:

  • (self)


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

def read(str)
  force_binary str
  self[:a1].read str[0, 2]
  self[:a2].read str[2, 2]
  self[:a3].read str[4, 2]
  self[:a4].read str[6, 2]
  self[:a5].read str[8, 2]
  self[:a6].read str[10, 2]
  self[:a7].read str[12, 2]
  self[:a8].read str[14, 2]
  self
end

#to_xString

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

Returns:



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

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