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 := spliceFrom(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 (c *TCPConn) writeTo(w io.Writer) (int64, error) {
58 if n, err, handled := spliceTo(w, c.fd); handled {
59 return n, err
60 }
61 return genericWriteTo(c, w)
62 }
63
64 func (sd *sysDialer) dialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
65 if h := sd.testHookDialTCP; h != nil {
66 return h(ctx, sd.network, laddr, raddr)
67 }
68 if h := testHookDialTCP; h != nil {
69 return h(ctx, sd.network, laddr, raddr)
70 }
71 return sd.doDialTCP(ctx, laddr, raddr)
72 }
73
74 func (sd *sysDialer) doDialTCP(ctx context.Context, laddr, raddr *TCPAddr) (*TCPConn, error) {
75 return sd.doDialTCPProto(ctx, laddr, raddr, 0)
76 }
77
78 func (sd *sysDialer) doDialTCPProto(ctx context.Context, laddr, raddr *TCPAddr, proto int) (*TCPConn, error) {
79 ctrlCtxFn := sd.Dialer.ControlContext
80 if ctrlCtxFn == nil && sd.Dialer.Control != nil {
81 ctrlCtxFn = func(ctx context.Context, network, address string, c syscall.RawConn) error {
82 return sd.Dialer.Control(network, address, c)
83 }
84 }
85 fd, err := internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, proto, "dial", ctrlCtxFn)
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 for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
112 if err == nil {
113 fd.Close()
114 }
115 fd, err = internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, proto, "dial", ctrlCtxFn)
116 }
117
118 if err != nil {
119 return nil, err
120 }
121 return newTCPConn(fd, sd.Dialer.KeepAlive, sd.Dialer.KeepAliveConfig, testPreHookSetKeepAlive, testHookSetKeepAlive), nil
122 }
123
124 func selfConnect(fd *netFD, err error) bool {
125
126 if err != nil {
127 return false
128 }
129
130
131
132
133
134
135
136
137
138 if fd.laddr == nil || fd.raddr == nil {
139 return true
140 }
141 l := fd.laddr.(*TCPAddr)
142 r := fd.raddr.(*TCPAddr)
143 return l.Port == r.Port && l.IP.Equal(r.IP)
144 }
145
146 func spuriousENOTAVAIL(err error) bool {
147 if op, ok := err.(*OpError); ok {
148 err = op.Err
149 }
150 if sys, ok := err.(*os.SyscallError); ok {
151 err = sys.Err
152 }
153 return err == syscall.EADDRNOTAVAIL
154 }
155
156 func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
157
158 func (ln *TCPListener) accept() (*TCPConn, error) {
159 fd, err := ln.fd.accept()
160 if err != nil {
161 return nil, err
162 }
163 return newTCPConn(fd, ln.lc.KeepAlive, ln.lc.KeepAliveConfig, testPreHookSetKeepAlive, testHookSetKeepAlive), nil
164 }
165
166 func (ln *TCPListener) close() error {
167 return ln.fd.Close()
168 }
169
170 func (ln *TCPListener) file() (*os.File, error) {
171 f, err := ln.fd.dup()
172 if err != nil {
173 return nil, err
174 }
175 return f, nil
176 }
177
178 func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
179 return sl.listenTCPProto(ctx, laddr, 0)
180 }
181
182 func (sl *sysListener) listenTCPProto(ctx context.Context, laddr *TCPAddr, proto int) (*TCPListener, error) {
183 var ctrlCtxFn func(ctx context.Context, network, address string, c syscall.RawConn) error
184 if sl.ListenConfig.Control != nil {
185 ctrlCtxFn = func(ctx context.Context, network, address string, c syscall.RawConn) error {
186 return sl.ListenConfig.Control(network, address, c)
187 }
188 }
189 fd, err := internetSocket(ctx, sl.network, laddr, nil, syscall.SOCK_STREAM, proto, "listen", ctrlCtxFn)
190 if err != nil {
191 return nil, err
192 }
193 return &TCPListener{fd: fd, lc: sl.ListenConfig}, nil
194 }
195
View as plain text