| 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 GO_LICENSE file. |
| 4 |
|
| 5 |
package sctp |
| 6 |
|
| 7 |
import ( |
| 8 |
"net" |
| 9 |
"os" |
| 10 |
"sync" |
| 11 |
"syscall" |
| 12 |
) |
| 13 |
|
| 14 |
//from https://github.com/golang/go |
| 15 |
// Boolean to int. |
| 16 |
func boolint(b bool) int { |
| 17 |
if b { |
| 18 |
return 1 |
| 19 |
} |
| 20 |
return 0 |
| 21 |
} |
| 22 |
|
| 23 |
//from https://github.com/golang/go |
| 24 |
func ipToSockaddr(family int, ip net.IP, port int, zone string) (syscall.Sockaddr, error) { |
| 25 |
switch family { |
| 26 |
case syscall.AF_INET: |
| 27 |
if len(ip) == 0 { |
| 28 |
ip = net.IPv4zero |
| 29 |
} |
| 30 |
ip4 := ip.To4() |
| 31 |
if ip4 == nil { |
| 32 |
return nil, &net.AddrError{Err: "non-IPv4 address", Addr: ip.String()} |
| 33 |
} |
| 34 |
sa := &syscall.SockaddrInet4{Port: port} |
| 35 |
copy(sa.Addr[:], ip4) |
| 36 |
return sa, nil |
| 37 |
case syscall.AF_INET6: |
| 38 |
// In general, an IP wildcard address, which is either |
| 39 |
// "0.0.0.0" or "::", means the entire IP addressing |
| 40 |
// space. For some historical reason, it is used to |
| 41 |
// specify "any available address" on some operations |
| 42 |
// of IP node. |
| 43 |
// |
| 44 |
// When the IP node supports IPv4-mapped IPv6 address, |
| 45 |
// we allow an listener to listen to the wildcard |
| 46 |
// address of both IP addressing spaces by specifying |
| 47 |
// IPv6 wildcard address. |
| 48 |
if len(ip) == 0 || ip.Equal(net.IPv4zero) { |
| 49 |
ip = net.IPv6zero |
| 50 |
} |
| 51 |
// We accept any IPv6 address including IPv4-mapped |
| 52 |
// IPv6 address. |
| 53 |
ip6 := ip.To16() |
| 54 |
if ip6 == nil { |
| 55 |
return nil, &net.AddrError{Err: "non-IPv6 address", Addr: ip.String()} |
| 56 |
} |
| 57 |
//we set ZoneId to 0, as currently we use this functon only to probe the IP capabilities of the host |
| 58 |
//if real Zone handling is required, the zone cache implementation in golang/net should be pulled here |
| 59 |
sa := &syscall.SockaddrInet6{Port: port, ZoneId: 0} |
| 60 |
copy(sa.Addr[:], ip6) |
| 61 |
return sa, nil |
| 62 |
} |
| 63 |
return nil, &net.AddrError{Err: "invalid address family", Addr: ip.String()} |
| 64 |
} |
| 65 |
|
| 66 |
//from https://github.com/golang/go |
| 67 |
func sockaddr(a *net.TCPAddr, family int) (syscall.Sockaddr, error) { |
| 68 |
if a == nil { |
| 69 |
return nil, nil |
| 70 |
} |
| 71 |
return ipToSockaddr(family, a.IP, a.Port, a.Zone) |
| 72 |
} |
| 73 |
|
| 74 |
//from https://github.com/golang/go |
| 75 |
type ipStackCapabilities struct { |
| 76 |
sync.Once // guards following |
| 77 |
ipv4Enabled bool |
| 78 |
ipv6Enabled bool |
| 79 |
ipv4MappedIPv6Enabled bool |
| 80 |
} |
| 81 |
|
| 82 |
//from https://github.com/golang/go |
| 83 |
var ipStackCaps ipStackCapabilities |
| 84 |
|
| 85 |
//from https://github.com/golang/go |
| 86 |
// supportsIPv4 reports whether the platform supports IPv4 networking |
| 87 |
// functionality. |
| 88 |
func supportsIPv4() bool { |
| 89 |
ipStackCaps.Once.Do(ipStackCaps.probe) |
| 90 |
return ipStackCaps.ipv4Enabled |
| 91 |
} |
| 92 |
|
| 93 |
//from https://github.com/golang/go |
| 94 |
// supportsIPv6 reports whether the platform supports IPv6 networking |
| 95 |
// functionality. |
| 96 |
func supportsIPv6() bool { |
| 97 |
ipStackCaps.Once.Do(ipStackCaps.probe) |
| 98 |
return ipStackCaps.ipv6Enabled |
| 99 |
} |
| 100 |
|
| 101 |
//from https://github.com/golang/go |
| 102 |
// supportsIPv4map reports whether the platform supports mapping an |
| 103 |
// IPv4 address inside an IPv6 address at transport layer |
| 104 |
// protocols. See RFC 4291, RFC 4038 and RFC 3493. |
| 105 |
func supportsIPv4map() bool { |
| 106 |
ipStackCaps.Once.Do(ipStackCaps.probe) |
| 107 |
return ipStackCaps.ipv4MappedIPv6Enabled |
| 108 |
} |
| 109 |
|
| 110 |
//from https://github.com/golang/go |
| 111 |
// Probe probes IPv4, IPv6 and IPv4-mapped IPv6 communication |
| 112 |
// capabilities which are controlled by the IPV6_V6ONLY socket option |
| 113 |
// and kernel configuration. |
| 114 |
// |
| 115 |
// Should we try to use the IPv4 socket interface if we're only |
| 116 |
// dealing with IPv4 sockets? As long as the host system understands |
| 117 |
// IPv4-mapped IPv6, it's okay to pass IPv4-mapeed IPv6 addresses to |
| 118 |
// the IPv6 interface. That simplifies our code and is most |
| 119 |
// general. Unfortunately, we need to run on kernels built without |
| 120 |
// IPv6 support too. So probe the kernel to figure it out. |
| 121 |
func (p *ipStackCapabilities) probe() { |
| 122 |
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) |
| 123 |
switch err { |
| 124 |
case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT: |
| 125 |
case nil: |
| 126 |
syscall.Close(s) |
| 127 |
p.ipv4Enabled = true |
| 128 |
} |
| 129 |
var probes = []struct { |
| 130 |
laddr net.TCPAddr |
| 131 |
value int |
| 132 |
}{ |
| 133 |
// IPv6 communication capability |
| 134 |
{laddr: net.TCPAddr{IP: net.IPv6loopback}, value: 1}, |
| 135 |
// IPv4-mapped IPv6 address communication capability |
| 136 |
{laddr: net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}, value: 0}, |
| 137 |
} |
| 138 |
|
| 139 |
for i := range probes { |
| 140 |
s, err := syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP) |
| 141 |
if err != nil { |
| 142 |
continue |
| 143 |
} |
| 144 |
defer syscall.Close(s) |
| 145 |
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value) |
| 146 |
sa, err := sockaddr(&(probes[i].laddr), syscall.AF_INET6) |
| 147 |
if err != nil { |
| 148 |
continue |
| 149 |
} |
| 150 |
if err := syscall.Bind(s, sa); err != nil { |
| 151 |
continue |
| 152 |
} |
| 153 |
if i == 0 { |
| 154 |
p.ipv6Enabled = true |
| 155 |
} else { |
| 156 |
p.ipv4MappedIPv6Enabled = true |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
//from https://github.com/golang/go |
| 162 |
//Change: we check the first IP address in the list of candidate SCTP IP addresses |
| 163 |
func (a *SCTPAddr) isWildcard() bool { |
| 164 |
if a == nil { |
| 165 |
return true |
| 166 |
} |
| 167 |
if 0 == len(a.IPAddrs) { |
| 168 |
return true |
| 169 |
} |
| 170 |
|
| 171 |
return a.IPAddrs[0].IP.IsUnspecified() |
| 172 |
} |
| 173 |
|
| 174 |
func (a *SCTPAddr) family() int { |
| 175 |
if a != nil { |
| 176 |
for _, ip := range a.IPAddrs { |
| 177 |
if ip.IP.To4() == nil { |
| 178 |
return syscall.AF_INET6 |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
return syscall.AF_INET |
| 183 |
} |
| 184 |
|
| 185 |
//from https://github.com/golang/go |
| 186 |
func favoriteAddrFamily(network string, laddr *SCTPAddr, raddr *SCTPAddr, mode string) (family int, ipv6only bool) { |
| 187 |
switch network[len(network)-1] { |
| 188 |
case '4': |
| 189 |
return syscall.AF_INET, false |
| 190 |
case '6': |
| 191 |
return syscall.AF_INET6, true |
| 192 |
} |
| 193 |
|
| 194 |
if mode == "listen" && (laddr == nil || laddr.isWildcard()) { |
| 195 |
if supportsIPv4map() || !supportsIPv4() { |
| 196 |
return syscall.AF_INET6, false |
| 197 |
} |
| 198 |
if laddr == nil { |
| 199 |
return syscall.AF_INET, false |
| 200 |
} |
| 201 |
return laddr.family(), false |
| 202 |
} |
| 203 |
|
| 204 |
if (laddr == nil || laddr.family() == syscall.AF_INET) && |
| 205 |
(raddr == nil || raddr.family() == syscall.AF_INET) { |
| 206 |
return syscall.AF_INET, false |
| 207 |
} |
| 208 |
return syscall.AF_INET6, false |
| 209 |
} |
| 210 |
|
| 211 |
//from https://github.com/golang/go |
| 212 |
//Changes: it is for SCTP only |
| 213 |
func setDefaultSockopts(s int, family int, ipv6only bool) error { |
| 214 |
if family == syscall.AF_INET6 { |
| 215 |
// Allow both IP versions even if the OS default |
| 216 |
// is otherwise. Note that some operating systems |
| 217 |
// never admit this option. |
| 218 |
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, boolint(ipv6only)) |
| 219 |
} |
| 220 |
// Allow broadcast. |
| 221 |
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)) |
| 222 |
} |