-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
1154 lines (1034 loc) · 31.9 KB
/
conn.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package utp_go
import (
"context"
"errors"
"fmt"
"math"
"time"
"github.com/ethereum/go-ethereum/log"
)
const (
Initiator EndpointType = iota
Acceptor
)
const (
ConnConnecting ConnStateType = iota
ConnConnected
ConnClosed
)
const DefaultMaxIdleTimeout = 60 * time.Second
const DefaultWindowSize = 1024 * 1024
const DefaultBufferSize = 1024 * 1024
var (
ErrEmptyDataPayload = errors.New("empty data payload")
ErrConnInvalidAckNum = errors.New("invalid ack number")
ErrInvalidFin = errors.New("invalid fin")
ErrInvalidSeqNum = errors.New("invalid seq number")
ErrInvalidSyn = errors.New("invalid syn")
ErrReset = errors.New("reset")
ErrSynFromAcceptor = errors.New("syn from acceptor")
ErrTimedOut = errors.New("timed out")
)
type EndpointType int
type Endpoint struct {
Type EndpointType
SynNum uint16
SynAck uint16
Attempts int
}
type ClosingRecord struct {
LocalFin *uint16
RemoteFin *uint16
}
type ConnStateType int
type ConnState struct {
stateType ConnStateType
connectedCh chan error
RecvBuf *receiveBuffer
SendBuf *sendBuffer
SentPackets *sentPackets
closing *ClosingRecord
Err error
}
func NewConnState(connected chan error) *ConnState {
return &ConnState{
stateType: ConnConnecting,
connectedCh: connected,
}
}
type ConnectionConfig struct {
MaxPacketSize uint16
MaxConnAttempts int
MaxIdleTimeout time.Duration
InitialTimeout time.Duration
MinTimeout time.Duration
MaxTimeout time.Duration
TargetDelay time.Duration
WindowSize uint32
BufferSize int
}
func NewConnectionConfig() *ConnectionConfig {
return &ConnectionConfig{
MaxConnAttempts: 6,
MaxIdleTimeout: DefaultMaxIdleTimeout,
MaxPacketSize: defaultMaxPacketSizeBytes,
InitialTimeout: defaultInitialTimeout,
MinTimeout: defaultMinTimeout,
MaxTimeout: defaultMaxTimeout,
TargetDelay: defaultTargetMicros,
WindowSize: DefaultWindowSize,
BufferSize: DefaultBufferSize,
}
}
func fromConnConfig(config *ConnectionConfig) *ctrlConfig {
ctrlConfigPtr := defaultCtrlConfig()
ctrlConfigPtr.MaxPacketSizeBytes = uint32(config.MaxPacketSize)
ctrlConfigPtr.InitialTimeout = config.InitialTimeout
ctrlConfigPtr.MinTimeout = config.MinTimeout
ctrlConfigPtr.MaxTimeout = config.MaxTimeout
ctrlConfigPtr.TargetDelayMicros = uint32(config.TargetDelay.Microseconds())
ctrlConfigPtr.WindowSize = config.WindowSize
return ctrlConfigPtr
}
type queuedWrite struct {
data []byte
written int
resultCh chan *readOrWriteResult
}
type readOrWriteResult struct {
Err error
Len int
Data []byte
}
type connection struct {
ctx context.Context
logger log.Logger
state *ConnState
cid *ConnectionId
config *ConnectionConfig
endpoint *Endpoint
peerTsDiff time.Duration
peerRecvWindow uint32
socketEvents chan *socketEvent
//unacked *delayMap[*packet]
unacked *timeWheel[*packet]
unackTimeoutCh chan *packet
handleExpiration expireFunc[*packet]
reads chan *readOrWriteResult
readable chan struct{}
pendingWrites []*queuedWrite
writable chan struct{}
latestTimeout *time.Time
}
func newConnection(
ctx context.Context,
logger log.Logger,
cid *ConnectionId,
config *ConnectionConfig,
syn *packet,
connected chan error,
socketEvents chan *socketEvent,
reads chan *readOrWriteResult,
) *connection {
var endpoint *Endpoint
var peerTsDiff time.Duration
var peerRecvWindow uint32
if syn != nil {
synAck := RandomUint16()
endpoint = &Endpoint{
Type: Acceptor,
SynNum: syn.Header.SeqNum,
SynAck: synAck,
}
now := time.Now().UnixMicro()
peerTsDiff = time.Duration(now-syn.Header.Timestamp) * time.Microsecond
peerRecvWindow = syn.Header.WndSize
} else {
synNum := RandomUint16()
endpoint = &Endpoint{
Type: Initiator,
SynNum: synNum,
Attempts: 0,
}
peerTsDiff = 0
peerRecvWindow = math.MaxUint32
}
unackTimeoutCh := make(chan *packet, 1000)
handleExpiration := func(key any, pkt *packet) {
unackTimeoutCh <- pkt
}
return &connection{
ctx: ctx,
logger: logger,
state: NewConnState(connected),
cid: cid,
config: config,
endpoint: endpoint,
peerTsDiff: peerTsDiff,
peerRecvWindow: peerRecvWindow,
socketEvents: socketEvents,
unacked: newTimeWheel[*packet](config.InitialTimeout/4, 8, handleExpiration),
unackTimeoutCh: unackTimeoutCh,
reads: reads,
readable: make(chan struct{}, 1000),
pendingWrites: make([]*queuedWrite, 0),
writable: make(chan struct{}, 1000),
latestTimeout: nil,
}
}
func (c *connection) eventLoop(stream *UtpStream) error {
defer c.unacked.stop()
if c.logger.Enabled(BASE_CONTEXT, log.LevelInfo) {
c.logger.Info("uTP conn starting", "dst.peer", c.cid.Peer, "cid.Send", c.cid.Send, "cid.Recv", c.cid.Recv)
}
// Initialize connection based on endpoint type
if c.endpoint.Type == Initiator {
synSeqNum := c.endpoint.SynNum
synPkt := c.synPacket(synSeqNum)
c.socketEvents <- &socketEvent{
Type: outgoing,
Packet: synPkt,
ConnectionId: c.cid,
}
if c.logger.Enabled(BASE_CONTEXT, log.LevelDebug) {
c.logger.Debug("put a initial syn packet to delay map", "socketEvents.len", len(c.socketEvents), "dst.peer", c.cid.Peer, "synSeqNum", synSeqNum)
}
c.unacked.put(synSeqNum, synPkt, c.config.InitialTimeout)
c.endpoint.Attempts = 1
} else {
syn := c.endpoint.SynNum
synAck := c.endpoint.SynAck
statePacket := c.statePacket()
if c.logger.Enabled(BASE_CONTEXT, log.LevelDebug) {
c.logger.Debug("a initial state packet", "peer", c.cid.Peer, "cid.Send", c.cid.Send, "cid.Recv", c.cid.Recv)
}
c.socketEvents <- &socketEvent{
Type: outgoing,
Packet: statePacket,
ConnectionId: c.cid,
}
recvBuf := newReceiveBufferWithLogger(c.config.BufferSize, syn, c.logger)
sendBuf := newSendBuffer(c.config.BufferSize)
congestionCtrl := newDefaultController(fromConnConfig(c.config))
sentPacketsHolder := newSentPackets(synAck-1, congestionCtrl, c.logger) // wrapping subtraction
if c.state != nil && c.state.connectedCh != nil {
c.state.connectedCh <- nil
} else {
panic("connection in invalid statePacket prior to event loop beginning")
}
c.state.stateType = ConnConnected
c.state.RecvBuf = recvBuf
c.state.SendBuf = sendBuf
c.state.SentPackets = sentPacketsHolder
}
idleTimer := time.NewTimer(c.config.MaxIdleTimeout)
resetIdleTimer := func() {
idleTimer.Reset(c.config.MaxIdleTimeout)
}
defer idleTimer.Stop()
handleIncoming := func(event *streamEvent) {
if event.Type == streamIncoming {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("incoming packet",
"stream.streamEvents.len", len(stream.streamEvents),
"src.peer", c.cid.Peer,
"packet.type", event.Packet.Header.PacketType.String(),
"packet.seqNum", event.Packet.Header.SeqNum,
"packet.ackNum", event.Packet.Header.AckNum,
"buf.len", len(event.Packet.Body))
}
// reset idle timeout
resetIdleTimer()
c.onPacket(event.Packet, time.Now())
} else if event.Type == streamShutdown {
stream.shutdown.Store(true)
}
}
handleWrites := func(write *queuedWrite, ok bool) {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("get queued write from writes", "dst.peer", c.cid.Peer, "content", len(write.data))
}
resetIdleTimer()
c.onWrite(write)
}
handleReadable := func() {
c.processReads()
}
handleWritable := func() {
currentTime := time.Now()
c.logger.Trace("has data to continually writing start")
c.processWrites(currentTime)
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("has data to continually writing end...", "c.writable.len", len(c.writable), "duration", time.Since(currentTime))
}
}
handleTimeout := func(timeoutPkt *packet) {
if c.logger.Enabled(BASE_CONTEXT, log.LevelDebug) {
c.logger.Debug("unack timeout",
"seq", timeoutPkt.Header.SeqNum,
"ack", timeoutPkt.Header.AckNum,
"item.key", timeoutPkt.Header.SeqNum,
"type", timeoutPkt.Header.PacketType)
}
c.onTimeout(timeoutPkt, time.Now())
}
handleIdleTimeout := func() {
if c.state.stateType != ConnClosed {
c.logger.Warn("idle timeout, closing...")
c.state.stateType = ConnClosed
c.state.Err = ErrTimedOut
}
}
handleCtxDone := func() {
if !stream.shutdown.Load() {
c.logger.Info("ctx done, uTP conn initiating shutdown...", "err", c.ctx.Err())
stream.shutdown.Store(true)
}
}
var maxStreamEventLen int
for {
maxStreamEventLen = max(maxStreamEventLen, len(stream.streamEvents))
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("connection event count",
"maxStreamEventLen", maxStreamEventLen,
"streamEvents.len", len(stream.streamEvents),
"readable.len", len(c.readable),
"writeable.len", len(c.writable))
}
select {
case event := <-stream.streamEvents:
handleIncoming(event)
goto afterSelect
default:
}
select {
case event := <-stream.streamEvents:
handleIncoming(event)
goto afterSelect
case write, ok := <-stream.writes:
handleWrites(write, ok)
goto afterSelect
case <-c.readable:
handleReadable()
goto afterSelect
case <-c.writable:
handleWritable()
goto afterSelect
default:
}
select {
case event := <-stream.streamEvents:
handleIncoming(event)
case write, ok := <-stream.writes:
handleWrites(write, ok)
case <-c.readable:
handleReadable()
case <-c.writable:
handleWritable()
case timeoutPkt := <-c.unackTimeoutCh:
handleTimeout(timeoutPkt)
case <-idleTimer.C:
handleIdleTimeout()
case <-c.ctx.Done():
handleCtxDone()
c.logger.Error("stream context done, will force stop...", "c.cid.peer", c.cid.Peer, "c.cid.Send", c.cid.Send, "c.cid.Recv", c.cid.Recv)
return c.ctx.Err()
}
afterSelect:
if stream.shutdown.Load() && c.state.stateType != ConnClosed {
c.shutdown()
}
if c.state.stateType == ConnClosed {
c.logger.Info("uTP conn closing...", "err", c.state.Err, "c.cid.Send", c.cid.Send, "c.cid.Recv", c.cid.Recv)
c.processReads()
c.processWrites(time.Now())
if c.state.RecvBuf != nil {
c.state.RecvBuf.close()
}
c.socketEvents <- &socketEvent{
Type: socketShutdown,
ConnectionId: c.cid,
}
return c.state.Err
}
}
}
func (c *connection) shutdown() {
switch c.state.stateType {
case ConnConnecting:
c.state.stateType = ConnClosed
case ConnClosed:
// ignore
case ConnConnected:
if c.state.closing != nil {
localFin := c.state.closing.LocalFin
// If we have not sent our FIN, and there are no pending writes, and there is no
// pending data in the send buffer, then send our FIN
if localFin == nil && len(c.pendingWrites) == 0 && c.state.SendBuf.IsEmpty() {
recvWindow := uint32(c.state.RecvBuf.Available())
seqNum := c.state.SentPackets.NextSeqNum()
ackNum := c.state.RecvBuf.AckNum()
selectiveAck := c.state.RecvBuf.SelectiveAck()
fin := NewPacketBuilder(
st_fin,
c.cid.Send,
NowMicro(),
recvWindow,
seqNum,
).WithAckNum(ackNum).WithSelectiveAck(selectiveAck).Build()
c.state.closing.LocalFin = &seqNum
c.logger.Info("transmitting FIN", "dst.Peer", c.cid.Peer, "dst.Send", c.cid.Send, "dst.Recv", c.cid.Recv, "seq", seqNum)
c.transmit(fin, time.Now())
}
} else {
var localFin *uint16
if len(c.pendingWrites) == 0 && c.state.SendBuf.IsEmpty() {
recvWindow := uint32(c.state.RecvBuf.Available())
seqNum := c.state.SentPackets.NextSeqNum()
ackNum := c.state.RecvBuf.AckNum()
selectiveAck := c.state.RecvBuf.SelectiveAck()
fin := NewPacketBuilder(
st_fin,
c.cid.Send,
NowMicro(),
recvWindow,
seqNum,
).WithAckNum(ackNum).
WithSelectiveAck(selectiveAck).
Build()
localFin = &seqNum
c.logger.Info("transmitting FIN", "dst.Peer", c.cid.Peer, "dst.Send", c.cid.Send, "dst.Recv", c.cid.Recv, "seq", seqNum)
c.transmit(fin, time.Now())
}
c.logger.Info("init closingRecord", "dst.peer", c.cid.Peer, "dst.send", c.cid.Send, "dst.recv", c.cid.Recv, "localFin", localFin)
c.state.closing = &ClosingRecord{
LocalFin: localFin,
RemoteFin: nil,
}
}
}
}
func (c *connection) processWrites(now time.Time) {
switch c.state.stateType {
case ConnConnecting:
return
case ConnClosed:
c.logger.Warn("connection is closed, will not process writing",
"c.cid.send", c.cid.Send, "c.cid.recv", c.cid.Recv)
result := &readOrWriteResult{
Err: c.state.Err,
}
for _, w := range c.pendingWrites {
w.resultCh <- result
}
return
default:
}
// Compose data packets
windowSize := minUint32(c.state.SentPackets.Window(), c.peerRecvWindow)
var payloads [][]byte
for windowSize > 0 {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("has window size to send a packet data in sendBuffer", "windowSize", windowSize)
}
maxDataSize := minUint32(windowSize, uint32(c.config.MaxPacketSize-64))
data := make([]byte, maxDataSize)
n := c.state.SendBuf.Read(data)
if n == 0 {
break
}
payloads = append(payloads, data[:n])
windowSize -= uint32(n)
}
for windowSize == 0 && len(c.writable) > 2 {
<-c.writable
}
// Write pending data to send buffer
for len(c.pendingWrites) > 0 {
bufSpace := c.state.SendBuf.Available()
if bufSpace <= 0 {
break
}
writeReq := c.pendingWrites[0]
if len(writeReq.data) <= bufSpace {
c.state.SendBuf.Write(writeReq.data)
result := &readOrWriteResult{
Len: len(writeReq.data) + writeReq.written,
}
writeReq.resultCh <- result
c.pendingWrites = c.pendingWrites[1:]
} else {
nextWrite := writeReq.data[:bufSpace]
remainingData := writeReq.data[bufSpace:]
c.state.SendBuf.Write(nextWrite)
writeReq.data = remainingData
writeReq.written += bufSpace
}
c.writable <- struct{}{}
}
// transmit data packets
seqNum := c.state.SentPackets.NextSeqNum()
recvWindow := uint32(c.state.RecvBuf.Available())
ackNum := c.state.RecvBuf.AckNum()
selectiveAck := c.state.RecvBuf.SelectiveAck()
for _, payload := range payloads {
packetInst := NewPacketBuilder(
st_data,
c.cid.Send,
NowMicro(),
recvWindow,
seqNum,
).WithPayload(payload).WithTsDiffMicros(uint32(c.peerTsDiff.Microseconds())).WithAckNum(ackNum).WithSelectiveAck(selectiveAck).Build()
c.transmit(packetInst, now)
seqNum = seqNum + 1 // wrapping add in uint16
}
}
func (c *connection) onWrite(writeReq *queuedWrite) {
writeReq.written = 0
switch c.state.stateType {
case ConnConnecting:
// There are 0 bytes written so far
c.pendingWrites = append(c.pendingWrites, writeReq)
case ConnConnected:
if c.state.closing != nil {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("append a queuedWrite to pending writes when closing the conn...")
}
if c.state.closing.LocalFin == nil && c.state.closing.RemoteFin != nil {
c.pendingWrites = append(c.pendingWrites, writeReq)
} else {
writeReq.resultCh <- &readOrWriteResult{Len: 0}
}
} else {
c.logger.Debug("append a queuedWrite to pending writes")
c.pendingWrites = append(c.pendingWrites, writeReq)
}
case ConnClosed:
c.logger.Warn("discard a queuedWrite when closed the conn...")
result := &readOrWriteResult{
Err: c.state.Err,
Len: 0,
}
writeReq.resultCh <- result
}
c.processWrites(time.Now())
c.writable <- struct{}{}
}
func (c *connection) processReads() {
var recvBuf *receiveBuffer
switch c.state.stateType {
case ConnConnecting:
return
case ConnConnected:
recvBuf = c.state.RecvBuf
case ConnClosed:
result := &readOrWriteResult{
Err: c.state.Err,
}
c.reads <- result
c.logger.Debug("read eof...")
return
}
currentTime := time.Now()
if recvBuf != nil && c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("read data saving in the recvBuf, start...", "available", recvBuf.Available(), "isEmpty", recvBuf.IsEmpty())
}
for recvBuf != nil && !recvBuf.IsEmpty() {
buf := make([]byte, c.config.MaxPacketSize)
n := recvBuf.Read(buf)
if n == 0 {
break
}
c.reads <- &readOrWriteResult{Data: buf, Len: n}
}
if recvBuf != nil && c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("read data saving in the recvBuf, end...", "duration", currentTime, "available", recvBuf.Available(), "isEmpty", recvBuf.IsEmpty())
}
// If we have reached eof, send an empty resultCh to all pending reads
if c.eof() {
c.reads <- &readOrWriteResult{Data: make([]byte, 0)}
}
}
func (c *connection) eof() bool {
switch c.state.stateType {
case ConnConnecting:
return false
case ConnConnected:
if c.state.closing != nil && c.state.closing.RemoteFin != nil {
return c.state.RecvBuf.AckNum() == *c.state.closing.RemoteFin
}
return false
case ConnClosed:
return true
default:
return false
}
}
func (c *connection) onTimeout(packet *packet, now time.Time) {
switch c.state.stateType {
case ConnConnecting:
if c.endpoint.Type == Acceptor {
return
}
if c.endpoint.Attempts >= c.config.MaxConnAttempts {
c.logger.Error("quitting connection attempt", "attempts", c.endpoint.Attempts)
err := ErrTimedOut
if c.state.connectedCh != nil {
c.state.connectedCh <- err
}
c.state.stateType = ConnClosed
c.state.Err = err
return
} else {
seq := c.endpoint.SynNum
logMsg := fmt.Sprintf("retrying connection, after %d attempts", c.endpoint.Attempts)
switch c.endpoint.Attempts {
case 1, 0:
c.logger.Trace(logMsg)
case 2:
c.logger.Debug(logMsg)
case 3:
c.logger.Info(logMsg)
default:
c.logger.Warn(logMsg)
}
c.endpoint.Attempts += 1
// Double previous timeout for exponential backoff on each attempt
timeout := c.config.InitialTimeout * time.Duration(math.Pow(1.5, float64(c.endpoint.Attempts)))
c.unacked.put(seq, packet, timeout)
// Re-send SYN packet
synPacket := c.synPacket(seq)
c.socketEvents <- &socketEvent{
Type: outgoing,
Packet: synPacket,
ConnectionId: c.cid,
}
}
case ConnConnected:
// If the timed out packet is a SYN, do nothing
if packet.Header.PacketType == st_syn {
return
}
// Handle timeout amplification prevention
var isTimeout bool
if c.latestTimeout != nil {
isTimeout = time.Since(*c.latestTimeout) > c.state.SentPackets.Timeout()
} else {
isTimeout = true
}
if isTimeout {
c.state.SentPackets.OnTimeout()
currentTime := time.Now()
c.latestTimeout = ¤tTime
}
// Rebuild and retransmit packet
recvWindow := uint32(c.state.RecvBuf.Available())
nowMicros := time.Now().UnixMicro()
tsDiffMicros := uint32(c.peerTsDiff.Microseconds())
newPacket := NewPacketBuilder(packet.Header.PacketType, packet.Header.ConnectionId, uint32(nowMicros), recvWindow, packet.Header.SeqNum).
WithAckNum(c.state.RecvBuf.AckNum()).
WithSelectiveAck(c.state.RecvBuf.SelectiveAck()).
WithTsDiffMicros(tsDiffMicros).
WithPayload(packet.Body).
Build()
c.transmit(newPacket, now)
default:
}
}
func (c *connection) onPacket(packet *packet, now time.Time) {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("on packet start...",
"packet.body.len", len(packet.Body),
"packet.type", packet.Header.PacketType.String(),
"packet.cid", packet.Header.ConnectionId,
"packet.seqnum", packet.Header.SeqNum,
"packet.acknum", packet.Header.AckNum,
"packet.windowSize", packet.Header.WndSize,
"now", now)
}
nowMicros := time.Now().UnixMicro()
c.peerRecvWindow = packet.Header.WndSize
// Cap the diff to handle clock differences between machines
peerTsDiff := time.Microsecond * time.Duration(nowMicros-int64(packet.Header.TimestampDiff))
if peerTsDiff > c.config.MaxIdleTimeout {
c.peerTsDiff = time.Second
} else {
c.peerTsDiff = peerTsDiff
}
// Handle different packet types
var err error
switch packet.Header.PacketType {
case st_syn:
c.onSyn(packet.Header.SeqNum)
case st_state:
c.onState(packet.Header.SeqNum, packet.Header.AckNum)
case st_data:
err = c.onData(packet.Header.SeqNum, packet.Body)
case st_fin:
err = c.onFin(packet.Header.SeqNum, packet.Body)
case st_reset:
c.onReset()
}
if err != nil {
c.logger.Warn("on packet handle data or fin err", "err", err)
}
// Process acknowledgments
switch packet.Header.PacketType {
case st_state, st_data, st_fin:
delay := time.Duration(packet.Header.TimestampDiff) * time.Microsecond
if err = c.processAck(packet.Header.AckNum, packet.Eack, delay, now); err != nil {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("ack does not correspond to known seq_num",
"packet.type", packet.Header.PacketType,
"packet.seqNum", packet.Header.SeqNum,
"packet.ackNum", packet.Header.AckNum,
"err", err)
}
}
default:
}
// Handle retransmissions
c.retransmitLostPackets(now)
// Send STATE packet if appropriate
switch packet.Header.PacketType {
case st_syn, st_data, st_fin:
if statePacket := c.statePacket(); statePacket != nil {
if c.logger.Enabled(BASE_CONTEXT, log.LevelDebug) {
c.logger.Debug("create a state packet to send out",
"packet.seqNum", statePacket.Header.SeqNum,
"packet.ackNum", statePacket.Header.AckNum,
"packet.cid", statePacket.Header.ConnectionId)
}
event := socketEvent{
Type: outgoing,
Packet: statePacket,
ConnectionId: c.cid,
}
c.socketEvents <- &event
}
}
// Notify writable on STATE packets
if packet.Header.PacketType == st_state {
c.writable <- struct{}{}
}
// Notify readable on data or FIN
if len(packet.Body) > 0 || packet.Header.PacketType == st_fin {
c.readable <- struct{}{}
}
// Handle connection closing cases
if c.state.stateType == ConnConnected && c.state.closing != nil && c.state.closing.LocalFin != nil {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("try close connection locally...", "dst.Peer", c.cid.Peer, "dst.Send", c.cid.Send, "dst.Recv", c.cid.Recv)
}
lastAckNum, isNone := c.state.SentPackets.LastAckNum()
if !isNone && lastAckNum == *c.state.closing.LocalFin {
c.state.stateType = ConnClosed
c.state.Err = nil
}
}
if c.state.stateType == ConnConnected && c.state.closing != nil && c.state.closing.RemoteFin != nil {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("close connection remotely...", "dst.Peer", c.cid.Peer, "dst.Send", c.cid.Send, "dst.Recv", c.cid.Recv)
}
if !c.state.SentPackets.HasUnackedPackets() && c.state.RecvBuf.AckNum() == *c.state.closing.RemoteFin {
c.processReads()
c.state.stateType = ConnClosed
c.state.Err = nil
}
}
if c.state.stateType == ConnConnected && c.state.closing != nil && c.state.closing.RemoteFin != nil && c.state.closing.LocalFin != nil {
c.state.stateType = ConnClosed
c.state.Err = nil
}
}
func (c *connection) processAck(
ackNum uint16,
selectiveAck *SelectiveAck,
delay time.Duration,
now time.Time,
) error {
if c.state.stateType != ConnConnected {
return nil
}
fullAcked, selectedAcks, err := c.state.SentPackets.onAck(
ackNum, selectiveAck, delay, now)
if err != nil {
seqRange := c.state.SentPackets.SeqNumRange()
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("sent packets onAck has error",
"err", err,
"cid.send", c.cid.Send,
"cid.recv", c.cid.Recv,
"ackNum", ackNum,
"seqStart",
seqRange.start,
"seqEnd", seqRange.end)
}
if errors.Is(err, ErrInvalidAckNum) {
c.reset(err)
return err
}
return err
}
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("process ack",
"cid.send", c.cid.Send,
"cid.recv", c.cid.Recv,
"fullAcked.start", fullAcked.start,
"fullAcked.end", fullAcked.end)
}
c.unacked.retain(func(key any) bool {
// return true to remove
return fullAcked.Contains(key.(uint16))
})
for _, selectedAck := range selectedAcks {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("process ack, will remove acked num from innerMap",
"cid.send", c.cid.Send,
"cid.recv", c.cid.Recv,
"ackNum", selectedAck)
}
c.unacked.remove(selectedAck)
}
return nil
}
func (c *connection) onSyn(seqNum uint16) {
var err error
if c.endpoint.Type == Acceptor {
// If we are the accepting endpoint, check whether the SYN is a retransmission
// A non-matching sequence number is incorrect behavior
if seqNum != c.endpoint.SynNum {
err = ErrInvalidSyn
}
} else {
// If we are the initiating endpoint, then an incoming SYN is incorrect behavior
err = ErrSynFromAcceptor
}
if err != nil {
if c.state.stateType != ConnClosed {
c.reset(err)
}
}
}
func (c *connection) onState(seqNum, ackNum uint16) {
if ConnConnecting != c.state.stateType {
return
}
if c.endpoint.Type == Initiator && ackNum == c.endpoint.SynNum {
// NOTE: In a deviation from the specification, we initialize the ACK num
// to the sequence number of the SYN-ACK minus 1. This is consistent with
// the reference implementation and the libtorrent implementation.
c.logger.Info("connect success, will initial connection state...",
"cid.send", c.cid.Send, "cid.recv", c.cid.Recv)
recvBuf := newReceiveBufferWithLogger(c.config.BufferSize, seqNum-1, c.logger) // wrapping subtraction for uint16
sendBuf := newSendBuffer(c.config.BufferSize)
congestionCtrl := newDefaultController(fromConnConfig(c.config))
sentPacketsHolder := newSentPackets(c.endpoint.SynNum, congestionCtrl, c.logger)
close(c.state.connectedCh)
c.state.stateType = ConnConnected
c.state.RecvBuf = recvBuf
c.state.SendBuf = sendBuf
c.state.SentPackets = sentPacketsHolder
}
}
func (c *connection) onData(seqNum uint16, data []byte) error {
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("on data packet", "seqNum", seqNum, "data.len", len(data))
}
// If the data payload is empty, then reset the connection
if len(data) == 0 {
c.reset(ErrEmptyDataPayload)
}
switch c.state.stateType {
case ConnConnecting:
if c.endpoint.Type == Acceptor {
return errors.New("unreachable: connection should be marked established")
} else {
// connection being established.
c.logger.Info("connection being established, ignore", "src.peer", c.cid.Peer, "seqNum", seqNum)
return nil
}
case ConnConnected:
if c.state.closing != nil && c.state.closing.RemoteFin != nil {
// connection is closing
c.logger.Info("connection has received remote FIN", "src.peer", c.cid.Peer, "seqNum", seqNum)
start := c.state.RecvBuf.InitSeqNum()
seqRange := newCircularRangeInclusive(start, *c.state.closing.RemoteFin)
if !seqRange.Contains(seqNum) {
c.state.stateType = ConnClosed
c.state.Err = ErrInvalidSeqNum
return nil
}
}
// not closing should send data
if len(data) <= c.state.RecvBuf.Available() {
err := c.state.RecvBuf.Write(data, seqNum)
if err != nil {
c.logger.Warn("write data to recv buffer, but available space is not enough",
"src.peer", c.cid.Peer, "seqNum", seqNum, "data.len", len(data))
}
if c.logger.Enabled(BASE_CONTEXT, log.LevelTrace) {
c.logger.Trace("write data to recv buffer",
"src.peer", c.cid.Peer, "seqNum", seqNum, "data.len", len(data), "nextAckNum", c.state.RecvBuf.AckNum())
}
return err
}
default:
// do nothing
}
return nil
}
func (c *connection) onFin(seqNum uint16, data []byte) error {
switch c.state.stateType {
case ConnConnecting, ConnClosed:
return nil
case ConnConnected:
if c.state.closing != nil {
if c.state.closing.RemoteFin != nil {
// If we have already received a FIN, a subsequent FIN with a different
// sequence number is incorrect behavior
if seqNum != *c.state.closing.RemoteFin {
c.reset(ErrInvalidFin)
}
} else {
c.logger.Warn("received FIN", "seq", seqNum, "c.cid.send", c.cid.Send, "c.cid.recv", c.cid.Recv)
remoteFin := seqNum
c.state.closing.RemoteFin = &remoteFin
return c.state.RecvBuf.Write(data, seqNum)
}
} else {
// Register the FIN with the receive buffer
if err := c.state.RecvBuf.Write(data, seqNum); err != nil {
return err