racket2 1.0.9
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/ipv6-option +40 -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 +460 -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 +156 -0
@@ -0,0 +1,110 @@
|
|
1
|
+
# $Id: dns.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 L5
|
30
|
+
# Domain Name System
|
31
|
+
class DNS < RacketPart
|
32
|
+
# Transaction ID
|
33
|
+
unsigned :tx_id, 16
|
34
|
+
# Response
|
35
|
+
unsigned :response, 1
|
36
|
+
# Opcode
|
37
|
+
unsigned :opcode, 4
|
38
|
+
# Authoritative?
|
39
|
+
unsigned :authoritative, 1
|
40
|
+
# Truncated?
|
41
|
+
unsigned :truncated, 1
|
42
|
+
# Recursion Desired?
|
43
|
+
unsigned :recursion_d, 1
|
44
|
+
# Recursion Available?
|
45
|
+
unsigned :recursion_a, 1
|
46
|
+
# Reserved
|
47
|
+
unsigned :reserved, 1
|
48
|
+
# Answer authenticated?
|
49
|
+
unsigned :auth, 1
|
50
|
+
# Non-authenticated data OK?
|
51
|
+
unsigned :nonauth, 1
|
52
|
+
# Reply Code
|
53
|
+
unsigned :reply_code, 4
|
54
|
+
# Number of questions
|
55
|
+
unsigned :question_rr, 16
|
56
|
+
# Number of answer RR
|
57
|
+
unsigned :answer_rr, 16
|
58
|
+
# Number of authority RR
|
59
|
+
unsigned :authority_rr, 16
|
60
|
+
# Number of additional RR
|
61
|
+
unsigned :additional_rr, 16
|
62
|
+
rest :payload
|
63
|
+
|
64
|
+
def initialize(*args)
|
65
|
+
super
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add an additional record. Automatically increases +additional_rr+
|
69
|
+
def add_additional(name, type, klass)
|
70
|
+
self.payload += self.add_record(name, type, klass).encode
|
71
|
+
self.additional_rr += 1
|
72
|
+
end
|
73
|
+
|
74
|
+
# Add an answer record. Automatically increases +answer_rr+
|
75
|
+
def add_answer(name, type, klass)
|
76
|
+
self.payload += self.add_record(name, type, klass).encode
|
77
|
+
self.answer_rr += 1
|
78
|
+
end
|
79
|
+
|
80
|
+
# Add an authority record. Automatically increases +authority_rr+
|
81
|
+
# XXX: broken. authns records are much more complicated than this.
|
82
|
+
def add_authority(name, type, klass)
|
83
|
+
self.payload += self.add_record(name, type, klass).encode
|
84
|
+
self.authority_rr += 1
|
85
|
+
end
|
86
|
+
|
87
|
+
# Add a question record. Automatically increases +question_rr+
|
88
|
+
def add_question(name, type, klass)
|
89
|
+
self.payload += add_record(name, type, klass).encode
|
90
|
+
self.question_rr += 1
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
def add_record(name, type, klass)
|
95
|
+
q = Racket::Misc::VT.new(2,2)
|
96
|
+
name.split(/\./).each do |p|
|
97
|
+
lv = Racket::Misc::LV.new(1)
|
98
|
+
lv.values << p
|
99
|
+
lv.lengths << p.length
|
100
|
+
q.value << lv.encode
|
101
|
+
end
|
102
|
+
q.types << type
|
103
|
+
q.types << klass
|
104
|
+
q
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# $Id: hsrp.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 L5
|
30
|
+
# Hot Standby Router Protocol: HSRP
|
31
|
+
#
|
32
|
+
# RFC2281 (http://www.faqs.org/rfcs/rfc2281.html)
|
33
|
+
class HSRP < RacketPart
|
34
|
+
HSRP_HELLO = 0
|
35
|
+
HSRP_COUP = 1
|
36
|
+
HSRP_RESIGN = 2
|
37
|
+
|
38
|
+
HSRP_INITIAL = 0
|
39
|
+
HSRP_LEARN = 1
|
40
|
+
HSRP_LISTEN = 2
|
41
|
+
HSRP_SPEAK = 4
|
42
|
+
HSRP_STANDBY = 8
|
43
|
+
HSRP_ACTIVE = 16
|
44
|
+
|
45
|
+
# Version of the HSRP message contained in this packet. Defaults to 0
|
46
|
+
unsigned :version, 8
|
47
|
+
# Type of the HSRP message contained in this packet.
|
48
|
+
unsigned :opcode, 8
|
49
|
+
# Current state of the router sending the message
|
50
|
+
unsigned :state, 8
|
51
|
+
# Time between the hello messages that this router sends. Obviously only
|
52
|
+
# useful in hello messages
|
53
|
+
unsigned :hellotime, 8
|
54
|
+
# Length of time that this hello message should be considered valid.
|
55
|
+
# Obviously only useful in hello messages.
|
56
|
+
unsigned :holdtime, 8, { :default => 10 }
|
57
|
+
# Priority used to determine active and standby routers. Higher priorities
|
58
|
+
# win, but a higher IP address wins in the event of a tie.
|
59
|
+
unsigned :priority, 8
|
60
|
+
# Standby group
|
61
|
+
unsigned :group, 8
|
62
|
+
# reserved, never used, should be 0
|
63
|
+
unsigned :reserved, 8
|
64
|
+
# Clear-text, 8-character reused password. Defaults to 'cisco'
|
65
|
+
text :password, 64, { :default => 'cisco' }
|
66
|
+
# Virtual IP address used by this group
|
67
|
+
octets :vip, 32
|
68
|
+
# Payload. Generally unused.
|
69
|
+
rest :payload
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# $Id: misc.rb 167 2009-12-18 05:26:57Z 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 L5
|
30
|
+
# Miscelaneous L3 helper methods
|
31
|
+
module Misc
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# $Id: ntp.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 L5
|
30
|
+
# Network time protocol
|
31
|
+
class NTP < RacketPart
|
32
|
+
# Leap Indicator
|
33
|
+
unsigned :leap, 2
|
34
|
+
# Local Clock Status
|
35
|
+
unsigned :status, 6
|
36
|
+
# Reference Clock Type
|
37
|
+
unsigned :type, 8
|
38
|
+
# Local Clock Precision
|
39
|
+
unsigned :precision, 16
|
40
|
+
# Estimated Error
|
41
|
+
unsigned :error_est, 32
|
42
|
+
# Estimated Drift Rate
|
43
|
+
unsigned :drift_est, 32
|
44
|
+
# Reference Clock Identifier
|
45
|
+
unsigned :ref_clock, 32
|
46
|
+
# Reference Timestamp
|
47
|
+
unsigned :ref_ts, 64
|
48
|
+
# Originate Timestamp
|
49
|
+
unsigned :orig_ts, 64
|
50
|
+
# Receive Timestamp
|
51
|
+
unsigned :rec_ts, 64
|
52
|
+
# Transmit Timestamp
|
53
|
+
unsigned :tr_ts, 64
|
54
|
+
rest :payload
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
# vim: set ts=2 et sw=2:
|
59
|
+
|
data/lib/racket/misc.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# $Id: misc.rb 125 2009-11-29 01:15:58Z 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
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'misc/*.rb')).each { |f| require f }
|
29
|
+
|
30
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# $Id: lv.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 Misc
|
30
|
+
# Simple class to represent data structures that
|
31
|
+
# consist of an arbitrary number of length value pairs.
|
32
|
+
class LV
|
33
|
+
# An array containing the values parsed from this LV
|
34
|
+
attr_accessor :values
|
35
|
+
# The lengths of the values parsed from this LV
|
36
|
+
attr_accessor :lengths
|
37
|
+
# everything else
|
38
|
+
attr_accessor :rest
|
39
|
+
|
40
|
+
# Create a new LV object whose L sizes are specified in +args+
|
41
|
+
def initialize(*args)
|
42
|
+
@sizes = args
|
43
|
+
@values = []
|
44
|
+
@lengths = []
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def decode(data)
|
49
|
+
n = 0
|
50
|
+
values = []
|
51
|
+
lengths = []
|
52
|
+
@sizes.each do |s|
|
53
|
+
# XXX: raise an error here if there is not enough data to
|
54
|
+
# unpack this next LV
|
55
|
+
lengths[n] = data.unpack("#{punpack_string(s)}")[0]
|
56
|
+
data = data.slice(s, data.length)
|
57
|
+
values[n] = data.unpack("a#{lengths[n]}")[0]
|
58
|
+
data = data.slice(lengths[n], data.length)
|
59
|
+
n += 1
|
60
|
+
end
|
61
|
+
|
62
|
+
# data now contains "rest"
|
63
|
+
[lengths, values, data]
|
64
|
+
end
|
65
|
+
|
66
|
+
def decode!(data)
|
67
|
+
@lengths, @values, @rest = self.decode(data)
|
68
|
+
end
|
69
|
+
|
70
|
+
def encode
|
71
|
+
n = 0
|
72
|
+
s = ""
|
73
|
+
@lengths.each do |l|
|
74
|
+
s << [l].pack("#{punpack_string(@sizes[n])}")
|
75
|
+
s << [@values[n]].pack("a#{l}")
|
76
|
+
n += 1
|
77
|
+
end
|
78
|
+
s
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_s
|
82
|
+
encode
|
83
|
+
end
|
84
|
+
|
85
|
+
def to_str
|
86
|
+
encode
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def punpack_string(size)
|
92
|
+
s = ""
|
93
|
+
case size
|
94
|
+
when 1
|
95
|
+
s << "C"
|
96
|
+
when 2
|
97
|
+
s << "n"
|
98
|
+
when 4
|
99
|
+
s << "N"
|
100
|
+
else
|
101
|
+
raise ArgumentError, "Size #{s} not supported"
|
102
|
+
end
|
103
|
+
s
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
# vim: set ts=2 et sw=2:
|
@@ -0,0 +1,61 @@
|
|
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
|
+
module Racket
|
29
|
+
module Misc
|
30
|
+
# Return a number that is at most size bits long
|
31
|
+
def Misc.randbits(size)
|
32
|
+
bits = 0
|
33
|
+
srand Time.now.usec
|
34
|
+
0.upto(size-1) {
|
35
|
+
bits <<= 1
|
36
|
+
bits |= rand(2)
|
37
|
+
}
|
38
|
+
bits
|
39
|
+
end
|
40
|
+
|
41
|
+
# Return a byte that is at most size bytes long
|
42
|
+
def Misc.randbytes(size)
|
43
|
+
bytes = 0
|
44
|
+
0.upto(size-1) {
|
45
|
+
bytes <<= 8
|
46
|
+
bytes |= randbits(8)
|
47
|
+
}
|
48
|
+
bytes
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return a string that is at most size characters long
|
52
|
+
def Misc.randstring(size)
|
53
|
+
s = ""
|
54
|
+
0.upto(size-1) {
|
55
|
+
s += sprintf("%c", randbytes(1))
|
56
|
+
}
|
57
|
+
s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
# vim: set ts=2 et sw=2:
|