github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/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  // TCPListener is a TCP network listener. Clients should typically
   223  // use variables of type Listener instead of assuming TCP.
   224  type TCPListener struct {
   225  	fd *netFD
   226  }
   227  
   228  // AcceptTCP accepts the next incoming call and returns the new
   229  // connection.
   230  func (l *TCPListener) AcceptTCP() (*TCPConn, error) {
   231  	if !l.ok() {
   232  		return nil, syscall.EINVAL
   233  	}
   234  	c, err := l.accept()
   235  	if err != nil {
   236  		return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   237  	}
   238  	return c, nil
   239  }
   240  
   241  // Accept implements the Accept method in the Listener interface; it
   242  // waits for the next call and returns a generic Conn.
   243  func (l *TCPListener) Accept() (Conn, error) {
   244  	if !l.ok() {
   245  		return nil, syscall.EINVAL
   246  	}
   247  	c, err := l.accept()
   248  	if err != nil {
   249  		return nil, &OpError{Op: "accept", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   250  	}
   251  	return c, nil
   252  }
   253  
   254  // Close stops listening on the TCP address.
   255  // Already Accepted connections are not closed.
   256  func (l *TCPListener) Close() error {
   257  	if !l.ok() {
   258  		return syscall.EINVAL
   259  	}
   260  	if err := l.close(); err != nil {
   261  		return &OpError{Op: "close", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   262  	}
   263  	return nil
   264  }
   265  
   266  // Addr returns the listener's network address, a *TCPAddr.
   267  // The Addr returned is shared by all invocations of Addr, so
   268  // do not modify it.
   269  func (l *TCPListener) Addr() Addr { return l.fd.laddr }
   270  
   271  // SetDeadline sets the deadline associated with the listener.
   272  // A zero time value disables the deadline.
   273  func (l *TCPListener) SetDeadline(t time.Time) error {
   274  	if !l.ok() {
   275  		return syscall.EINVAL
   276  	}
   277  	if err := l.fd.pfd.SetDeadline(t); err != nil {
   278  		return &OpError{Op: "set", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   279  	}
   280  	return nil
   281  }
   282  
   283  // File returns a copy of the underlying os.File, set to blocking
   284  // mode. It is the caller's responsibility to close f when finished.
   285  // Closing l does not affect f, and closing f does not affect l.
   286  //
   287  // The returned os.File's file descriptor is different from the
   288  // connection's. Attempting to change properties of the original
   289  // using this duplicate may or may not have the desired effect.
   290  func (l *TCPListener) File() (f *os.File, err error) {
   291  	if !l.ok() {
   292  		return nil, syscall.EINVAL
   293  	}
   294  	f, err = l.file()
   295  	if err != nil {
   296  		return nil, &OpError{Op: "file", Net: l.fd.net, Source: nil, Addr: l.fd.laddr, Err: err}
   297  	}
   298  	return
   299  }
   300  
   301  // ListenTCP acts like Listen for TCP networks.
   302  //
   303  // The network must be a TCP network name; see func Dial for details.
   304  //
   305  // If the IP field of laddr is nil or an unspecified IP address,
   306  // ListenTCP listens on all available unicast and anycast IP addresses
   307  // of the local system.
   308  // If the Port field of laddr is 0, a port number is automatically
   309  // chosen.
   310  func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
   311  	switch network {
   312  	case "tcp", "tcp4", "tcp6":
   313  	default:
   314  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
   315  	}
   316  	if laddr == nil {
   317  		laddr = &TCPAddr{}
   318  	}
   319  	ln, err := listenTCP(context.Background(), network, laddr)
   320  	if err != nil {
   321  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
   322  	}
   323  	return ln, nil
   324  }