-
Notifications
You must be signed in to change notification settings - Fork 3
/
protocol.go
577 lines (483 loc) · 13.2 KB
/
protocol.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package cattp
//Package cattp implements CAT_TP packet decoding for pcap streams.
//It is supposed to support both, cleint and server side implementations as well in the future.
import(
"math/rand"
"fmt"
"crypto/md5"
"encoding/base64"
"strings"
"strconv"
)
const ( // PDU TYPES
UNK = iota
SYN = iota
SYNACK = iota
ACK = iota
RST = iota
DATAACK = iota
NUL = iota
EAK = iota
)
const ( // PDU Flags
FLAG_MASK = 0xFC
SYN_FLAG = 0x80
ACK_FLAG = 0x40
EAK_FLAG = 0x20
RST_FLAG = 0x10
NUL_FLAG = 0x08
SEG_FLAG = 0x04
)
const ( // Header Constants
BASE_HLEN = 18
SYN_HLEN = 5
)
const ( // Default Values
DefaultMaxPDUSize = 1024
DefaultMaxSDUSize = 512
DefaultSrcPort = 1
DefaultDestPort = 9000
DefaultWindowSize = 10
MaxHeaderLen = 255
MaxIdenficationLen = MaxHeaderLen - (BASE_HLEN + 4 + 1)
MaxEAKs = 118 /*(MaxHederLen - (BASE_HLEN))/2 */
)
type Header struct {
raw []byte
hdr [18]byte
synpduhdr [SYN_HLEN]byte
identification []byte
rstpduhdr byte
ackpduhdr [2]byte
eakpduhdr []byte
payload []byte
}
func NewSeqNo() uint16 {
return uint16(rand.Intn(0xffff))
}
func (p *Header) NeedsAck() bool { t:= p.Type(); return t== DATAACK || t == NUL || t == SYN }
func (p *Header) SupportsEAK() bool { return p.ACK() && !p.SYN() }
func (p* Header) Flags() byte { return p.hdr[0] & 0xfc }
func (p *Header) SYN() bool { return (p.hdr[0] & SYN_FLAG) > 0 }
func (p *Header) ACK() bool { return (p.hdr[0] & ACK_FLAG) > 0 }
func (p *Header) EAK() bool { return (p.hdr[0] & EAK_FLAG) > 0 }
func (p *Header) RST() bool { return (p.hdr[0] & RST_FLAG) > 0 }
func (p *Header) NUL() bool { return (p.hdr[0] & NUL_FLAG) > 0 }
func (p *Header) SEG() bool { return (p.hdr[0] & SEG_FLAG) > 0 }
func (p *Header) Type() int {
switch p.hdr[0] & FLAG_MASK {
case SYN_FLAG, SYN_FLAG | SEG_FLAG:
return SYN
case RST_FLAG, RST_FLAG | ACK_FLAG:
return RST
case SYN_FLAG | ACK_FLAG:
return SYNACK
case EAK_FLAG | ACK_FLAG,EAK_FLAG | ACK_FLAG | SEG_FLAG:
return EAK
case NUL_FLAG | ACK_FLAG:
return NUL
case ACK_FLAG:
if p.DataLen() > 0 {
return DATAACK
} else {
return ACK
}
default:
return UNK
}
}
func (p *Header) TypeS() (ret string) {
switch p.Type() {
case SYN:
ret = "SYN"
case SYNACK:
ret = "SYNACK"
case ACK:
ret = "ACK"
case DATAACK:
ret = "DATAACK"
case RST:
ret = "RST"
case NUL:
ret = "NUL"
case EAK:
ret = "EAK"
default:
ret = "UNK"
}
return
}
func (p *Header) Version() byte { return byte(p.hdr[0] & 0x03) }
func (p *Header) HeaderLen() uint8 { return uint8(p.hdr[3]) }
func (p *Header) SrcPort() uint16 { return (uint16(p.hdr[4]) << 8) | uint16(p.hdr[5]) }
func (p *Header) DestPort() uint16 { return (uint16(p.hdr[6]) << 8) | uint16(p.hdr[7]) }
func (p *Header) DataLen() uint16 { return (uint16(p.hdr[8]) << 8) | uint16(p.hdr[9]) }
func (p *Header) TotalLen() int { return len(p.raw) }
func (p *Header) SeqNo() uint16 { return (uint16(p.hdr[10]) << 8) | uint16(p.hdr[11]) }
func (p *Header) AckNo() uint16 { return (uint16(p.hdr[12]) << 8) | uint16(p.hdr[13]) }
func (p *Header) WindowSize() uint16 { return (uint16(p.hdr[14]) << 8) | uint16(p.hdr[15]) }
func (p *Header) CheckSum() uint16 { return (uint16(p.hdr[16]) << 8) | uint16(p.hdr[17]) }
func (p *Header) MaxPDUSize() uint16 { return (uint16(p.synpduhdr[0]) << 8) | uint16(p.synpduhdr[1]) }
func (p *Header) MaxSDUSize() uint16 { return (uint16(p.synpduhdr[2]) << 8) | uint16(p.synpduhdr[3]) }
func (p *Header) IdentificationLen() byte { return byte(p.synpduhdr[4]) }
func (p *Header) Identification() []byte { return p.identification }
func (p *Header) ReasonCode() byte { return p.rstpduhdr }
func (p *Header) Payload() []byte { return p.payload }
func (p *Header) ExtendedAcks() []uint16 {
ret := make([]uint16,int(len(p.eakpduhdr) / 2))
for i,_ := range ret {
idx := i * 2
ret[i] = (uint16(p.eakpduhdr[idx]) << 8) | uint16(p.eakpduhdr[idx+1])
}
return ret
}
// ReasonString returns a human readable message for the reason code of the RST packet.
func (p *Header) ReasonString() string {
switch p.ReasonCode() {
case 0: return "Normal ending;"
case 1: return "Connection set-up failed, illegal parameters;"
case 2: return "Temporarily unable to set up this connection;"
case 3: return "Requested Port not available;"
case 4: return "Unexpected PDU received;"
case 5: return "Maximum retries exceeded;"
case 6: return "Version not supported;"
default: return fmt.Sprintf("NOT_YET_IMPLEMENTED ReasonCode: %d",int(p.ReasonCode()))
}
}
func (p *Header) FlagString() string {
syn := "..."
ack := "..."
rst := "..."
nul := "..."
seg := "..."
eak := "..."
if p.SYN() {
syn = "SYN"
}
if p.ACK() {
ack = "ACK"
}
if p.RST() {
rst = "RST"
}
if p.EAK() {
eak = "EAK"
}
if p.NUL() {
nul = "NUL"
}
if p.SEG() {
seg = "SEG"
}
return syn + ack + eak + rst + nul + seg
}
func (p *Header) TypeString() string {
switch p.Type() {
case SYN,SYNACK:
return fmt.Sprintf("|il %3d",p.IdentificationLen())
case RST:
return fmt.Sprintf("|rc %2d|%s",int(p.ReasonCode()),p.ReasonString())
case EAK:
acks := p.ExtendedAcks()
sacks := make([]string,len(acks))
for i,a := range acks { sacks[i] = strconv.Itoa(int(a)) }
return fmt.Sprintf("|acks %s", strings.Join(sacks,","))
default:
return fmt.Sprintf("|-")
}
}
// HashString returns a hash over the entire cattp packet (udp payload)
func (p *Header) HashString() string {
bhash := md5.Sum(p.raw)
nhash:= make ([]byte,len(bhash))
for i,b := range bhash {
nhash[i] = b
}
return base64.StdEncoding.EncodeToString(nhash)
}
func (p *Header) BinaryString(mc ...int) string {
ret := OctetDumper.Dump(p.raw)
if len(mc) > 0 && len(ret) > mc[0] {
by := []byte(ret)
return string(by[:mc[0]])
}
return ret
}
func (p *Header) Raw() []byte {
if len(p.raw) <= 0 {
//TODO: more accurate
buf := make([]byte,DefaultMaxPDUSize)
p.raw = buf
c:=0
//Copy standatd header
copy(p.raw[:len(p.hdr)],p.hdr[:])
c+= len(p.hdr)
if p.SYN() {
copy(buf[c:],p.synpduhdr[:])
c+=len(p.synpduhdr)
copy(buf[c:],p.identification)
c+=len(p.identification)
}
if p.RST() {
buf[BASE_HLEN] = p.ReasonCode()
c++
}
if p.EAK() {
copy(buf[c:],p.eakpduhdr)
c+=len(p.eakpduhdr)
}
p.setHeaderLen(byte(c))
if len(p.payload) > 0 {
copy(buf[c:],p.payload)
c+=len(p.payload)
p.setDataLen(uint16(len(p.payload)))
}
p.raw = buf[:c]
}
return p.raw
}
// NewHeader creates a new cattp packet based on an existing pcap packet. Error is returned if
// the pcap packet is not a cattp packet.
func NewHeader(raw []byte) (ret *Header,err error) {
ret = &Header{raw: raw, eakpduhdr: make([]byte,0)}
if len(raw) < len(ret.hdr) {
return nil, fmt.Errorf("No cattp header. %d/%d",len(raw),BASE_HLEN)
}
c := 0 // Amount of consumed header bytes
for ;c<len(ret.hdr);c++ {
ret.hdr[c] = raw[c]
}
switch ret.Type() {
case SYN,SYNACK: //TODO: Asuming SYNACK has same pdu as SYN
//parse additional header for SYN PDU
for i:=0; i < len(ret.synpduhdr); {
ret.synpduhdr[i] = raw[c]
i++
c++
}
//parse variable Identification field.
ret.identification = raw[c:c+int(ret.synpduhdr[4])]
c+=int(ret.synpduhdr[4])
case ACK:
//Payload is just taken based on DataLen field.
case RST:
//just take the reason code here
ret.rstpduhdr = raw[c]
c++
case NUL:
// No Payload except for eack which is covered above.
case EAK:
toack := int(ret.HeaderLen()) - len(ret.hdr)
if toack % 2 != 0 || toack < 2{
return nil,fmt.Errorf("Invalid EAK header (%d byte eak header)", toack)
}
ret.eakpduhdr = raw[c:(c+toack)]
c+=toack
//TODO: deal with data
default:
//TODO: comment in once all other types have been implemented.
//Log.Printf("E|Unknown pdu type: %d",ret.Type())
}
if c != int(ret.HeaderLen()) {
err = fmt.Errorf("HeaderLen does not match %d/%d",c,ret.HeaderLen())
return
}
end := c + int(ret.DataLen())
if end != len(raw) {
err = fmt.Errorf("PacketLen does not match received data length: %d/%d. Possibly fragmentation error.",end,len(raw))
return
}
ret.payload = raw[c:end]
// Check checksum
if ret.CheckSum() != ret.computeCheckSum() {
err = fmt.Errorf("Checksum incorrect: %d/%d",ret.computeCheckSum(),ret.CheckSum())
}
return
}
func (p *Header) computeCheckSum() uint16 {
var sum uint32
var i uint
var l uint = uint(len(p.raw))
for i =0; i < l; i++ {
sum += uint32(p.raw[i]) << (8 * ((i+1)%2))
}
sum -= uint32(p.CheckSum())
fold := uint16((sum & 0xffff0000) >> 16)
sum+=uint32(fold)
return uint16(^sum)
}
func (p *Header) setShort(idx byte,d uint16) {
p.raw[idx] = byte((d & 0xff00) >> 8)
p.raw[idx + 1] = byte(d & 0x00ff)
p.hdr[idx] = p.raw[idx]
p.hdr[idx + 1] = p.raw[idx + 1]
}
func (p *Header) setFlags(flags byte, version byte) {
p.raw[0] = flags | version
p.hdr[0] = p.raw[0]
}
func (p *Header) setHeaderLen(l byte) { p.raw[3] = l; p.hdr[3] = l }
func (p *Header) setRFU(rfu uint16) { p.setShort(1,rfu) }
func (p *Header) setSrcPort(port uint16) { p.setShort(4,port) }
func (p *Header) setDestPort(port uint16) { p.setShort(6,port) }
func (p *Header) setDataLen(l uint16) { p.setShort(8,l) }
func (p *Header) setSeqNo(no uint16) { p.setShort(10,no) }
func (p *Header) setAckNo(no uint16) { p.setShort(12,no) }
func (p *Header) setWindowSize(size uint16) { p.setShort(14,size) }
func (p *Header) setCheckSum(sum uint16) { p.setShort(16,sum) }
func (p *Header) setReasonCode(c byte) { p.rstpduhdr = c }
func (p *Header) setExtendedAcks(eaks []uint16) {
if len(eaks) > 0 {
p.setFlags(p.Flags() | EAK_FLAG,p.Version())
p.eakpduhdr = make([]byte,2*len(eaks))
for i,eak := range eaks {
idx := i *2
p.eakpduhdr[idx] = byte( (eak & 0xff00) >> 8 )
p.eakpduhdr[idx + 1] = byte(eak & 0x00ff)
}
//Regenerate packet data
p.raw = nil
p.Raw()
}
}
func (p *Header) setData(d []byte) {
p.payload = make([]byte,len(d))
for i:=0;i < len(d);i++ {
p.payload[i] = d[i]
p.raw[BASE_HLEN + i] = d[i]
}
p.setDataLen(uint16(len(d)))
}
func (p *Header) setMaxPDUSize(size uint16) {
p.synpduhdr[0] = byte( ( size & 0xff00) >> 8 )
p.synpduhdr[1] = byte( size & 0x00ff)
p.raw[BASE_HLEN] = p.synpduhdr[0]
p.raw[BASE_HLEN + 1] = p.synpduhdr[1]
}
func (p *Header) setMaxSDUSize(size uint16) {
p.synpduhdr[2] = byte( ( size & 0xff00) >> 8 )
p.synpduhdr[3] = byte( size & 0x00ff)
p.raw[BASE_HLEN + 2] = p.synpduhdr[2]
p.raw[BASE_HLEN + 3] = p.synpduhdr[3]
}
func (p *Header) setIdentification(d []byte) {
l := len(d)
p.identification = make([]byte,l)
p.synpduhdr[4] = byte(l)
p.raw[BASE_HLEN + 4] = byte(l)
for i:=0;i < l;i++ {
p.raw[BASE_HLEN + SYN_HLEN + i] = d[i]
p.identification[i] = d[i]
}
}
func (p *Header) UpdateCheckSum() { p.setCheckSum(p.computeCheckSum()) }
func (p *Header) String() string {
return fmt.Sprintf( "|%s|%d > %d|seq %5d|ack %5d|ws %5d |hl %3d |dl %5d |chk %5d %s",
p.FlagString(),
p.SrcPort(),
p.DestPort(),
p.SeqNo(),
p.AckNo(),
p.WindowSize(),
p.HeaderLen(),
p.DataLen(),
p.CheckSum(),
p.TypeString(),
)
}
func New(flags, version byte,srcport, destport,seq,ack, wsize, datlen uint16) (ret *Header){
ret = &Header{}
ret.raw = make([]byte,BASE_HLEN + datlen)
ret.setFlags(flags,version)
ret.setRFU(0) //RFU not yet used
ret.setSrcPort(srcport)
ret.setDestPort(destport)
ret.setSeqNo(seq)
ret.setAckNo(ack)
ret.setWindowSize(wsize)
ret.setHeaderLen(BASE_HLEN)
ret.UpdateCheckSum()
return
}
func NewNUL(version byte,srcport, destport,seq,ack, wsize uint16) *Header{
return New( NUL_FLAG | ACK_FLAG,
version,
srcport,
destport,
seq,
ack,
wsize,
0)
}
func NewRST(version byte, srcport, destport,seq,ack uint16, rc byte) (ret *Header) {
ret = New( RST_FLAG,
version,
srcport,
destport,
seq,
ack,
0,
1 )
ret.setReasonCode(0)
ret.setHeaderLen(BASE_HLEN + 1)
ret.UpdateCheckSum()
return
}
func NewSYN(version byte,srcport, destport, wsize, maxpdusize, maxsdusize uint16, identification []byte) (ret *Header) {
al := uint16(SYN_HLEN + len(identification))
ret = New( SYN_FLAG,
0,
srcport,
destport,
NewSeqNo(),
0,
wsize,
al )
ret.setMaxPDUSize(maxpdusize)
ret.setMaxSDUSize(maxsdusize)
ret.setIdentification(identification)
ret.setHeaderLen(uint8(BASE_HLEN + (4 + 1) + len(identification)))
ret.UpdateCheckSum()
return
}
func NewSYNACK(syn *Header, wsize,maxpdusize, maxsdusize uint16, ids ...[]byte) (ret *Header) {
identification := make([]byte,0)
if len(ids) > 0 {
identification = ids[0]
}
ret = NewSYN( 0,
syn.DestPort(),
syn.SrcPort(),
wsize,
maxsdusize,
maxsdusize,
identification )
ret.setFlags(SYN_FLAG | ACK_FLAG, syn.Version())
ret.setAckNo(syn.SeqNo())
ret.UpdateCheckSum()
return
}
func NewACK(p *Header, seqno, wsize uint16) *Header {
return New( ACK_FLAG,
p.Version(),
p.DestPort(),
p.SrcPort(),
seqno,
p.SeqNo(),
wsize,
0 )
}
func (p *Header) SetEAK(eaks []uint16) error{
if len(eaks) > MaxEAKs {
return fmt.Errorf("Too many EAKs in one packet: %d/%d",len(eaks),MaxEAKs)
}
p.setExtendedAcks(eaks)
p.UpdateCheckSum()
return nil
}
func NewDataACK(version byte, srcport,destport,seqno,ackno,wsize uint16, data []byte) (ret *Header) {
ret = New(ACK_FLAG,version,srcport,destport,seqno,ackno,wsize,uint16(len(data)))
ret.setData(data)
ret.UpdateCheckSum()
return
}