ipsock_posix.go 5.5 KB

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