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
|
|
|
|
|
2012-11-08 09:35:16 -07:00
|
|
|
import (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
2012-11-08 09:35:16 -07:00
|
|
|
"syscall"
|
|
|
|
)
|
2012-01-26 09:31:42 -07:00
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
func sockaddrToUDP(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 &UDPAddr{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 &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) 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 *UDPAddr) 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-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) readFrom(b []byte) (int, *UDPAddr, error) {
|
2015-04-16 08:10:56 -06:00
|
|
|
var addr *UDPAddr
|
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 = &UDPAddr{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
|
|
|
addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-04-16 08:10:56 -06:00
|
|
|
return n, addr, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, 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 = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2012-09-24 15:57:32 -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
|
|
|
addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UDPConn) writeTo(b []byte, addr *UDPAddr) (int, error) {
|
2012-01-26 09:31:42 -07:00
|
|
|
if c.fd.isConnected {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, ErrWriteToConnected
|
2012-01-26 09:31:42 -07: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-23 10:59:43 -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 *UDPConn) writeMsg(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
|
2015-02-04 19:05:53 -07:00
|
|
|
if c.fd.isConnected && addr != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, ErrWriteToConnected
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
2015-02-04 19:05:53 -07:00
|
|
|
if !c.fd.isConnected && addr == nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, errMissingAddress
|
2011-08-17 11:28:29 -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)
|
net: remove redundant argument check from Dial on udp, unix networks
The net package consists of thin three layers like the follwoing;
- Exposed API, that contains net.Dial, net.DialUDP, net.DialUnix
- Socket and network file descriptor, that contains net.netFD and
its methods, helper functions such as dialUDP, dialUnix
- Network pollster, that contains net.pollDesc and its methods
This CL removes redundant argument check which is already done by
API layer.
R=golang-dev, dave, bradfitz
CC=golang-dev
https://golang.org/cl/13092043
2013-08-21 19:33:06 -06:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func dialUDP(ctx context.Context, net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
fd, err := internetSocket(ctx, net, laddr, raddr, syscall.SOCK_DGRAM, 0, "dial")
|
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 newUDPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenUDP(ctx context.Context, network string, laddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
fd, err := internetSocket(ctx, network, laddr, nil, syscall.SOCK_DGRAM, 0, "listen")
|
2012-01-23 10:59:43 -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 newUDPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenMulticastUDP(ctx context.Context, network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
fd, err := internetSocket(ctx, network, gaddr, nil, syscall.SOCK_DGRAM, 0, "listen")
|
2012-01-31 09:53:26 -07:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2012-01-31 09:53:26 -07:00
|
|
|
}
|
|
|
|
c := newUDPConn(fd)
|
2013-01-14 16:53:12 -07:00
|
|
|
if ip4 := gaddr.IP.To4(); ip4 != nil {
|
|
|
|
if err := listenIPv4MulticastUDP(c, ifi, ip4); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
c.Close()
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2012-01-31 09:53:26 -07:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := listenIPv6MulticastUDP(c, ifi, gaddr.IP); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
c.Close()
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2012-01-31 09:53:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2012-01-31 09:53:26 -07:00
|
|
|
func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
|
|
|
|
if ifi != nil {
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := setIPv4MulticastInterface(c.fd, ifi); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := setIPv4MulticastLoopback(c.fd, false); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
return err
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := joinIPv4Group(c.fd, ifi, ip); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
return err
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|
2012-01-31 09:53:26 -07:00
|
|
|
return nil
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2012-01-31 09:53:26 -07:00
|
|
|
func listenIPv6MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
|
|
|
|
if ifi != nil {
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := setIPv6MulticastInterface(c.fd, ifi); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
return err
|
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := setIPv6MulticastLoopback(c.fd, false); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
return err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-01-14 16:53:12 -07:00
|
|
|
if err := joinIPv6Group(c.fd, ifi, ip); err != nil {
|
2012-01-31 09:53:26 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2011-08-18 10:22:02 -06:00
|
|
|
}
|