Skip to content

Commit b461313

Browse files
gbn: complete handshake if client has completed it
This fixes a bug where the server would not complete the handshake if the server restarted the handshake after sending it's SYN, which the client then received and then completed the handshake. This could previously happen if the server timedout when waiting for the clients SYN response, or if the client's SYNACK was lost due to packet loss.
1 parent ea84c35 commit b461313

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

gbn/gbn_server.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func (g *GoBackNConn) serverHandshake() error { // nolint:gocyclo
8484
var n uint8
8585
var resent bool
8686

87+
handshakeLoop:
8788
for {
8889
g.log.Debugf("Waiting for client SYN")
8990
select {
@@ -111,6 +112,24 @@ func (g *GoBackNConn) serverHandshake() error { // nolint:gocyclo
111112

112113
switch msg.(type) {
113114
case *PacketSYN:
115+
116+
case *PacketSYNACK, *PacketData:
117+
// If we receive a SYNACK or DATA packet after we have
118+
// restarted the handshake, we can be sure that the
119+
// client has received our SYN and has completed the
120+
// handshake. We can therefore complete the handshake
121+
// ourselves.
122+
if resent {
123+
g.log.Tracef("Received %T after restarting "+
124+
"handshake", msg)
125+
g.timeoutManager.Received(msg)
126+
127+
break handshakeLoop
128+
}
129+
130+
g.log.Tracef("Expected SYN, got %T", msg)
131+
132+
continue
114133
default:
115134
g.log.Tracef("Expected SYN, got %T", msg)
116135
continue
@@ -169,6 +188,8 @@ func (g *GoBackNConn) serverHandshake() error { // nolint:gocyclo
169188

170189
switch msg.(type) {
171190
case *PacketSYNACK:
191+
g.log.Debugf("Received SYNACK")
192+
172193
// Notify the timeout manager we've received the SYNACK
173194
// response from the counterparty.
174195
g.timeoutManager.Received(msg)
@@ -185,8 +206,6 @@ func (g *GoBackNConn) serverHandshake() error { // nolint:gocyclo
185206
break
186207
}
187208

188-
g.log.Debugf("Received SYNACK")
189-
190209
// Set all variables that are dependent on the value of N that we get
191210
// from the client
192211
g.setN(n)

0 commit comments

Comments
 (0)