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