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.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/README +76 -0
  3. data/examples/arp-send +24 -0
  4. data/examples/arp-send2 +30 -0
  5. data/examples/cdp +39 -0
  6. data/examples/cdp-spew +52 -0
  7. data/examples/dhcp +42 -0
  8. data/examples/dhcp-spew +48 -0
  9. data/examples/dns +38 -0
  10. data/examples/egp +30 -0
  11. data/examples/hsrp +43 -0
  12. data/examples/hsrp_takeover +69 -0
  13. data/examples/icmp-recv +34 -0
  14. data/examples/icmp-spew +50 -0
  15. data/examples/icmpv6 +84 -0
  16. data/examples/icmpv6-spew +50 -0
  17. data/examples/igmpv1 +27 -0
  18. data/examples/igmpv2 +27 -0
  19. data/examples/igrp-send +25 -0
  20. data/examples/ipv6 +35 -0
  21. data/examples/nat-pimp +32 -0
  22. data/examples/ntp +38 -0
  23. data/examples/ntp2 +42 -0
  24. data/examples/sctp +32 -0
  25. data/examples/stp-send +21 -0
  26. data/examples/synflood +147 -0
  27. data/examples/tcp +43 -0
  28. data/examples/tcp2udp +65 -0
  29. data/examples/udp +46 -0
  30. data/examples/vrrp +34 -0
  31. data/examples/vtp +28 -0
  32. data/lib/racket/l2/eightotwodotthree.rb +48 -0
  33. data/lib/racket/l2/ethernet.rb +62 -0
  34. data/lib/racket/l2/llc.rb +50 -0
  35. data/lib/racket/l2/misc.rb +67 -0
  36. data/lib/racket/l2/snap.rb +40 -0
  37. data/lib/racket/l2/vlan.rb +61 -0
  38. data/lib/racket/l2/vtp.rb +124 -0
  39. data/lib/racket/l2.rb +30 -0
  40. data/lib/racket/l3/arp.rb +63 -0
  41. data/lib/racket/l3/cdp.rb +85 -0
  42. data/lib/racket/l3/egp.rb +53 -0
  43. data/lib/racket/l3/ipv4.rb +132 -0
  44. data/lib/racket/l3/ipv6.rb +66 -0
  45. data/lib/racket/l3/misc.rb +159 -0
  46. data/lib/racket/l3/stp.rb +81 -0
  47. data/lib/racket/l3.rb +30 -0
  48. data/lib/racket/l4/gre.rb +65 -0
  49. data/lib/racket/l4/icmp.rb +295 -0
  50. data/lib/racket/l4/icmpv6.rb +446 -0
  51. data/lib/racket/l4/igmpv1.rb +79 -0
  52. data/lib/racket/l4/igmpv2.rb +76 -0
  53. data/lib/racket/l4/igrp.rb +138 -0
  54. data/lib/racket/l4/misc.rb +35 -0
  55. data/lib/racket/l4/sctp.rb +163 -0
  56. data/lib/racket/l4/tcp.rb +152 -0
  57. data/lib/racket/l4/udp.rb +81 -0
  58. data/lib/racket/l4/vrrp.rb +95 -0
  59. data/lib/racket/l4.rb +30 -0
  60. data/lib/racket/l5/bootp.rb +106 -0
  61. data/lib/racket/l5/dns.rb +110 -0
  62. data/lib/racket/l5/hsrp.rb +73 -0
  63. data/lib/racket/l5/misc.rb +35 -0
  64. data/lib/racket/l5/nat-pmp.rb +41 -0
  65. data/lib/racket/l5/ntp.rb +59 -0
  66. data/lib/racket/l5.rb +30 -0
  67. data/lib/racket/misc/lv.rb +108 -0
  68. data/lib/racket/misc/misc.rb +62 -0
  69. data/lib/racket/misc/orderedhash.rb +63 -0
  70. data/lib/racket/misc/raw.rb +35 -0
  71. data/lib/racket/misc/tlv.rb +103 -0
  72. data/lib/racket/misc/vt.rb +114 -0
  73. data/lib/racket/misc.rb +30 -0
  74. data/lib/racket/racket.rb +166 -0
  75. data/lib/racket/racketpart.rb +66 -0
  76. data/lib/racket.rb +4 -0
  77. data/test/l2/ts_ethernet.rb +22 -0
  78. data/test/l2/ts_misc.rb +23 -0
  79. data/test/l2/ts_vlan.rb +15 -0
  80. data/test/l3/ts_ipv4.rb +44 -0
  81. data/test/l3/ts_ipv6.rb +26 -0
  82. data/test/l3/ts_misc.rb +31 -0
  83. data/test/l4/ts_icmp.rb +38 -0
  84. data/test/l4/ts_tcp.rb +55 -0
  85. data/test/l4/ts_udp.rb +40 -0
  86. data/test/misc/ts_lv.rb +59 -0
  87. data/test/misc/ts_orderedhash.rb +33 -0
  88. data/test/misc/ts_tlv.rb +47 -0
  89. data/test/misc/ts_vt.rb +56 -0
  90. data/test/ts_all.rb +14 -0
  91. metadata +153 -0
@@ -0,0 +1,138 @@
1
+ # $Id: igrp.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
+ # Internet Gateway Routing Protocol: IGRP
31
+ #
32
+ # http://www.cisco.com/warp/public/103/5.html
33
+ #
34
+ # Every routing entry has the following fields:
35
+ # uchar number[3]; /* 3 significant octets of IP address */
36
+ # uchar delay[3]; /* delay, in tens of microseconds */
37
+ # uchar bandwidth[3]; /* bandwidth, in units of 1 Kbit/sec */
38
+ # uchar mtu[2]; /* MTU, in octets */
39
+ # uchar reliability; /* percent packets successfully tx/rx */
40
+ # uchar load; /* percent of channel occupied */
41
+ # uchar hopcount; /* hop count */
42
+ class IGRP < RacketPart
43
+ IGRP_UPDATE = 1
44
+ IGRP_REQUEST = 2
45
+
46
+ # Version of the IGRP message contained in this packet.
47
+ # Defaults to 1. Anything else is currently invalid
48
+ unsigned :version, 4, { :default => 1 }
49
+ # Type of the IGRP message contained in this packet.
50
+ unsigned :opcode, 4
51
+ # Serial number which is incremented whenever the routing
52
+ # table is updated.
53
+ unsigned :edition, 8
54
+ # Autonomous system number
55
+ unsigned :asystem, 16
56
+ # Number of interior routes contained in this update message
57
+ unsigned :ninterior, 16
58
+ # Number of system routes contained in this update message
59
+ unsigned :nsystem, 16
60
+ # Number of exterior routes contained in this update message
61
+ unsigned :nexterior, 16
62
+ # Checksum (IP)
63
+ unsigned :checksum, 16
64
+ # Payload. Generally unused.
65
+ rest :payload
66
+
67
+ def initialize(*args)
68
+ @interior_routes = []
69
+ @system_routes = []
70
+ @exterior_routes = []
71
+ super
72
+ end
73
+
74
+ # Add a system route to this IGRP packet
75
+ def add_system(ip, delay, bw, mtu, rel, load, hop)
76
+ @system_routes << add_entry(ip, delay, bw, mtu, rel, load, hop)
77
+ self.nsystem += 1
78
+ end
79
+
80
+ # Add an interior route to this IGRP packet
81
+ def add_interior(ip, delay, bw, mtu, rel, load, hop)
82
+ @interior_routes << add_entry(ip, delay, bw, mtu, rel, load, hop)
83
+ self.ninterior += 1
84
+ end
85
+
86
+ # Add an exterior route to this IGRP packet
87
+ def add_exterior(ip, delay, bw, mtu, rel, load, hop)
88
+ @exterior_routes << add_entry(ip, delay, bw, mtu, rel, load, hop)
89
+ self.nexterior += 1
90
+ end
91
+
92
+ # Compute and set the checksum of this IGRP packet
93
+ def checksum!
94
+ self.checksum = compute_checksum
95
+ end
96
+
97
+ # Is the checksum correct?
98
+ def checksum?
99
+ self.checksum == compute_checksum
100
+ end
101
+
102
+ # Fix everything up in preparation for sending.
103
+ def fix!
104
+ [@interior_routes, @system_routes, @exterior_routes].flatten.each do |r|
105
+ self.payload += r
106
+ end
107
+ checksum!
108
+ end
109
+
110
+ private
111
+
112
+ def add_entry(ip, delay, bw, mtu, rel, load, hop)
113
+ # tmp should be 12 bytes long
114
+ tmp = ((((((((ip << 8*3) | delay) << 8*3) | bw) << 8*2) | mtu) << 8*1)| rel)
115
+ # now split it up into 3, 4-byte chunks suitable for an 'N' pack
116
+ tmp1 = (0xffffffff0000000000000000 & tmp) >> 8*8
117
+ tmp2 = (0x00000000ffffffff00000000 & tmp) >> 8*4
118
+ tmp3 = (0x0000000000000000ffffffff & tmp)
119
+ [tmp1, tmp2, tmp3, load, hop].pack("NNNCC")
120
+ end
121
+
122
+ def compute_checksum
123
+ tmp = []
124
+ tmp << (self.version << 4 | self.opcode)
125
+ tmp << self.edition
126
+ tmp << self.asystem
127
+ tmp << self.ninterior
128
+ tmp << self.nsystem
129
+ tmp << self.nexterior
130
+ tmp << 0
131
+ tmp << payload
132
+ L3::Misc.checksum(tmp.pack("Cnnnnnna*"))
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,35 @@
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 L4
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,163 @@
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 L4
30
+ # Stream Control Transmission Protocol
31
+ # http://tools.ietf.org/html/rfc4960
32
+ require 'zlib'
33
+ class SCTP < RacketPart
34
+ # Source port
35
+ unsigned :src_port, 16
36
+ # Destination port
37
+ unsigned :dst_port, 16
38
+ # Verification tag
39
+ unsigned :verification, 32
40
+ # Checksum
41
+ unsigned :checksum, 32
42
+ rest :payload
43
+
44
+ # Add a new SCTP chunk (see http://tools.ietf.org/html/rfc4960)
45
+ def add_chunk(type, flags, length, data)
46
+ @chunks << [ type, flags, length, data ]
47
+ end
48
+
49
+ def checksum?
50
+ self.checksum == compute_checksum
51
+ end
52
+
53
+ def checksum!
54
+ self.checksum = compute_checksum
55
+ end
56
+
57
+ # (really, just set the checksum)
58
+ def fix!
59
+ self.payload = ""
60
+ @chunks.each do |c|
61
+ self.payload += c.pack("CCna*")
62
+ end
63
+ self.checksum!
64
+ end
65
+
66
+ def initialize(*args)
67
+ @chunks = []
68
+ super
69
+ end
70
+
71
+ private
72
+ def compute_checksum
73
+ crc_c = [
74
+ 0x00000000, 0xF26B8303, 0xE13B70F7, 0x1350F3F4,
75
+ 0xC79A971F, 0x35F1141C, 0x26A1E7E8, 0xD4CA64EB,
76
+ 0x8AD958CF, 0x78B2DBCC, 0x6BE22838, 0x9989AB3B,
77
+ 0x4D43CFD0, 0xBF284CD3, 0xAC78BF27, 0x5E133C24,
78
+ 0x105EC76F, 0xE235446C, 0xF165B798, 0x030E349B,
79
+ 0xD7C45070, 0x25AFD373, 0x36FF2087, 0xC494A384,
80
+ 0x9A879FA0, 0x68EC1CA3, 0x7BBCEF57, 0x89D76C54,
81
+ 0x5D1D08BF, 0xAF768BBC, 0xBC267848, 0x4E4DFB4B,
82
+ 0x20BD8EDE, 0xD2D60DDD, 0xC186FE29, 0x33ED7D2A,
83
+ 0xE72719C1, 0x154C9AC2, 0x061C6936, 0xF477EA35,
84
+ 0xAA64D611, 0x580F5512, 0x4B5FA6E6, 0xB93425E5,
85
+ 0x6DFE410E, 0x9F95C20D, 0x8CC531F9, 0x7EAEB2FA,
86
+ 0x30E349B1, 0xC288CAB2, 0xD1D83946, 0x23B3BA45,
87
+ 0xF779DEAE, 0x05125DAD, 0x1642AE59, 0xE4292D5A,
88
+ 0xBA3A117E, 0x4851927D, 0x5B016189, 0xA96AE28A,
89
+ 0x7DA08661, 0x8FCB0562, 0x9C9BF696, 0x6EF07595,
90
+ 0x417B1DBC, 0xB3109EBF, 0xA0406D4B, 0x522BEE48,
91
+ 0x86E18AA3, 0x748A09A0, 0x67DAFA54, 0x95B17957,
92
+ 0xCBA24573, 0x39C9C670, 0x2A993584, 0xD8F2B687,
93
+ 0x0C38D26C, 0xFE53516F, 0xED03A29B, 0x1F682198,
94
+ 0x5125DAD3, 0xA34E59D0, 0xB01EAA24, 0x42752927,
95
+ 0x96BF4DCC, 0x64D4CECF, 0x77843D3B, 0x85EFBE38,
96
+ 0xDBFC821C, 0x2997011F, 0x3AC7F2EB, 0xC8AC71E8,
97
+ 0x1C661503, 0xEE0D9600, 0xFD5D65F4, 0x0F36E6F7,
98
+ 0x61C69362, 0x93AD1061, 0x80FDE395, 0x72966096,
99
+ 0xA65C047D, 0x5437877E, 0x4767748A, 0xB50CF789,
100
+ 0xEB1FCBAD, 0x197448AE, 0x0A24BB5A, 0xF84F3859,
101
+ 0x2C855CB2, 0xDEEEDFB1, 0xCDBE2C45, 0x3FD5AF46,
102
+ 0x7198540D, 0x83F3D70E, 0x90A324FA, 0x62C8A7F9,
103
+ 0xB602C312, 0x44694011, 0x5739B3E5, 0xA55230E6,
104
+ 0xFB410CC2, 0x092A8FC1, 0x1A7A7C35, 0xE811FF36,
105
+ 0x3CDB9BDD, 0xCEB018DE, 0xDDE0EB2A, 0x2F8B6829,
106
+ 0x82F63B78, 0x709DB87B, 0x63CD4B8F, 0x91A6C88C,
107
+ 0x456CAC67, 0xB7072F64, 0xA457DC90, 0x563C5F93,
108
+ 0x082F63B7, 0xFA44E0B4, 0xE9141340, 0x1B7F9043,
109
+ 0xCFB5F4A8, 0x3DDE77AB, 0x2E8E845F, 0xDCE5075C,
110
+ 0x92A8FC17, 0x60C37F14, 0x73938CE0, 0x81F80FE3,
111
+ 0x55326B08, 0xA759E80B, 0xB4091BFF, 0x466298FC,
112
+ 0x1871A4D8, 0xEA1A27DB, 0xF94AD42F, 0x0B21572C,
113
+ 0xDFEB33C7, 0x2D80B0C4, 0x3ED04330, 0xCCBBC033,
114
+ 0xA24BB5A6, 0x502036A5, 0x4370C551, 0xB11B4652,
115
+ 0x65D122B9, 0x97BAA1BA, 0x84EA524E, 0x7681D14D,
116
+ 0x2892ED69, 0xDAF96E6A, 0xC9A99D9E, 0x3BC21E9D,
117
+ 0xEF087A76, 0x1D63F975, 0x0E330A81, 0xFC588982,
118
+ 0xB21572C9, 0x407EF1CA, 0x532E023E, 0xA145813D,
119
+ 0x758FE5D6, 0x87E466D5, 0x94B49521, 0x66DF1622,
120
+ 0x38CC2A06, 0xCAA7A905, 0xD9F75AF1, 0x2B9CD9F2,
121
+ 0xFF56BD19, 0x0D3D3E1A, 0x1E6DCDEE, 0xEC064EED,
122
+ 0xC38D26C4, 0x31E6A5C7, 0x22B65633, 0xD0DDD530,
123
+ 0x0417B1DB, 0xF67C32D8, 0xE52CC12C, 0x1747422F,
124
+ 0x49547E0B, 0xBB3FFD08, 0xA86F0EFC, 0x5A048DFF,
125
+ 0x8ECEE914, 0x7CA56A17, 0x6FF599E3, 0x9D9E1AE0,
126
+ 0xD3D3E1AB, 0x21B862A8, 0x32E8915C, 0xC083125F,
127
+ 0x144976B4, 0xE622F5B7, 0xF5720643, 0x07198540,
128
+ 0x590AB964, 0xAB613A67, 0xB831C993, 0x4A5A4A90,
129
+ 0x9E902E7B, 0x6CFBAD78, 0x7FAB5E8C, 0x8DC0DD8F,
130
+ 0xE330A81A, 0x115B2B19, 0x020BD8ED, 0xF0605BEE,
131
+ 0x24AA3F05, 0xD6C1BC06, 0xC5914FF2, 0x37FACCF1,
132
+ 0x69E9F0D5, 0x9B8273D6, 0x88D28022, 0x7AB90321,
133
+ 0xAE7367CA, 0x5C18E4C9, 0x4F48173D, 0xBD23943E,
134
+ 0xF36E6F75, 0x0105EC76, 0x12551F82, 0xE03E9C81,
135
+ 0x34F4F86A, 0xC69F7B69, 0xD5CF889D, 0x27A40B9E,
136
+ 0x79B737BA, 0x8BDCB4B9, 0x988C474D, 0x6AE7C44E,
137
+ 0xBE2DA0A5, 0x4C4623A6, 0x5F16D052, 0xAD7D5351
138
+ ]
139
+
140
+ crc32 = ~0
141
+ header = [ self.src_port, self.dst_port, self.verification, 0 ].pack("nnNN")
142
+ puts "Header is #{header.length} bytes long, table is #{crc_c.size} big"
143
+ header.each_byte do |b|
144
+ crc32 = (crc32 >> 8) ^ crc_c[(crc32^b) & 0xFF]
145
+ puts "crc32 is now #{crc32}"
146
+ end
147
+
148
+ crc32 = ~crc32
149
+
150
+ byte0 = crc32 & 0xff
151
+ byte1 = (crc32 >> 8) & 0xff
152
+ byte2 = (crc32 >> 16) & 0xff
153
+ byte3 = (crc32 >> 24) & 0xff
154
+
155
+ crc32 = ((byte0 << 24) |
156
+ (byte1 << 16) |
157
+ (byte2 << 8) |
158
+ byte3)
159
+ end
160
+ end
161
+ end
162
+ end
163
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,152 @@
1
+ # $Id: tcp.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
+ # Transmission Control Protocol: TCP
31
+ #
32
+ # RFC793 (http://www.faqs.org/rfcs/rfc793.html)
33
+ class TCP < RacketPart
34
+ # Source port
35
+ unsigned :src_port, 16
36
+ # Destination port
37
+ unsigned :dst_port, 16
38
+ # Sequence number
39
+ unsigned :seq, 32
40
+ # Acknowledgement number
41
+ unsigned :ack, 32
42
+ # Data Offset
43
+ unsigned :offset, 4
44
+ # Reserved
45
+ unsigned :reserved, 4
46
+ # CWR
47
+ unsigned :flag_cwr, 1
48
+ # ECE
49
+ unsigned :flag_ece, 1
50
+ # URG
51
+ unsigned :flag_urg, 1
52
+ # ACK
53
+ unsigned :flag_ack, 1
54
+ # PSH
55
+ unsigned :flag_psh, 1
56
+ # RST
57
+ unsigned :flag_rst, 1
58
+ # SYN
59
+ unsigned :flag_syn, 1
60
+ # FIN
61
+ unsigned :flag_fin, 1
62
+ # Window size
63
+ unsigned :window, 16
64
+ # Checksum
65
+ unsigned :checksum, 16
66
+ # Urgent pointer
67
+ unsigned :urg, 16
68
+ # Payload
69
+ rest :payload
70
+
71
+ # Add an TCP option to this TCP object.
72
+ # All rejiggering will happen when the call to fix!
73
+ # happens automagically.
74
+ # If the result is not on a 32-bit boundry, pad with NOPs.
75
+ def add_option(number, value)
76
+ t = Misc::TLV.new(1,1)
77
+ t.type = number
78
+ t.value = value
79
+ t.length = value.length + 2
80
+ opts = t.encode
81
+ opts += ("\x01" * (4-(opts.size % 4))) if (opts.size % 4) != 0
82
+ @options << opts
83
+ end
84
+
85
+ # Add a raw TCP option to this packet. It is entirely your
86
+ # responsibility (or irresponsibility, as the case may be) to
87
+ # properly pad or otherwise define this
88
+ def add_raw_option(value)
89
+ @options << value
90
+ end
91
+
92
+ # Check the checksum for this TCP packet
93
+ def checksum?(ip_src, ip_dst)
94
+ self.checksum == compute_checksum(ip_src, ip_dst)
95
+ end
96
+
97
+ # Compute and set the checksum for this TCP packet
98
+ def checksum!(ip_src, ip_dst)
99
+ self.checksum = compute_checksum(ip_src, ip_dst)
100
+ end
101
+
102
+ # Fix this packet up for proper sending. Sets the length
103
+ # and checksum properly.
104
+ def fix!(ip_src, ip_dst, next_payload)
105
+ newpayload = @options.join
106
+
107
+ # pad to a multiple of 32 bits
108
+ if ((self.class.bit_length/8 + newpayload.length) % 4 != 0)
109
+ # fill the beginning as needed with NOPs
110
+ while ((self.class.bit_length/8 + newpayload.length) % 4 != 4)
111
+ puts (self.class.bit_length/8 + newpayload.length) % 4
112
+ newpayload = "\x01#{newpayload}"
113
+ end
114
+ end
115
+
116
+ self.payload = newpayload + self.payload + next_payload
117
+ self.offset = self.class.bit_length/32 + newpayload.length/4
118
+ self.checksum!(ip_src, ip_dst)
119
+ self.payload = newpayload
120
+ end
121
+
122
+ def initialize(*args)
123
+ @options = []
124
+ super
125
+ @autofix = false
126
+ end
127
+
128
+ private
129
+ # Compute the checksum for this TCP packet
130
+ def compute_checksum(ip_src, ip_dst)
131
+ tmp = self.offset << 12
132
+ tmp = tmp | (0x0f00 & (self.reserved << 8))
133
+ tmp = tmp | (0x00ff & (
134
+ (self.flag_cwr << 7 & 0b10000000) +
135
+ (self.flag_ece << 6 & 0b01000000) +
136
+ (self.flag_urg << 5 & 0b00100000) +
137
+ (self.flag_ack << 4 & 0b00010000) +
138
+ (self.flag_psh << 3 & 0b00001000) +
139
+ (self.flag_rst << 2 & 0b00000100) +
140
+ (self.flag_syn << 1 & 0b00000010) +
141
+ (self.flag_fin << 0 & 0b00000001)
142
+ ))
143
+
144
+ pseudo = [L3::Misc.ipv42long(ip_src), L3::Misc.ipv42long(ip_dst), 6, self.class.bit_length/8 + self.payload.length ]
145
+ header = [self.src_port, self.dst_port, self.seq, self.ack, tmp,
146
+ self.window, 0, self.urg, self.payload]
147
+ L3::Misc.checksum((pseudo << header).flatten.pack("NNnnnnNNnnnna*"))
148
+ end
149
+ end
150
+ end
151
+ end
152
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,81 @@
1
+ # $Id: udp.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
+ # User Datagram Protocol: UDP
31
+ #
32
+ # RFC768 (http://www.faqs.org/rfcs/rfc768.html)
33
+ class UDP < RacketPart
34
+ # Source Port
35
+ unsigned :src_port, 16
36
+ # Destination Port
37
+ unsigned :dst_port, 16
38
+ # Datagram Length
39
+ unsigned :len, 16
40
+ # Checksum
41
+ unsigned :checksum, 16
42
+ # Payload
43
+ rest :payload
44
+
45
+ # Check the checksum for this UDP datagram
46
+ def checksum?(src_ip, dst_ip)
47
+ self.checksum == 0 || (self.checksum == compute_checksum(src_ip, dst_ip))
48
+ end
49
+
50
+ # Compute and set the checksum for this UDP datagram
51
+ def checksum!(src_ip, dst_ip)
52
+ # set the checksum to 0 for usage in the pseudo header...
53
+ self.checksum = 0
54
+ self.checksum = compute_checksum(src_ip, dst_ip)
55
+ end
56
+
57
+ # Fix this packet up for proper sending. Sets the length
58
+ # and checksum properly.
59
+ def fix!(src_ip, dst_ip)
60
+ self.len = self.class.bit_length/8 + self.payload.length
61
+ self.checksum!(src_ip, dst_ip)
62
+ end
63
+
64
+ def initialize(*args)
65
+ super
66
+ @autofix = false
67
+ end
68
+
69
+ private
70
+ # Compute the checksum for this UDP datagram
71
+ def compute_checksum(src_ip, dst_ip)
72
+ # pseudo header used for checksum calculation as per RFC 768
73
+ pseudo = [L3::Misc.ipv42long(src_ip), L3::Misc.ipv42long(dst_ip), 17, self.payload.length + self.class.bit_length/8 ]
74
+ header = [self.src_port, self.dst_port, self.payload.length + self.class.bit_length/8, 0, self.payload]
75
+ L3::Misc.checksum((pseudo << header).flatten.pack("NNnnnnnna*"))
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+ # vim: set ts=2 et sw=2:
@@ -0,0 +1,95 @@
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 L4
30
+ # Virtual Router Redundancy Protocol (VRRP)
31
+ # http://tools.ietf.org/html/rfc2338
32
+ # http://tools.ietf.org/html/rfc3768
33
+ class VRRP < RacketPart
34
+ # Version
35
+ unsigned :version, 4
36
+ # VRRP packet type
37
+ unsigned :type, 4
38
+ # Virtual Router Identifier (VRID)
39
+ unsigned :id, 8
40
+ # the sending VRRP router's priority for the virtual router.
41
+ # Higher values equal higher priority.
42
+ unsigned :priority, 8
43
+ # Total number of IPs contained in this VRRP message
44
+ unsigned :num_ips, 8
45
+ # Authentication type (0, 1, 2)
46
+ unsigned :auth_type, 8
47
+ # Advertisement interval
48
+ unsigned :interval, 8
49
+ # Checksum
50
+ unsigned :checksum, 16
51
+ rest :payload
52
+
53
+ # Add a new IP to this message
54
+ def add_ip(ip)
55
+ @ips << L3::Misc.ipv42long(ip)
56
+ end
57
+
58
+ # Add authentication data
59
+ def add_auth(authdata)
60
+ @authdata = authdata[0,8].ljust(32, "\x00")
61
+ end
62
+
63
+ # Validate the checksum
64
+ def checksum?
65
+ self.checksum == compute_checksum
66
+ end
67
+
68
+ # compute and set the checksum
69
+ def checksum!
70
+ self.checksum = compute_checksum
71
+ end
72
+
73
+ # (really, just set the checksum)
74
+ def fix!
75
+ self.payload = [@ips, @authdata].flatten.pack("N#{@ips.size}a*")
76
+ self.num_ips = @ips.size
77
+ self.checksum!
78
+ end
79
+
80
+ def initialize(*args)
81
+ @ips = []
82
+ @authdata = ""
83
+ super
84
+ end
85
+
86
+ private
87
+ def compute_checksum
88
+ # pseudo header used for checksum calculation as per RFC 768
89
+ pseudo = [ ((self.version << 4) | self.type), self.id, self.priority, self.num_ips, self.auth_type, self.interval, 0, self.payload ]
90
+ L3::Misc.checksum(pseudo.pack("CCCCCCna*"))
91
+ end
92
+ end
93
+ end
94
+ end
95
+ # vim: set ts=2 et sw=2:
data/lib/racket/l4.rb ADDED
@@ -0,0 +1,30 @@
1
+ # $Id: l4.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__), 'l4/*.rb')).each { |f| require f }
29
+
30
+ # vim: set ts=2 et sw=2: