Class: PacketGen::Header::IPv6
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/ipv6.rb
Overview
IPv6 header class
Defined Under Namespace
Classes: Addr
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#dst ⇒ String
(also: #destination)
Getter for dst attribute.
-
#flow_label ⇒ Object
Returns the value of attribute flow_label.
-
#hop ⇒ Integer
Getter for hop attribute.
-
#length ⇒ Integer
Getter for length attribute.
-
#next ⇒ Integer
Getter for next attribute.
-
#src ⇒ String
(also: #source)
Getter for src attribute.
-
#traffic_class ⇒ Object
Returns the value of attribute traffic_class.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#calc_length ⇒ Integer
Compute length and set
lenfield. -
#initialize(options = {}) ⇒ IPv6
constructor
A new instance of IPv6.
-
#pseudo_header_sum ⇒ Integer
Get IPv6 part of pseudo header sum.
-
#read(str) ⇒ self
Read a IP header from a string.
-
#to_s ⇒ String
Get binary string.
-
#to_w(iface = nil) ⇒ void
Send IPv6 packet on wire.
Methods included from HeaderClassMethods
Methods included from HeaderMethods
#header_id, #ip_header, #packet, #packet=
Methods included from StructFu
#clone, #set_endianness, #sz, #typecast
Methods inherited from Struct
Constructor Details
#initialize(options = {}) ⇒ IPv6
Returns a new instance of IPv6.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/packetgen/header/ipv6.rb', line 97 def initialize(={}) super [:version] || 6, [:traffic_class] || 0, [:flow_label] || 0, Int16.new([:length]), Int8.new([:next]), Int8.new([:hop] || 64), Addr.new.parse([:src] || '::1'), Addr.new.parse([:dst] || '::1'), StructFu::String.new.read([:body]) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def body @body end |
#dst ⇒ String Also known as: destination
Getter for dst attribute
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def dst @dst end |
#flow_label ⇒ Object
Returns the value of attribute flow_label
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def flow_label @flow_label end |
#hop ⇒ Integer
Getter for hop attribute
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def hop @hop end |
#length ⇒ Integer
Getter for length attribute
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def length @length end |
#next ⇒ Integer
Getter for next attribute
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def next @next end |
#src ⇒ String Also known as: source
Getter for src attribute
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def src @src end |
#traffic_class ⇒ Object
Returns the value of attribute traffic_class
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def traffic_class @traffic_class end |
#version ⇒ Object
Returns the value of attribute version
9 10 11 |
# File 'lib/packetgen/header/ipv6.rb', line 9 def version @version end |
Instance Method Details
#calc_length ⇒ Integer
Compute length and set len field
132 133 134 |
# File 'lib/packetgen/header/ipv6.rb', line 132 def calc_length self.length = body.sz end |
#pseudo_header_sum ⇒ Integer
Get IPv6 part of pseudo header sum.
214 215 216 217 218 219 |
# File 'lib/packetgen/header/ipv6.rb', line 214 def pseudo_header_sum sum = 0 self[:src].each { |word| sum += word.to_i } self[:dst].each { |word| sum += word.to_i } sum end |
#read(str) ⇒ self
Read a IP header from a string
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/packetgen/header/ipv6.rb', line 112 def read(str) return self if str.nil? raise ParseError, 'string too short for Eth' if str.size < self.sz force_binary str first32 = str[0, 4].unpack('N').first self.version = first32 >> 28 self.traffic_class = (first32 >> 20) & 0xff self.flow_label = first32 & 0xfffff self[:length].read str[4, 2] self[:next].read str[6, 1] self[:hop].read str[7, 1] self[:src].read str[8, 16] self[:dst].read str[24, 16] self[:body].read str[40..-1] self end |
#to_s ⇒ String
Get binary string
207 208 209 210 |
# File 'lib/packetgen/header/ipv6.rb', line 207 def to_s first32 = (version << 28) | (traffic_class << 20) | flow_label [first32].pack('N') << to_a[3..-1].map { |field| field.to_s }.join end |
#to_w(iface = nil) ⇒ void
This method returns an undefined value.
Send IPv6 packet on wire.
When sending packet at IPv6 level, version, flow_label and length fields are set by kernel. Source address should be a unicast address assigned to the host. To set any of this fields, use Eth#to_w.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/packetgen/header/ipv6.rb', line 228 def to_w(iface=nil) sock = Socket.new(Socket::AF_INET6, Socket::SOCK_RAW, self.next) sockaddrin = Socket.sockaddr_in(0, dst) # IPv6 RAW sockets don't have IPHDRINCL option to send IPv6 header. # So, header must be built using ancillary data. # Only src address, traffic_class and hop_limit can be set this way. hop_limit = Socket::AncillaryData.int(Socket::AF_INET6, Socket::IPPROTO_IPV6, Socket::IPV6_HOPLIMIT, hop) tc = Socket::AncillaryData.int(Socket::AF_INET6, Socket::IPPROTO_IPV6, Socket::IPV6_TCLASS, traffic_class) # src address is set through PKT_INFO, which needs interface index. ifaddr = Socket.getifaddrs.find { |ia| ia.name == iface } raise WireError, "unknown #{iface} interface" if ifaddr.nil? pkt_info = Socket::AncillaryData.ipv6_pktinfo(Addrinfo.ip(src), ifaddr.ifindex) sock.sendmsg body.to_s, 0, sockaddrin, hop_limit, tc, pkt_info end |