...
Run Format

Source file src/net/tcpsock_posix.go

     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	// +build darwin dragonfly freebsd linux nacl 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: zoneToString(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 (c *TCPConn) readFrom(r io.Reader) (int64, error) {
    44		if n, err, handled := sendFile(c.fd, r); handled {
    45			return n, err
    46		}
    47		return genericReadFrom(c, r)
    48	}
    49	
    50	func dialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
    51		if testHookDialTCP != nil {
    52			return testHookDialTCP(ctx, net, laddr, raddr)
    53		}
    54		return doDialTCP(ctx, net, laddr, raddr)
    55	}
    56	
    57	func doDialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
    58		fd, err := internetSocket(ctx, net, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
    59	
    60		// TCP has a rarely used mechanism called a 'simultaneous connection' in
    61		// which Dial("tcp", addr1, addr2) run on the machine at addr1 can
    62		// connect to a simultaneous Dial("tcp", addr2, addr1) run on the machine
    63		// at addr2, without either machine executing Listen. If laddr == nil,
    64		// it means we want the kernel to pick an appropriate originating local
    65		// address. Some Linux kernels cycle blindly through a fixed range of
    66		// local ports, regardless of destination port. If a kernel happens to
    67		// pick local port 50001 as the source for a Dial("tcp", "", "localhost:50001"),
    68		// then the Dial will succeed, having simultaneously connected to itself.
    69		// This can only happen when we are letting the kernel pick a port (laddr == nil)
    70		// and when there is no listener for the destination address.
    71		// It's hard to argue this is anything other than a kernel bug. If we
    72		// see this happen, rather than expose the buggy effect to users, we
    73		// close the fd and try again. If it happens twice more, we relent and
    74		// use the result. See also:
    75		//	https://golang.org/issue/2690
    76		//	http://stackoverflow.com/questions/4949858/
    77		//
    78		// The opposite can also happen: if we ask the kernel to pick an appropriate
    79		// originating local address, sometimes it picks one that is already in use.
    80		// So if the error is EADDRNOTAVAIL, we have to try again too, just for
    81		// a different reason.
    82		//
    83		// The kernel socket code is no doubt enjoying watching us squirm.
    84		for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
    85			if err == nil {
    86				fd.Close()
    87			}
    88			fd, err = internetSocket(ctx, net, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
    89		}
    90	
    91		if err != nil {
    92			return nil, err
    93		}
    94		return newTCPConn(fd), nil
    95	}
    96	
    97	func selfConnect(fd *netFD, err error) bool {
    98		// If the connect failed, we clearly didn't connect to ourselves.
    99		if err != nil {
   100			return false
   101		}
   102	
   103		// The socket constructor can return an fd with raddr nil under certain
   104		// unknown conditions. The errors in the calls there to Getpeername
   105		// are discarded, but we can't catch the problem there because those
   106		// calls are sometimes legally erroneous with a "socket not connected".
   107		// Since this code (selfConnect) is already trying to work around
   108		// a problem, we make sure if this happens we recognize trouble and
   109		// ask the DialTCP routine to try again.
   110		// TODO: try to understand what's really going on.
   111		if fd.laddr == nil || fd.raddr == nil {
   112			return true
   113		}
   114		l := fd.laddr.(*TCPAddr)
   115		r := fd.raddr.(*TCPAddr)
   116		return l.Port == r.Port && l.IP.Equal(r.IP)
   117	}
   118	
   119	func spuriousENOTAVAIL(err error) bool {
   120		if op, ok := err.(*OpError); ok {
   121			err = op.Err
   122		}
   123		if sys, ok := err.(*os.SyscallError); ok {
   124			err = sys.Err
   125		}
   126		return err == syscall.EADDRNOTAVAIL
   127	}
   128	
   129	func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
   130	
   131	func (ln *TCPListener) accept() (*TCPConn, error) {
   132		fd, err := ln.fd.accept()
   133		if err != nil {
   134			return nil, err
   135		}
   136		return newTCPConn(fd), nil
   137	}
   138	
   139	func (ln *TCPListener) close() error {
   140		return ln.fd.Close()
   141	}
   142	
   143	func (ln *TCPListener) file() (*os.File, error) {
   144		f, err := ln.fd.dup()
   145		if err != nil {
   146			return nil, err
   147		}
   148		return f, nil
   149	}
   150	
   151	func listenTCP(ctx context.Context, network string, laddr *TCPAddr) (*TCPListener, error) {
   152		fd, err := internetSocket(ctx, network, laddr, nil, syscall.SOCK_STREAM, 0, "listen")
   153		if err != nil {
   154			return nil, err
   155		}
   156		return &TCPListener{fd}, nil
   157	}
   158	

View as plain text