-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdu.go
472 lines (442 loc) · 13.3 KB
/
pdu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package main
import (
"encoding/binary"
"fmt"
"io"
"log"
)
const (
// PDU Types
serialNotify uint8 = 0
serialQuery uint8 = 1
resetQuery uint8 = 2
cacheResponse uint8 = 3
ipv4Prefix uint8 = 4
ipv6Prefix uint8 = 6
endOfData uint8 = 7
cacheReset uint8 = 8
routerKey uint8 = 9
errorReport uint8 = 10
// protocol versions
version0 uint8 = 0
version1 uint8 = 1
minPDULength = 8
headPDULength = 2
// flags
withdraw uint8 = 0
announce uint8 = 1
)
// headerPDU is used to extract the header of each incoming PDU
type headerPDU struct {
Version uint8
Ptype uint8
}
type serialNotifyPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | Session ID |
| 1 | 0 | |
+-------------------------------------------+
| |
| Length=12 |
| |
+-------------------------------------------+
| |
| Serial Number |
| |
`-------------------------------------------'
*/
Session uint16
Serial uint32
}
func (p *serialNotifyPDU) serialize(wr io.Writer) {
log.Printf("Sending a serial notify PDU: %+v\n", *p)
pdu := struct {
version uint8
ptype uint8
session uint16
length uint32
serial uint32
}{
version1,
serialNotify,
p.Session,
uint32(12),
p.Serial,
}
binary.Write(wr, binary.BigEndian, pdu)
}
type serialQueryPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | Session ID |
| 1 | 1 | |
+-------------------------------------------+
| |
| Length=12 |
| |
+-------------------------------------------+
| |
| Serial Number |
| |
`-------------------------------------------'
*/
Session uint16
Length uint32
Serial uint32
}
type resetQueryPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | zero |
| 1 | 2 | |
+-------------------------------------------+
| |
| Length=8 |
| |
`-------------------------------------------'
*/
Zero uint16
Length uint32
}
type cacheResponsePDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | Session ID |
| 1 | 3 | |
+-------------------------------------------+
| |
| Length=8 |
| |
`-------------------------------------------'
*/
sessionID uint16
}
func (p *cacheResponsePDU) serialize(wr io.Writer) {
log.Printf("Sending a cache Response PDU: %v\n", *p)
pdu := struct {
version uint8
ptype uint8
session uint16
length uint32
}{
version1,
cacheResponse,
p.sessionID,
uint32(8),
}
binary.Write(wr, binary.BigEndian, pdu)
}
type ipv4PrefixPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | zero |
| 1 | 4 | |
+-------------------------------------------+
| |
| Length=20 |
| |
+-------------------------------------------+
| | Prefix | Max | |
| Flags | Length | Length | zero |
| | 0..32 | 0..32 | |
+-------------------------------------------+
| |
| IPv4 Prefix |
| |
+-------------------------------------------+
| |
| Autonomous System Number |
| |
`-------------------------------------------'
*/
flags uint8
min uint8
max uint8
prefix [4]byte
asn uint32
}
func (p *ipv4PrefixPDU) serialize(wr io.Writer) {
pdu := struct {
version uint8
ptype uint8
zero16 uint16
length uint32
flags uint8
min uint8
max uint8
zero8 uint8
prefix [4]byte
asn uint32
}{
version1,
ipv4Prefix,
uint16(0),
uint32(20),
p.flags,
p.min,
p.max,
uint8(0),
p.prefix,
p.asn,
}
binary.Write(wr, binary.BigEndian, pdu)
}
type ipv6PrefixPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | zero |
| 1 | 6 | |
+-------------------------------------------+
| |
| Length=32 |
| |
+-------------------------------------------+
| | Prefix | Max | |
| Flags | Length | Length | zero |
| | 0..128 | 0..128 | |
+-------------------------------------------+
| |
+--- ---+
| |
+--- IPv6 Prefix ---+
| |
+--- ---+
| |
+-------------------------------------------+
| |
| Autonomous System Number |
| |
`-------------------------------------------'
*/
flags uint8
min uint8
max uint8
prefix [16]byte
asn uint32
}
func (p *ipv6PrefixPDU) serialize(wr io.Writer) {
pdu := struct {
version uint8
ptype uint8
zero16 uint16
length uint32
flags uint8
min uint8
max uint8
zero8 uint8
prefix [16]byte
asn uint32
}{
version1,
ipv6Prefix,
uint16(0),
uint32(32),
p.flags,
p.min,
p.max,
uint8(0),
p.prefix,
p.asn,
}
binary.Write(wr, binary.BigEndian, pdu)
}
type endOfDataPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | Session ID |
| 1 | 7 | |
+-------------------------------------------+
| |
| Length=24 |
| |
+-------------------------------------------+
| |
| Serial Number |
| |
+-------------------------------------------+
| |
| Refresh Interval |
| |
+-------------------------------------------+
| |
| Retry Interval |
| |
+-------------------------------------------+
| |
| Expire Interval |
| |
`-------------------------------------------'
*/
session uint16
serial uint32
refresh uint32
retry uint32
expire uint32
}
func (p *endOfDataPDU) serialize(wr io.Writer) {
log.Printf("Sending end of data PDU: %v\n", *p)
pdu := struct {
version uint8
ptype uint8
session uint16
length uint32
serual uint32
refresh uint32
retry uint32
expire uint32
}{
version1,
endOfData,
p.session,
uint32(24),
p.serial,
p.refresh,
p.retry,
p.expire,
}
binary.Write(wr, binary.BigEndian, pdu)
}
type cacheResetPDU struct { /*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | zero |
| 1 | 8 | |
+-------------------------------------------+
| |
| Length=8 |
| |
`-------------------------------------------'
*/
}
func (p *cacheResetPDU) serialize(wr io.Writer) {
log.Printf("Sending a cache reset PDU: %v\n", *p)
pdu := struct {
version uint8
ptype uint8
zero uint16
length uint32
}{
version1,
cacheReset,
uint16(0),
uint32(8),
}
binary.Write(wr, binary.BigEndian, pdu)
}
type errorReportPDU struct {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | Error Code |
| 1 | 10 | |
+-------------------------------------------+
| |
| Length |
| |
+-------------------------------------------+
| |
| Length of Encapsulated PDU |
| |
+-------------------------------------------+
| |
~ Erroneous PDU ~
| |
+-------------------------------------------+
| |
| Length of Error Text |
| |
+-------------------------------------------+
| |
| Arbitrary Text |
| of |
~ Error Diagnostic Message ~
| |
`-------------------------------------------'
*/
code uint16
report string
}
func (p *errorReportPDU) serialize(wr io.Writer) {
log.Printf("Sending an error report PDU: %v\n", *p)
// length of encapped PDU 0 for now
// not encapping PDU, so empty field there
reportLength := len([]byte(p.report))
totalLength := 128 + reportLength
binary.Write(wr, binary.BigEndian, version1)
binary.Write(wr, binary.BigEndian, errorReport)
binary.Write(wr, binary.BigEndian, p.code)
binary.Write(wr, binary.BigEndian, totalLength)
binary.Write(wr, binary.BigEndian, uint32(0))
binary.Write(wr, binary.BigEndian, reportLength)
binary.Write(wr, binary.BigEndian, p.report)
}
func getSerialQueryPDU(pdu []byte) serialQueryPDU {
var q serialQueryPDU
q.Session = binary.BigEndian.Uint16(pdu[:2])
q.Length = binary.BigEndian.Uint32(pdu[2:6])
q.Serial = binary.BigEndian.Uint32(pdu[6:10])
return q
}
// getPDU will return a byte slice which contains a PDU.
func getPDU(r io.Reader) ([]byte, error) {
/*
0 8 16 24 31
.-------------------------------------------.
| Protocol | PDU | |
| Version | Type | Session ID |
+-------------------------------------------+
| |
| Length |
| |
`-------------------------------------------'
*/
buf := make([]byte, minPDULength)
if _, err := io.ReadFull(r, buf); err != nil {
return nil, err
}
// Read the rest of the PDU, minus the header.
length := binary.BigEndian.Uint32(buf[4:8]) - 8
if length > 0 {
lr := io.LimitReader(r, int64(length))
data := make([]byte, length)
if _, err := io.ReadFull(lr, data); err != nil {
return nil, err
}
buf = append(buf, data...)
}
return buf, nil
}
// decodePDUHeader does a size and version check. Otherwise it returns just the header.
func decodePDUHeader(pdu []byte) (headerPDU, error) {
var header headerPDU
if len(pdu) < headPDULength {
return header, fmt.Errorf("PDU headers have a minimin size of 2. PDU passed has length %d", len(pdu))
}
if int(pdu[0]) != 1 {
return header, fmt.Errorf("only version 1 is supported. PDU has version %d", int(pdu[0]))
}
header.Version = uint8(pdu[0])
header.Ptype = uint8(pdu[1])
// PDU types currently number from 0 to 10, excluding 5. Anything else is invalid.
if header.Ptype > 10 || header.Ptype == 5 {
return header, fmt.Errorf("unsupported pdu version received: %d", header.Ptype)
}
return header, nil
}