1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build aix darwin dragonfly freebsd hurd js,wasm linux nacl netbsd openbsd solaris windows
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))}
26 func (a *TCPAddr) family() int {
27 if a == nil || len(a.IP) <= IPv4len {
28 return syscall.AF_INET
30 if a.IP.To4() != nil {
31 return syscall.AF_INET
33 return syscall.AF_INET6
36 func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
40 return ipToSockaddr(family, a.IP, a.Port, a.Zone)
43 func (a *TCPAddr) toLocal(net string) sockaddr {
44 return &TCPAddr{loopbackIP(net), a.Port, a.Zone}
47 func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
48 if n, err, handled := splice(c.fd, r); handled {
51 if n, err, handled := sendFile(c.fd, r); handled {
54 return genericReadFrom(c, r)
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)
61 return sd.doDialTCP(ctx, laddr, raddr)
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)
67 // TCP has a rarely used mechanism called a 'simultaneous connection' in
68 // which Dial("tcp", addr1, addr2) run on the machine at addr1 can
69 // connect to a simultaneous Dial("tcp", addr2, addr1) run on the machine
70 // at addr2, without either machine executing Listen. If laddr == nil,
71 // it means we want the kernel to pick an appropriate originating local
72 // address. Some Linux kernels cycle blindly through a fixed range of
73 // local ports, regardless of destination port. If a kernel happens to
74 // pick local port 50001 as the source for a Dial("tcp", "", "localhost:50001"),
75 // then the Dial will succeed, having simultaneously connected to itself.
76 // This can only happen when we are letting the kernel pick a port (laddr == nil)
77 // and when there is no listener for the destination address.
78 // It's hard to argue this is anything other than a kernel bug. If we
79 // see this happen, rather than expose the buggy effect to users, we
80 // close the fd and try again. If it happens twice more, we relent and
81 // use the result. See also:
82 // https://golang.org/issue/2690
83 // https://stackoverflow.com/questions/4949858/
85 // The opposite can also happen: if we ask the kernel to pick an appropriate
86 // originating local address, sometimes it picks one that is already in use.
87 // So if the error is EADDRNOTAVAIL, we have to try again too, just for
88 // a different reason.
90 // The kernel socket code is no doubt enjoying watching us squirm.
91 for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
95 fd, err = internetSocket(ctx, sd.network, laddr, raddr, syscall.SOCK_STREAM, 0, "dial", sd.Dialer.Control)
101 return newTCPConn(fd), nil
104 func selfConnect(fd *netFD, err error) bool {
105 // If the connect failed, we clearly didn't connect to ourselves.
110 // The socket constructor can return an fd with raddr nil under certain
111 // unknown conditions. The errors in the calls there to Getpeername
112 // are discarded, but we can't catch the problem there because those
113 // calls are sometimes legally erroneous with a "socket not connected".
114 // Since this code (selfConnect) is already trying to work around
115 // a problem, we make sure if this happens we recognize trouble and
116 // ask the DialTCP routine to try again.
117 // TODO: try to understand what's really going on.
118 if fd.laddr == nil || fd.raddr == nil {
121 l := fd.laddr.(*TCPAddr)
122 r := fd.raddr.(*TCPAddr)
123 return l.Port == r.Port && l.IP.Equal(r.IP)
126 func spuriousENOTAVAIL(err error) bool {
127 if op, ok := err.(*OpError); ok {
130 if sys, ok := err.(*os.SyscallError); ok {
133 return err == syscall.EADDRNOTAVAIL
136 func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
138 func (ln *TCPListener) accept() (*TCPConn, error) {
139 fd, err := ln.fd.accept()
144 if ln.lc.KeepAlive >= 0 {
145 setKeepAlive(fd, true)
146 ka := ln.lc.KeepAlive
147 if ln.lc.KeepAlive == 0 {
148 ka = defaultTCPKeepAlive
150 setKeepAlivePeriod(fd, ka)
155 func (ln *TCPListener) close() error {
159 func (ln *TCPListener) file() (*os.File, error) {
160 f, err := ln.fd.dup()
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)
172 return &TCPListener{fd: fd, lc: sl.ListenConfig}, nil