Retro68/gcc/libgo/go/net/ipsock_posix.go

202 lines
6.5 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00: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.
2015-08-28 15:33:40 +00:00
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
2014-09-21 17:33:12 +00:00
// Internet protocol family sockets for POSIX
2012-03-27 23:13:14 +00:00
package net
2014-09-21 17:33:12 +00:00
import (
2017-04-10 11:32:00 +00:00
"runtime"
2014-09-21 17:33:12 +00:00
"syscall"
"time"
)
2017-04-10 11:32:00 +00:00
// BUG(rsc,mikio): On DragonFly BSD and OpenBSD, listening on the
// "tcp" and "udp" networks does not listen for both IPv4 and IPv6
// connections. This is due to the fact that IPv4 traffic will not be
// routed to an IPv6 socket - two separate sockets are required if
// both address families are to be supported.
// See inet6(4) for details.
2014-09-21 17:33:12 +00:00
func probeIPv4Stack() bool {
2017-04-10 11:32:00 +00:00
s, err := socketFunc(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
2014-09-21 17:33:12 +00:00
switch err {
case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
return false
case nil:
2017-04-10 11:32:00 +00:00
closeFunc(s)
2014-09-21 17:33:12 +00:00
}
return true
}
2012-03-27 23:13:14 +00: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 {
2014-09-21 17:33:12 +00:00
laddr TCPAddr
2015-08-28 15:33:40 +00:00
value int
2012-03-27 23:13:14 +00:00
}{
// IPv6 communication capability
2015-08-28 15:33:40 +00:00
{laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
2012-03-27 23:13:14 +00:00
// IPv6 IPv4-mapped address communication capability
2015-08-28 15:33:40 +00:00
{laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
var supps [2]bool
switch runtime.GOOS {
case "dragonfly", "openbsd":
// Some released versions of DragonFly BSD pretend to
// accept IPV6_V6ONLY=0 successfully, but the state
// still stays IPV6_V6ONLY=1. Eventually DragonFly BSD
// stops preteding, but the transition period would
// cause unpredictable behavior and we need to avoid
// it.
//
// OpenBSD also doesn't support IPV6_V6ONLY=0 but it
// never pretends to accept IPV6_V6OLY=0. It always
// returns an error and we don't need to probe the
// capability.
probes = probes[:1]
}
2012-03-27 23:13:14 +00:00
for i := range probes {
2017-04-10 11:32:00 +00:00
s, err := socketFunc(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
2012-03-27 23:13:14 +00:00
if err != nil {
continue
}
2017-04-10 11:32:00 +00:00
defer closeFunc(s)
2015-08-28 15:33:40 +00:00
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
2014-09-21 17:33:12 +00:00
sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
2012-03-27 23:13:14 +00:00
if err != nil {
continue
}
2014-09-21 17:33:12 +00:00
if err := syscall.Bind(s, sa); err != nil {
2012-03-27 23:13:14 +00:00
continue
}
2017-04-10 11:32:00 +00:00
supps[i] = true
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return supps[0], supps[1]
2012-03-27 23:13:14 +00:00
}
// favoriteAddrFamily returns the appropriate address family to
// the given net, laddr, raddr and mode. At first it figures
// address family out from the net. If mode indicates "listen"
2014-09-21 17:33:12 +00: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
2017-04-10 11:32:00 +00:00
// capabilities, or does not support IPv4, we assume that
// the user wants to listen on both IPv4 and IPv6 wildcard
// addresses over an AF_INET6 socket with IPV6_V6ONLY=0.
// Otherwise we prefer an IPv4 wildcard address listen over
// an AF_INET socket.
2014-09-21 17:33:12 +00:00
//
// 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) {
2012-03-27 23:13:14 +00:00
switch net[len(net)-1] {
case '4':
2014-09-21 17:33:12 +00:00
return syscall.AF_INET, false
2012-03-27 23:13:14 +00:00
case '6':
2014-09-21 17:33:12 +00:00
return syscall.AF_INET6, true
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
2017-04-10 11:32:00 +00:00
if supportsIPv4map || !supportsIPv4 {
2014-09-21 17:33:12 +00:00
return syscall.AF_INET6, false
}
if laddr == nil {
return syscall.AF_INET, false
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
return laddr.family(), false
2012-03-27 23:13:14 +00:00
}
if (laddr == nil || laddr.family() == syscall.AF_INET) &&
(raddr == nil || raddr.family() == syscall.AF_INET) {
2014-09-21 17:33:12 +00:00
return syscall.AF_INET, false
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
return syscall.AF_INET6, false
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
// Internet sockets (TCP, UDP, IP)
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string, cancel <-chan struct{}) (fd *netFD, err error) {
2014-09-21 17:33:12 +00:00
family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
2017-04-10 11:32:00 +00:00
return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline, cancel)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
2012-03-27 23:13:14 +00:00
switch family {
case syscall.AF_INET:
if len(ip) == 0 {
ip = IPv4zero
}
if ip = ip.To4(); ip == nil {
2017-04-10 11:32:00 +00:00
return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
sa := new(syscall.SockaddrInet4)
2012-03-27 23:13:14 +00:00
for i := 0; i < IPv4len; i++ {
2014-09-21 17:33:12 +00:00
sa.Addr[i] = ip[i]
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
sa.Port = port
return sa, nil
2012-03-27 23:13:14 +00: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",
2014-09-21 17:33:12 +00:00
// which it refuses to do. Rewrite to the IPv6 unspecified address.
2012-03-27 23:13:14 +00:00
if ip.Equal(IPv4zero) {
ip = IPv6zero
}
if ip = ip.To16(); ip == nil {
2017-04-10 11:32:00 +00:00
return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
sa := new(syscall.SockaddrInet6)
2012-03-27 23:13:14 +00:00
for i := 0; i < IPv6len; i++ {
2014-09-21 17:33:12 +00:00
sa.Addr[i] = ip[i]
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
sa.Port = port
sa.ZoneId = uint32(zoneToInt(zone))
return sa, nil
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
2012-03-27 23:13:14 +00:00
}