Source file
src/net/ipsock_posix.go
1
2
3
4
5
6
7 package net
8
9 import (
10 "context"
11 "internal/poll"
12 "net/netip"
13 "runtime"
14 "syscall"
15 _ "unsafe"
16 )
17
18
19
20
21
22
23
24
25
26
27
28 func (p *ipStackCapabilities) probe() {
29 switch runtime.GOOS {
30 case "js", "wasip1":
31
32 p.ipv4Enabled = true
33 p.ipv6Enabled = true
34 p.ipv4MappedIPv6Enabled = true
35 return
36 }
37
38 s, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
39 switch err {
40 case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
41 case nil:
42 poll.CloseFunc(s)
43 p.ipv4Enabled = true
44 }
45 var probes = []struct {
46 laddr TCPAddr
47 value int
48 }{
49
50 {laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
51
52 {laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
53 }
54 switch runtime.GOOS {
55 case "dragonfly", "openbsd":
56
57
58
59 probes = probes[:1]
60 }
61 for i := range probes {
62 s, err := sysSocket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
63 if err != nil {
64 continue
65 }
66 defer poll.CloseFunc(s)
67 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
68 sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
69 if err != nil {
70 continue
71 }
72 if err := syscall.Bind(s, sa); err != nil {
73 continue
74 }
75 if i == 0 {
76 p.ipv6Enabled = true
77 } else {
78 p.ipv4MappedIPv6Enabled = true
79 }
80 }
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 func favoriteAddrFamily(network string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
135 switch network[len(network)-1] {
136 case '4':
137 return syscall.AF_INET, false
138 case '6':
139 return syscall.AF_INET6, true
140 }
141
142 if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
143 if supportsIPv4map() || !supportsIPv4() {
144 return syscall.AF_INET6, false
145 }
146 if laddr == nil {
147 return syscall.AF_INET, false
148 }
149 return laddr.family(), false
150 }
151
152 if (laddr == nil || laddr.family() == syscall.AF_INET) &&
153 (raddr == nil || raddr.family() == syscall.AF_INET) {
154 return syscall.AF_INET, false
155 }
156 return syscall.AF_INET6, false
157 }
158
159 func internetSocket(ctx context.Context, net string, laddr, raddr sockaddr, sotype, proto int, mode string, ctrlCtxFn func(context.Context, string, string, syscall.RawConn) error) (fd *netFD, err error) {
160 switch runtime.GOOS {
161 case "aix", "windows", "openbsd", "js", "wasip1":
162 if mode == "dial" && raddr.isWildcard() {
163 raddr = raddr.toLocal(net)
164 }
165 }
166 family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
167 return socket(ctx, net, family, sotype, proto, ipv6only, laddr, raddr, ctrlCtxFn)
168 }
169
170 func ipToSockaddrInet4(ip IP, port int) (syscall.SockaddrInet4, error) {
171 if len(ip) == 0 {
172 ip = IPv4zero
173 }
174 ip4 := ip.To4()
175 if ip4 == nil {
176 return syscall.SockaddrInet4{}, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
177 }
178 sa := syscall.SockaddrInet4{Port: port}
179 copy(sa.Addr[:], ip4)
180 return sa, nil
181 }
182
183 func ipToSockaddrInet6(ip IP, port int, zone string) (syscall.SockaddrInet6, error) {
184
185
186
187
188
189
190
191
192
193
194 if len(ip) == 0 || ip.Equal(IPv4zero) {
195 ip = IPv6zero
196 }
197
198
199 ip6 := ip.To16()
200 if ip6 == nil {
201 return syscall.SockaddrInet6{}, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
202 }
203 sa := syscall.SockaddrInet6{Port: port, ZoneId: uint32(zoneCache.index(zone))}
204 copy(sa.Addr[:], ip6)
205 return sa, nil
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219 func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
220 switch family {
221 case syscall.AF_INET:
222 sa, err := ipToSockaddrInet4(ip, port)
223 if err != nil {
224 return nil, err
225 }
226 return &sa, nil
227 case syscall.AF_INET6:
228 sa, err := ipToSockaddrInet6(ip, port, zone)
229 if err != nil {
230 return nil, err
231 }
232 return &sa, nil
233 }
234 return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
235 }
236
237 func addrPortToSockaddrInet4(ap netip.AddrPort) (syscall.SockaddrInet4, error) {
238
239
240
241
242
243
244 addr := ap.Addr()
245 if !addr.Is4() && !addr.Is4In6() {
246 return syscall.SockaddrInet4{}, &AddrError{Err: "non-IPv4 address", Addr: addr.String()}
247 }
248 sa := syscall.SockaddrInet4{
249 Addr: addr.As4(),
250 Port: int(ap.Port()),
251 }
252 return sa, nil
253 }
254
255 func addrPortToSockaddrInet6(ap netip.AddrPort) (syscall.SockaddrInet6, error) {
256
257
258
259
260
261
262 addr := ap.Addr()
263 if !addr.IsValid() {
264 return syscall.SockaddrInet6{}, &AddrError{Err: "non-IPv6 address", Addr: addr.String()}
265 }
266 sa := syscall.SockaddrInet6{
267 Addr: addr.As16(),
268 Port: int(ap.Port()),
269 ZoneId: uint32(zoneCache.index(addr.Zone())),
270 }
271 return sa, nil
272 }
273
View as plain text