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


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

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


11
12
13
# File 'lib/packetfu/protos/ipv6/header.rb', line 11

def a1
  @a1
end

#a2Object

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2


11
12
13
# File 'lib/packetfu/protos/ipv6/header.rb', line 11

def a2
  @a2
end

#a3Object

Returns the value of attribute a3

Returns:

  • (Object)

    the current value of a3


11
12
13
# File 'lib/packetfu/protos/ipv6/header.rb', line 11

def a3
  @a3
end

#a4Object

Returns the value of attribute a4

Returns:

  • (Object)

    the current value of a4


11
12
13
# File 'lib/packetfu/protos/ipv6/header.rb', line 11

def a4
  @a4
end

Instance Method Details

#read(str) ⇒ Object

Reads in a string and casts it as an IPv6 address


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

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.


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

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.


29
30
31
# File 'lib/packetfu/protos/ipv6/header.rb', line 29

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.


24
25
26
# File 'lib/packetfu/protos/ipv6/header.rb', line 24

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

#to_xObject

Returns the address as a colon-delimited hex string.


34
35
36
# File 'lib/packetfu/protos/ipv6/header.rb', line 34

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