2011-03-25 14:42:25 -04:00
|
|
|
// Copyright 2011 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.
|
|
|
|
|
|
2014-09-18 19:17:55 +09:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
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 16:48:57 -04:00
|
|
|
|
2011-03-25 14:42:25 -04:00
|
|
|
package net
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"syscall"
|
|
|
|
|
)
|
|
|
|
|
|
2012-02-01 00:36:45 +09:00
|
|
|
func newFileFD(f *os.File) (*netFD, error) {
|
2013-08-05 15:43:45 -07:00
|
|
|
fd, err := dupCloseOnExec(int(f.Fd()))
|
2012-02-01 00:36:45 +09:00
|
|
|
if err != nil {
|
2015-04-18 16:53:55 +09:00
|
|
|
return nil, err
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
2013-08-05 15:43:45 -07:00
|
|
|
|
2013-01-28 08:54:15 -08:00
|
|
|
if err = syscall.SetNonblock(fd, true); err != nil {
|
net: add socket system call hooks for testing
This change adds socket system call hooks to existing test cases for
simulating a bit complicated network conditions to help making timeout
and dual IP stack test cases work more properly in followup changes.
Also test cases print debugging information in non-short mode like the
following:
Leaked goroutines:
net.TestWriteTimeout.func2(0xc20802a5a0, 0xc20801d000, 0x1000, 0x1000, 0xc2081d2ae0)
/go/src/net/timeout_test.go:170 +0x98
created by net.TestWriteTimeout
/go/src/net/timeout_test.go:173 +0x745
net.runDatagramPacketConnServer(0xc2080730e0, 0x2bd270, 0x3, 0x2c1770, 0xb, 0xc2081d2ba0, 0xc2081d2c00)
/go/src/net/server_test.go:398 +0x667
created by net.TestTimeoutUDP
/go/src/net/timeout_test.go:247 +0xc9
(snip)
Leaked sockets:
3: {Cookie:615726511685632 Err:<nil> SocketErr:0}
5: {Cookie:7934075906097152 Err:<nil> SocketErr:0}
Socket statistical information:
{Family:1 Type:805306370 Protocol:0 Opened:17 Accepted:0 Connected:5 Closed:17}
{Family:2 Type:805306369 Protocol:0 Opened:450 Accepted:234 Connected:279 Closed:636}
{Family:1 Type:805306369 Protocol:0 Opened:11 Accepted:5 Connected:5 Closed:16}
{Family:28 Type:805306369 Protocol:0 Opened:95 Accepted:22 Connected:16 Closed:116}
{Family:2 Type:805306370 Protocol:0 Opened:84 Accepted:0 Connected:34 Closed:83}
{Family:28 Type:805306370 Protocol:0 Opened:52 Accepted:0 Connected:4 Closed:52}
Change-Id: I0e84be59a0699bc31245c78e2249423459b8cdda
Reviewed-on: https://go-review.googlesource.com/6390
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-03-01 12:27:01 +09:00
|
|
|
closeFunc(fd)
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 21:20:15 +09:00
|
|
|
return nil, os.NewSyscallError("setnonblock", err)
|
2013-01-28 08:54:15 -08:00
|
|
|
}
|
2011-03-25 14:42:25 -04:00
|
|
|
|
2012-07-22 01:48:15 +09:00
|
|
|
sotype, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
|
2012-02-01 00:36:45 +09:00
|
|
|
if err != nil {
|
net: add socket system call hooks for testing
This change adds socket system call hooks to existing test cases for
simulating a bit complicated network conditions to help making timeout
and dual IP stack test cases work more properly in followup changes.
Also test cases print debugging information in non-short mode like the
following:
Leaked goroutines:
net.TestWriteTimeout.func2(0xc20802a5a0, 0xc20801d000, 0x1000, 0x1000, 0xc2081d2ae0)
/go/src/net/timeout_test.go:170 +0x98
created by net.TestWriteTimeout
/go/src/net/timeout_test.go:173 +0x745
net.runDatagramPacketConnServer(0xc2080730e0, 0x2bd270, 0x3, 0x2c1770, 0xb, 0xc2081d2ba0, 0xc2081d2c00)
/go/src/net/server_test.go:398 +0x667
created by net.TestTimeoutUDP
/go/src/net/timeout_test.go:247 +0xc9
(snip)
Leaked sockets:
3: {Cookie:615726511685632 Err:<nil> SocketErr:0}
5: {Cookie:7934075906097152 Err:<nil> SocketErr:0}
Socket statistical information:
{Family:1 Type:805306370 Protocol:0 Opened:17 Accepted:0 Connected:5 Closed:17}
{Family:2 Type:805306369 Protocol:0 Opened:450 Accepted:234 Connected:279 Closed:636}
{Family:1 Type:805306369 Protocol:0 Opened:11 Accepted:5 Connected:5 Closed:16}
{Family:28 Type:805306369 Protocol:0 Opened:95 Accepted:22 Connected:16 Closed:116}
{Family:2 Type:805306370 Protocol:0 Opened:84 Accepted:0 Connected:34 Closed:83}
{Family:28 Type:805306370 Protocol:0 Opened:52 Accepted:0 Connected:4 Closed:52}
Change-Id: I0e84be59a0699bc31245c78e2249423459b8cdda
Reviewed-on: https://go-review.googlesource.com/6390
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-03-01 12:27:01 +09:00
|
|
|
closeFunc(fd)
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 21:20:15 +09:00
|
|
|
return nil, os.NewSyscallError("getsockopt", err)
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2011-10-12 13:36:45 -04:00
|
|
|
family := syscall.AF_UNSPEC
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr := sockaddrToTCP
|
2012-08-23 20:54:00 +09:00
|
|
|
lsa, _ := syscall.Getsockname(fd)
|
|
|
|
|
switch lsa.(type) {
|
2011-03-25 14:42:25 -04:00
|
|
|
case *syscall.SockaddrInet4:
|
2011-10-12 13:36:45 -04:00
|
|
|
family = syscall.AF_INET
|
2012-07-22 01:48:15 +09:00
|
|
|
if sotype == syscall.SOCK_DGRAM {
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToUDP
|
2012-07-22 01:48:15 +09:00
|
|
|
} else if sotype == syscall.SOCK_RAW {
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToIP
|
|
|
|
|
}
|
|
|
|
|
case *syscall.SockaddrInet6:
|
2011-10-12 13:36:45 -04:00
|
|
|
family = syscall.AF_INET6
|
2012-07-22 01:48:15 +09:00
|
|
|
if sotype == syscall.SOCK_DGRAM {
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToUDP
|
2012-07-22 01:48:15 +09:00
|
|
|
} else if sotype == syscall.SOCK_RAW {
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToIP
|
|
|
|
|
}
|
|
|
|
|
case *syscall.SockaddrUnix:
|
2011-10-12 13:36:45 -04:00
|
|
|
family = syscall.AF_UNIX
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToUnix
|
2012-07-22 01:48:15 +09:00
|
|
|
if sotype == syscall.SOCK_DGRAM {
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToUnixgram
|
2012-07-22 01:48:15 +09:00
|
|
|
} else if sotype == syscall.SOCK_SEQPACKET {
|
2011-03-25 14:42:25 -04:00
|
|
|
toAddr = sockaddrToUnixpacket
|
|
|
|
|
}
|
2015-04-18 16:53:55 +09:00
|
|
|
default:
|
|
|
|
|
closeFunc(fd)
|
|
|
|
|
return nil, syscall.EPROTONOSUPPORT
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
2012-08-23 20:54:00 +09:00
|
|
|
laddr := toAddr(lsa)
|
2012-09-19 01:33:03 +09:00
|
|
|
rsa, _ := syscall.Getpeername(fd)
|
|
|
|
|
raddr := toAddr(rsa)
|
2011-03-25 14:42:25 -04:00
|
|
|
|
2012-07-22 01:48:15 +09:00
|
|
|
netfd, err := newFD(fd, family, sotype, laddr.Network())
|
2012-02-01 00:36:45 +09:00
|
|
|
if err != nil {
|
net: add socket system call hooks for testing
This change adds socket system call hooks to existing test cases for
simulating a bit complicated network conditions to help making timeout
and dual IP stack test cases work more properly in followup changes.
Also test cases print debugging information in non-short mode like the
following:
Leaked goroutines:
net.TestWriteTimeout.func2(0xc20802a5a0, 0xc20801d000, 0x1000, 0x1000, 0xc2081d2ae0)
/go/src/net/timeout_test.go:170 +0x98
created by net.TestWriteTimeout
/go/src/net/timeout_test.go:173 +0x745
net.runDatagramPacketConnServer(0xc2080730e0, 0x2bd270, 0x3, 0x2c1770, 0xb, 0xc2081d2ba0, 0xc2081d2c00)
/go/src/net/server_test.go:398 +0x667
created by net.TestTimeoutUDP
/go/src/net/timeout_test.go:247 +0xc9
(snip)
Leaked sockets:
3: {Cookie:615726511685632 Err:<nil> SocketErr:0}
5: {Cookie:7934075906097152 Err:<nil> SocketErr:0}
Socket statistical information:
{Family:1 Type:805306370 Protocol:0 Opened:17 Accepted:0 Connected:5 Closed:17}
{Family:2 Type:805306369 Protocol:0 Opened:450 Accepted:234 Connected:279 Closed:636}
{Family:1 Type:805306369 Protocol:0 Opened:11 Accepted:5 Connected:5 Closed:16}
{Family:28 Type:805306369 Protocol:0 Opened:95 Accepted:22 Connected:16 Closed:116}
{Family:2 Type:805306370 Protocol:0 Opened:84 Accepted:0 Connected:34 Closed:83}
{Family:28 Type:805306370 Protocol:0 Opened:52 Accepted:0 Connected:4 Closed:52}
Change-Id: I0e84be59a0699bc31245c78e2249423459b8cdda
Reviewed-on: https://go-review.googlesource.com/6390
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-03-01 12:27:01 +09:00
|
|
|
closeFunc(fd)
|
2011-03-28 23:40:01 -04:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-08-06 23:42:33 +09:00
|
|
|
if err := netfd.init(); err != nil {
|
|
|
|
|
netfd.Close()
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2012-09-19 01:33:03 +09:00
|
|
|
netfd.setAddr(laddr, raddr)
|
2012-02-01 00:36:45 +09:00
|
|
|
return netfd, nil
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-18 16:53:55 +09:00
|
|
|
func fileConn(f *os.File) (Conn, error) {
|
2011-03-25 14:42:25 -04:00
|
|
|
fd, err := newFileFD(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
switch fd.laddr.(type) {
|
|
|
|
|
case *TCPAddr:
|
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
|
case *UDPAddr:
|
|
|
|
|
return newUDPConn(fd), nil
|
|
|
|
|
case *IPAddr:
|
|
|
|
|
return newIPConn(fd), nil
|
2012-08-23 20:54:00 +09:00
|
|
|
case *UnixAddr:
|
|
|
|
|
return newUnixConn(fd), nil
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
|
|
|
|
fd.Close()
|
2012-02-17 10:04:29 +11:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-18 16:53:55 +09:00
|
|
|
func fileListener(f *os.File) (Listener, error) {
|
2011-03-25 14:42:25 -04:00
|
|
|
fd, err := newFileFD(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
switch laddr := fd.laddr.(type) {
|
|
|
|
|
case *TCPAddr:
|
|
|
|
|
return &TCPListener{fd}, nil
|
|
|
|
|
case *UnixAddr:
|
|
|
|
|
return &UnixListener{fd, laddr.Name}, nil
|
|
|
|
|
}
|
|
|
|
|
fd.Close()
|
2012-02-17 10:04:29 +11:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-18 16:53:55 +09:00
|
|
|
func filePacketConn(f *os.File) (PacketConn, error) {
|
2011-03-25 14:42:25 -04:00
|
|
|
fd, err := newFileFD(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
switch fd.laddr.(type) {
|
|
|
|
|
case *UDPAddr:
|
|
|
|
|
return newUDPConn(fd), nil
|
2014-01-28 03:18:27 -08:00
|
|
|
case *IPAddr:
|
|
|
|
|
return newIPConn(fd), nil
|
2011-03-25 14:42:25 -04:00
|
|
|
case *UnixAddr:
|
|
|
|
|
return newUnixConn(fd), nil
|
|
|
|
|
}
|
|
|
|
|
fd.Close()
|
2012-02-17 10:04:29 +11:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-25 14:42:25 -04:00
|
|
|
}
|