2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-08-17 11:28:29 -06:00
|
|
|
// 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 (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
2011-08-17 11:28:29 -06:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-26 19:07:52 -06:00
|
|
|
func (a *TCPAddr) toLocal(net string) sockaddr {
|
|
|
|
return &TCPAddr{loopbackIP(net), a.Port, a.Zone}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07: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
|
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return genericReadFrom(c, r)
|
2012-11-12 20:56:28 -07:00
|
|
|
}
|
2012-02-12 21:25:55 -07:00
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func dialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
if testHookDialTCP != nil {
|
|
|
|
return testHookDialTCP(ctx, net, laddr, raddr)
|
|
|
|
}
|
|
|
|
return doDialTCP(ctx, net, laddr, raddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doDialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
fd, err := internetSocket(ctx, net, laddr, raddr, 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
|
2016-03-01 16:21:55 -07:00
|
|
|
// at addr2, without either machine executing Listen. If laddr == nil,
|
2012-02-12 21:25:55 -07:00
|
|
|
// it means we want the kernel to pick an appropriate originating local
|
2016-03-01 16:21:55 -07:00
|
|
|
// address. Some Linux kernels cycle blindly through a fixed range of
|
|
|
|
// local ports, regardless of destination port. If a kernel happens to
|
2012-02-12 21:25:55 -07:00
|
|
|
// 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.
|
2016-03-01 16:21:55 -07:00
|
|
|
// It's hard to argue this is anything other than a kernel bug. If we
|
2012-02-12 21:25:55 -07:00
|
|
|
// see this happen, rather than expose the buggy effect to users, we
|
2016-03-01 16:21:55 -07:00
|
|
|
// close the fd and try again. If it happens twice more, we relent and
|
|
|
|
// use the result. See also:
|
2015-07-10 17:17:11 -06:00
|
|
|
// https://golang.org/issue/2690
|
2012-02-12 21:25:55 -07:00
|
|
|
// 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()
|
|
|
|
}
|
2016-04-14 18:47:25 -06:00
|
|
|
fd, err = internetSocket(ctx, net, laddr, raddr, syscall.SOCK_STREAM, 0, "dial")
|
2012-02-12 21:25:55 -07:00
|
|
|
}
|
|
|
|
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, 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 {
|
2015-07-27 21:28:09 -06:00
|
|
|
if op, ok := err.(*OpError); ok {
|
|
|
|
err = op.Err
|
|
|
|
}
|
|
|
|
if sys, ok := err.(*os.SyscallError); ok {
|
|
|
|
err = sys.Err
|
|
|
|
}
|
|
|
|
return err == syscall.EADDRNOTAVAIL
|
2012-08-06 14:32:00 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) accept() (*TCPConn, error) {
|
|
|
|
fd, err := ln.fd.accept()
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return newTCPConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) close() error {
|
|
|
|
return ln.fd.Close()
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) file() (*os.File, error) {
|
|
|
|
f, err := ln.fd.dup()
|
2015-04-18 01:53:55 -06:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return f, nil
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2012-11-12 20:56:28 -07:00
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenTCP(ctx context.Context, network string, laddr *TCPAddr) (*TCPListener, error) {
|
|
|
|
fd, err := internetSocket(ctx, network, laddr, nil, syscall.SOCK_STREAM, 0, "listen")
|
2012-11-12 20:56:28 -07:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, 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
|
|
|
}
|