2011-08-17 11:28:29 -06: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-08 09:35:16 -07:00
|
|
|
import (
|
2012-11-13 00:18:37 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
2012-11-08 09:35:16 -07:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2012-09-26 01:11:49 -06:00
|
|
|
// TCPConn is an implementation of the Conn interface for TCP network
|
|
|
|
// connections.
|
2011-08-17 11:28:29 -06:00
|
|
|
type TCPConn struct {
|
2012-11-13 00:18:37 -07: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-11-30 12:41:50 -07:00
|
|
|
return genericReadFrom(c, r)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2011-10-31 09:47:44 -06:00
|
|
|
// CloseRead shuts down the reading side of the TCP connection.
|
|
|
|
// Most callers should just use Close.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) CloseRead() error {
|
2011-10-31 09:47:44 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-10-31 09:47:44 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
return c.fd.CloseRead()
|
2011-10-31 09:47:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWrite shuts down the writing side of the TCP connection.
|
|
|
|
// Most callers should just use Close.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) CloseWrite() error {
|
2011-10-31 09:47:44 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-10-31 09:47:44 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07: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-07-30 01:29:51 -06:00
|
|
|
// SetKeepAlivePeriod sets period between keep alives.
|
|
|
|
func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
|
|
|
|
return syscall.EPLAN9
|
|
|
|
}
|
|
|
|
|
2012-11-13 00:18:37 -07: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-02-16 18:59:30 -07:00
|
|
|
return syscall.EPLAN9
|
2011-10-31 09:47:44 -06:00
|
|
|
}
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// DialTCP connects to the remote address raddr on the network net,
|
2012-09-26 01:11:49 -06:00
|
|
|
// which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is
|
|
|
|
// used as the local address for the connection.
|
2012-11-30 12:41:50 -07:00
|
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
return dialTCP(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
2012-11-13 00:18:37 -07:00
|
|
|
func dialTCP(net string, laddr, raddr *TCPAddr, deadline time.Time) (*TCPConn, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
if !deadline.IsZero() {
|
|
|
|
panic("net.dialTCP: deadline not implemented on Plan 9")
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
net, os, syscall: Plan 9: adjust error handling
syscall: Use NewError for all system errors and introduce
some new errors for compatibility with other packages
and proper error handling in net. Also introduce
Temporary and Timeout methods on ErrorString.
net: Make errors from dial, accept, listen functions follow the
OpError standard and discern whether the underlying
error came from syscall. Since Plan 9 uses a correspondence
between file and network operations, all system error
reporting happens through the underlying file operation.
In Go code, we go through package os for file operations,
so there is another level of indirection in error types.
This change allows us to compare the errors with those in
package syscall, when appropriate.
os: Just use the error string already present in package os,
instead of calling out to package syscall.
R=rsc, ality, rminnich, bradfitz
CC=golang-dev
https://golang.org/cl/7398054
2013-02-27 22:43:21 -07:00
|
|
|
return nil, &OpError{"dial", net, raddr, UnknownNetworkError(net)}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if raddr == nil {
|
2012-01-23 10:59:43 -07:00
|
|
|
return nil, &OpError{"dial", net, nil, errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
fd, err := dialPlan9(net, laddr, raddr)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-02-19 18:11:17 -07:00
|
|
|
return newTCPConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-09-26 01:11:49 -06:00
|
|
|
// TCPListener is a TCP network listener. Clients should typically
|
|
|
|
// use variables of type Listener instead of assuming TCP.
|
2011-08-17 11:28:29 -06:00
|
|
|
type TCPListener struct {
|
2012-11-13 00:18:37 -07:00
|
|
|
fd *netFD
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-11-13 00:18:37 -07:00
|
|
|
// AcceptTCP accepts the next incoming call and returns the new
|
2013-09-06 13:00:03 -06:00
|
|
|
// connection.
|
2012-11-13 00:18:37 -07: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-06-06 16:38:56 -06:00
|
|
|
func (l *TCPListener) Close() error {
|
2012-11-13 00:18:37 -07:00
|
|
|
if l == nil || l.fd == nil || l.fd.ctl == nil {
|
2012-06-06 16:38:56 -06:00
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
if _, err := l.fd.ctl.WriteString("hangup"); err != nil {
|
|
|
|
l.fd.ctl.Close()
|
net, os, syscall: Plan 9: adjust error handling
syscall: Use NewError for all system errors and introduce
some new errors for compatibility with other packages
and proper error handling in net. Also introduce
Temporary and Timeout methods on ErrorString.
net: Make errors from dial, accept, listen functions follow the
OpError standard and discern whether the underlying
error came from syscall. Since Plan 9 uses a correspondence
between file and network operations, all system error
reporting happens through the underlying file operation.
In Go code, we go through package os for file operations,
so there is another level of indirection in error types.
This change allows us to compare the errors with those in
package syscall, when appropriate.
os: Just use the error string already present in package os,
instead of calling out to package syscall.
R=rsc, ality, rminnich, bradfitz
CC=golang-dev
https://golang.org/cl/7398054
2013-02-27 22:43:21 -07:00
|
|
|
return &OpError{"close", l.fd.ctl.Name(), l.fd.laddr, err}
|
2012-06-06 16:38:56 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
return l.fd.ctl.Close()
|
2012-06-06 16:38:56 -06:00
|
|
|
}
|
|
|
|
|
2012-11-13 00:18:37 -07:00
|
|
|
// 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-08-13 05:00:58 -06:00
|
|
|
return l.fd.setDeadline(t)
|
2012-11-13 00:18:37 -07: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-04-23 17:32:11 -06: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.
|
2013-02-25 17:26:40 -07:00
|
|
|
func (l *TCPListener) File() (f *os.File, err error) { return l.dup() }
|
2012-11-13 00:18:37 -07:00
|
|
|
|
2012-09-26 01:11:49 -06: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-03-29 00:06:43 -06: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-13 00:18:37 -07:00
|
|
|
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
net, os, syscall: Plan 9: adjust error handling
syscall: Use NewError for all system errors and introduce
some new errors for compatibility with other packages
and proper error handling in net. Also introduce
Temporary and Timeout methods on ErrorString.
net: Make errors from dial, accept, listen functions follow the
OpError standard and discern whether the underlying
error came from syscall. Since Plan 9 uses a correspondence
between file and network operations, all system error
reporting happens through the underlying file operation.
In Go code, we go through package os for file operations,
so there is another level of indirection in error types.
This change allows us to compare the errors with those in
package syscall, when appropriate.
os: Just use the error string already present in package os,
instead of calling out to package syscall.
R=rsc, ality, rminnich, bradfitz
CC=golang-dev
https://golang.org/cl/7398054
2013-02-27 22:43:21 -07:00
|
|
|
return nil, &OpError{"listen", net, laddr, UnknownNetworkError(net)}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if laddr == nil {
|
2012-11-12 20:56:28 -07:00
|
|
|
laddr = &TCPAddr{}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
fd, err := listenPlan9(net, laddr)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
return &TCPListener{fd}, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|