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

Integer(4 bits)   :ipv6_v      Default: 6     # Versiom
Integer(8 bits)   :ipv6_class  Defualt: 0     # Class
Integer(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.



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

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



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def body
  @body
end

#ipv6_classObject

Getter for the traffic class.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_class
  @ipv6_class
end

#ipv6_dstObject

Getter for the destination address.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_dst
  @ipv6_dst
end

#ipv6_hopObject

Getter for the hop limit.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_hop
  @ipv6_hop
end

#ipv6_labelObject

Getter for the flow label.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_label
  @ipv6_label
end

#ipv6_lenObject

Getter for the payload length.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_len
  @ipv6_len
end

#ipv6_nextObject

Getter for the next protocol header.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_next
  @ipv6_next
end

#ipv6_srcObject

Getter for the source address.



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_src
  @ipv6_src
end

#ipv6_vObject

Getter for the version (usually, 6).



76
77
78
# File 'lib/packetfu/protos/ipv6/header.rb', line 76

def ipv6_v
  @ipv6_v
end

Instance Method Details

#ipv6_calc_lenObject

Calculates the payload length.



153
154
155
# File 'lib/packetfu/protos/ipv6/header.rb', line 153

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.



178
179
180
# File 'lib/packetfu/protos/ipv6/header.rb', line 178

def ipv6_daddr
  self[:ipv6_dst].to_x
end

#ipv6_daddr=(str) ⇒ Object

Set the destination address in a more readable form.



183
184
185
# File 'lib/packetfu/protos/ipv6/header.rb', line 183

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

#ipv6_recalc(arg = :all) ⇒ Object

Recalculates the calculatable fields for this object.



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

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

#ipv6_saddrObject Also known as: ipv6_src_readable

Get the source address in a more readable form.



168
169
170
# File 'lib/packetfu/protos/ipv6/header.rb', line 168

def ipv6_saddr
  self[:ipv6_src].to_x
end

#ipv6_saddr=(str) ⇒ Object

Set the source address in a more readable form.



173
174
175
# File 'lib/packetfu/protos/ipv6/header.rb', line 173

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

#read(str) ⇒ Object

Reads a string to populate the object.



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

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.



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

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