github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/net/tcpsock.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  package net
     6  
     7  import (
     8  	"context"
     9  	"io"
    10  	"os"
    11  	"syscall"
    12  	"time"
    13  )
    14  
    15  // BUG(mikio): On Windows, the File method of TCPListener is not
    16  // implemented.
    17  
    18  // TCPAddr represents the address of a TCP end point.
    19  type TCPAddr struct {
    20  	IP   IP
    21  	Port int
    22  	Zone string // IPv6 scoped addressing zone
    23  }
    24  
    25  // Network returns the address's network name, "tcp".
    26  func (a *TCPAddr) Network() string { return "tcp" }
    27  
    28  func (a *TCPAddr) String() string {
    29  	if a == nil {
    30  		return "<nil>"
    31  	}
    32  	ip := ipEmptyString(a.IP)
    33  	if a.Zone != "" {
    34  		return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
    35  	}
    36  	return JoinHostPort(ip, itoa(a.Port))
    37  }
    38  
    39  func (a *TCPAddr) isWildcard() bool {
    40  	if a == nil || a.IP == nil {
    41  		return true
    42  	}
    43  	return a.IP.IsUnspecified()
    44  }
    45  
    46  func (a *TCPAddr) opAddr() Addr {
    47  	if a == nil {
    48  		return nil
    49  	}
    50  	return a
    51  }
    52  
    53  // ResolveTCPAddr returns an address of TCP end point.
    54  //
    55  // The network must be a TCP network name.
    56  //
    57  // If the host in the address parameter is not a literal IP address or
    58  // the port is not a literal port number, ResolveTCPAddr resolves the
    59  // address to an address of TCP end point.
    60  // Otherwise, it parses the address as a pair of literal IP address
    61  // and port number.
    62  // The address parameter can use a host name, but this is not
    63  // recommended, because it will return at most one of the host name's
    64  // IP addresses.
    65  //
    66  // See func Dial for a description of the network and address
    67  // parameters.
    68  func ResolveTCPAddr(network, address string) (*TCPAddr, error) {
    69  	switch network {
    70  	case "tcp", "tcp4", "tcp6":
    71  	case "": // a hint wildcard for Go 1.0 undocumented behavior
    72  		network = "tcp"
    73  	default:
    74  		return nil, UnknownNetworkError(network)
    75  	}
    76  	addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	return addrs.forResolve(network, address).(*TCPAddr), nil
    81  }
    82  
    83  // TCPConn is an implementation of the Conn interface for TCP network
    84  // connections.
    85  type TCPConn struct {
    86  	conn
    87  }
    88  
    89  // SyscallConn returns a raw network connection.
    90  // This implements the syscall.Conn interface.
    91  func (c *TCPConn) SyscallConn() (syscall.RawConn, error) {
    92  	if !c.ok() {
    93  		return nil, syscall.EINVAL
    94  	}
    95  	return newRawConn(c.fd)
    96  }
    97  
    98  // ReadFrom implements the io.ReaderFrom ReadFrom method.
    99  func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
   100  	if !c.ok() {
   101  		return 0, syscall.EINVAL
   102  	}
   103  	n, err := c.readFrom(r)
   104  	if err != nil && err != io.EOF {
   105  		err = &OpError{Op: "readfrom", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   106  	}
   107  	return n, err
   108  }
   109  
   110  // CloseRead shuts down the reading side of the TCP connection.
   111  // Most callers should just use Close.
   112  func (c *TCPConn) CloseRead() error {
   113  	if !c.ok() {
   114  		return syscall.EINVAL
   115  	}
   116  	if err := c.fd.closeRead(); err != nil {
   117  		return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   118  	}
   119  	return nil
   120  }
   121  
   122  // CloseWrite shuts down the writing side of the TCP connection.
   123  // Most callers should just use Close.
   124  func (c *TCPConn) CloseWrite() error {
   125  	if !c.ok() {
   126  		return syscall.EINVAL
   127  	}
   128  	if err := c.fd.closeWrite(); err != nil {
   129  		return &OpError{Op: "close", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   130  	}
   131  	return nil
   132  }
   133  
   134  // SetLinger sets the behavior of Close on a connection which still
   135  // has data waiting to be sent or to be acknowledged.
   136  //
   137  // If sec < 0 (the default), the operating system finishes sending the
   138  // data in the background.
   139  //
   140  // If sec == 0, the operating system discards any unsent or
   141  // unacknowledged data.
   142  //
   143  // If sec > 0, the data is sent in the background as with sec < 0. On
   144  // some operating systems after sec seconds have elapsed any remaining
   145  // unsent data may be discarded.
   146  func (c *TCPConn) SetLinger(sec int) error {
   147  	if !c.ok() {
   148  		return syscall.EINVAL
   149  	}
   150  	if err := setLinger(c.fd, sec); err != nil {
   151  		return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   152  	}
   153  	return nil
   154  }
   155  
   156  // SetKeepAlive sets whether the operating system should send
   157  // keepalive messages on the connection.
   158  func (c *TCPConn) SetKeepAlive(keepalive bool) error {
   159  	if !c.ok() {
   160  		return syscall.EINVAL
   161  	}
   162  	if err := setKeepAlive(c.fd, keepalive); err != nil {
   163  		return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   164  	}
   165  	return nil
   166  }
   167  
   168  // SetKeepAlivePeriod sets period between keep alives.
   169  func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
   170  	if !c.ok() {
   171  		return syscall.EINVAL
   172  	}
   173  	if err := setKeepAlivePeriod(c.fd, d); err != nil {
   174  		return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   175  	}
   176  	return nil
   177  }
   178  
   179  // SetNoDelay controls whether the operating system should delay
   180  // packet transmission in hopes of sending fewer packets (Nagle's
   181  // algorithm).  The default is true (no delay), meaning that data is
   182  // sent as soon as possible after a Write.
   183  func (c *TCPConn) SetNoDelay(noDelay bool) error {
   184  	if !c.ok() {
   185  		return syscall.EINVAL
   186  	}
   187  	if err := setNoDelay(c.fd, noDelay); err != nil {
   188  		return &OpError{Op: "set", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   189  	}
   190  	return nil
   191  }
   192  
   193  func newTCPConn(fd *netFD) *TCPConn {
   194  	c := &TCPConn{conn{fd}}
   195  	setNoDelay(c.fd, true)
   196  	return c
   197  }
   198  
   199  // DialTCP acts like Dial for TCP networks.
   200  //
   201  // The network must be a TCP network name; see func Dial for details.
   202  //
   203  // If laddr is nil, a local address is automatically chosen.
   204  // If the IP field of raddr is nil or an unspecified IP address, the
   205  // local system is assumed.
   206  func DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
   207  	switch network {
   208  	case "tcp", "tcp4", "tcp6":
   209  	default:
   210  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
   211  	}
   212  	if raddr == nil {
   213  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
   214  	}
   215  	c, err := dialTCP(context.Background(), network, laddr, raddr)
   216  	if err != nil {
   217  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
   218  	}
   219  	return c, nil
   220  }
   221  
   222  func DialTCPContext(ctx context.Context, network string, laddr, raddr *TCPAddr) (*TCPConn, error) {
   223  	switch network {
   224  	case "tcp", "tcp4", "tcp6":
   225  	default:
   226  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
   227  	}
   228  	if raddr == nil {
   229  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
   230  	}
   231  	c, err := dialTCP(ctx, network, laddr, raddr)
   232  	if err != nil {
   233  		return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
   234  	}
   235  	return c, nil
   236  }
   237  
   238  // TCPListener is a TCP network listener. Clients should typically
   239  // use variables of type Listener instead of assuming TCP.
   240  type TCPListener struct {
   241  	fd *netFD
   242  }
   243  
   244  // SyscallConn returns a raw network connection.
   245  // This implements the syscall.Conn interface.
   246  //
   247  // The returned RawConn only supports calling Control. Read and
   248  // Write return an error.
   249  func (l *TCPListener) SyscallConn() (syscall.RawConn, error) {
   250  	if !l.ok() {
   251  		return nil, syscall.EINVAL
   252  	}
   253  	return newRawListener(l.fd)
   254  }
   255  
   256  // AcceptTCP accepts the next incoming call and returns the new
   257  // connection.
   258  func (l *TCPListener) AcceptTCP() (*TCPConn, error) {
   259  	if !l.ok() {
   260  		return nil, syscall.EINVAL
   261  	}
   262  	c, err := l.accept()
   263  	if err != nil {
   264  		return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   265  	}
   266  	return c, nil
   267  }
   268  
   269  // Accept implements the Accept method in the Listener interface; it
   270  // waits for the next call and returns a generic Conn.
   271  func (l *TCPListener) Accept() (Conn, error) {
   272  	if !l.ok() {
   273  		return nil, syscall.EINVAL
   274  	}
   275  	c, err := l.accept()
   276  	if err != nil {
   277  		return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   278  	}
   279  	return c, nil
   280  }
   281  
   282  // Close stops listening on the TCP address.
   283  // Already Accepted connections are not closed.
   284  func (l *TCPListener) Close() error {
   285  	if !l.ok() {
   286  		return syscall.EINVAL
   287  	}
   288  	if err := l.close(); err != nil {
   289  		return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   290  	}
   291  	return nil
   292  }
   293  
   294  // Addr returns the listener's network address, a *TCPAddr.
   295  // The Addr returned is shared by all invocations of Addr, so
   296  // do not modify it.
   297  func (l *TCPListener) Addr() Addr { return l.fd.laddr }
   298  
   299  // SetDeadline sets the deadline associated with the listener.
   300  // A zero time value disables the deadline.
   301  func (l *TCPListener) SetDeadline(t time.Time) error {
   302  	if !l.ok() {
   303  		return syscall.EINVAL
   304  	}
   305  	if err := l.fd.pfd.SetDeadline(t); err != nil {
   306  		return &OpError{Op: "set", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   307  	}
   308  	return nil
   309  }
   310  
   311  // File returns a copy of the underlying os.File, set to blocking
   312  // mode. It is the caller's responsibility to close f when finished.
   313  // Closing l does not affect f, and closing f does not affect l.
   314  //
   315  // The returned os.File's file descriptor is different from the
   316  // connection's. Attempting to change properties of the original
   317  // using this duplicate may or may not have the desired effect.
   318  func (l *TCPListener) File() (f *os.File, err error) {
   319  	if !l.ok() {
   320  		return nil, syscall.EINVAL
   321  	}
   322  	f, err = l.file()
   323  	if err != nil {
   324  		return nil, &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   325  	}
   326  	return
   327  }
   328  
   329  // ListenTCP acts like Listen for TCP networks.
   330  //
   331  // The network must be a TCP network name; see func Dial for details.
   332  //
   333  // If the IP field of laddr is nil or an unspecified IP address,
   334  // ListenTCP listens on all available unicast and anycast IP addresses
   335  // of the local system.
   336  // If the Port field of laddr is 0, a port number is automatically
   337  // chosen.
   338  func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
   339  	switch network {
   340  	case "tcp", "tcp4", "tcp6":
   341  	default:
   342  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
   343  	}
   344  	if laddr == nil {
   345  		laddr = &TCPAddr{}
   346  	}
   347  	ln, err := listenTCP(context.Background(), network, laddr)
   348  	if err != nil {
   349  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
   350  	}
   351  	return ln, nil
   352  }