Class: PacketFu::AddrIpv6

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/protos/ipv6/header.rb

Overview

AddrIpv6 handles addressing for IPv6Header

Header Definition

Int32 :a1
Int32 :a2
Int32 :a3
Int32 :a4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

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

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ AddrIpv6

Returns a new instance of AddrIpv6.



17
18
19
20
21
22
23
# File 'lib/packetfu/protos/ipv6/header.rb', line 17

def initialize(args={})
  super(
    Int32.new(args[:a1]),
    Int32.new(args[:a2]),
    Int32.new(args[:a3]),
    Int32.new(args[:a4]))
end

Instance Attribute Details

#a1Object

Returns the value of attribute a1

Returns:

  • (Object)

    the current value of a1



13
14
15
# File 'lib/packetfu/protos/ipv6/header.rb', line 13

def a1
  @a1
end

#a2Object

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2



13
14
15
# File 'lib/packetfu/protos/ipv6/header.rb', line 13

def a2
  @a2
end

#a3Object

Returns the value of attribute a3

Returns:

  • (Object)

    the current value of a3



13
14
15
# File 'lib/packetfu/protos/ipv6/header.rb', line 13

def a3
  @a3
end

#a4Object

Returns the value of attribute a4

Returns:

  • (Object)

    the current value of a4



13
14
15
# File 'lib/packetfu/protos/ipv6/header.rb', line 13

def a4
  @a4
end

Instance Method Details

#read(str) ⇒ Object

Reads in a string and casts it as an IPv6 address



41
42
43
44
45
46
47
48
49
# File 'lib/packetfu/protos/ipv6/header.rb', line 41

def read(str)
  force_binary(str)
  return self if str.nil?
  self[:a1].read str[0,4]
  self[:a2].read str[4,4]
  self[:a3].read str[8,4]
  self[:a4].read str[12,4]
  self
end

#read_x(str) ⇒ Object

Reads in a colon-delimited hex string and casts it as an IPv6 address.



52
53
54
55
56
57
58
59
# File 'lib/packetfu/protos/ipv6/header.rb', line 52

def read_x(str)
  addr = IPAddr.new(str).to_i
  self[:a1]=Int32.new(addr >> 96)
  self[:a2]=Int32.new((addr & 0x00000000ffffffff0000000000000000) >> 64)
  self[:a3]=Int32.new((addr & 0x0000000000000000ffffffff00000000) >> 32)
  self[:a4]=Int32.new(addr & 0x000000000000000000000000ffffffff)
  self
end

#to_iObject

Returns the address as a fairly ginormous integer.



31
32
33
# File 'lib/packetfu/protos/ipv6/header.rb', line 31

def to_i
  (a1.to_i << 96) + (a2.to_i << 64) + (a3.to_i << 32) + a4.to_i
end

#to_sObject

Returns the address in string format.



26
27
28
# File 'lib/packetfu/protos/ipv6/header.rb', line 26

def to_s
  self.to_a.map {|x| x.to_s}.join
end

#to_xObject

Returns the address as a colon-delimited hex string.



36
37
38
# File 'lib/packetfu/protos/ipv6/header.rb', line 36

def to_x
  IPAddr.new(self.to_i, Socket::AF_INET6).to_s
end