Class: PacketFu::IPv6Header

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

Overview

IPv6Header is complete IPv6 struct, used in IPv6Packet.

Header Definition

Fixnum (4 bits)   :ipv6_v      Default: 6     # Versiom
Fixnum (8 bits)   :ipv6_class  Defualt: 0     # Class
Fixnum (20 bits)  :ipv6_label  Defualt: 0     # Label
Int16             :ipv6_len    Default: calc  # Payload length
Int8              :ipv6_next                  # Next Header
Int8              :ipv6_hop    Default: 0xff  # Hop limit
AddrIpv6          :ipv6_src
AddrIpv6          :ipv6_dst
String            :body

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ IPv6Header

Returns a new instance of IPv6Header


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/packetfu/protos/ipv6/header.rb', line 79

def initialize(args={})
  super(
    (args[:ipv6_v] || 6),
    (args[:ipv6_class] || 0),
    (args[:ipv6_label] || 0),
    Int16.new(args[:ipv6_len]),
    Int8.new(args[:ipv6_next]),
    Int8.new(args[:ipv6_hop] || 0xff),
    AddrIpv6.new.read(args[:ipv6_src] || ("\x00" * 16)),
    AddrIpv6.new.read(args[:ipv6_dst] || ("\x00" * 16)),
    StructFu::String.new.read(args[:body])
  )
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def body
  @body
end

#ipv6_classObject

Getter for the traffic class.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_class
  @ipv6_class
end

#ipv6_dstObject

Getter for the destination address.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_dst
  @ipv6_dst
end

#ipv6_hopObject

Getter for the hop limit.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_hop
  @ipv6_hop
end

#ipv6_labelObject

Getter for the flow label.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_label
  @ipv6_label
end

#ipv6_lenObject

Getter for the payload length.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_len
  @ipv6_len
end

#ipv6_nextObject

Getter for the next protocol header.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_next
  @ipv6_next
end

#ipv6_srcObject

Getter for the source address.


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_src
  @ipv6_src
end

#ipv6_vObject

Getter for the version (usually, 6).


74
75
76
# File 'lib/packetfu/protos/ipv6/header.rb', line 74

def ipv6_v
  @ipv6_v
end

Instance Method Details

#ipv6_calc_lenObject

Calculates the payload length.


151
152
153
# File 'lib/packetfu/protos/ipv6/header.rb', line 151

def ipv6_calc_len
  self[:ipv6_len] = body.to_s.length
end

#ipv6_daddrObject Also known as: ipv6_dst_readable

Get the destination address in a more readable form.


176
177
178
# File 'lib/packetfu/protos/ipv6/header.rb', line 176

def ipv6_daddr
  self[:ipv6_dst].to_x
end

#ipv6_daddr=(str) ⇒ Object

Set the destination address in a more readable form.


181
182
183
# File 'lib/packetfu/protos/ipv6/header.rb', line 181

def ipv6_daddr=(str)
  self[:ipv6_dst].read_x(str)
end

#ipv6_recalc(arg = :all) ⇒ Object

Recalculates the calculatable fields for this object.


156
157
158
159
160
161
162
163
# File 'lib/packetfu/protos/ipv6/header.rb', line 156

def ipv6_recalc(arg=:all)
  case arg
  when :ipv6_len
    ipv6_calc_len
  when :all
    ipv6_recalc(:len)
  end
end

#ipv6_saddrObject Also known as: ipv6_src_readable

Get the source address in a more readable form.


166
167
168
# File 'lib/packetfu/protos/ipv6/header.rb', line 166

def ipv6_saddr
  self[:ipv6_src].to_x
end

#ipv6_saddr=(str) ⇒ Object

Set the source address in a more readable form.


171
172
173
# File 'lib/packetfu/protos/ipv6/header.rb', line 171

def ipv6_saddr=(str)
  self[:ipv6_src].read_x(str)
end

#read(str) ⇒ Object

Reads a string to populate the object.


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/packetfu/protos/ipv6/header.rb', line 102

def read(str)
  force_binary(str)
  return self if str.nil?
  self[:ipv6_v] = str[0,1].unpack("C").first >> 4
  self[:ipv6_class] = (str[0,2].unpack("n").first & 0x0ff0) >> 4
  self[:ipv6_label] = str[0,4].unpack("N").first & 0x000fffff
  self[:ipv6_len].read(str[4,2])
  self[:ipv6_next].read(str[6,1])
  self[:ipv6_hop].read(str[7,1])
  self[:ipv6_src].read(str[8,16])
  self[:ipv6_dst].read(str[24,16])
  self[:body].read(str[40,str.size]) if str.size > 40
  self
end

#to_sObject

Returns the object in string form.


94
95
96
97
98
99
# File 'lib/packetfu/protos/ipv6/header.rb', line 94

def to_s
  bytes_v_class_label = [(self.ipv6_v << 28) +
   (self.ipv6_class << 20) +
   self.ipv6_label].pack("N")
  bytes_v_class_label + (self.to_a[3,6].map {|x| x.to_s}.join)
end