mt_racket 1.0.12a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README +76 -0
- data/examples/arp-send +24 -0
- data/examples/arp-send2 +30 -0
- data/examples/cdp +39 -0
- data/examples/cdp-spew +52 -0
- data/examples/dhcp +42 -0
- data/examples/dhcp-spew +48 -0
- data/examples/dns +38 -0
- data/examples/egp +30 -0
- data/examples/hsrp +43 -0
- data/examples/hsrp_takeover +69 -0
- data/examples/icmp-recv +34 -0
- data/examples/icmp-spew +50 -0
- data/examples/icmpv6 +84 -0
- data/examples/icmpv6-spew +50 -0
- data/examples/igmpv1 +27 -0
- data/examples/igmpv2 +27 -0
- data/examples/igrp-send +25 -0
- data/examples/ipv6 +35 -0
- data/examples/nat-pimp +32 -0
- data/examples/ntp +38 -0
- data/examples/ntp2 +42 -0
- data/examples/sctp +32 -0
- data/examples/stp-send +21 -0
- data/examples/synflood +147 -0
- data/examples/tcp +43 -0
- data/examples/tcp2udp +65 -0
- data/examples/udp +46 -0
- data/examples/vrrp +34 -0
- data/examples/vtp +28 -0
- data/lib/racket/l2/eightotwodotthree.rb +48 -0
- data/lib/racket/l2/ethernet.rb +62 -0
- data/lib/racket/l2/llc.rb +50 -0
- data/lib/racket/l2/misc.rb +67 -0
- data/lib/racket/l2/snap.rb +40 -0
- data/lib/racket/l2/vlan.rb +61 -0
- data/lib/racket/l2/vtp.rb +124 -0
- data/lib/racket/l2.rb +30 -0
- data/lib/racket/l3/arp.rb +63 -0
- data/lib/racket/l3/cdp.rb +85 -0
- data/lib/racket/l3/egp.rb +53 -0
- data/lib/racket/l3/ipv4.rb +132 -0
- data/lib/racket/l3/ipv6.rb +66 -0
- data/lib/racket/l3/misc.rb +159 -0
- data/lib/racket/l3/stp.rb +81 -0
- data/lib/racket/l3.rb +30 -0
- data/lib/racket/l4/gre.rb +65 -0
- data/lib/racket/l4/icmp.rb +295 -0
- data/lib/racket/l4/icmpv6.rb +446 -0
- data/lib/racket/l4/igmpv1.rb +79 -0
- data/lib/racket/l4/igmpv2.rb +76 -0
- data/lib/racket/l4/igrp.rb +138 -0
- data/lib/racket/l4/misc.rb +35 -0
- data/lib/racket/l4/sctp.rb +163 -0
- data/lib/racket/l4/tcp.rb +152 -0
- data/lib/racket/l4/udp.rb +81 -0
- data/lib/racket/l4/vrrp.rb +95 -0
- data/lib/racket/l4.rb +30 -0
- data/lib/racket/l5/bootp.rb +106 -0
- data/lib/racket/l5/dns.rb +110 -0
- data/lib/racket/l5/hsrp.rb +73 -0
- data/lib/racket/l5/misc.rb +35 -0
- data/lib/racket/l5/nat-pmp.rb +41 -0
- data/lib/racket/l5/ntp.rb +59 -0
- data/lib/racket/l5.rb +30 -0
- data/lib/racket/misc/lv.rb +108 -0
- data/lib/racket/misc/misc.rb +62 -0
- data/lib/racket/misc/orderedhash.rb +63 -0
- data/lib/racket/misc/raw.rb +35 -0
- data/lib/racket/misc/tlv.rb +103 -0
- data/lib/racket/misc/vt.rb +114 -0
- data/lib/racket/misc.rb +30 -0
- data/lib/racket/racket.rb +166 -0
- data/lib/racket/racketpart.rb +66 -0
- data/lib/racket.rb +4 -0
- data/test/l2/ts_ethernet.rb +22 -0
- data/test/l2/ts_misc.rb +23 -0
- data/test/l2/ts_vlan.rb +15 -0
- data/test/l3/ts_ipv4.rb +44 -0
- data/test/l3/ts_ipv6.rb +26 -0
- data/test/l3/ts_misc.rb +31 -0
- data/test/l4/ts_icmp.rb +38 -0
- data/test/l4/ts_tcp.rb +55 -0
- data/test/l4/ts_udp.rb +40 -0
- data/test/misc/ts_lv.rb +59 -0
- data/test/misc/ts_orderedhash.rb +33 -0
- data/test/misc/ts_tlv.rb +47 -0
- data/test/misc/ts_vt.rb +56 -0
- data/test/ts_all.rb +14 -0
- metadata +153 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
# $Id$
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#
|
28
|
+
module Racket
|
29
|
+
module L3
|
30
|
+
# Exterior Gateway protocol (EGP)
|
31
|
+
# http://tools.ietf.org/html/rfc904
|
32
|
+
# XXX: TODO, add support for tacking on the various message types
|
33
|
+
class EGP < RacketPart
|
34
|
+
# EGP version
|
35
|
+
unsigned :version, 8
|
36
|
+
# Message type
|
37
|
+
unsigned :type, 8
|
38
|
+
# Message code (subtype)
|
39
|
+
unsigned :code, 8
|
40
|
+
# Message dependent status information
|
41
|
+
unsigned :status, 8
|
42
|
+
# Checksum
|
43
|
+
unsigned :checksum, 16
|
44
|
+
# assigned number identifying the particular autonomous system
|
45
|
+
unsigned :asn, 16
|
46
|
+
# Sequence number
|
47
|
+
unsigned :sequence, 16
|
48
|
+
# Payload
|
49
|
+
rest :payload
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# $Id: ipv4.rb 14 2008-03-02 05:42:30Z warchild $
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#
|
28
|
+
module Racket
|
29
|
+
module L3
|
30
|
+
# Internet Protcol Version 4 (IPV4)
|
31
|
+
#
|
32
|
+
# RFC791 (http://www.ietf.org/rfc/rfc791.txt)
|
33
|
+
class IPv4 < RacketPart
|
34
|
+
# Version (defaults to 4)
|
35
|
+
unsigned :version, 4, { :default => 4 }
|
36
|
+
# Header length in multiples of 4 octets (defaults to 5)
|
37
|
+
unsigned :hlen, 4, { :default => 5 }
|
38
|
+
# Type of Service
|
39
|
+
unsigned :tos, 8
|
40
|
+
# Datagram length
|
41
|
+
unsigned :len, 16
|
42
|
+
# Identifier
|
43
|
+
unsigned :id, 16
|
44
|
+
# Flags
|
45
|
+
unsigned :flags, 3
|
46
|
+
# Fragmentation offset
|
47
|
+
unsigned :foffset, 13
|
48
|
+
# Time-to-live
|
49
|
+
unsigned :ttl, 8
|
50
|
+
# Protocol
|
51
|
+
unsigned :protocol, 8
|
52
|
+
# Checksum
|
53
|
+
unsigned :checksum, 16, "Checksum"
|
54
|
+
# Source IP address, passed as four octets separated by periods
|
55
|
+
# (192.168.1.2)
|
56
|
+
octets :src_ip, 32
|
57
|
+
# Destination IP address, passed as four octets separated by periods
|
58
|
+
# (192.168.1.2)
|
59
|
+
octets :dst_ip, 32
|
60
|
+
# Payload
|
61
|
+
rest :payload
|
62
|
+
|
63
|
+
def initialize(*args)
|
64
|
+
@options = []
|
65
|
+
super
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add an IPv4 option to this IPv4 object.
|
69
|
+
# All rejiggering will happen when the call to fix!
|
70
|
+
# happens automagically
|
71
|
+
def add_option(number, value)
|
72
|
+
t = Racket::Misc::TLV.new(1,1)
|
73
|
+
t.type = number
|
74
|
+
t.value = value
|
75
|
+
t.length = value.length + 2
|
76
|
+
@options << t.encode
|
77
|
+
end
|
78
|
+
|
79
|
+
# Check the checksum for this IP datagram
|
80
|
+
def checksum?
|
81
|
+
self.checksum == compute_checksum
|
82
|
+
end
|
83
|
+
|
84
|
+
# Compute and set the checksum for this IP datagram
|
85
|
+
def checksum!
|
86
|
+
self.checksum = compute_checksum
|
87
|
+
end
|
88
|
+
|
89
|
+
# Perform all the niceties necessary prior to sending
|
90
|
+
# this IP datagram out. Append the options, update len and hlen,
|
91
|
+
# and fix the checksum.
|
92
|
+
def fix!
|
93
|
+
newpayload = @options.join
|
94
|
+
|
95
|
+
# pad to a multiple of 32 bits
|
96
|
+
if (newpayload.length % 4 != 0)
|
97
|
+
# fill the beginning as needed with NOPs
|
98
|
+
while (newpayload.length % 4 != 3)
|
99
|
+
newpayload = "\x01#{newpayload}"
|
100
|
+
end
|
101
|
+
|
102
|
+
# make sure the last byte is an EOL
|
103
|
+
if (newpayload.length % 4 == 3)
|
104
|
+
newpayload += "\x00"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
self.payload = newpayload + self.payload
|
109
|
+
self.hlen += newpayload.length/4
|
110
|
+
self.len = self.payload.length + self.class.bit_length/8
|
111
|
+
self.checksum!
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
private
|
116
|
+
# Compute the checksum for this IP datagram
|
117
|
+
def compute_checksum
|
118
|
+
pseudo = []
|
119
|
+
pseudo << ((((self.version << 4) | self.hlen) << 8) | self.tos)
|
120
|
+
pseudo << self.len
|
121
|
+
pseudo << self.id
|
122
|
+
pseudo << ((self.flags << 13) | self.foffset)
|
123
|
+
pseudo << ((self.ttl << 8) | self.protocol)
|
124
|
+
pseudo << 0
|
125
|
+
pseudo << L3::Misc.ipv42long(self.src_ip)
|
126
|
+
pseudo << L3::Misc.ipv42long(self.dst_ip)
|
127
|
+
L3::Misc.checksum(pseudo.pack("nnnnnnNN"))
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# $Id: ipv6.rb 14 2008-03-02 05:42:30Z warchild $
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#
|
28
|
+
module Racket
|
29
|
+
module L3
|
30
|
+
# Internet Protocol Version 6 (IPV6)
|
31
|
+
# RFC2460
|
32
|
+
class IPv6 < RacketPart
|
33
|
+
# IP Version (defaults to 6)
|
34
|
+
unsigned :version, 4, { :default => 6 }
|
35
|
+
# Traffic class
|
36
|
+
unsigned :tclass, 8
|
37
|
+
# Flow label
|
38
|
+
unsigned :flow, 20
|
39
|
+
# Payload length
|
40
|
+
unsigned :plen, 16
|
41
|
+
# Next header type
|
42
|
+
unsigned :nhead, 8
|
43
|
+
# Hop limit
|
44
|
+
unsigned :ttl, 8, { :default => 200 }
|
45
|
+
# Source IP address. Must be passed as an integer
|
46
|
+
unsigned :src_ip, 128
|
47
|
+
# Destination IP address. Must be passed as an integer
|
48
|
+
unsigned :dst_ip, 128
|
49
|
+
# Payload
|
50
|
+
rest :payload
|
51
|
+
|
52
|
+
def initialize(*args)
|
53
|
+
@headers = []
|
54
|
+
super
|
55
|
+
@autofix = true
|
56
|
+
end
|
57
|
+
|
58
|
+
# Adjust plen to match the payload
|
59
|
+
def fix!
|
60
|
+
self.plen = self.payload.length
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,159 @@
|
|
1
|
+
# $Id: misc.rb 14 2008-03-02 05:42:30Z warchild $
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#
|
28
|
+
require 'ipaddr'
|
29
|
+
module Racket
|
30
|
+
module L3
|
31
|
+
# Miscelaneous L3 helper methods
|
32
|
+
module Misc
|
33
|
+
# given an IPv4 address packed as an integer
|
34
|
+
# return the friendly "dotted quad"
|
35
|
+
def Misc.long2ipv4(long)
|
36
|
+
quad = Array.new(4)
|
37
|
+
quad[0] = (long >> 24) & 255
|
38
|
+
quad[1] = (long >> 16) & 255
|
39
|
+
quad[2] = (long >> 8 ) & 255
|
40
|
+
quad[3] = long & 255
|
41
|
+
quad.join(".")
|
42
|
+
end
|
43
|
+
|
44
|
+
def Misc.randomipv4
|
45
|
+
Misc.long2ipv4(rand(2**32))
|
46
|
+
end
|
47
|
+
|
48
|
+
# Compute link local address for a given mac address
|
49
|
+
# From Daniele Bellucci
|
50
|
+
def Misc.linklocaladdr(mac)
|
51
|
+
mac = mac.split(":")
|
52
|
+
mac[0] = (mac[0].to_i(16) ^ (1 << 1)).to_s(16)
|
53
|
+
["fe80", "", mac[0,2].join, mac[2,2].join("ff:fe"), mac[4,2].join].join(":")
|
54
|
+
end
|
55
|
+
|
56
|
+
# Given a long, convert it to an IPv6 address,
|
57
|
+
# optionally compressing the address returned
|
58
|
+
def Misc.long2ipv6(long, compress=true)
|
59
|
+
ipv6 = []
|
60
|
+
ipv6[0] = long >> 112
|
61
|
+
ipv6[1] = (long >> 96) & (0xFFFF)
|
62
|
+
ipv6[2] = (long >> 80) & (0xFFFF)
|
63
|
+
ipv6[3] = (long >> 64) & (0xFFFF)
|
64
|
+
ipv6[4] = (long >> 48) & (0xFFFF)
|
65
|
+
ipv6[5] = (long >> 32) & (0xFFFF)
|
66
|
+
ipv6[6] = (long >> 16) & (0xFFFF)
|
67
|
+
ipv6[7] = long & (0xFFFF)
|
68
|
+
|
69
|
+
ipv6 = ipv6.map { |o| o.to_s(16) }.join(":")
|
70
|
+
compress ? Misc.compressipv6(ipv6) : ipv6
|
71
|
+
end
|
72
|
+
|
73
|
+
# Compress an IPv6 address
|
74
|
+
# Inspired by Daniele Bellucci and jacked from ipaddr
|
75
|
+
def Misc.compressipv6(ipv6)
|
76
|
+
ipv6.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
|
77
|
+
loop do
|
78
|
+
break if ipv6.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::')
|
79
|
+
break if ipv6.sub!(/\b0:0:0:0:0:0:0\b/, ':')
|
80
|
+
break if ipv6.sub!(/\b0:0:0:0:0:0\b/, ':')
|
81
|
+
break if ipv6.sub!(/\b0:0:0:0:0\b/, ':')
|
82
|
+
break if ipv6.sub!(/\b0:0:0:0\b/, ':')
|
83
|
+
break if ipv6.sub!(/\b0:0:0\b/, ':')
|
84
|
+
break if ipv6.sub!(/\b0:0\b/, ':')
|
85
|
+
break
|
86
|
+
end
|
87
|
+
|
88
|
+
ipv6.sub!(/:{3,}/, '::')
|
89
|
+
|
90
|
+
if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\Z/i =~ ipv6
|
91
|
+
ipv6 = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
|
92
|
+
end
|
93
|
+
|
94
|
+
ipv6
|
95
|
+
end
|
96
|
+
|
97
|
+
def Misc.randomipv6
|
98
|
+
Misc.long2ipv6(rand(2**128))
|
99
|
+
end
|
100
|
+
|
101
|
+
# given a string representing an IPv6
|
102
|
+
# address, return the integer representation
|
103
|
+
def Misc.ipv62long(ip)
|
104
|
+
IPAddr.new(ip).to_i
|
105
|
+
end
|
106
|
+
|
107
|
+
# In addition to the regular multicast addresses, each unicast address
|
108
|
+
# has a special multicast address called its solicited-node address. This
|
109
|
+
# address is created through a special mapping from the device’s unicast
|
110
|
+
# address. Solicited-node addresses are used by the IPv6 Neighbor
|
111
|
+
# Discovery (ND) protocol to provide more efficient address resolution
|
112
|
+
# than the ARP technique used in IPv4.
|
113
|
+
# From Daniele Bellucci
|
114
|
+
def Misc.soll_mcast_addr6(addr)
|
115
|
+
h = addr.split(':')[-2, 2]
|
116
|
+
m = []
|
117
|
+
m << 'ff'
|
118
|
+
m << (h[0].to_i(16) & 0xff).to_s(16)
|
119
|
+
m << ((h[1].to_i(16) & (0xff << 8)) >> 8).to_s(16)
|
120
|
+
m << (h[1].to_i(16) & 0xff).to_s(16)
|
121
|
+
'ff02::1:' + [m[0,2].join, m[2,2].join].join(':')
|
122
|
+
end
|
123
|
+
|
124
|
+
#
|
125
|
+
def Misc.soll_mcast_mac(addr)
|
126
|
+
h = addr.split(':')[-2, 2]
|
127
|
+
m = []
|
128
|
+
m << 'ff'
|
129
|
+
m << (h[0].to_i(16) & 0xff).to_s(16)
|
130
|
+
m << ((h[1].to_i(16) & (0xff << 8)) >> 8).to_s(16)
|
131
|
+
m << (h[1].to_i(16) & 0xff).to_s(16)
|
132
|
+
'33:33:' + m.join(':')
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# given a "dotted quad" representing an IPv4
|
137
|
+
# address, return the integer representation
|
138
|
+
def Misc.ipv42long(ip)
|
139
|
+
IPAddr.new(ip).to_i
|
140
|
+
end
|
141
|
+
|
142
|
+
# Calculate the checksum. 16 bit one's complement of the one's
|
143
|
+
# complement sum of all 16 bit words
|
144
|
+
def Misc.checksum(data)
|
145
|
+
checksum = 0
|
146
|
+
unpack = "S#{data.length / 2}#{data.length % 2 == 1 ? 'C' : ''}"
|
147
|
+
|
148
|
+
data.unpack(unpack).each { |x|
|
149
|
+
checksum += x
|
150
|
+
}
|
151
|
+
|
152
|
+
checksum = (checksum >> 16) + (checksum & 0xffff)
|
153
|
+
checksum = ~((checksum >> 16) + checksum) & 0xffff
|
154
|
+
([checksum].pack("S*")).unpack("n*")[0]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# $Id: stp.rb 14 2008-03-02 05:42:30Z warchild $
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#
|
28
|
+
module Racket
|
29
|
+
module L3
|
30
|
+
# Spanning Tree Protocol
|
31
|
+
#
|
32
|
+
# http://en.wikipedia.org/wiki/Spanning_tree_protocol
|
33
|
+
class STP < RacketPart
|
34
|
+
# Protocol identifier
|
35
|
+
unsigned :protocol, 16, { :default => 0 }
|
36
|
+
# Protocol version
|
37
|
+
unsigned :version, 8, { :default => 2}
|
38
|
+
# BPDU type
|
39
|
+
unsigned :bpdu_type, 8, { :default => 2 }
|
40
|
+
# BPDU Flag -- Topology Change Acknowledgement
|
41
|
+
unsigned :bpdu_flag_change_ack, 1
|
42
|
+
# BPDU Flag -- Agreement
|
43
|
+
unsigned :bpdu_flag_agreement, 1
|
44
|
+
# BPDU Flag -- Forwarding
|
45
|
+
unsigned :bpdu_flag_forwarding, 1
|
46
|
+
# BPDU Flag -- Learning
|
47
|
+
unsigned :bpdu_flag_learning, 1
|
48
|
+
# BPDU Flag -- Port Role
|
49
|
+
unsigned :bpdu_flag_port_role, 2
|
50
|
+
# BPDU Flag -- Proposal
|
51
|
+
unsigned :bpdu_flag_proposal, 1
|
52
|
+
# BPDU Flag -- Topology Change
|
53
|
+
unsigned :bpdu_flag_change, 1
|
54
|
+
# Root wtf? Not sure what this is XXX
|
55
|
+
unsigned :root_wtf, 16
|
56
|
+
# Root Identifier
|
57
|
+
hex_octets :root_id, 48
|
58
|
+
# Root Path Cost
|
59
|
+
unsigned :root_cost, 32
|
60
|
+
# Bridge WTF? Not sure what this is XXX
|
61
|
+
unsigned :bridge_wtf, 16
|
62
|
+
# Bridge Identifier
|
63
|
+
hex_octets :bridge_id, 48
|
64
|
+
# Port Identifier
|
65
|
+
unsigned :port_id, 16
|
66
|
+
# Message age
|
67
|
+
unsigned :msg_age, 16
|
68
|
+
# Max age
|
69
|
+
unsigned :max_age, 16
|
70
|
+
# Hello time
|
71
|
+
unsigned :hello_time, 16
|
72
|
+
# Forward delay
|
73
|
+
unsigned :forward_delay, 16
|
74
|
+
# Version 1 Length
|
75
|
+
unsigned :v1_len, 8
|
76
|
+
# Payload
|
77
|
+
rest :payload
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
# vim: set ts=2 et sw=2:
|
data/lib/racket/l3.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# $Id: l3.rb 14 2008-03-02 05:42:30Z warchild $
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
|
28
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'l3/*.rb')).each { |f| require f }
|
29
|
+
|
30
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# $Id: gre.rb 14 2008-03-02 05:42:30Z warchild $
|
2
|
+
#
|
3
|
+
# Copyright (c) 2008, Jon Hart
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
# * Redistributions of source code must retain the above copyright
|
9
|
+
# notice, this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer in the
|
12
|
+
# documentation and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the <organization> nor the
|
14
|
+
# names of its contributors may be used to endorse or promote products
|
15
|
+
# derived from this software without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY Jon Hart ``AS IS'' AND ANY
|
18
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL Jon Hart BE LIABLE FOR ANY
|
21
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
22
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
23
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
24
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#
|
28
|
+
module Racket
|
29
|
+
module L4
|
30
|
+
# XXX: currently broken. all of the "optional" fields must be made dynamic
|
31
|
+
class GRE < RacketPart
|
32
|
+
# Is a checksum present?
|
33
|
+
unsigned :checksum_present, 1
|
34
|
+
# Is routing information present?
|
35
|
+
unsigned :routing_present, 1
|
36
|
+
# Is a key present?
|
37
|
+
unsigned :key_present, 1
|
38
|
+
# Is a sequence number present?
|
39
|
+
unsigned :seq_present, 1
|
40
|
+
# Strict source route
|
41
|
+
unsigned :ssr, 1
|
42
|
+
# How many additional encapsulations are present?
|
43
|
+
unsigned :recursion, 3
|
44
|
+
# Flags
|
45
|
+
unsigned :flags, 5
|
46
|
+
# Version
|
47
|
+
unsigned :version, 3
|
48
|
+
# Protocol type
|
49
|
+
unsigned :protocol, 16
|
50
|
+
# Checksum
|
51
|
+
unsigned :checksum, 16
|
52
|
+
# Offset
|
53
|
+
unsigned :offset, 16
|
54
|
+
# Key
|
55
|
+
unsigned :key, 32
|
56
|
+
# Sequence Number
|
57
|
+
unsigned :seq, 32
|
58
|
+
# Routing
|
59
|
+
unsigned :routing, 32
|
60
|
+
# Payload
|
61
|
+
rest :payload
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
# vim: set ts=2 et sw=2:
|