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.
|
|
|
|
|
2011-12-21 03:44:47 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd 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
|
|
|
|
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
|
|
|
// Internet protocol family sockets for POSIX
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
package net
|
|
|
|
|
2012-11-08 09:35:16 -07:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
2011-08-17 11:28:29 -06:00
|
|
|
|
|
|
|
// Should we try to use the IPv4 socket interface if we're
|
|
|
|
// only dealing with IPv4 sockets? As long as the host system
|
|
|
|
// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
|
|
|
|
// interface. That simplifies our code and is most general.
|
|
|
|
// Unfortunately, we need to run on kernels built without IPv6
|
|
|
|
// support too. So probe the kernel to figure it out.
|
|
|
|
//
|
|
|
|
// probeIPv6Stack probes both basic IPv6 capability and IPv6 IPv4-
|
|
|
|
// mapping capability which is controlled by IPV6_V6ONLY socket
|
|
|
|
// option and/or kernel state "net.inet6.ip6.v6only".
|
|
|
|
// It returns two boolean values. If the first boolean value is
|
|
|
|
// true, kernel supports basic IPv6 functionality. If the second
|
|
|
|
// boolean value is true, kernel supports IPv6 IPv4-mapping.
|
|
|
|
func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
|
|
|
|
var probes = []struct {
|
|
|
|
la TCPAddr
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
// IPv6 communication capability
|
|
|
|
{TCPAddr{IP: ParseIP("::1")}, false},
|
|
|
|
// IPv6 IPv4-mapped address communication capability
|
|
|
|
{TCPAddr{IP: IPv4(127, 0, 0, 1)}, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range probes {
|
2011-11-13 20:44:52 -07:00
|
|
|
s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
|
|
|
|
if err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer closesocket(s)
|
2012-03-05 08:13:10 -07:00
|
|
|
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
|
2011-08-17 11:28:29 -06:00
|
|
|
sa, err := probes[i].la.toAddr().sockaddr(syscall.AF_INET6)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
err = syscall.Bind(s, sa)
|
|
|
|
if err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
probes[i].ok = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return probes[0].ok, probes[1].ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// favoriteAddrFamily returns the appropriate address family to
|
2012-02-14 09:59:18 -07:00
|
|
|
// the given net, laddr, raddr and mode. At first it figures
|
2011-08-17 11:28:29 -06:00
|
|
|
// address family out from the net. If mode indicates "listen"
|
2012-03-05 08:13:10 -07:00
|
|
|
// and laddr is a wildcard, it assumes that the user wants to
|
|
|
|
// make a passive connection with a wildcard address family, both
|
|
|
|
// AF_INET and AF_INET6, and a wildcard address like following:
|
|
|
|
//
|
|
|
|
// 1. A wild-wild listen, "tcp" + ""
|
|
|
|
// If the platform supports both IPv6 and IPv6 IPv4-mapping
|
|
|
|
// capabilities, we assume that the user want to listen on
|
|
|
|
// both IPv4 and IPv6 wildcard address over an AF_INET6
|
|
|
|
// socket with IPV6_V6ONLY=0. Otherwise we prefer an IPv4
|
|
|
|
// wildcard address listen over an AF_INET socket.
|
|
|
|
//
|
|
|
|
// 2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
|
|
|
|
// Same as 1.
|
|
|
|
//
|
|
|
|
// 3. A wild-ipv6wild listen, "tcp" + "[::]"
|
|
|
|
// Almost same as 1 but we prefer an IPv6 wildcard address
|
|
|
|
// listen over an AF_INET6 socket with IPV6_V6ONLY=0 when
|
|
|
|
// the platform supports IPv6 capability but not IPv6 IPv4-
|
|
|
|
// mapping capability.
|
|
|
|
//
|
|
|
|
// 4. A ipv4-ipv4wild listen, "tcp4" + "" or "0.0.0.0"
|
|
|
|
// We use an IPv4 (AF_INET) wildcard address listen.
|
|
|
|
//
|
|
|
|
// 5. A ipv6-ipv6wild listen, "tcp6" + "" or "[::]"
|
|
|
|
// We use an IPv6 (AF_INET6, IPV6_V6ONLY=1) wildcard address
|
|
|
|
// listen.
|
|
|
|
//
|
|
|
|
// Otherwise guess: if the addresses are IPv4 then returns AF_INET,
|
|
|
|
// or else returns AF_INET6. It also returns a boolean value what
|
|
|
|
// designates IPV6_V6ONLY option.
|
|
|
|
//
|
|
|
|
// Note that OpenBSD allows neither "net.inet6.ip6.v6only=1" change
|
|
|
|
// nor IPPROTO_IPV6 level IPV6_V6ONLY socket option setting.
|
|
|
|
func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net[len(net)-1] {
|
|
|
|
case '4':
|
2012-03-05 08:13:10 -07:00
|
|
|
return syscall.AF_INET, false
|
2011-08-17 11:28:29 -06:00
|
|
|
case '6':
|
2012-03-05 08:13:10 -07:00
|
|
|
return syscall.AF_INET6, true
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-04-24 21:29:14 -06:00
|
|
|
if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
|
2012-03-05 08:13:10 -07:00
|
|
|
if supportsIPv4map {
|
|
|
|
return syscall.AF_INET6, false
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-04-24 21:29:14 -06:00
|
|
|
if laddr == nil {
|
|
|
|
return syscall.AF_INET, false
|
|
|
|
}
|
2012-03-05 08:13:10 -07:00
|
|
|
return laddr.family(), false
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (laddr == nil || laddr.family() == syscall.AF_INET) &&
|
|
|
|
(raddr == nil || raddr.family() == syscall.AF_INET) {
|
2012-03-05 08:13:10 -07:00
|
|
|
return syscall.AF_INET, false
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-03-05 08:13:10 -07:00
|
|
|
return syscall.AF_INET6, false
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-03-05 08:13:10 -07:00
|
|
|
// Internet sockets (TCP, UDP, IP)
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2012-03-05 08:13:10 -07:00
|
|
|
// A sockaddr represents a TCP, UDP or IP network address that can
|
2011-08-17 11:28:29 -06:00
|
|
|
// be converted into a syscall.Sockaddr.
|
|
|
|
type sockaddr interface {
|
|
|
|
Addr
|
|
|
|
family() int
|
2012-03-05 08:13:10 -07:00
|
|
|
isWildcard() bool
|
|
|
|
sockaddr(family int) (syscall.Sockaddr, error)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-11-08 09:35:16 -07:00
|
|
|
func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
var la, ra syscall.Sockaddr
|
2012-03-05 08:13:10 -07:00
|
|
|
family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
|
2011-08-17 11:28:29 -06:00
|
|
|
if laddr != nil {
|
2012-02-22 03:08:19 -07:00
|
|
|
if la, err = laddr.sockaddr(family); err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if raddr != nil {
|
2012-02-22 03:08:19 -07:00
|
|
|
if ra, err = raddr.sockaddr(family); err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
}
|
2012-11-08 09:35:16 -07:00
|
|
|
fd, err = socket(net, family, sotype, proto, ipv6only, la, ra, deadline, toAddr)
|
2012-02-22 03:08:19 -07:00
|
|
|
if err != nil {
|
2011-08-17 11:28:29 -06:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
|
|
|
|
Error:
|
|
|
|
addr := raddr
|
|
|
|
if mode == "listen" {
|
|
|
|
addr = laddr
|
|
|
|
}
|
2012-02-22 03:08:19 -07:00
|
|
|
return nil, &OpError{mode, net, addr, err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
switch family {
|
|
|
|
case syscall.AF_INET:
|
|
|
|
if len(ip) == 0 {
|
|
|
|
ip = IPv4zero
|
|
|
|
}
|
|
|
|
if ip = ip.To4(); ip == nil {
|
|
|
|
return nil, InvalidAddrError("non-IPv4 address")
|
|
|
|
}
|
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
|
|
|
sa := new(syscall.SockaddrInet4)
|
2011-08-17 11:28:29 -06:00
|
|
|
for i := 0; i < IPv4len; i++ {
|
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
|
|
|
sa.Addr[i] = ip[i]
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
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
|
|
|
sa.Port = port
|
|
|
|
return sa, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
case syscall.AF_INET6:
|
|
|
|
if len(ip) == 0 {
|
|
|
|
ip = IPv6zero
|
|
|
|
}
|
|
|
|
// IPv4 callers use 0.0.0.0 to mean "announce on any available address".
|
|
|
|
// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
|
2012-03-05 08:13:10 -07:00
|
|
|
// which it refuses to do. Rewrite to the IPv6 unspecified address.
|
2011-08-17 11:28:29 -06:00
|
|
|
if ip.Equal(IPv4zero) {
|
|
|
|
ip = IPv6zero
|
|
|
|
}
|
|
|
|
if ip = ip.To16(); ip == nil {
|
|
|
|
return nil, InvalidAddrError("non-IPv6 address")
|
|
|
|
}
|
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
|
|
|
sa := new(syscall.SockaddrInet6)
|
2011-08-17 11:28:29 -06:00
|
|
|
for i := 0; i < IPv6len; i++ {
|
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
|
|
|
sa.Addr[i] = ip[i]
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
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
|
|
|
sa.Port = port
|
|
|
|
sa.ZoneId = uint32(zoneToInt(zone))
|
|
|
|
return sa, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil, InvalidAddrError("unexpected socket family")
|
|
|
|
}
|