Source file
src/net/tcpsock.go
1
2
3
4
5 package net
6
7 import (
8 "context"
9 "internal/itoa"
10 "io"
11 "net/netip"
12 "os"
13 "syscall"
14 "time"
15 )
16
17
18
19
20
21 type TCPAddr struct {
22 IP IP
23 Port int
24 Zone string
25 }
26
27
28
29
30
31
32 func (a *TCPAddr) AddrPort() netip.AddrPort {
33 if a == nil {
34 return netip.AddrPort{}
35 }
36 na, _ := netip.AddrFromSlice(a.IP)
37 na = na.WithZone(a.Zone)
38 return netip.AddrPortFrom(na, uint16(a.Port))
39 }
40
41
42 func (a *TCPAddr) Network() string { return "tcp" }
43
44 func (a *TCPAddr) String() string {
45 if a == nil {
46 return "<nil>"
47 }
48 ip := ipEmptyString(a.IP)
49 if a.Zone != "" {
50 return JoinHostPort(ip+"%"+a.Zone, itoa.Itoa(a.Port))
51 }
52 return JoinHostPort(ip, itoa.Itoa(a.Port))
53 }
54
55 func (a *TCPAddr) isWildcard() bool {
56 if a == nil || a.IP == nil {
57 return true
58 }
59 return a.IP.IsUnspecified()
60 }
61
62 func (a *TCPAddr) opAddr() Addr {
63 if a == nil {
64 return nil
65 }
66 return a
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
85 switch network {
86 case "tcp", "tcp4", "tcp6":
87 case "":
88 network = "tcp"
89 default:
90 return nil, UnknownNetworkError(network)
91 }
92 addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address)
93 if err != nil {
94 return nil, err
95 }
96 return addrs.forResolve(network, address).(*TCPAddr), nil
97 }
98
99
100
101
102 func TCPAddrFromAddrPort(addr netip.AddrPort) *TCPAddr {
103 return &TCPAddr{
104 IP: addr.Addr().AsSlice(),
105 Zone: addr.Addr().Zone(),
106 Port: int(addr.Port()),
107 }
108 }
109
110
111
112 type TCPConn struct {
113 conn
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 type KeepAliveConfig struct {
133
134 Enable bool
135
136
137
138
139 Idle time.Duration
140
141
142
143 Interval time.Duration
144
145
146
147
148 Count int
149 }
150
151
152
153 func (c *TCPConn) SyscallConn() (syscall.RawConn, error) {
154 if !c.ok() {
155 return nil, syscall.EINVAL
156 }
157 return newRawConn(c.fd), nil
158 }
159
160
161 func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
162 if !c.ok() {
163 return 0, syscall.EINVAL
164 }
165 n, err := c.readFrom(r)
166 if err != nil && err != io.EOF {
167 err = &OpError{Op: "readfrom", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
168 }
169 return n, err
170 }
171
172
173 func (c *TCPConn) WriteTo(w io.Writer) (int64, error) {
174 if !c.ok() {
175 return 0, syscall.EINVAL
176 }
177 n, err := c.writeTo(w)
178 if err != nil && err != io.EOF {
179 err = &OpError{Op: "writeto", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
180 }
181 return n, err
182 }
183
184
185
186 func (c *TCPConn) CloseRead() error {
187 if !c.ok() {
188 return syscall.EINVAL
189 }
190 if err := c.fd.closeRead(); err != nil {
191 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
192 }
193 return nil
194 }
195
196
197
198 func (c *TCPConn) CloseWrite() error {
199 if !c.ok() {
200 return syscall.EINVAL
201 }
202 if err := c.fd.closeWrite(); err != nil {
203 return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
204 }
205 return nil
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 func (c *TCPConn) SetLinger(sec int) error {
223 if !c.ok() {
224 return syscall.EINVAL
225 }
226 if err := setLinger(c.fd, sec); err != nil {
227 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
228 }
229 return nil
230 }
231
232
233
234 func (c *TCPConn) SetKeepAlive(keepalive bool) error {
235 if !c.ok() {
236 return syscall.EINVAL
237 }
238 if err := setKeepAlive(c.fd, keepalive); err != nil {
239 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
240 }
241 return nil
242 }
243
244
245
246
247
248
249 func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
250 if !c.ok() {
251 return syscall.EINVAL
252 }
253 if err := setKeepAliveIdle(c.fd, d); err != nil {
254 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
255 }
256 return nil
257 }
258
259
260
261
262
263 func (c *TCPConn) SetNoDelay(noDelay bool) error {
264 if !c.ok() {
265 return syscall.EINVAL
266 }
267 if err := setNoDelay(c.fd, noDelay); err != nil {
268 return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
269 }
270 return nil
271 }
272
273
274
275
276
277
278
279
280
281
282 func (c *TCPConn) MultipathTCP() (bool, error) {
283 if !c.ok() {
284 return false, syscall.EINVAL
285 }
286 return isUsingMultipathTCP(c.fd), nil
287 }
288
289 func newTCPConn(fd *netFD, keepAliveIdle time.Duration, keepAliveCfg KeepAliveConfig, preKeepAliveHook func(*netFD), keepAliveHook func(KeepAliveConfig)) *TCPConn {
290 setNoDelay(fd, true)
291 if !keepAliveCfg.Enable && keepAliveIdle >= 0 {
292 keepAliveCfg = KeepAliveConfig{
293 Enable: true,
294 Idle: keepAliveIdle,
295 }
296 }
297 c := &TCPConn{conn{fd}}
298 if keepAliveCfg.Enable {
299 if preKeepAliveHook != nil {
300 preKeepAliveHook(fd)
301 }
302 c.SetKeepAliveConfig(keepAliveCfg)
303 if keepAliveHook != nil {
304 keepAliveHook(keepAliveCfg)
305 }
306 }
307 return c
308 }
309
310
311
312
313
314
315
316
317 func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
318 switch network {
319 case "tcp", "tcp4", "tcp6":
320 default:
321 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
322 }
323 if raddr == nil {
324 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
325 }
326 sd := &sysDialer{network: network, address: raddr.String()}
327 c, err := sd.dialTCP(context.Background(), laddr, raddr)
328 if err != nil {
329 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
330 }
331 return c, nil
332 }
333
334
335
336 type TCPListener struct {
337 fd *netFD
338 lc ListenConfig
339 }
340
341
342
343
344
345
346 func (l *TCPListener) SyscallConn() (syscall.RawConn, error) {
347 if !l.ok() {
348 return nil, syscall.EINVAL
349 }
350 return newRawListener(l.fd), nil
351 }
352
353
354
355 func (l *TCPListener) AcceptTCP() (*TCPConn, error) {
356 if !l.ok() {
357 return nil, syscall.EINVAL
358 }
359 c, err := l.accept()
360 if err != nil {
361 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
362 }
363 return c, nil
364 }
365
366
367
368 func (l *TCPListener) Accept() (Conn, error) {
369 if !l.ok() {
370 return nil, syscall.EINVAL
371 }
372 c, err := l.accept()
373 if err != nil {
374 return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
375 }
376 return c, nil
377 }
378
379
380
381 func (l *TCPListener) Close() error {
382 if !l.ok() {
383 return syscall.EINVAL
384 }
385 if err := l.close(); err != nil {
386 return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
387 }
388 return nil
389 }
390
391
392
393
394 func (l *TCPListener) Addr() Addr { return l.fd.laddr }
395
396
397
398 func (l *TCPListener) SetDeadline(t time.Time) error {
399 if !l.ok() {
400 return syscall.EINVAL
401 }
402 return l.fd.SetDeadline(t)
403 }
404
405
406
407
408
409
410
411
412 func (l *TCPListener) File() (f *os.File, err error) {
413 if !l.ok() {
414 return nil, syscall.EINVAL
415 }
416 f, err = l.file()
417 if err != nil {
418 return nil, &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
419 }
420 return
421 }
422
423
424
425
426
427
428
429
430
431
432 func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
433 switch network {
434 case "tcp", "tcp4", "tcp6":
435 default:
436 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
437 }
438 if laddr == nil {
439 laddr = &TCPAddr{}
440 }
441 sl := &sysListener{network: network, address: laddr.String()}
442 ln, err := sl.listenTCP(context.Background(), laddr)
443 if err != nil {
444 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
445 }
446 return ln, nil
447 }
448
449
450 func roundDurationUp(d time.Duration, to time.Duration) time.Duration {
451 return (d + to - 1) / to
452 }
453
View as plain text