2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// TCP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2012-01-25 22:54:22 +01:00
|
|
|
"time"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// BUG(rsc): On OpenBSD, listening on the "tcp" network does not listen for
|
|
|
|
// both IPv4 and IPv6 connections. This is due to the fact that IPv4 traffic
|
|
|
|
// will not be routed to an IPv6 socket - two separate sockets are required
|
|
|
|
// if both AFs are to be supported. See inet6(4) on OpenBSD for details.
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (a *TCPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2012-12-13 00:13:29 +01:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port, a.Zone)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TCPAddr) toAddr() sockaddr {
|
|
|
|
if a == nil { // nil *TCPAddr
|
|
|
|
return nil // nil interface
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPConn is an implementation of the Conn interface
|
|
|
|
// for TCP network connections.
|
|
|
|
type TCPConn struct {
|
2012-10-23 06:31:11 +02:00
|
|
|
conn
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTCPConn(fd *netFD) *TCPConn {
|
2012-10-23 06:31:11 +02:00
|
|
|
c := &TCPConn{conn{fd}}
|
2011-09-16 17:47:21 +02:00
|
|
|
c.SetNoDelay(true)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom implements the io.ReaderFrom ReadFrom method.
|
2011-12-03 03:17:34 +01: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
|
|
|
|
}
|
|
|
|
return genericReadFrom(c, r)
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// CloseRead shuts down the reading side of the TCP connection.
|
|
|
|
// Most callers should just use Close.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) CloseRead() error {
|
2011-10-27 01:57:58 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
return c.fd.CloseRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWrite shuts down the writing side of the TCP connection.
|
|
|
|
// Most callers should just use Close.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) CloseWrite() error {
|
2011-10-27 01:57:58 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
|
|
|
return c.fd.CloseWrite()
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// SetLinger sets the behavior of Close() on a connection
|
|
|
|
// which still has data waiting to be sent or to be acknowledged.
|
|
|
|
//
|
|
|
|
// If sec < 0 (the default), Close returns immediately and
|
|
|
|
// the operating system finishes sending the data in the background.
|
|
|
|
//
|
|
|
|
// If sec == 0, Close returns immediately and the operating system
|
|
|
|
// discards any unsent or unacknowledged data.
|
|
|
|
//
|
|
|
|
// If sec > 0, Close blocks for at most sec seconds waiting for
|
|
|
|
// data to be sent and acknowledged.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) SetLinger(sec int) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return setLinger(c.fd, sec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKeepAlive sets whether the operating system should send
|
|
|
|
// keepalive messages on the connection.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) SetKeepAlive(keepalive bool) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return setKeepAlive(c.fd, keepalive)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNoDelay controls whether the operating system should delay
|
|
|
|
// packet transmission in hopes of sending fewer packets
|
|
|
|
// (Nagle's algorithm). The default is true (no delay), meaning
|
|
|
|
// that data is sent as soon as possible after a Write.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *TCPConn) SetNoDelay(noDelay bool) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return setNoDelay(c.fd, noDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialTCP connects to the remote address raddr on the network net,
|
|
|
|
// which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used
|
|
|
|
// as the local address for the connection.
|
2012-02-09 09:19:58 +01:00
|
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
2012-11-21 08:03:38 +01:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if raddr == nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, &OpError{"dial", net, nil, errMissingAddress}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
return dialTCP(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
func dialTCP(net string, laddr, raddr *TCPAddr, deadline time.Time) (*TCPConn, error) {
|
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), deadline, syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
|
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
|
|
|
|
// at addr2, without either machine executing Listen. If laddr == nil,
|
|
|
|
// it means we want the kernel to pick an appropriate originating local
|
|
|
|
// address. Some Linux kernels cycle blindly through a fixed range of
|
|
|
|
// local ports, regardless of destination port. If a kernel happens to
|
|
|
|
// 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.
|
|
|
|
// It's hard to argue this is anything other than a kernel bug. If we
|
|
|
|
// see this happen, rather than expose the buggy effect to users, we
|
|
|
|
// close the fd and try again. If it happens twice more, we relent and
|
|
|
|
// use the result. See also:
|
|
|
|
// http://golang.org/issue/2690
|
|
|
|
// 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()
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
fd, err = internetSocket(net, laddr.toAddr(), raddr.toAddr(), deadline, syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
|
2012-03-02 17:38:43 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
if err != nil {
|
|
|
|
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 {
|
|
|
|
e, ok := err.(*OpError)
|
|
|
|
return ok && e.Err == syscall.EADDRNOTAVAIL
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// TCPListener is a TCP network listener.
|
|
|
|
// Clients should typically use variables of type Listener
|
|
|
|
// instead of assuming TCP.
|
|
|
|
type TCPListener struct {
|
|
|
|
fd *netFD
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptTCP accepts the next incoming call and returns the new connection
|
|
|
|
// and the remote address.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (l *TCPListener) AcceptTCP() (c *TCPConn, err error) {
|
2012-11-21 08:03:38 +01:00
|
|
|
if l == nil || l.fd == nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
fd, err := l.fd.accept(sockaddrToTCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the Accept method in the Listener interface;
|
|
|
|
// it waits for the next call and returns a generic Conn.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (l *TCPListener) Accept() (c Conn, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
c1, err := l.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops listening on the TCP address.
|
|
|
|
// Already Accepted connections are not closed.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (l *TCPListener) Close() error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if l == nil || l.fd == nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return l.fd.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address, a *TCPAddr.
|
|
|
|
func (l *TCPListener) Addr() Addr { return l.fd.laddr }
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
// SetDeadline sets the deadline associated with the listener.
|
|
|
|
// A zero time value disables the deadline.
|
|
|
|
func (l *TCPListener) SetDeadline(t time.Time) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if l == nil || l.fd == nil {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
return setDeadline(l.fd, t)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
|
|
|
// It is the caller's responsibility to close f when finished.
|
2012-05-04 17:01:11 +02:00
|
|
|
// Closing l does not affect f, and closing f does not affect l.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() }
|
2012-11-21 08:03:38 +01:00
|
|
|
|
|
|
|
// ListenTCP announces on the TCP address laddr and returns a TCP listener.
|
|
|
|
// Net must be "tcp", "tcp4", or "tcp6".
|
|
|
|
// If laddr has a port of 0, it means to listen on some available port.
|
|
|
|
// The caller can use l.Addr() to retrieve the chosen address.
|
|
|
|
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
|
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if laddr == nil {
|
|
|
|
laddr = &TCPAddr{}
|
|
|
|
}
|
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), nil, noDeadline, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = syscall.Listen(fd.sysfd, listenerBacklog)
|
|
|
|
if err != nil {
|
|
|
|
closesocket(fd.sysfd)
|
|
|
|
return nil, &OpError{"listen", net, laddr, err}
|
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
return &TCPListener{fd}, nil
|
2012-11-21 08:03:38 +01:00
|
|
|
}
|