forked from libp2p/go-reuseport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl_unix.go
399 lines (335 loc) · 8.75 KB
/
impl_unix.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
// +build darwin freebsd dragonfly netbsd openbsd linux
package reuseport
import (
"context"
"golang.org/x/sys/unix"
"net"
"os"
"strconv"
"syscall"
"time"
"github.com/libp2p/go-reuseport/singlepoll"
sockaddrnet "github.com/libp2p/go-sockaddr/net"
)
const (
filePrefix = "port."
)
// Wrapper around the socket system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
func socket(family, socktype, protocol int) (fd int, err error) {
syscall.ForkLock.RLock()
fd, err = unix.Socket(family, socktype, protocol)
if err == nil {
unix.CloseOnExec(fd)
}
syscall.ForkLock.RUnlock()
if err != nil {
return -1, err
}
// cant set it until after connect
// if err = unix.SetNonblock(fd, true); err != nil {
// unix.Close(fd)
// return -1, err
// }
if err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1); err != nil {
unix.Close(fd)
return -1, err
}
if err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
unix.Close(fd)
return -1, err
}
// set setLinger to 5 as reusing exact same (srcip:srcport, dstip:dstport)
// will otherwise fail on connect.
if err = setLinger(fd, 5); err != nil {
unix.Close(fd)
return -1, err
}
return fd, nil
}
func dial(ctx context.Context, dialer net.Dialer, netw, addr string) (c net.Conn, err error) {
var (
fd int
lfamily int
rfamily int
socktype int
lprotocol int
rprotocol int
file *os.File
deadline time.Time
remoteSockaddr unix.Sockaddr
localSockaddr unix.Sockaddr
)
netAddr, err := ResolveAddr(netw, addr)
if err != nil {
return nil, err
}
switch netAddr.(type) {
case *net.TCPAddr, *net.UDPAddr:
default:
return nil, ErrUnsupportedProtocol
}
switch {
case !dialer.Deadline.IsZero():
deadline = dialer.Deadline
case dialer.Timeout != 0:
deadline = time.Now().Add(dialer.Timeout)
}
ctxdeadline, ok := ctx.Deadline()
if ok && ctxdeadline.Before(deadline) {
deadline = ctxdeadline
}
localSockaddr = sockaddrnet.NetAddrToSockaddr(dialer.LocalAddr)
remoteSockaddr = sockaddrnet.NetAddrToSockaddr(netAddr)
rfamily = sockaddrnet.NetAddrAF(netAddr)
rprotocol = sockaddrnet.NetAddrIPPROTO(netAddr)
socktype = sockaddrnet.NetAddrSOCK(netAddr)
if dialer.LocalAddr != nil {
switch dialer.LocalAddr.(type) {
case *net.TCPAddr, *net.UDPAddr:
default:
return nil, ErrUnsupportedProtocol
}
// check family and protocols match.
lfamily = sockaddrnet.NetAddrAF(dialer.LocalAddr)
lprotocol = sockaddrnet.NetAddrIPPROTO(dialer.LocalAddr)
if lfamily != rfamily || lprotocol != rprotocol {
return nil, &net.AddrError{Err: "unexpected address type", Addr: netAddr.String()}
}
}
// look at dialTCP in http://golang.org/src/net/tcpsock_posix.go .... !
// here we just try again 3 times.
for i := 0; i < 3; i++ {
if !deadline.IsZero() && deadline.Before(time.Now()) {
err = errTimeout
break
}
if fd, err = socket(rfamily, socktype, rprotocol); err != nil {
return nil, err
}
if localSockaddr != nil {
if err = unix.Bind(fd, localSockaddr); err != nil {
unix.Close(fd)
return nil, err
}
}
if err = unix.SetNonblock(fd, true); err != nil {
unix.Close(fd)
return nil, err
}
if err = connect(ctx, fd, remoteSockaddr, deadline); err != nil {
unix.Close(fd)
if ctx.Err() != nil {
return nil, ctx.Err()
}
continue // try again.
}
break
}
if err != nil {
return nil, err
}
if rprotocol == unix.IPPROTO_TCP {
sa, err := unix.Getsockname(fd)
if err != nil {
unix.Close(fd)
return nil, err
}
ra, err := unix.Getpeername(fd)
if err != nil {
unix.Close(fd)
return nil, err
}
// Need to call setLinger 0. Otherwise, the close will block for the linger period. Linux bug?
switch s := sa.(type) {
case *unix.SockaddrInet4:
if r, ok := ra.(*unix.SockaddrInet4); ok && r.Addr == s.Addr && r.Port == s.Port {
setLinger(fd, 0)
unix.Close(fd)
return nil, ErrDialSelf
}
case *unix.SockaddrInet6:
if r, ok := ra.(*unix.SockaddrInet6); ok && r.Addr == s.Addr && r.Port == s.Port && r.ZoneId == s.ZoneId {
setLinger(fd, 0)
unix.Close(fd)
return nil, ErrDialSelf
}
}
// by default golang/net sets TCP no delay to true.
if err = setNoDelay(fd, true); err != nil {
unix.Close(fd)
return nil, err
}
}
// NOTE:XXX: never call unix.Close on fd after os.NewFile
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
fd = -1 // so we don't touch it, we handled the control to Golang with NewFile
if c, err = net.FileConn(file); err != nil {
_ = file.Close() // shouldn't error either way
return nil, err
}
if err = file.Close(); err != nil {
c.Close()
return nil, err
}
// c = wrapConnWithRemoteAddr(c, netAddr)
return c, err
}
func listen(netw, addr string) (fd int, err error) {
var (
family int
socktype int
protocol int
sockaddr unix.Sockaddr
)
netAddr, err := ResolveAddr(netw, addr)
if err != nil {
return -1, err
}
switch netAddr.(type) {
case *net.TCPAddr, *net.UDPAddr:
default:
return -1, ErrUnsupportedProtocol
}
family = sockaddrnet.NetAddrAF(netAddr)
protocol = sockaddrnet.NetAddrIPPROTO(netAddr)
sockaddr = sockaddrnet.NetAddrToSockaddr(netAddr)
socktype = sockaddrnet.NetAddrSOCK(netAddr)
if fd, err = socket(family, socktype, protocol); err != nil {
return -1, err
}
if err = unix.Bind(fd, sockaddr); err != nil {
unix.Close(fd)
return -1, err
}
if protocol == unix.IPPROTO_TCP {
// by default golang/net sets TCP no delay to true.
if err = setNoDelay(fd, true); err != nil {
unix.Close(fd)
return -1, err
}
}
if err = unix.SetNonblock(fd, true); err != nil {
unix.Close(fd)
return -1, err
}
return fd, nil
}
func listenStream(netw, addr string) (l net.Listener, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
// Set backlog size to the maximum
if err = unix.Listen(fd, unix.SOMAXCONN); err != nil {
unix.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FileListener(file); err != nil {
unix.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
unix.Close(fd)
l.Close()
return nil, err
}
return l, err
}
func listenPacket(netw, addr string) (p net.PacketConn, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if p, err = net.FilePacketConn(file); err != nil {
unix.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
unix.Close(fd)
p.Close()
return nil, err
}
return p, err
}
func listenUDP(netw, addr string) (c net.Conn, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if c, err = net.FileConn(file); err != nil {
unix.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
unix.Close(fd)
return nil, err
}
return c, err
}
// this is close to the connect() function inside stdlib/net
func connect(ctx context.Context, fd int, ra unix.Sockaddr, deadline time.Time) error {
if !deadline.IsZero() {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, deadline)
defer cancel()
}
switch err := unix.Connect(fd, ra); err {
case unix.EINPROGRESS, unix.EALREADY, unix.EINTR:
case nil, unix.EISCONN:
if !deadline.IsZero() && deadline.Before(time.Now()) {
return errTimeout
}
if ctx.Err() != nil {
return ctx.Err()
}
return nil
default:
return err
}
for {
if err := singlepoll.PollPark(ctx, fd, "w"); err != nil {
return err
}
// if err := fd.pd.WaitWrite(); err != nil {
// return err
// }
// i'd use the above fd.pd.WaitWrite to poll io correctly, just like net sockets...
// but of course, it uses the damn runtime_* functions that _cannot_ be used by
// non-go-stdlib source... seriously guys, this is not nice.
// we're relegated to using unix.Select (what nightmare that is) or using
// a simple but totally bogus time-based wait. such garbage.
nerr, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_ERROR)
if err != nil {
return err
}
switch err = syscall.Errno(nerr); err {
case unix.EINPROGRESS, unix.EALREADY, unix.EINTR:
continue
case syscall.Errno(0), unix.EISCONN:
if !deadline.IsZero() && deadline.Before(time.Now()) {
return errTimeout
}
return nil
default:
return err
}
}
}
var errTimeout = &timeoutError{}
type timeoutError struct{}
func (e *timeoutError) Error() string { return "i/o timeout" }
func (e *timeoutError) Timeout() bool { return true }
func (e *timeoutError) Temporary() bool { return true }