2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2010 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
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sockaddrToIP(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 &IPAddr{IP: sa.Addr[0:]}
|
2011-08-17 11:28:29 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-04-27 04:00:09 -06:00
|
|
|
return &IPAddr{IP: sa.Addr[0:], Zone: zoneCache.name(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IPAddr) 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 *IPAddr) 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, 0, a.Zone)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-10-26 19:07:52 -06:00
|
|
|
func (a *IPAddr) toLocal(net string) sockaddr {
|
|
|
|
return &IPAddr{loopbackIP(net), a.Zone}
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) readFrom(b []byte) (int, *IPAddr, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
// TODO(cw,rsc): consider using readv if we know the family
|
|
|
|
// type to avoid the header trim/copy
|
2012-01-21 05:51:53 -07:00
|
|
|
var addr *IPAddr
|
2014-04-02 18:06:51 -06:00
|
|
|
n, sa, err := c.fd.readFrom(b)
|
2011-08-17 11:28:29 -06:00
|
|
|
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
|
|
|
addr = &IPAddr{IP: sa.Addr[0:]}
|
2015-01-18 00:28:15 -07:00
|
|
|
n = stripIPv4Header(n, b)
|
2011-08-17 11:28:29 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-04-27 04:00:09 -06:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneCache.name(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
return n, addr, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 00:28:15 -07:00
|
|
|
func stripIPv4Header(n int, b []byte) int {
|
|
|
|
if len(b) < 20 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
l := int(b[0]&0x0f) << 2
|
|
|
|
if 20 > l || l > len(b) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
if b[0]>>4 != 4 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
copy(b, b[l:])
|
|
|
|
return n - l
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
|
2012-09-24 15:57:32 -06:00
|
|
|
var sa syscall.Sockaddr
|
2014-04-02 18:06:51 -06:00
|
|
|
n, oobn, flags, sa, err = c.fd.readMsg(b, oob)
|
2012-09-24 15:57:32 -06:00
|
|
|
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
|
|
|
addr = &IPAddr{IP: sa.Addr[0:]}
|
2012-09-24 15:57:32 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-04-27 04:00:09 -06:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneCache.name(int(sa.ZoneId))}
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) writeTo(b []byte, addr *IPAddr) (int, error) {
|
2014-04-28 21:37:16 -06:00
|
|
|
if c.fd.isConnected {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, ErrWriteToConnected
|
2014-04-28 21:37:16 -06:00
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, errMissingAddress
|
2013-08-18 04:19:36 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
return c.fd.writeTo(b, sa)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) writeMsg(b, oob []byte, addr *IPAddr) (n, oobn int, err error) {
|
2014-04-28 21:37:16 -06:00
|
|
|
if c.fd.isConnected {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, ErrWriteToConnected
|
2014-04-28 21:37:16 -06:00
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, errMissingAddress
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
2016-03-02 04:08:18 -07:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, err
|
2016-03-02 04:08:18 -07:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
return c.fd.writeMsg(b, oob, sa)
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func dialIP(ctx context.Context, netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
|
2017-04-13 03:00:35 -06:00
|
|
|
network, proto, err := parseNetwork(ctx, netProto, true)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
switch network {
|
2011-08-17 11:28:29 -06:00
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, UnknownNetworkError(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if raddr == nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, errMissingAddress
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2016-04-14 18:47:25 -06:00
|
|
|
fd, err := internetSocket(ctx, network, laddr, raddr, syscall.SOCK_RAW, proto, "dial")
|
2012-01-21 05:51:53 -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 newIPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenIP(ctx context.Context, netProto string, laddr *IPAddr) (*IPConn, error) {
|
2017-04-13 03:00:35 -06:00
|
|
|
network, proto, err := parseNetwork(ctx, netProto, true)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
switch network {
|
2011-08-17 11:28:29 -06:00
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, UnknownNetworkError(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2016-04-14 18:47:25 -06:00
|
|
|
fd, err := internetSocket(ctx, network, laddr, nil, syscall.SOCK_RAW, proto, "listen")
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|