This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
/
tcp_conn.go
474 lines (404 loc) · 10.1 KB
/
tcp_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
package core
/*
#cgo CFLAGS: -I./c/include
#include "lwip/tcp.h"
*/
import "C"
import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"sync"
"time"
"unsafe"
)
type tcpConnState uint
const (
// tcpNewConn is the initial state.
tcpNewConn tcpConnState = iota
// tcpConnecting indicates the handler is still connecting remote host.
tcpConnecting
// tcpConnected indicates the connection has been established, handler
// may write data to TUN, and read data from TUN.
tcpConnected
// tcpWriteClosed indicates the handler has closed the writing side
// of the connection, no more data will send to TUN, but handler can still
// read data from TUN.
tcpWriteClosed
// tcpReceiveClosed indicates lwIP has received a FIN segment from
// local peer, the reading side is closed, no more data can be read
// from TUN, but handler can still write data to TUN.
tcpReceiveClosed
// tcpClosing indicates both reading side and writing side are closed,
// resources deallocation will be triggered at any time in lwIP callbacks.
tcpClosing
// tcpAborting indicates the connection is aborting, resources deallocation
// will be triggered at any time in lwIP callbacks.
tcpAborting
// tcpClosed indicates the connection has been closed, resources were freed.
tcpClosed
// tcpErrord indicates an fatal error occured on the connection, resources
// were freed.
tcpErrored
)
type tcpConn struct {
sync.Mutex
pcb *C.struct_tcp_pcb
handler TCPConnHandler
remoteAddr *net.TCPAddr
localAddr *net.TCPAddr
connKeyArg unsafe.Pointer
connKey uint32
canWrite *sync.Cond // Condition variable to implement TCP backpressure.
state tcpConnState
sndPipeReader *io.PipeReader
sndPipeWriter *io.PipeWriter
closeOnce sync.Once
closeErr error
}
func newTCPConn(pcb *C.struct_tcp_pcb, handler TCPConnHandler) (TCPConn, error) {
connKeyArg := newConnKeyArg()
connKey := rand.Uint32()
setConnKeyVal(unsafe.Pointer(connKeyArg), connKey)
// Pass the key as arg for subsequent tcp callbacks.
C.tcp_arg(pcb, unsafe.Pointer(connKeyArg))
// Register callbacks.
setTCPRecvCallback(pcb)
setTCPSentCallback(pcb)
setTCPErrCallback(pcb)
setTCPPollCallback(pcb, C.u8_t(TCP_POLL_INTERVAL))
pipeReader, pipeWriter := io.Pipe()
conn := &tcpConn{
pcb: pcb,
handler: handler,
localAddr: ParseTCPAddr(ipAddrNTOA(pcb.remote_ip), uint16(pcb.remote_port)),
remoteAddr: ParseTCPAddr(ipAddrNTOA(pcb.local_ip), uint16(pcb.local_port)),
connKeyArg: connKeyArg,
connKey: connKey,
canWrite: sync.NewCond(&sync.Mutex{}),
state: tcpNewConn,
sndPipeReader: pipeReader,
sndPipeWriter: pipeWriter,
}
// Associate conn with key and save to the global map.
tcpConns.Store(connKey, conn)
// Connecting remote host could take some time, do it in another goroutine
// to prevent blocking the lwip thread.
conn.Lock()
conn.state = tcpConnecting
conn.Unlock()
go func() {
err := handler.Handle(TCPConn(conn), conn.remoteAddr)
if err != nil {
conn.Abort()
} else {
conn.Lock()
if conn.state != tcpConnecting {
conn.Unlock()
return
}
conn.state = tcpConnected
conn.Unlock()
lwipMutex.Lock()
if pcb.refused_data != nil {
C.tcp_process_refused_data(pcb)
}
lwipMutex.Unlock()
}
}()
return conn, NewLWIPError(LWIP_ERR_OK)
}
func (conn *tcpConn) RemoteAddr() net.Addr {
return conn.remoteAddr
}
func (conn *tcpConn) LocalAddr() net.Addr {
return conn.localAddr
}
func (conn *tcpConn) SetDeadline(t time.Time) error {
return nil
}
func (conn *tcpConn) SetReadDeadline(t time.Time) error {
return nil
}
func (conn *tcpConn) SetWriteDeadline(t time.Time) error {
return nil
}
func (conn *tcpConn) receiveCheck() error {
conn.Lock()
defer conn.Unlock()
switch conn.state {
case tcpConnected:
fallthrough
case tcpWriteClosed:
return nil
case tcpNewConn:
fallthrough
case tcpConnecting:
return NewLWIPError(LWIP_ERR_CONN)
case tcpAborting:
fallthrough
case tcpClosed:
fallthrough
case tcpReceiveClosed:
fallthrough
case tcpClosing:
return NewLWIPError(LWIP_ERR_CLSD)
case tcpErrored:
conn.abortInternal()
return NewLWIPError(LWIP_ERR_ABRT)
default:
panic("unexpected error")
}
return nil
}
func (conn *tcpConn) Receive(data []byte) error {
if err := conn.receiveCheck(); err != nil {
return err
}
n, err := conn.sndPipeWriter.Write(data)
if err != nil {
return NewLWIPError(LWIP_ERR_CLSD)
}
C.tcp_recved(conn.pcb, C.u16_t(n))
return NewLWIPError(LWIP_ERR_OK)
}
func (conn *tcpConn) Read(data []byte) (int, error) {
conn.Lock()
if conn.state == tcpReceiveClosed {
conn.Unlock()
return 0, io.EOF
}
if conn.state >= tcpClosing {
conn.Unlock()
return 0, io.ErrClosedPipe
}
conn.Unlock()
// Handler should get EOF.
n, err := conn.sndPipeReader.Read(data)
if err == io.ErrClosedPipe {
err = io.EOF
}
return n, err
}
// writeInternal enqueues data to snd_buf, and treats ERR_MEM returned by tcp_write not an error,
// but instead tells the caller that data is not successfully enqueued, and should try
// again another time. By calling this function, the lwIP thread is assumed to be already
// locked by the caller.
func (conn *tcpConn) writeInternal(data []byte) (int, error) {
err := C.tcp_write(conn.pcb, unsafe.Pointer(&data[0]), C.u16_t(len(data)), C.TCP_WRITE_FLAG_COPY)
if err == C.ERR_OK {
C.tcp_output(conn.pcb)
return len(data), nil
} else if err == C.ERR_MEM {
return 0, nil
}
return 0, fmt.Errorf("tcp_write failed (%v)", int(err))
}
func (conn *tcpConn) writeCheck() error {
conn.Lock()
defer conn.Unlock()
switch conn.state {
case tcpConnecting:
fallthrough
case tcpConnected:
fallthrough
case tcpReceiveClosed:
return nil
case tcpWriteClosed:
fallthrough
case tcpClosing:
fallthrough
case tcpClosed:
fallthrough
case tcpErrored:
fallthrough
case tcpAborting:
return io.ErrClosedPipe
default:
panic("unexpected error")
}
return nil
}
func (conn *tcpConn) Write(data []byte) (int, error) {
totalWritten := 0
conn.canWrite.L.Lock()
defer conn.canWrite.L.Unlock()
for len(data) > 0 {
if err := conn.writeCheck(); err != nil {
return totalWritten, err
}
lwipMutex.Lock()
toWrite := len(data)
if toWrite > int(conn.pcb.snd_buf) {
// Write at most the size of the LWIP buffer.
toWrite = int(conn.pcb.snd_buf)
}
if toWrite > 0 {
written, err := conn.writeInternal(data[0:toWrite])
totalWritten += written
if err != nil {
lwipMutex.Unlock()
return totalWritten, err
}
data = data[written:len(data)]
}
lwipMutex.Unlock()
if len(data) == 0 {
break // Don't block if all the data has been written.
}
conn.canWrite.Wait()
}
return totalWritten, nil
}
func (conn *tcpConn) CloseWrite() error {
conn.Lock()
if conn.state >= tcpClosing || conn.state == tcpWriteClosed {
conn.Unlock()
return nil
}
if conn.state == tcpReceiveClosed {
conn.state = tcpClosing
} else {
conn.state = tcpWriteClosed
}
conn.Unlock()
lwipMutex.Lock()
// FIXME Handle tcp_shutdown error.
C.tcp_shutdown(conn.pcb, 0, 1)
lwipMutex.Unlock()
return nil
}
func (conn *tcpConn) CloseRead() error {
return conn.sndPipeReader.Close()
}
func (conn *tcpConn) Sent(len uint16) error {
// Some packets are acknowledged by local client, check if any pending data to send.
return conn.checkState()
}
func (conn *tcpConn) checkClosing() error {
conn.Lock()
defer conn.Unlock()
if conn.state == tcpClosing {
conn.closeInternal()
return NewLWIPError(LWIP_ERR_OK)
}
return nil
}
func (conn *tcpConn) checkAborting() error {
conn.Lock()
defer conn.Unlock()
if conn.state == tcpAborting {
conn.abortInternal()
return NewLWIPError(LWIP_ERR_ABRT)
}
return nil
}
func (conn *tcpConn) isClosed() bool {
conn.Lock()
defer conn.Unlock()
return conn.state == tcpClosed
}
func (conn *tcpConn) checkState() error {
if conn.isClosed() {
return nil
}
err := conn.checkClosing()
if err != nil {
return err
}
err = conn.checkAborting()
if err != nil {
return err
}
// Signal the writer to try writting.
conn.canWrite.Broadcast()
return NewLWIPError(LWIP_ERR_OK)
}
func (conn *tcpConn) Close() error {
conn.closeOnce.Do(conn.close)
return conn.closeErr
}
func (conn *tcpConn) close() {
err := conn.CloseRead()
if err != nil {
conn.closeErr = err
}
err = conn.CloseWrite()
if err != nil {
conn.closeErr = err
}
}
func (conn *tcpConn) setLocalClosed() error {
conn.Lock()
defer conn.Unlock()
if conn.state >= tcpClosing || conn.state == tcpReceiveClosed {
return nil
}
// Causes the read half of the pipe returns.
conn.sndPipeWriter.Close()
if conn.state == tcpWriteClosed {
conn.state = tcpClosing
} else {
conn.state = tcpReceiveClosed
}
conn.canWrite.Broadcast()
return nil
}
// Never call this function outside of the lwIP thread.
func (conn *tcpConn) closeInternal() error {
C.tcp_arg(conn.pcb, nil)
C.tcp_recv(conn.pcb, nil)
C.tcp_sent(conn.pcb, nil)
C.tcp_err(conn.pcb, nil)
C.tcp_poll(conn.pcb, nil, 0)
conn.release()
// FIXME Handle error.
err := C.tcp_close(conn.pcb)
if err == C.ERR_OK {
return nil
} else {
return errors.New(fmt.Sprintf("close TCP connection failed, lwip error code %d", int(err)))
}
}
// Never call this function outside of the lwIP thread since it calls
// tcp_abort() and in that case we must return ERR_ABRT to lwIP.
func (conn *tcpConn) abortInternal() {
conn.release()
C.tcp_abort(conn.pcb)
}
func (conn *tcpConn) Abort() {
conn.Lock()
// If it's in tcpErrored state, the pcb was already freed.
if conn.state < tcpAborting {
conn.state = tcpAborting
}
conn.Unlock()
lwipMutex.Lock()
conn.checkState()
lwipMutex.Unlock()
}
func (conn *tcpConn) Err(err error) {
conn.Lock()
defer conn.Unlock()
conn.release()
conn.state = tcpErrored
conn.canWrite.Broadcast()
}
func (conn *tcpConn) LocalClosed() error {
conn.setLocalClosed()
return conn.checkState()
}
func (conn *tcpConn) release() {
if _, found := tcpConns.Load(conn.connKey); found {
freeConnKeyArg(conn.connKeyArg)
tcpConns.Delete(conn.connKey)
}
conn.sndPipeWriter.Close()
conn.sndPipeReader.Close()
conn.state = tcpClosed
}
func (conn *tcpConn) Poll() error {
return conn.checkState()
}