2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-09-16 17:47:21 +02:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-07-22 20:15:38 +02:00
|
|
|
"context"
|
2011-09-16 17:47:21 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sockaddrToTCP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 00:13:29 +01:00
|
|
|
return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2011-09-16 17:47:21 +02:00
|
|
|
case *syscall.SockaddrInet6:
|
2012-12-13 00:13:29 +01:00
|
|
|
return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TCPAddr) family() int {
|
2011-10-27 01:57:58 +02:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-09-16 17:47:21 +02:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2013-11-06 20:49:01 +01:00
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port, a.Zone)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
func (a *TCPAddr) toLocal(net string) sockaddr {
|
|
|
|
return &TCPAddr{loopbackIP(net), a.Port, a.Zone}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if n, err, handled := sendFile(c.fd, r); handled {
|
|
|
|
return n, err
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return genericReadFrom(c, r)
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func dialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
if testHookDialTCP != nil {
|
|
|
|
return testHookDialTCP(ctx, net, laddr, raddr)
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return doDialTCP(ctx, net, laddr, raddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func doDialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
fd, err := internetSocket(ctx, net, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
|
2012-03-02 17:38:43 +01:00
|
|
|
|
|
|
|
// TCP has a rarely used mechanism called a 'simultaneous connection' in
|
|
|
|
// which Dial("tcp", addr1, addr2) run on the machine at addr1 can
|
|
|
|
// connect to a simultaneous Dial("tcp", addr2, addr1) run on the machine
|
2016-07-22 20:15:38 +02:00
|
|
|
// at addr2, without either machine executing Listen. If laddr == nil,
|
2012-03-02 17:38:43 +01:00
|
|
|
// it means we want the kernel to pick an appropriate originating local
|
2016-07-22 20:15:38 +02:00
|
|
|
// address. Some Linux kernels cycle blindly through a fixed range of
|
|
|
|
// local ports, regardless of destination port. If a kernel happens to
|
2012-03-02 17:38:43 +01:00
|
|
|
// pick local port 50001 as the source for a Dial("tcp", "", "localhost:50001"),
|
|
|
|
// then the Dial will succeed, having simultaneously connected to itself.
|
|
|
|
// This can only happen when we are letting the kernel pick a port (laddr == nil)
|
|
|
|
// and when there is no listener for the destination address.
|
2016-07-22 20:15:38 +02:00
|
|
|
// It's hard to argue this is anything other than a kernel bug. If we
|
2012-03-02 17:38:43 +01:00
|
|
|
// see this happen, rather than expose the buggy effect to users, we
|
2016-07-22 20:15:38 +02:00
|
|
|
// close the fd and try again. If it happens twice more, we relent and
|
|
|
|
// use the result. See also:
|
2015-10-31 01:59:47 +01:00
|
|
|
// https://golang.org/issue/2690
|
2012-03-02 17:38:43 +01:00
|
|
|
// http://stackoverflow.com/questions/4949858/
|
2012-10-23 06:31:11 +02:00
|
|
|
//
|
|
|
|
// The opposite can also happen: if we ask the kernel to pick an appropriate
|
|
|
|
// originating local address, sometimes it picks one that is already in use.
|
|
|
|
// So if the error is EADDRNOTAVAIL, we have to try again too, just for
|
|
|
|
// a different reason.
|
|
|
|
//
|
|
|
|
// The kernel socket code is no doubt enjoying watching us squirm.
|
|
|
|
for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
|
|
|
|
if err == nil {
|
|
|
|
fd.Close()
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
fd, err = internetSocket(ctx, net, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
if err != nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func selfConnect(fd *netFD, err error) bool {
|
|
|
|
// If the connect failed, we clearly didn't connect to ourselves.
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
// The socket constructor can return an fd with raddr nil under certain
|
|
|
|
// unknown conditions. The errors in the calls there to Getpeername
|
|
|
|
// are discarded, but we can't catch the problem there because those
|
|
|
|
// calls are sometimes legally erroneous with a "socket not connected".
|
|
|
|
// Since this code (selfConnect) is already trying to work around
|
|
|
|
// a problem, we make sure if this happens we recognize trouble and
|
|
|
|
// ask the DialTCP routine to try again.
|
|
|
|
// TODO: try to understand what's really going on.
|
|
|
|
if fd.laddr == nil || fd.raddr == nil {
|
|
|
|
return true
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
l := fd.laddr.(*TCPAddr)
|
|
|
|
r := fd.raddr.(*TCPAddr)
|
|
|
|
return l.Port == r.Port && l.IP.Equal(r.IP)
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func spuriousENOTAVAIL(err error) bool {
|
2015-10-31 01:59:47 +01:00
|
|
|
if op, ok := err.(*OpError); ok {
|
|
|
|
err = op.Err
|
|
|
|
}
|
|
|
|
if sys, ok := err.(*os.SyscallError); ok {
|
|
|
|
err = sys.Err
|
|
|
|
}
|
|
|
|
return err == syscall.EADDRNOTAVAIL
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (ln *TCPListener) accept() (*TCPConn, error) {
|
|
|
|
fd, err := ln.fd.accept()
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return newTCPConn(fd), nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (ln *TCPListener) close() error {
|
|
|
|
return ln.fd.Close()
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func (ln *TCPListener) file() (*os.File, error) {
|
|
|
|
f, err := ln.fd.dup()
|
2015-10-31 01:59:47 +01:00
|
|
|
if err != nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
return f, nil
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
func listenTCP(ctx context.Context, network string, laddr *TCPAddr) (*TCPListener, error) {
|
|
|
|
fd, err := internetSocket(ctx, network, laddr, nil, syscall.SOCK_STREAM, 0, "listen")
|
2012-11-21 08:03:38 +01:00
|
|
|
if err != nil {
|
2016-07-22 20:15:38 +02:00
|
|
|
return nil, err
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
return &TCPListener{fd}, nil
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|