-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcphandler.go
556 lines (440 loc) · 11.1 KB
/
tcphandler.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
package egressguy
import (
"errors"
"net"
"os"
"sort"
"sync"
"time"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
)
type TcpHandler interface {
SetConn(conn *TcpConn)
HandlePacket(packet gopacket.Packet, tcp *layers.TCP) error
}
// tcp writer with reliable transmission
// resends data until acked
// but kept as simple as possible, no congestion control
type ReliableWriterHandler struct {
conn *TcpConn
buf []byte
bufLock sync.Mutex
serverSeq uint32
serverWindowScale uint32
serverWindow uint32
lastWrite time.Time
}
func NewReliableWriterHandler() *ReliableWriterHandler {
return &ReliableWriterHandler{
buf: make([]byte, 0),
}
}
func (c *ReliableWriterHandler) SetConn(conn *TcpConn) {
c.conn = conn
if conn == nil {
return
}
c.serverWindowScale = 1
c.serverSeq = conn.Seq
go func() {
t := time.NewTicker(time.Second)
for range t.C {
if c.conn == nil || c.conn.State == TCP_CONNECTION_FINISHED {
break
}
c.writeCheck()
}
}()
}
func (c *ReliableWriterHandler) writeCheck() bool {
if len(c.buf) == 0 || c.conn == nil {
return false
}
writeAhead := c.conn.Seq - c.serverSeq
maxWrite := c.serverSeq + min(c.serverWindow, uint32(len(c.buf)))
now := time.Now()
if now.Sub(c.lastWrite) > time.Second {
// resend
c.conn.Seq = c.serverSeq
} else if writeAhead >= c.serverWindow {
return false
} else if writeAhead >= uint32(len(c.buf)) {
return false
}
if c.conn.Seq < c.serverSeq {
c.conn.Seq = c.serverSeq
}
c.bufLock.Lock()
for maxWrite > c.conn.Seq {
off := c.conn.Seq - c.serverSeq
d := c.buf[off:min(int(off)+int(c.conn.Mss), len(c.buf))]
tcp := c.conn.NewPacket()
// although it's incorrect behavior
// it doesn't matter in this case
tcp.ACK = true
tcp.PSH = true
if err := c.conn.SendPacket(&tcp, d); err != nil {
c.bufLock.Unlock()
return false
}
}
c.lastWrite = now
c.bufLock.Unlock()
return true
}
func (c *ReliableWriterHandler) Write(payload []byte) (int, error) {
if c.conn != nil && c.conn.State == TCP_CONNECTION_FINISHED {
return 0, net.ErrClosed
}
c.bufLock.Lock()
c.buf = append(c.buf, payload...)
c.bufLock.Unlock()
c.writeCheck()
return len(payload), nil
}
func (c *ReliableWriterHandler) HandlePacket(packet gopacket.Packet, tcp *layers.TCP) error {
if tcp.ACK {
if c.serverSeq < tcp.Ack {
// advance buffer
c.bufLock.Lock()
c.buf = c.buf[tcp.Ack-c.serverSeq:]
c.bufLock.Unlock()
c.serverSeq = tcp.Ack
}
c.serverWindow = uint32(tcp.Window) * c.serverWindowScale
w := c.writeCheck()
if tcp.SYN {
for _, opt := range tcp.Options {
if opt.OptionType == layers.TCPOptionKindWindowScale {
c.serverWindowScale = 1 << uint32(opt.OptionData[0])
}
}
if !w {
// send ack
ack := c.conn.NewPacket()
ack.ACK = true
if err := c.conn.SendPacket(&ack, nil); err != nil {
return err
}
}
}
}
return nil
}
func (c *ReliableWriterHandler) Close() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}
// sends some data, then acks incoming data
// suitable for protocols without handshake, such as HTTP
type AckHandler struct {
*ReliableWriterHandler
counter uint8
lastCounter uint8
cachedAck layers.TCP
}
func NewAckHandler() *AckHandler {
h := AckHandler{
ReliableWriterHandler: NewReliableWriterHandler(),
}
// synack increase seq by 1
h.Write([]byte{0xff})
return &h
}
func NewAckHandlerWithReliableWriterHandler(h *ReliableWriterHandler) *AckHandler {
return &AckHandler{
ReliableWriterHandler: h,
}
}
func (c *AckHandler) SetConn(conn *TcpConn) {
c.ReliableWriterHandler.SetConn(conn)
if conn == nil {
return
}
c.cachedAck = conn.NewPacket()
c.cachedAck.ACK = true
}
func (c *AckHandler) HandlePacket(packet gopacket.Packet, tcp *layers.TCP) error {
switch {
case c.conn.State == TCP_CONNECTION_ESTABLISHED:
c.ReliableWriterHandler.HandlePacket(packet, tcp)
if tcp.FIN {
c.conn.setClosed(false)
} else if tcp.RST {
c.conn.setClosed(false)
return nil
} else if len(tcp.Payload) == 0 {
return nil
} else if c.counter != 0 {
c.counter--
if c.conn.Ack+uint32(len(tcp.Payload))+uint32(c.conn.Mss)*2 > tcp.Seq {
return nil
} else {
c.lastCounter = 1
}
}
c.counter = c.lastCounter + 1
c.lastCounter = max(c.counter, 5)
c.conn.Ack = tcp.Seq + uint32(len(tcp.Payload))
c.cachedAck.Seq = c.conn.Seq
c.cachedAck.Ack = c.conn.Ack
if err := c.conn.SendPacket(&c.cachedAck, nil); err != nil {
return err
}
case c.conn.State == TCP_CONNECTION_SYN_SENT:
if tcp.RST || tcp.FIN {
c.conn.setClosed(false)
return nil
} else if !tcp.SYN || !tcp.ACK {
return nil
}
c.conn.State = TCP_CONNECTION_ESTABLISHED
c.conn.Ack = tcp.Seq
for _, opt := range tcp.Options {
if opt.OptionType == layers.TCPOptionKindMSS {
mss := uint16(opt.OptionData[0])<<8 | uint16(opt.OptionData[1])
if mss < c.conn.Mss {
c.conn.Mss = mss
}
}
}
c.conn.Seq++
c.conn.Ack++
c.ReliableWriterHandler.HandlePacket(packet, tcp)
case c.conn.State == TCP_CONNECTION_FINISHED:
c.conn.Instance.RemoveListener(c.conn)
}
return nil
}
type ReliableReaderHandler struct {
*ReliableWriterHandler
windowSize uint16
windowSizeScaled uint32
offset uint32
buf []byte
bufLock sync.Mutex
segments []tcpSegment
recv chan struct{}
readDeadline time.Time
}
type tcpSegment struct {
start uint32
end uint32
}
func NewReliableReaderHandler() *ReliableReaderHandler {
h := NewReliableWriterHandler()
h.Write([]byte{0xff})
return NewReliableReaderHandlerWithReliableWriterHandler(h)
}
func NewReliableReaderHandlerWithReliableWriterHandler(h *ReliableWriterHandler) *ReliableReaderHandler {
scale := uint32(1) << TCP_WINDOW_SCALE
w := (0x10000 / scale) * scale
return &ReliableReaderHandler{
ReliableWriterHandler: h,
windowSize: uint16(w / scale),
windowSizeScaled: w,
buf: make([]byte, 0, w<<1),
segments: make([]tcpSegment, 0, 16),
recv: make(chan struct{}, 1),
}
}
func (c *ReliableReaderHandler) SetConn(conn *TcpConn) {
c.ReliableWriterHandler.SetConn(conn)
close(c.recv)
if conn == nil {
return
}
c.recv = make(chan struct{}, 1)
c.offset = conn.Ack
}
func (c *ReliableReaderHandler) HandlePacket(packet gopacket.Packet, tcp *layers.TCP) error {
switch {
case c.conn.State == TCP_CONNECTION_SYN_SENT:
if tcp.RST || tcp.FIN {
c.conn.setClosed(false)
return nil
} else if !tcp.SYN || !tcp.ACK {
return nil
}
c.conn.State = TCP_CONNECTION_ESTABLISHED
c.conn.Ack = tcp.Seq
for _, opt := range tcp.Options {
if opt.OptionType == layers.TCPOptionKindMSS {
mss := uint16(opt.OptionData[0])<<8 | uint16(opt.OptionData[1])
if mss < c.conn.Mss {
c.conn.Mss = mss
}
}
}
c.conn.Seq++
c.conn.Ack++
// advertise a reasonable window size to the server
c.conn.Win = c.windowSize
// although it's not a common case
// for SYNACK packet to have payload
// but it's possible
c.offset = c.conn.Ack
fallthrough
case c.conn.State == TCP_CONNECTION_ESTABLISHED:
c.ReliableWriterHandler.HandlePacket(packet, tcp)
if tcp.FIN {
c.conn.setClosed(false)
} else if tcp.RST {
c.conn.setClosed(false)
return nil
} else if len(tcp.Payload) == 0 {
return nil
}
c.bufLock.Lock()
defer c.bufLock.Unlock()
if tcp.Seq < c.offset {
if tcp.Seq+uint32(len(tcp.Payload)) > c.offset {
s := c.offset - tcp.Seq
tcp.Payload = tcp.Payload[s:]
tcp.Seq += s
} else {
return nil
}
}
// drop packet if it's out of window
// it might be a retransmission or bad packet
extend := tcp.Seq - c.offset + uint32(len(tcp.Payload))
if extend > uint32(len(c.buf))+c.windowSizeScaled {
return errors.New("out of window")
}
// extend buffer if necessary
if extend > uint32(len(c.buf)) {
c.buf = append(c.buf, make([]byte, extend-uint32(len(c.buf)))...)
}
segment := tcpSegment{
start: tcp.Seq - c.offset,
}
segment.end = segment.start + uint32(len(tcp.Payload))
// check if it's a duplicate segment
for _, s := range c.segments {
if segment.start >= s.start && segment.end <= s.end {
return nil
}
}
copy(c.buf[tcp.Seq-c.offset:], tcp.Payload)
c.segments = append(c.segments, segment)
previousAck := c.conn.Ack
c.mergeSegments()
// send ack if necessary
if c.conn.Ack > previousAck {
ack := c.conn.NewPacket()
ack.ACK = true
if err := c.conn.SendPacket(&ack, nil); err != nil {
return err
}
// notify reader
go func() {
c.recv <- struct{}{}
}()
}
case c.conn.State == TCP_CONNECTION_FINISHED:
c.conn.Instance.RemoveListener(c.conn)
}
return nil
}
// this function expects that the buffer mutex is locked
func (c *ReliableReaderHandler) mergeSegments() {
sort.Slice(c.segments, func(i, j int) bool {
return c.segments[i].start < c.segments[j].start
})
// merge segments and advance buffer
idx := -1
fulfilled := c.conn.Ack - c.offset
for i, s := range c.segments {
if s.start <= fulfilled {
fulfilled = max(fulfilled, s.end)
idx = i
}
}
if idx != -1 {
c.segments = c.segments[idx+1:]
}
c.conn.Ack = fulfilled + c.offset
}
func (c *ReliableReaderHandler) Read(b []byte) (n int, err error) {
if c.conn == nil || c.conn.State == TCP_CONNECTION_FINISHED {
return 0, net.ErrClosed
}
if !c.readDeadline.IsZero() {
if time.Now().After(c.readDeadline) {
return 0, os.ErrDeadlineExceeded
}
deadline := time.After(time.Until(c.readDeadline))
select {
case <-c.recv:
case <-c.conn.OnClose():
return 0, net.ErrClosed
case <-deadline:
return 0, os.ErrDeadlineExceeded
}
} else {
select {
case <-c.recv:
case <-c.conn.OnClose():
return 0, net.ErrClosed
}
}
c.bufLock.Lock()
defer c.bufLock.Unlock()
toRead := min(c.conn.Ack-c.offset, uint32(len(b)))
copy(b, c.buf[:toRead])
// advance buffer
c.buf = c.buf[toRead:]
c.offset += toRead
for i := 0; i < len(c.segments); i++ {
c.segments[i].start -= toRead
c.segments[i].end -= toRead
}
return int(toRead), nil
}
func (c *ReliableReaderHandler) SetReadDeadline(t time.Time) error {
c.readDeadline = t
return nil
}
type NetConnWrapper struct {
*ReliableReaderHandler
}
var _ net.Conn = &NetConnWrapper{}
func (c NetConnWrapper) Read(b []byte) (n int, err error) {
if c.ReliableReaderHandler == nil {
return 0, net.ErrClosed
}
return c.ReliableReaderHandler.Read(b)
}
func (c NetConnWrapper) Write(b []byte) (n int, err error) {
if c.ReliableReaderHandler == nil {
return 0, net.ErrClosed
}
return c.ReliableReaderHandler.Write(b)
}
func (c NetConnWrapper) LocalAddr() net.Addr {
_, src, _, srcPort, _ := c.ReliableWriterHandler.conn.ConnectionTuple()
return &net.TCPAddr{
IP: src,
Port: int(srcPort),
}
}
func (c NetConnWrapper) RemoteAddr() net.Addr {
_, _, dst, _, dstPort := c.ReliableWriterHandler.conn.ConnectionTuple()
return &net.TCPAddr{
IP: dst,
Port: int(dstPort),
}
}
func (c NetConnWrapper) SetDeadline(t time.Time) error {
return nil
}
func (c NetConnWrapper) SetWriteDeadline(t time.Time) error {
return nil
}
func (c NetConnWrapper) Close() error {
return nil
}