racket 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- 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/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.rb +4 -0
- data/lib/racket/l2.rb +30 -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/l3.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 +165 -0
- data/lib/racket/l3/stp.rb +81 -0
- data/lib/racket/l4.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/l5.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/ntp.rb +59 -0
- data/lib/racket/misc.rb +30 -0
- data/lib/racket/misc/lv.rb +108 -0
- data/lib/racket/misc/misc.rb +61 -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/racket.rb +164 -0
- data/lib/racket/racketpart.rb +66 -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 +182 -0
@@ -0,0 +1,85 @@
|
|
1
|
+
# $Id: cdp.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
|
+
# CDP -- Cisco Discovery Protocol
|
31
|
+
# http://www.cisco.biz/univercd/cc/td/doc/product/lan/trsrb/frames.htm#xtocid12
|
32
|
+
class CDP < RacketPart
|
33
|
+
# CDP Version (generally 1)
|
34
|
+
unsigned :version, 8, { :default => 1 }
|
35
|
+
# Time-to-live of the data in this message
|
36
|
+
unsigned :ttl, 8
|
37
|
+
# Checksum
|
38
|
+
unsigned :checksum, 16
|
39
|
+
# Payload of this CDP message. Generally untouched.
|
40
|
+
rest :payload
|
41
|
+
|
42
|
+
|
43
|
+
def initialize(*args)
|
44
|
+
super(*args)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Add a new field to this CDP message.
|
48
|
+
def add_field(type, value)
|
49
|
+
t = Racket::Misc::TLV.new(2,2)
|
50
|
+
t.type = type
|
51
|
+
t.value = value
|
52
|
+
t.length = 4 + value.length
|
53
|
+
self.payload += t.encode
|
54
|
+
end
|
55
|
+
|
56
|
+
# Check the checksum for this IP datagram
|
57
|
+
def checksum?
|
58
|
+
self.checksum == compute_checksum
|
59
|
+
end
|
60
|
+
|
61
|
+
# Compute and set the checksum for this IP datagram
|
62
|
+
def checksum!
|
63
|
+
self.checksum = compute_checksum
|
64
|
+
end
|
65
|
+
|
66
|
+
# Fix this CDP message up for sending.
|
67
|
+
def fix!
|
68
|
+
self.checksum!
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
# Compute the checksum for this IP datagram
|
74
|
+
def compute_checksum
|
75
|
+
pseudo = []
|
76
|
+
pseudo << ((self.version << 8) | self.ttl)
|
77
|
+
pseudo << 0
|
78
|
+
pseudo << self.payload
|
79
|
+
L3::Misc.checksum(pseudo.pack("nna*"))
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# $Id: egp.rb 127 2009-11-29 01:30:46Z jhart $
|
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,165 @@
|
|
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
|
+
num_shorts = data.length / 2
|
146
|
+
checksum = 0
|
147
|
+
count = data.length
|
148
|
+
|
149
|
+
data.unpack("S#{num_shorts}").each { |x|
|
150
|
+
checksum += x
|
151
|
+
count -= 2
|
152
|
+
}
|
153
|
+
|
154
|
+
if (count == 1)
|
155
|
+
checksum += data[data.length - 1]
|
156
|
+
end
|
157
|
+
|
158
|
+
checksum = (checksum >> 16) + (checksum & 0xffff)
|
159
|
+
checksum = ~((checksum >> 16) + checksum) & 0xffff
|
160
|
+
([checksum].pack("S*")).unpack("n*")[0]
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
# vim: set ts=2 et sw=2:
|