src/pkg/net/ipsock_posix.go - The Go Programming Language

Golang

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 freebsd linux netbsd openbsd windows
     6	
     7	package net
     8	
     9	import "syscall"
    10	
    11	// Should we try to use the IPv4 socket interface if we're
    12	// only dealing with IPv4 sockets?  As long as the host system
    13	// understands IPv6, it's okay to pass IPv4 addresses to the IPv6
    14	// interface.  That simplifies our code and is most general.
    15	// Unfortunately, we need to run on kernels built without IPv6
    16	// support too.  So probe the kernel to figure it out.
    17	//
    18	// probeIPv6Stack probes both basic IPv6 capability and IPv6 IPv4-
    19	// mapping capability which is controlled by IPV6_V6ONLY socket
    20	// option and/or kernel state "net.inet6.ip6.v6only".
    21	// It returns two boolean values.  If the first boolean value is
    22	// true, kernel supports basic IPv6 functionality.  If the second
    23	// boolean value is true, kernel supports IPv6 IPv4-mapping.
    24	func probeIPv6Stack() (supportsIPv6, supportsIPv4map bool) {
    25		var probes = []struct {
    26			la TCPAddr
    27			ok bool
    28		}{
    29			// IPv6 communication capability
    30			{TCPAddr{IP: ParseIP("::1")}, false},
    31			// IPv6 IPv4-mapped address communication capability
    32			{TCPAddr{IP: IPv4(127, 0, 0, 1)}, false},
    33		}
    34	
    35		for i := range probes {
    36			s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
    37			if err != nil {
    38				continue
    39			}
    40			defer closesocket(s)
    41			syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
    42			sa, err := probes[i].la.toAddr().sockaddr(syscall.AF_INET6)
    43			if err != nil {
    44				continue
    45			}
    46			err = syscall.Bind(s, sa)
    47			if err != nil {
    48				continue
    49			}
    50			probes[i].ok = true
    51		}
    52	
    53		return probes[0].ok, probes[1].ok
    54	}
    55	
    56	// favoriteAddrFamily returns the appropriate address family to
    57	// the given net, laddr, raddr and mode.  At first it figures
    58	// address family out from the net.  If mode indicates "listen"
    59	// and laddr is a wildcard, it assumes that the user wants to
    60	// make a passive connection with a wildcard address family, both
    61	// AF_INET and AF_INET6, and a wildcard address like following:
    62	//
    63	//	1. A wild-wild listen, "tcp" + ""
    64	//	If the platform supports both IPv6 and IPv6 IPv4-mapping
    65	//	capabilities, we assume that the user want to listen on
    66	//	both IPv4 and IPv6 wildcard address over an AF_INET6
    67	//	socket with IPV6_V6ONLY=0.  Otherwise we prefer an IPv4
    68	//	wildcard address listen over an AF_INET socket.
    69	//
    70	//	2. A wild-ipv4wild listen, "tcp" + "0.0.0.0"
    71	//	Same as 1.
    72	//
    73	//	3. A wild-ipv6wild listen, "tcp" + "[::]"
    74	//	Almost same as 1 but we prefer an IPv6 wildcard address
    75	//	listen over an AF_INET6 socket with IPV6_V6ONLY=0 when
    76	//	the platform supports IPv6 capability but not IPv6 IPv4-
    77	//	mapping capability.
    78	//
    79	//	4. A ipv4-ipv4wild listen, "tcp4" + "" or "0.0.0.0"
    80	//	We use an IPv4 (AF_INET) wildcard address listen.
    81	//
    82	//	5. A ipv6-ipv6wild listen, "tcp6" + "" or "[::]"
    83	//	We use an IPv6 (AF_INET6, IPV6_V6ONLY=1) wildcard address
    84	//	listen.
    85	//
    86	// Otherwise guess: if the addresses are IPv4 then returns AF_INET,
    87	// or else returns AF_INET6.  It also returns a boolean value what
    88	// designates IPV6_V6ONLY option.
    89	//
    90	// Note that OpenBSD allows neither "net.inet6.ip6.v6only=1" change
    91	// nor IPPROTO_IPV6 level IPV6_V6ONLY socket option setting.
    92	func favoriteAddrFamily(net string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
    93		switch net[len(net)-1] {
    94		case '4':
    95			return syscall.AF_INET, false
    96		case '6':
    97			return syscall.AF_INET6, true
    98		}
    99	
   100		if mode == "listen" && laddr.isWildcard() {
   101			if supportsIPv4map {
   102				return syscall.AF_INET6, false
   103			}
   104			return laddr.family(), false
   105		}
   106	
   107		if (laddr == nil || laddr.family() == syscall.AF_INET) &&
   108			(raddr == nil || raddr.family() == syscall.AF_INET) {
   109			return syscall.AF_INET, false
   110		}
   111		return syscall.AF_INET6, false
   112	}
   113	
   114	// Internet sockets (TCP, UDP, IP)
   115	
   116	// A sockaddr represents a TCP, UDP or IP network address that can
   117	// be converted into a syscall.Sockaddr.
   118	type sockaddr interface {
   119		Addr
   120		family() int
   121		isWildcard() bool
   122		sockaddr(family int) (syscall.Sockaddr, error)
   123	}
   124	
   125	func internetSocket(net string, laddr, raddr sockaddr, sotype, proto int, mode string, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
   126		var la, ra syscall.Sockaddr
   127		family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
   128		if laddr != nil {
   129			if la, err = laddr.sockaddr(family); err != nil {
   130				goto Error
   131			}
   132		}
   133		if raddr != nil {
   134			if ra, err = raddr.sockaddr(family); err != nil {
   135				goto Error
   136			}
   137		}
   138		fd, err = socket(net, family, sotype, proto, ipv6only, la, ra, toAddr)
   139		if err != nil {
   140			goto Error
   141		}
   142		return fd, nil
   143	
   144	Error:
   145		addr := raddr
   146		if mode == "listen" {
   147			addr = laddr
   148		}
   149		return nil, &OpError{mode, net, addr, err}
   150	}
   151	
   152	func ipToSockaddr(family int, ip IP, port int) (syscall.Sockaddr, error) {
   153		switch family {
   154		case syscall.AF_INET:
   155			if len(ip) == 0 {
   156				ip = IPv4zero
   157			}
   158			if ip = ip.To4(); ip == nil {
   159				return nil, InvalidAddrError("non-IPv4 address")
   160			}
   161			s := new(syscall.SockaddrInet4)
   162			for i := 0; i < IPv4len; i++ {
   163				s.Addr[i] = ip[i]
   164			}
   165			s.Port = port
   166			return s, nil
   167		case syscall.AF_INET6:
   168			if len(ip) == 0 {
   169				ip = IPv6zero
   170			}
   171			// IPv4 callers use 0.0.0.0 to mean "announce on any available address".
   172			// In IPv6 mode, Linux treats that as meaning "announce on 0.0.0.0",
   173			// which it refuses to do.  Rewrite to the IPv6 unspecified address.
   174			if ip.Equal(IPv4zero) {
   175				ip = IPv6zero
   176			}
   177			if ip = ip.To16(); ip == nil {
   178				return nil, InvalidAddrError("non-IPv6 address")
   179			}
   180			s := new(syscall.SockaddrInet6)
   181			for i := 0; i < IPv6len; i++ {
   182				s.Addr[i] = ip[i]
   183			}
   184			s.Port = port
   185			return s, nil
   186		}
   187		return nil, InvalidAddrError("unexpected socket family")
   188	}