github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/net/ipsock_posix.go (about)

     1  // Copyright 2009 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
     6  
     7  // Internet protocol family sockets for POSIX
     8  
     9  package net
    10  
    11  import (
    12  	"runtime"
    13  	"syscall"
    14  	"time"
    15  )
    16  
    17  // BUG(rsc,mikio): On DragonFly BSD and OpenBSD, listening on the
    18  // "tcp" and "udp" networks does not listen for both IPv4 and IPv6
    19  // connections. This is due to the fact that IPv4 traffic will not be
    20  // routed to an IPv6 socket - two separate sockets are required if
    21  // both address families are to be supported.
    22  // See inet6(4) for details.
    23  
    24  func probeIPv4Stack() bool {
    25  	s, err := socketFunc(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
    26  	switch err {
    27  	case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
    28  		return false
    29  	case nil:
    30  		closeFunc(s)
    31  	}
    32  	return true
    33  }
    34  
    35  // Should we try to use the IPv4 socket interface if we're
    36  // only dealing with IPv4 sockets?  As long as the host system
    37  // understands IPv6, it's okay to pass IPv4 addresses to the IPv6
    38  // interface.  That simplifies our code and is most general.
    39  // Unfortunately, we need to run on kernels built without IPv6
    40  // support too.  So probe the kernel to figure it out.
    41  //
    42  // probeIPv6Stack probes both basic IPv6 capability and IPv6 IPv4-
    43  // mapping capability which is controlled by IPV6_V6ONLY socket
    44  // option and/or kernel state "net.inet6.ip6.v6only".
    45  // It returns two boolean values.  If the first boolean value is
    46  // true, kernel supports basic IPv6 functionality.  If the second
    47  // boolean value is true, kernel supports IPv6 IPv4-mapping.
    48  func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
    49  	var probes = []struct {
    50  		laddr TCPAddr
    51  		value int
    52  	}{
    53  		// IPv6 communication capability
    54  		{laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
    55  		// IPv6 IPv4-mapped address communication capability
    56  		{laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
    57  	}
    58  	var supps [2]bool
    59  	switch runtime.GOOS {
    60  	case "dragonfly", "openbsd":
    61  		// Some released versions of DragonFly BSD pretend to
    62  		// accept IPV6_V6ONLY=0 successfully, but the state
    63  		// still stays IPV6_V6ONLY=1. Eventually DragonFly BSD
    64  		// stops preteding, but the transition period would
    65  		// cause unpredictable behavior and we need to avoid
    66  		// it.
    67  		//
    68  		// OpenBSD also doesn't support IPV6_V6ONLY=0 but it
    69  		// never pretends to accept IPV6_V6OLY=0. It always
    70  		// returns an error and we don't need to probe the
    71  		// capability.
    72  		probes = probes[:1]
    73  	}
    74  
    75  	for i := range probes {
    76  		s, err := socketFunc(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
    77  		if err != nil {
    78  			continue
    79  		}
    80  		defer closeFunc(s)
    81  		syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
    82  		sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
    83  		if err != nil {
    84  			continue
    85  		}
    86  		if err := syscall.Bind(s, sa); err != nil {
    87  			continue
    88  		}
    89  		supps[i] = true
    90  	}
    91  
    92  	return supps[0], supps[1]
    93  }
    94  
    95  // favoriteAddrFamily returns the appropriate address family to
    96  // the given net, laddr, raddr and mode.  At first it figures
    97  // address family out from the net.  If mode indicates "listen"
    98  // and laddr is a wildcard, it assumes that the user wants to
    99  // make a passive connection with a wildcard address family, both
   100  // AF_INET and AF_INET6, and a wildcard address like following:
   101  //
   102  //	1. A wild-wild listen, "tcp" + ""
   103  //	If the platform supports both IPv6 and IPv6 IPv4-mapping
   104  //	capabilities, or does not support IPv4, we assume that
   105  //	the user wants to listen on both IPv4 and IPv6 wildcard
   106  //	addresses over an AF_INET6 socket with IPV6_V6ONLY=0.
   107  //	Otherwise we prefer an IPv4 wildcard address listen over
   108  //	an AF_INET socket.
   109  //
   110  //	2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
   111  //	Same as 1.
   112  //
   113  //	3. A wild-ipv6wild listen, "tcp" + "[::]"
   114  //	Almost same as 1 but we prefer an IPv6 wildcard address
   115  //	listen over an AF_INET6 socket with IPV6_V6ONLY=0 when
   116  //	the platform supports IPv6 capability but not IPv6 IPv4-
   117  //	mapping capability.
   118  //
   119  //	4. A ipv4-ipv4wild listen, "tcp4" + "" or "0.0.0.0"
   120  //	We use an IPv4 (AF_INET) wildcard address listen.
   121  //
   122  //	5. A ipv6-ipv6wild listen, "tcp6" + "" or "[::]"
   123  //	We use an IPv6 (AF_INET6, IPV6_V6ONLY=1) wildcard address
   124  //	listen.
   125  //
   126  // Otherwise guess: if the addresses are IPv4 then returns AF_INET,
   127  // or else returns AF_INET6.  It also returns a boolean value what
   128  // designates IPV6_V6ONLY option.
   129  //
   130  // Note that OpenBSD allows neither "net.inet6.ip6.v6only=1" change
   131  // nor IPPROTO_IPV6 level IPV6_V6ONLY socket option setting.
   132  func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
   133  	switch net[len(net)-1] {
   134  	case '4':
   135  		return syscall.AF_INET, false
   136  	case '6':
   137  		return syscall.AF_INET6, true
   138  	}
   139  
   140  	if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
   141  		if supportsIPv4map || !supportsIPv4 {
   142  			return syscall.AF_INET6, false
   143  		}
   144  		if laddr == nil {
   145  			return syscall.AF_INET, false
   146  		}
   147  		return laddr.family(), false
   148  	}
   149  
   150  	if (laddr == nil || laddr.family() == syscall.AF_INET) &&
   151  		(raddr == nil || raddr.family() == syscall.AF_INET) {
   152  		return syscall.AF_INET, false
   153  	}
   154  	return syscall.AF_INET6, false
   155  }
   156  
   157  // Internet sockets (TCP, UDP, IP)
   158  
   159  func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string) (fd *netFD, err error) {
   160  	family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
   161  	return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline)
   162  }
   163  
   164  func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
   165  	switch family {
   166  	case syscall.AF_INET:
   167  		if len(ip) == 0 {
   168  			ip = IPv4zero
   169  		}
   170  		if ip = ip.To4(); ip == nil {
   171  			return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
   172  		}
   173  		sa := new(syscall.SockaddrInet4)
   174  		for i := 0; i < IPv4len; i++ {
   175  			sa.Addr[i] = ip[i]
   176  		}
   177  		sa.Port = port
   178  		return sa, nil
   179  	case syscall.AF_INET6:
   180  		if len(ip) == 0 {
   181  			ip = IPv6zero
   182  		}
   183  		// IPv4 callers use 0.0.0.0 to mean "announce on any available address".
   184  		// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
   185  		// which it refuses to do.  Rewrite to the IPv6 unspecified address.
   186  		if ip.Equal(IPv4zero) {
   187  			ip = IPv6zero
   188  		}
   189  		if ip = ip.To16(); ip == nil {
   190  			return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
   191  		}
   192  		sa := new(syscall.SockaddrInet6)
   193  		for i := 0; i < IPv6len; i++ {
   194  			sa.Addr[i] = ip[i]
   195  		}
   196  		sa.Port = port
   197  		sa.ZoneId = uint32(zoneToInt(zone))
   198  		return sa, nil
   199  	}
   200  	return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
   201  }