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.
|
|
|
|
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 07:47:42 -07:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris 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
|
|
|
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:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2011-08-17 11:28:29 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return &TCPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
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) {
|
2013-08-02 22:32:22 -06:00
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port, a.Zone)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2013-03-31 01:47:26 -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 {
|
net: consolidate common socket functions
In resolving 3507, the fix had to be applied individually to
the four *Conn types, tcp, udp, rawip and unix, due to the
duplicate code in each Conn type.
This CL consolidates the common net.Conn methods that all four
*Conn types implement into a base conn type.
Pros:
* The fix for 3507 would have only needed to be applied to one
method. Further improvements, such as possibly removing the
c.fd != nil check in c.ok(), would benefit from this CL.
* Nearly 300 lines removed from the net package.
* The public interface and documentation are not changed.
* I think this is an excellent example of the power of embedding.
Cons:
* The net package is already distributed over many files, this
CL adds another place to look.
* The fix for 3507 was a total of 16 lines changed, this follow
up CL could be considered to be an overreaction as new Conn types
are unlikely to be added in the near future.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6098047
2012-04-27 06:17:08 -06:00
|
|
|
conn
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTCPConn(fd *netFD) *TCPConn {
|
net: consolidate common socket functions
In resolving 3507, the fix had to be applied individually to
the four *Conn types, tcp, udp, rawip and unix, due to the
duplicate code in each Conn type.
This CL consolidates the common net.Conn methods that all four
*Conn types implement into a base conn type.
Pros:
* The fix for 3507 would have only needed to be applied to one
method. Further improvements, such as possibly removing the
c.fd != nil check in c.ok(), would benefit from this CL.
* Nearly 300 lines removed from the net package.
* The public interface and documentation are not changed.
* I think this is an excellent example of the power of embedding.
Cons:
* The net package is already distributed over many files, this
CL adds another place to look.
* The fix for 3507 was a total of 16 lines changed, this follow
up CL could be considered to be an overreaction as new Conn types
are unlikely to be added in the near future.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6098047
2012-04-27 06:17:08 -06:00
|
|
|
c := &TCPConn{conn{fd}}
|
2011-08-17 11:28:29 -06:00
|
|
|
c.SetNoDelay(true)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-04-16 08:10:56 -06:00
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
return n, err
|
|
|
|
}
|
2015-04-16 08:10:56 -06:00
|
|
|
n, err := genericReadFrom(c, r)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return n, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
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() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-09-28 09:12:38 -06:00
|
|
|
}
|
2015-04-16 21:24:42 -06:00
|
|
|
err := c.fd.closeRead()
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "close", Net: c.fd.net, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return err
|
2011-09-28 09:12:38 -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-09-28 09:12:38 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-09-28 09:12:38 -06:00
|
|
|
}
|
2015-04-16 21:24:42 -06:00
|
|
|
err := c.fd.closeWrite()
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "close", Net: c.fd.net, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return err
|
2011-09-28 09:12:38 -06:00
|
|
|
}
|
|
|
|
|
2014-05-13 21:12:01 -06:00
|
|
|
// SetLinger sets the behavior of Close on a connection which still
|
2013-03-31 01:47:26 -06:00
|
|
|
// has data waiting to be sent or to be acknowledged.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
2014-05-13 21:12:01 -06:00
|
|
|
// If sec < 0 (the default), the operating system finishes sending the
|
|
|
|
// data in the background.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
2014-05-13 21:12:01 -06:00
|
|
|
// If sec == 0, the operating system discards any unsent or
|
|
|
|
// unacknowledged data.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
2014-05-13 21:12:01 -06:00
|
|
|
// If sec > 0, the data is sent in the background as with sec < 0. On
|
|
|
|
// some operating systems after sec seconds have elapsed any remaining
|
|
|
|
// unsent data may be discarded.
|
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() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
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() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return setKeepAlive(c.fd, keepalive)
|
|
|
|
}
|
|
|
|
|
2013-07-30 01:31:57 -06:00
|
|
|
// SetKeepAlivePeriod sets period between keep alives.
|
2013-07-15 16:40:55 -06:00
|
|
|
func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
|
|
|
|
if !c.ok() {
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
return setKeepAlivePeriod(c.fd, d)
|
|
|
|
}
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// SetNoDelay controls whether the operating system should delay
|
2013-03-31 01:47:26 -06:00
|
|
|
// 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() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return setNoDelay(c.fd, noDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialTCP connects to the remote address raddr on the network net,
|
2013-03-31 01:47:26 -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-01-31 08:36:45 -07:00
|
|
|
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
2012-11-12 20:56:28 -07:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: UnknownNetworkError(net)}
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
if raddr == nil {
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, &OpError{Op: "dial", Net: net, Addr: nil, Err: errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-12 20:56:28 -07:00
|
|
|
return dialTCP(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
2012-02-12 21:25:55 -07:00
|
|
|
|
2012-11-12 20:56:28 -07:00
|
|
|
func dialTCP(net string, laddr, raddr *TCPAddr, deadline time.Time) (*TCPConn, error) {
|
2014-08-04 15:10:46 -06:00
|
|
|
fd, err := internetSocket(net, laddr, raddr, deadline, syscall.SOCK_STREAM, 0, "dial")
|
2012-02-12 21:25:55 -07:00
|
|
|
|
|
|
|
// TCP has a rarely used mechanism called a 'simultaneous connection' in
|
|
|
|
// which Dial("tcp", addr1, addr2) run on the machine at addr1 can
|
|
|
|
// connect to a simultaneous Dial("tcp", addr2, addr1) run on the machine
|
|
|
|
// at addr2, without either machine executing Listen. If laddr == nil,
|
|
|
|
// it means we want the kernel to pick an appropriate originating local
|
|
|
|
// address. Some Linux kernels cycle blindly through a fixed range of
|
|
|
|
// local ports, regardless of destination port. If a kernel happens to
|
|
|
|
// pick local port 50001 as the source for a Dial("tcp", "", "localhost:50001"),
|
|
|
|
// then the Dial will succeed, having simultaneously connected to itself.
|
|
|
|
// This can only happen when we are letting the kernel pick a port (laddr == nil)
|
|
|
|
// and when there is no listener for the destination address.
|
|
|
|
// It's hard to argue this is anything other than a kernel bug. If we
|
|
|
|
// see this happen, rather than expose the buggy effect to users, we
|
|
|
|
// close the fd and try again. If it happens twice more, we relent and
|
|
|
|
// use the result. See also:
|
|
|
|
// http://golang.org/issue/2690
|
|
|
|
// http://stackoverflow.com/questions/4949858/
|
2012-08-06 14:32:00 -06:00
|
|
|
//
|
|
|
|
// The opposite can also happen: if we ask the kernel to pick an appropriate
|
|
|
|
// originating local address, sometimes it picks one that is already in use.
|
|
|
|
// So if the error is EADDRNOTAVAIL, we have to try again too, just for
|
|
|
|
// a different reason.
|
|
|
|
//
|
|
|
|
// The kernel socket code is no doubt enjoying watching us squirm.
|
|
|
|
for i := 0; i < 2 && (laddr == nil || laddr.Port == 0) && (selfConnect(fd, err) || spuriousENOTAVAIL(err)); i++ {
|
|
|
|
if err == nil {
|
|
|
|
fd.Close()
|
|
|
|
}
|
2014-08-04 15:10:46 -06:00
|
|
|
fd, err = internetSocket(net, laddr, raddr, deadline, syscall.SOCK_STREAM, 0, "dial")
|
2012-02-12 21:25:55 -07:00
|
|
|
}
|
|
|
|
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, &OpError{Op: "dial", Net: net, Addr: raddr, Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:32:00 -06:00
|
|
|
func selfConnect(fd *netFD, err error) bool {
|
|
|
|
// If the connect failed, we clearly didn't connect to ourselves.
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2012-02-20 22:48:05 -07:00
|
|
|
// The socket constructor can return an fd with raddr nil under certain
|
|
|
|
// unknown conditions. The errors in the calls there to Getpeername
|
|
|
|
// are discarded, but we can't catch the problem there because those
|
|
|
|
// calls are sometimes legally erroneous with a "socket not connected".
|
|
|
|
// Since this code (selfConnect) is already trying to work around
|
|
|
|
// a problem, we make sure if this happens we recognize trouble and
|
|
|
|
// ask the DialTCP routine to try again.
|
|
|
|
// TODO: try to understand what's really going on.
|
|
|
|
if fd.laddr == nil || fd.raddr == nil {
|
|
|
|
return true
|
|
|
|
}
|
2012-02-12 21:25:55 -07:00
|
|
|
l := fd.laddr.(*TCPAddr)
|
|
|
|
r := fd.raddr.(*TCPAddr)
|
|
|
|
return l.Port == r.Port && l.IP.Equal(r.IP)
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:32:00 -06:00
|
|
|
func spuriousENOTAVAIL(err error) bool {
|
|
|
|
e, ok := err.(*OpError)
|
|
|
|
return ok && e.Err == syscall.EADDRNOTAVAIL
|
|
|
|
}
|
|
|
|
|
2013-03-31 01:47:26 -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 {
|
|
|
|
fd *netFD
|
|
|
|
}
|
|
|
|
|
2013-03-31 01:47:26 -06:00
|
|
|
// AcceptTCP accepts the next incoming call and returns the new
|
2013-09-06 13:00:03 -06:00
|
|
|
// connection.
|
2013-03-31 01:47:26 -06:00
|
|
|
func (l *TCPListener) AcceptTCP() (*TCPConn, error) {
|
2012-11-18 12:53:58 -07:00
|
|
|
if l == nil || l.fd == nil {
|
2012-02-16 16:04:29 -07:00
|
|
|
return nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2014-08-04 15:10:46 -06:00
|
|
|
fd, err := l.fd.accept()
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2015-04-16 23:35:54 -06:00
|
|
|
return nil, &OpError{Op: "accept", Net: l.fd.net, Addr: l.fd.laddr, Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2013-03-31 01:47:26 -06:00
|
|
|
// 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) {
|
|
|
|
c, err := l.AcceptTCP()
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-31 01:47:26 -06:00
|
|
|
return c, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-04-16 21:24:42 -06:00
|
|
|
err := l.fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "close", Net: l.fd.net, Addr: l.fd.laddr, Err: err}
|
|
|
|
}
|
|
|
|
return err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the listener's network address, a *TCPAddr.
|
2015-02-03 10:59:40 -07:00
|
|
|
// The Addr returned is shared by all invocations of Addr, so
|
|
|
|
// do not modify it.
|
2011-08-17 11:28:29 -06:00
|
|
|
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 {
|
2012-02-16 16:04:29 -07:00
|
|
|
return syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-08-13 05:00:58 -06:00
|
|
|
return l.fd.setDeadline(t)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2013-03-31 01:47:26 -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.
|
2012-04-23 17:27:19 -06:00
|
|
|
// 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.
|
2011-11-01 20:05:34 -06:00
|
|
|
func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() }
|
2012-11-12 20:56:28 -07:00
|
|
|
|
2013-03-29 00:06:43 -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
|
|
|
|
// 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-12 20:56:28 -07:00
|
|
|
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
|
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: UnknownNetworkError(net)}
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|
|
|
|
if laddr == nil {
|
|
|
|
laddr = &TCPAddr{}
|
|
|
|
}
|
2014-08-04 15:10:46 -06:00
|
|
|
fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_STREAM, 0, "listen")
|
2012-11-12 20:56:28 -07:00
|
|
|
if err != nil {
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, &OpError{Op: "listen", Net: net, Addr: laddr, Err: err}
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|
2012-12-03 04:00:50 -07:00
|
|
|
return &TCPListener{fd}, nil
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|