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.
|
|
|
|
|
2011-12-15 10:19:19 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// TCP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2012-01-18 17:24:06 -07:00
|
|
|
"time"
|
2011-08-17 11:28:29 -06:00
|
|
|
)
|
|
|
|
|
2011-08-26 13:38:02 -06: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-08-17 11:28:29 -06:00
|
|
|
func sockaddrToTCP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
return &TCPAddr{sa.Addr[0:], sa.Port}
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
return &TCPAddr{sa.Addr[0:], sa.Port}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TCPAddr) family() int {
|
2011-08-24 11:59:33 -06:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-08-17 11:28:29 -06:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
fd *netFD
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTCPConn(fd *netFD) *TCPConn {
|
|
|
|
c := &TCPConn{fd}
|
|
|
|
c.SetNoDelay(true)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TCPConn) ok() bool { return c != nil && c.fd != nil }
|
|
|
|
|
|
|
|
// Implementation of the Conn interface - see Conn for documentation.
|
|
|
|
|
|
|
|
// Read implements the net.Conn Read method.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) Read(b []byte) (n int, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return 0, os.EINVAL
|
|
|
|
}
|
|
|
|
return c.fd.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom implements the io.ReaderFrom ReadFrom method.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if n, err, handled := sendFile(c.fd, r); handled {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
return genericReadFrom(c, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements the net.Conn Write method.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) Write(b []byte) (n int, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return 0, os.EINVAL
|
|
|
|
}
|
|
|
|
return c.fd.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the TCP connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) Close() error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
err := c.fd.Close()
|
|
|
|
c.fd = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2011-09-28 09:12:38 -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-09-28 09:12:38 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return c.fd.CloseRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-09-28 09:12:38 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return c.fd.CloseWrite()
|
|
|
|
}
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// LocalAddr returns the local network address, a *TCPAddr.
|
|
|
|
func (c *TCPConn) LocalAddr() Addr {
|
|
|
|
if !c.ok() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.fd.laddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address, a *TCPAddr.
|
|
|
|
func (c *TCPConn) RemoteAddr() Addr {
|
|
|
|
if !c.ok() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.fd.raddr
|
|
|
|
}
|
|
|
|
|
2012-01-18 17:24:06 -07:00
|
|
|
// SetDeadline implements the net.Conn SetDeadline method.
|
|
|
|
func (c *TCPConn) SetDeadline(t time.Time) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
2012-01-18 17:24:06 -07:00
|
|
|
return setDeadline(c.fd, t)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-01-18 17:24:06 -07:00
|
|
|
// SetReadDeadline implements the net.Conn SetReadDeadline method.
|
|
|
|
func (c *TCPConn) SetReadDeadline(t time.Time) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
2012-01-18 17:24:06 -07:00
|
|
|
return setReadDeadline(c.fd, t)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-01-18 17:24:06 -07:00
|
|
|
// SetWriteDeadline implements the net.Conn SetWriteDeadline method.
|
|
|
|
func (c *TCPConn) SetWriteDeadline(t time.Time) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
2012-01-18 17:24:06 -07:00
|
|
|
return setWriteDeadline(c.fd, t)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadBuffer sets the size of the operating system's
|
|
|
|
// receive buffer associated with the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) SetReadBuffer(bytes int) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return setReadBuffer(c.fd, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteBuffer sets the size of the operating system's
|
|
|
|
// transmit buffer associated with the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) SetWriteBuffer(bytes int) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return setWriteBuffer(c.fd, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) SetLinger(sec int) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return setLinger(c.fd, sec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKeepAlive sets whether the operating system should send
|
|
|
|
// keepalive messages on the connection.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) SetKeepAlive(keepalive bool) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
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-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) SetNoDelay(noDelay bool) error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return setNoDelay(c.fd, noDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 c does not affect f, and closing f does not affect c.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (c *TCPConn) File() (f *os.File, err error) { return c.fd.dup() }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
|
|
|
// 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.
|
2011-11-01 20:05:34 -06:00
|
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err error) {
|
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
|
|
|
}
|
|
|
|
fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPListener is a TCP network listener.
|
|
|
|
// Clients should typically use variables of type Listener
|
|
|
|
// instead of assuming TCP.
|
|
|
|
type TCPListener struct {
|
|
|
|
fd *netFD
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-11-01 20:05:34 -06:00
|
|
|
func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-01-13 21:42:18 -07:00
|
|
|
err = syscall.Listen(fd.sysfd, listenerBacklog)
|
|
|
|
if err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
closesocket(fd.sysfd)
|
2012-01-23 10:59:43 -07:00
|
|
|
return nil, &OpError{"listen", net, laddr, err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
l = new(TCPListener)
|
|
|
|
l.fd = fd
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AcceptTCP accepts the next incoming call and returns the new connection
|
|
|
|
// and the remote address.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (l *TCPListener) AcceptTCP() (c *TCPConn, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if l == nil || l.fd == nil || l.fd.sysfd < 0 {
|
|
|
|
return nil, os.EINVAL
|
|
|
|
}
|
|
|
|
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-11-01 20:05:34 -06:00
|
|
|
func (l *TCPListener) Accept() (c Conn, err error) {
|
2011-08-17 11:28:29 -06: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-11-01 20:05:34 -06:00
|
|
|
func (l *TCPListener) Close() error {
|
2011-08-17 11:28:29 -06:00
|
|
|
if l == nil || l.fd == nil {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
return l.fd.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address, a *TCPAddr.
|
|
|
|
func (l *TCPListener) Addr() Addr { return l.fd.laddr }
|
|
|
|
|
2012-01-18 17:24:06 -07: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-08-17 11:28:29 -06:00
|
|
|
if l == nil || l.fd == nil {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
2012-01-18 17:24:06 -07:00
|
|
|
return setDeadline(l.fd, t)
|
2011-08-17 11:28:29 -06: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 c does not affect f, and closing f does not affect c.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() }
|