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