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.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// TCPConn is an implementation of the Conn interface for TCP network
|
|
|
|
// connections.
|
2011-09-16 17:47:21 +02:00
|
|
|
type TCPConn struct {
|
2012-11-21 08:03:38 +01:00
|
|
|
conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTCPConn(fd *netFD) *TCPConn {
|
|
|
|
return &TCPConn{conn{fd}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom implements the io.ReaderFrom ReadFrom method.
|
|
|
|
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
|
2012-12-13 00:13:29 +01:00
|
|
|
return genericReadFrom(c, r)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2011-12-02 20:34:41 +01: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-12-02 20:34:41 +01:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
return c.fd.CloseRead()
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-12-02 20:34:41 +01:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
return c.fd.CloseWrite()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (c *TCPConn) SetLinger(sec int) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKeepAlive sets whether the operating system should send
|
|
|
|
// keepalive messages on the connection.
|
|
|
|
func (c *TCPConn) SetKeepAlive(keepalive bool) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// SetKeepAlivePeriod sets period between keep alives.
|
|
|
|
func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// 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.
|
|
|
|
func (c *TCPConn) SetNoDelay(noDelay bool) error {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EPLAN9
|
2011-12-02 20:34:41 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// DialTCP connects to the remote address raddr on the network net,
|
2012-10-23 06:31:11 +02:00
|
|
|
// which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is
|
|
|
|
// used as the local address for the connection.
|
2012-12-13 00:13:29 +01:00
|
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
2012-11-21 08:03:38 +01:00
|
|
|
return dialTCP(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialTCP(net string, laddr, raddr *TCPAddr, deadline time.Time) (*TCPConn, error) {
|
|
|
|
if !deadline.IsZero() {
|
|
|
|
panic("net.dialTCP: deadline not implemented on Plan 9")
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
2013-07-16 08:54:42 +02:00
|
|
|
return nil, &OpError{"dial", net, raddr, 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
|
|
|
fd, err := dialPlan9(net, laddr, raddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2012-11-21 08:03:38 +01:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
return newTCPConn(fd), nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// TCPListener is a TCP network listener. Clients should typically
|
|
|
|
// use variables of type Listener instead of assuming TCP.
|
2011-09-16 17:47:21 +02:00
|
|
|
type TCPListener struct {
|
2012-11-21 08:03:38 +01:00
|
|
|
fd *netFD
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// AcceptTCP accepts the next incoming call and returns the new
|
2013-11-06 20:49:01 +01:00
|
|
|
// connection.
|
2012-11-21 08:03:38 +01:00
|
|
|
func (l *TCPListener) AcceptTCP() (*TCPConn, error) {
|
|
|
|
if l == nil || l.fd == nil || l.fd.ctl == nil {
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
fd, err := l.fd.acceptPlan9()
|
|
|
|
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.
|
|
|
|
func (l *TCPListener) Accept() (Conn, error) {
|
|
|
|
if l == nil || l.fd == nil || l.fd.ctl == nil {
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
c, err := l.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops listening on the TCP address.
|
|
|
|
// Already Accepted connections are not closed.
|
2012-10-23 06:31:11 +02:00
|
|
|
func (l *TCPListener) Close() error {
|
2012-11-21 08:03:38 +01:00
|
|
|
if l == nil || l.fd == nil || l.fd.ctl == nil {
|
2012-10-23 06:31:11 +02:00
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
if _, err := l.fd.ctl.WriteString("hangup"); err != nil {
|
|
|
|
l.fd.ctl.Close()
|
2013-07-16 08:54:42 +02:00
|
|
|
return &OpError{"close", l.fd.ctl.Name(), l.fd.laddr, err}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
return l.fd.ctl.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address, a *TCPAddr.
|
|
|
|
func (l *TCPListener) Addr() Addr { return l.fd.laddr }
|
|
|
|
|
|
|
|
// SetDeadline sets the deadline associated with the listener.
|
|
|
|
// A zero time value disables the deadline.
|
|
|
|
func (l *TCPListener) SetDeadline(t time.Time) error {
|
|
|
|
if l == nil || l.fd == nil || l.fd.ctl == nil {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return l.fd.setDeadline(t)
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 08:03:38 +01: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.
|
|
|
|
// Closing l does not affect f, and closing f does not affect l.
|
2013-07-16 08:54:42 +02:00
|
|
|
//
|
|
|
|
// The returned os.File's file descriptor is different from the
|
|
|
|
// connection's. Attempting to change properties of the original
|
|
|
|
// using this duplicate may or may not have the desired effect.
|
|
|
|
func (l *TCPListener) File() (f *os.File, err error) { return l.dup() }
|
2012-11-21 08:03:38 +01:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// ListenTCP announces on the TCP address laddr and returns a TCP
|
|
|
|
// listener. Net must be "tcp", "tcp4", or "tcp6". If laddr has a
|
2013-07-16 08:54:42 +02:00
|
|
|
// port of 0, ListenTCP will choose an available port. The caller can
|
|
|
|
// use the Addr method of TCPListener to retrieve the chosen address.
|
2012-11-21 08:03:38 +01:00
|
|
|
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
2013-07-16 08:54:42 +02:00
|
|
|
return nil, &OpError{"listen", net, laddr, UnknownNetworkError(net)}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
if laddr == nil {
|
2012-11-21 08:03:38 +01:00
|
|
|
laddr = &TCPAddr{}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
fd, err := listenPlan9(net, laddr)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2012-11-21 08:03:38 +01:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
return &TCPListener{fd}, nil
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|