...
Run Format

Source file src/pkg/net/ipsock_posix.go

     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		"syscall"
    13		"time"
    14	)
    15	
    16	func probeIPv4Stack() bool {
    17		s, err := syscall.Socket(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			closesocket(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			ok    bool
    45		}{
    46			// IPv6 communication capability
    47			{laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
    48			// IPv6 IPv4-mapped address communication capability
    49			{laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
    50		}
    51	
    52		for i := range probes {
    53			s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
    54			if err != nil {
    55				continue
    56			}
    57			defer closesocket(s)
    58			syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
    59			sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
    60			if err != nil {
    61				continue
    62			}
    63			if err := syscall.Bind(s, sa); err != nil {
    64				continue
    65			}
    66			probes[i].ok = true
    67		}
    68	
    69		return probes[0].ok, probes[1].ok
    70	}
    71	
    72	// favoriteAddrFamily returns the appropriate address family to
    73	// the given net, laddr, raddr and mode.  At first it figures
    74	// address family out from the net.  If mode indicates "listen"
    75	// and laddr is a wildcard, it assumes that the user wants to
    76	// make a passive connection with a wildcard address family, both
    77	// AF_INET and AF_INET6, and a wildcard address like following:
    78	//
    79	//	1. A wild-wild listen, "tcp" + ""
    80	//	If the platform supports both IPv6 and IPv6 IPv4-mapping
    81	//	capabilities, we assume that the user want to listen on
    82	//	both IPv4 and IPv6 wildcard address over an AF_INET6
    83	//	socket with IPV6_V6ONLY=0.  Otherwise we prefer an IPv4
    84	//	wildcard address listen over an AF_INET socket.
    85	//
    86	//	2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
    87	//	Same as 1.
    88	//
    89	//	3. A wild-ipv6wild listen, "tcp" + "[::]"
    90	//	Almost same as 1 but we prefer an IPv6 wildcard address
    91	//	listen over an AF_INET6 socket with IPV6_V6ONLY=0 when
    92	//	the platform supports IPv6 capability but not IPv6 IPv4-
    93	//	mapping capability.
    94	//
    95	//	4. A ipv4-ipv4wild listen, "tcp4" + "" or "0.0.0.0"
    96	//	We use an IPv4 (AF_INET) wildcard address listen.
    97	//
    98	//	5. A ipv6-ipv6wild listen, "tcp6" + "" or "[::]"
    99	//	We use an IPv6 (AF_INET6, IPV6_V6ONLY=1) wildcard address
   100	//	listen.
   101	//
   102	// Otherwise guess: if the addresses are IPv4 then returns AF_INET,
   103	// or else returns AF_INET6.  It also returns a boolean value what
   104	// designates IPV6_V6ONLY option.
   105	//
   106	// Note that OpenBSD allows neither "net.inet6.ip6.v6only=1" change
   107	// nor IPPROTO_IPV6 level IPV6_V6ONLY socket option setting.
   108	func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
   109		switch net[len(net)-1] {
   110		case '4':
   111			return syscall.AF_INET, false
   112		case '6':
   113			return syscall.AF_INET6, true
   114		}
   115	
   116		if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
   117			if supportsIPv4map {
   118				return syscall.AF_INET6, false
   119			}
   120			if laddr == nil {
   121				return syscall.AF_INET, false
   122			}
   123			return laddr.family(), false
   124		}
   125	
   126		if (laddr == nil || laddr.family() == syscall.AF_INET) &&
   127			(raddr == nil || raddr.family() == syscall.AF_INET) {
   128			return syscall.AF_INET, false
   129		}
   130		return syscall.AF_INET6, false
   131	}
   132	
   133	// Internet sockets (TCP, UDP, IP)
   134	
   135	func internetSocket(net string, laddr, raddr sockaddr, deadline time.Time, sotype, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
   136		family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
   137		return socket(net, family, sotype, proto, ipv6only, laddr, raddr, deadline, toAddr)
   138	}
   139	
   140	func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
   141		switch family {
   142		case syscall.AF_INET:
   143			if len(ip) == 0 {
   144				ip = IPv4zero
   145			}
   146			if ip = ip.To4(); ip == nil {
   147				return nil, InvalidAddrError("non-IPv4 address")
   148			}
   149			sa := new(syscall.SockaddrInet4)
   150			for i := 0; i < IPv4len; i++ {
   151				sa.Addr[i] = ip[i]
   152			}
   153			sa.Port = port
   154			return sa, nil
   155		case syscall.AF_INET6:
   156			if len(ip) == 0 {
   157				ip = IPv6zero
   158			}
   159			// IPv4 callers use 0.0.0.0 to mean "announce on any available address".
   160			// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
   161			// which it refuses to do.  Rewrite to the IPv6 unspecified address.
   162			if ip.Equal(IPv4zero) {
   163				ip = IPv6zero
   164			}
   165			if ip = ip.To16(); ip == nil {
   166				return nil, InvalidAddrError("non-IPv6 address")
   167			}
   168			sa := new(syscall.SockaddrInet6)
   169			for i := 0; i < IPv6len; i++ {
   170				sa.Addr[i] = ip[i]
   171			}
   172			sa.Port = port
   173			sa.ZoneId = uint32(zoneToInt(zone))
   174			return sa, nil
   175		}
   176		return nil, InvalidAddrError("unexpected socket family")
   177	}
   178	

View as plain text