Source file
src/net/tcpsock_posix.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "context"
11 "io"
12 "os"
13 "syscall"
14 )
15
16 func sockaddrToTCP(sa syscall.Sockaddr) Addr {
17 switch sa := sa.(type) {
18 case *syscall.SockaddrInet4:
19 return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port}
20 case *syscall.SockaddrInet6:
21 return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneCache.name(int(sa.ZoneId))}
22 }
23 return nil
24 }
25
26 func (a *TCPAddr) family() int {
27 if a == nil || len(a.IP) <= IPv4len {
28 return syscall.AF_INET
29 }
30 if a.IP.To4() != nil {
31 return syscall.AF_INET
32 }
33 return syscall.AF_INET6
34 }
35
36 func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
37 if a == nil {
38 return nil, nil
39 }
40 return ipToSockaddr(family, a.IP, a.Port, a.Zone)
41 }
42
43 func (a *TCPAddr) toLocal(net string) sockaddr {
44 return &TCPAddr{loopbackIP(net), a.Port, a.Zone}
45 }
46
47 func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
48 if n, err, handled := splice(c.fd, r); handled {
49 return n, err
50 }
51 if n, err, handled := sendFile(c.fd, r); handled {
52 return n, err
53 }
54 return genericReadFrom(c, r)
55 }
56
57 func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
58 if testHookDialTCP != nil {
59 return testHookDialTCP(ctx, sd.network, laddr, raddr)
60 }
61 return sd.doDialTCP(ctx, laddr, raddr)
62 }
63
64 func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
65 fd, err := internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, 0, "dial", sd.Dialer.Control)
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 for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
92 if err == nil {
93 fd.Close()
94 }
95 fd, err = internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, 0, "dial", sd.Dialer.Control)
96 }
97
98 if err != nil {
99 return nil, err
100 }
101 return newTCPConn(fd), nil
102 }
103
104 func selfConnect(fd *netFD, err error) bool {
105
106 if err != nil {
107 return false
108 }
109
110
111
112
113
114
115
116
117
118 if fd.laddr == nil || fd.raddr == nil {
119 return true
120 }
121 l := fd.laddr.(*TCPAddr)
122 r := fd.raddr.(*TCPAddr)
123 return l.Port == r.Port && l.IP.Equal(r.IP)
124 }
125
126 func spuriousENOTAVAIL(err error) bool {
127 if op, ok := err.(*OpError); ok {
128 err = op.Err
129 }
130 if sys, ok := err.(*os.SyscallError); ok {
131 err = sys.Err
132 }
133 return err == syscall.EADDRNOTAVAIL
134 }
135
136 func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
137
138 func (ln *TCPListener) accept() (*TCPConn, error) {
139 fd, err := ln.fd.accept()
140 if err != nil {
141 return nil, err
142 }
143 tc := newTCPConn(fd)
144 if ln.lc.KeepAlive >= 0 {
145 setKeepAlive(fd, true)
146 ka := ln.lc.KeepAlive
147 if ln.lc.KeepAlive == 0 {
148 ka = defaultTCPKeepAlive
149 }
150 setKeepAlivePeriod(fd, ka)
151 }
152 return tc, nil
153 }
154
155 func (ln *TCPListener) close() error {
156 return ln.fd.Close()
157 }
158
159 func (ln *TCPListener) file() (*os.File, error) {
160 f, err := ln.fd.dup()
161 if err != nil {
162 return nil, err
163 }
164 return f, nil
165 }
166
167 func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
168 fd, err := internetSocket(ctx, sl.network, laddr, nil, syscall.SOCK_STREAM, 0, "listen", sl.ListenConfig.Control)
169 if err != nil {
170 return nil, err
171 }
172 return &TCPListener{fd: fd, lc: sl.ListenConfig}, nil
173 }
174
View as plain text