Class: Scriptroute::IPv6

Inherits:
IP
  • Object
show all
Defined in:
lib/scriptroute/packets.rb

Direct Known Subclasses

ICMP6, UDP6

Constant Summary collapse

IPPROTO_ICMP6 =
58
@@creators =
Hash.new

Constants inherited from IP

Scriptroute::IP::IPPROTO_TCP, Scriptroute::IP::IPPROTO_UDP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IP

#to_bytes

Constructor Details

#initialize(p) ⇒ IPv6

Returns a new instance of IPv6.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/scriptroute/packets.rb', line 47

def initialize(p) 
  if(p.is_a?(Fixnum)) then
    @ip6_src = @ip6_dst = @ip6flow = nil
    @ip6_hlim = 64
    @ip6_nxt = p
    # actually, unlikely 7 or 13 are at the moment
    # either, but let's start here.
    $stderr.puts "unlikely v6 protocol header type #{p} is supported" unless [ 7, 13, 58 ].include?(p) 
  else
    raise "need a next header type for constructing a v6 packet"
  end
end

Instance Attribute Details

#ip6_dstObject

Returns the value of attribute ip6_dst.



31
32
33
# File 'lib/scriptroute/packets.rb', line 31

def ip6_dst
  @ip6_dst
end

#ip6_flowObject

Returns the value of attribute ip6_flow.



30
31
32
# File 'lib/scriptroute/packets.rb', line 30

def ip6_flow
  @ip6_flow
end

#ip6_hlimObject

Returns the value of attribute ip6_hlim.



30
31
32
# File 'lib/scriptroute/packets.rb', line 30

def ip6_hlim
  @ip6_hlim
end

#ip6_srcObject

Returns the value of attribute ip6_src.



30
31
32
# File 'lib/scriptroute/packets.rb', line 30

def ip6_src
  @ip6_src
end

Class Method Details

.creator(str) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/scriptroute/packets.rb', line 33

def IPv6.creator(str)
  flow, plen, nxt, hlim, saddr, daddr = str.unpack("Nncca16a16")
  if(@@creators[nxt]) then
    pkt = (@@creators[nxt]).call(str[40..40+plen])
    pkt.ipv6_unmarshal(str)
    pkt
  else
    raise "unknown IPv6 next header #%d in %s" % [ nxt, str.unpack("H*") ]
  end
end

Instance Method Details

#ip_ttl=(ttl) ⇒ Object

for simplicity.



60
61
62
# File 'lib/scriptroute/packets.rb', line 60

def ip_ttl=(ttl)
  @ip6_hlim = ttl
end

#ipv6_unmarshal(str) ⇒ Object



43
44
45
46
# File 'lib/scriptroute/packets.rb', line 43

def ipv6_unmarshal(str)
  @ip6_flow, @ip6_plen, @ip6_nxt, @ip6_hlim, ip6_saddr, ip6_daddr = str.unpack("Nncca16a16")
  @ip6_src, @ip6_dst = [ip6_saddr, ip6_daddr].map { |addr| IPAddr.new_ntoh(addr) }
end

#marshalObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/scriptroute/packets.rb', line 81

def marshal
  # probably should force src/dst internal
  # representation to be IPAddr via accessor methods
  # rather than reconvert here.
  puts "unlikely you wanted zero hlim" unless @ip6_hlim and @ip6_hlim > 0
  puts "unlikely you wanted no next protocol" unless @ip6_nxt and @ip6_nxt > 0
  puts "unlikely you wanted nil payload len" unless ip_payload_len
  v6packet = [ 6 << 4, 0, 0, ip_payload_len, @ip6_nxt, @ip6_hlim, 
    IPAddr.new(@ip6_src, Socket::AF_INET6).hton, 
    IPAddr.new(@ip6_dst, Socket::AF_INET6).hton ].pack("ccnncca16a16")
end

#to_sObject



92
93
94
# File 'lib/scriptroute/packets.rb', line 92

def to_s 
  "%s -> %s hlim=%d" % [ (@ip6_src or "::0"), (@ip6_dst or "::0"), @ip6_hlim ]
end