github.com/m10x/go/src@v0.0.0-20220112094212-ba61592315da/net/tcpsock_posix.go (about) 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. 4 5 //go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || windows 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 // 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/ 84 // 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. 89 // 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++ { 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 // If the connect failed, we clearly didn't connect to ourselves. 106 if err != nil { 107 return false 108 } 109 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 { 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 }