Class: PacketGen::Header::IPv6
- Defined in:
- lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6.rb,
lib/packetgen/header/ipv6/addr.rb,
lib/packetgen/header/ipv6/extension.rb,
lib/packetgen/header/ipv6/hop_by_hop.rb
Overview
IPv6 (RFC 8200)
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| Traffic Class | Flow Label |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload Length | Next Header | Hop Limit |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ Source Address +
| |
+ +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ Destination Address +
| |
+ +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
A IPv6 header consists of:
-
a first 32-bit word (#u32, of
BinStruct::Int32
type) composed of:-
a 4-bit #version field,
-
a 8-bit #traffic_class field,
-
a 20-bit #flow_label field,
-
-
a payload length field (#length,
BinStruct::Int16
type}), -
a next header field (#next,
BinStruct::Int8
type), -
a hop-limit field (#hop,
BinStruct::Int8
type), -
and a #body (
BinStruct::String
type).
Add IPv6 extensions
In IPv6, optional extensions are encoded in separate headers that may be placed between the IPv6 header and the upper-layer header.
In PacketGen, a IPv6 extension is processedf as a classical header:
pkt = PacketGen.gen('IPv6')
# Add a HopByHop extension
pkt.add('IPv6::HopByHop')
pkt.ipv6_hopbyhop. << { type: 'router_alert', value: [0].pack('n') }
# Add another header
pkt.add('UDP')
Defined Under Namespace
Classes: Addr, ArrayOfAddr, Extension, HopByHop, Option, Options, Pad1
Constant Summary collapse
- ETHERTYPE =
IPv6 Ether type
0x86dd
Instance Attribute Summary collapse
-
#body ⇒ BinStruct::String, Headerable
IPv6 body.
-
#dst ⇒ Addr
IPv6 destination address.
-
#flow_label ⇒ Object
20-bit flow_label attribute.
-
#hop ⇒ Integer
8-bit IPv6 hop limit.
-
#length ⇒ Integer
16-bit word of IPv6 payload length.
-
#next ⇒ Integer
8-bit IPv6 next payload value.
-
#src ⇒ Addr
IPv6 source address.
-
#traffic_class ⇒ Integer
8-bit traffic_class attribute.
-
#u32 ⇒ Integer
First 32-bit word of IPv6 header.
-
#version ⇒ Integer
4-bit version attribute.
Class Method Summary collapse
-
.bind(header_klass, args = {}) ⇒ Object
Bind a upper header to IPv6 and its defined extension headers.
- .old_bind ⇒ Object
Instance Method Summary collapse
-
#calc_length ⇒ Integer
Compute length and set #length field.
- #inspect ⇒ String
-
#parse? ⇒ Boolean
Check version field.
-
#pseudo_header_checksum ⇒ Integer
Get IPv6 part of pseudo header checksum.
-
#reply! ⇒ self
Invert source and destination addresses.
-
#to_w(_iface = nil) ⇒ void
Send IPv6 packet on wire.
Methods inherited from Base
calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header
Methods included from PacketGen::Headerable
#added_to_packet, included, #method_name, #packet, #packet=, #protocol_name, #read, #to_s
Constructor Details
This class inherits a constructor from PacketGen::Header::Base
Instance Attribute Details
#body ⇒ BinStruct::String, Headerable
IPv6 body
133 |
# File 'lib/packetgen/header/ipv6.rb', line 133 define_attr :body, BinStruct::String |
#dst ⇒ Addr
IPv6 destination address
129 |
# File 'lib/packetgen/header/ipv6.rb', line 129 define_attr :dst, Addr, default: '::1' |
#flow_label ⇒ Object
20-bit flow_label attribute
109 |
# File 'lib/packetgen/header/ipv6.rb', line 109 define_bit_attr :u32, default: 0x60000000, version: 4, traffic_class: 8, flow_label: 20 |
#hop ⇒ Integer
8-bit IPv6 hop limit
121 |
# File 'lib/packetgen/header/ipv6.rb', line 121 define_attr :hop, BinStruct::Int8, default: 64 |
#length ⇒ Integer
16-bit word of IPv6 payload length
113 |
# File 'lib/packetgen/header/ipv6.rb', line 113 define_attr :length, BinStruct::Int16 |
#next ⇒ Integer
8-bit IPv6 next payload value
117 |
# File 'lib/packetgen/header/ipv6.rb', line 117 define_attr :next, BinStruct::Int8 |
#src ⇒ Addr
IPv6 source address
125 |
# File 'lib/packetgen/header/ipv6.rb', line 125 define_attr :src, Addr, default: '::1' |
#traffic_class ⇒ Integer
8-bit traffic_class attribute
109 |
# File 'lib/packetgen/header/ipv6.rb', line 109 define_bit_attr :u32, default: 0x60000000, version: 4, traffic_class: 8, flow_label: 20 |
#u32 ⇒ Integer
First 32-bit word of IPv6 header
109 |
# File 'lib/packetgen/header/ipv6.rb', line 109 define_bit_attr :u32, default: 0x60000000, version: 4, traffic_class: 8, flow_label: 20 |
#version ⇒ Integer
4-bit version attribute
109 |
# File 'lib/packetgen/header/ipv6.rb', line 109 define_bit_attr :u32, default: 0x60000000, version: 4, traffic_class: 8, flow_label: 20 |
Class Method Details
.bind(header_klass, args = {}) ⇒ Object
Bind a upper header to IPv6 and its defined extension headers.
212 213 214 215 216 217 218 |
# File 'lib/packetgen/header/ipv6.rb', line 212 def bind(header_klass, args={}) IPv6.old_bind(header_klass, args) IPv6.constants .map { |cname| IPv6.const_get(cname) } .select { |klass| klass.is_a?(Class) && (klass < Extension) } .each { |klass| klass.bind(header_klass, args) } end |
.old_bind ⇒ Object
207 |
# File 'lib/packetgen/header/ipv6.rb', line 207 alias old_bind bind |
Instance Method Details
#calc_length ⇒ Integer
Compute length and set #length field
137 138 139 |
# File 'lib/packetgen/header/ipv6.rb', line 137 def calc_length Base.calculate_and_set_length self, header_in_size: false end |
#inspect ⇒ String
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/packetgen/header/ipv6.rb', line 162 def inspect super do |attr| next unless attr == :u32 str = Inspect.inspect_attribute(attr, self[attr]) shift = Inspect.shift_level str << shift + Inspect::FMT_ATTR % ['', 'version', version] tclass = Inspect.int_dec_hex(traffic_class, 2) str << shift + Inspect::FMT_ATTR % ['', 'tclass', tclass] fl_value = Inspect.int_dec_hex(flow_label, 5) str << shift + Inspect::FMT_ATTR % ['', 'flow_label', fl_value] end end |
#parse? ⇒ Boolean
Check version field
178 179 180 |
# File 'lib/packetgen/header/ipv6.rb', line 178 def parse? version == 6 end |
#pseudo_header_checksum ⇒ Integer
Get IPv6 part of pseudo header checksum.
143 144 145 146 147 148 |
# File 'lib/packetgen/header/ipv6.rb', line 143 def pseudo_header_checksum sum = 0 self[:src].to_a.each { |word| sum += word.to_i } self[:dst].to_a.each { |word| sum += word.to_i } sum end |
#reply! ⇒ self
Invert source and destination addresses
185 186 187 188 |
# File 'lib/packetgen/header/ipv6.rb', line 185 def reply! self[:src], self[:dst] = self[:dst], self[:src] self end |
#to_w(_iface = nil) ⇒ void
This method returns an undefined value.
Send IPv6 packet on wire. All attributes may be set (even #version).
154 155 156 157 158 159 |
# File 'lib/packetgen/header/ipv6.rb', line 154 def to_w(_iface=nil) sock = Socket.new(Socket::AF_INET6, Socket::SOCK_RAW, Socket::IPPROTO_RAW) sockaddrin = Socket.sockaddr_in(0, dst) sock.send(to_s, 0, sockaddrin) sock.close end |