annotate libgo/go/net/ipsock_posix.go @ 143:76e1cf5455ef

add cbc_gc test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 19:24:05 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
5 // +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris windows
111
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 package net
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 import (
kono
parents:
diff changeset
10 "context"
kono
parents:
diff changeset
11 "internal/poll"
kono
parents:
diff changeset
12 "runtime"
kono
parents:
diff changeset
13 "syscall"
kono
parents:
diff changeset
14 )
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 // Probe probes IPv4, IPv6 and IPv4-mapped IPv6 communication
kono
parents:
diff changeset
17 // capabilities which are controlled by the IPV6_V6ONLY socket option
kono
parents:
diff changeset
18 // and kernel configuration.
kono
parents:
diff changeset
19 //
kono
parents:
diff changeset
20 // Should we try to use the IPv4 socket interface if we're only
kono
parents:
diff changeset
21 // dealing with IPv4 sockets? As long as the host system understands
kono
parents:
diff changeset
22 // IPv4-mapped IPv6, it's okay to pass IPv4-mapeed IPv6 addresses to
kono
parents:
diff changeset
23 // the IPv6 interface. That simplifies our code and is most
kono
parents:
diff changeset
24 // general. Unfortunately, we need to run on kernels built without
kono
parents:
diff changeset
25 // IPv6 support too. So probe the kernel to figure it out.
kono
parents:
diff changeset
26 func (p *ipStackCapabilities) probe() {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
27 s, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
111
kono
parents:
diff changeset
28 switch err {
kono
parents:
diff changeset
29 case syscall.EAFNOSUPPORT, syscall.EPROTONOSUPPORT:
kono
parents:
diff changeset
30 case nil:
kono
parents:
diff changeset
31 poll.CloseFunc(s)
kono
parents:
diff changeset
32 p.ipv4Enabled = true
kono
parents:
diff changeset
33 }
kono
parents:
diff changeset
34 var probes = []struct {
kono
parents:
diff changeset
35 laddr TCPAddr
kono
parents:
diff changeset
36 value int
kono
parents:
diff changeset
37 }{
kono
parents:
diff changeset
38 // IPv6 communication capability
kono
parents:
diff changeset
39 {laddr: TCPAddr{IP: ParseIP("::1")}, value: 1},
kono
parents:
diff changeset
40 // IPv4-mapped IPv6 address communication capability
kono
parents:
diff changeset
41 {laddr: TCPAddr{IP: IPv4(127, 0, 0, 1)}, value: 0},
kono
parents:
diff changeset
42 }
kono
parents:
diff changeset
43 switch runtime.GOOS {
kono
parents:
diff changeset
44 case "dragonfly", "openbsd":
kono
parents:
diff changeset
45 // The latest DragonFly BSD and OpenBSD kernels don't
kono
parents:
diff changeset
46 // support IPV6_V6ONLY=0. They always return an error
kono
parents:
diff changeset
47 // and we don't need to probe the capability.
kono
parents:
diff changeset
48 probes = probes[:1]
kono
parents:
diff changeset
49 }
kono
parents:
diff changeset
50 for i := range probes {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
51 s, err := sysSocket(syscall.AF_INET6, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
111
kono
parents:
diff changeset
52 if err != nil {
kono
parents:
diff changeset
53 continue
kono
parents:
diff changeset
54 }
kono
parents:
diff changeset
55 defer poll.CloseFunc(s)
kono
parents:
diff changeset
56 syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, probes[i].value)
kono
parents:
diff changeset
57 sa, err := probes[i].laddr.sockaddr(syscall.AF_INET6)
kono
parents:
diff changeset
58 if err != nil {
kono
parents:
diff changeset
59 continue
kono
parents:
diff changeset
60 }
kono
parents:
diff changeset
61 if err := syscall.Bind(s, sa); err != nil {
kono
parents:
diff changeset
62 continue
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64 if i == 0 {
kono
parents:
diff changeset
65 p.ipv6Enabled = true
kono
parents:
diff changeset
66 } else {
kono
parents:
diff changeset
67 p.ipv4MappedIPv6Enabled = true
kono
parents:
diff changeset
68 }
kono
parents:
diff changeset
69 }
kono
parents:
diff changeset
70 }
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 // favoriteAddrFamily returns the appropriate address family for the
kono
parents:
diff changeset
73 // given network, laddr, raddr and mode.
kono
parents:
diff changeset
74 //
kono
parents:
diff changeset
75 // If mode indicates "listen" and laddr is a wildcard, we assume that
kono
parents:
diff changeset
76 // the user wants to make a passive-open connection with a wildcard
kono
parents:
diff changeset
77 // address family, both AF_INET and AF_INET6, and a wildcard address
kono
parents:
diff changeset
78 // like the following:
kono
parents:
diff changeset
79 //
kono
parents:
diff changeset
80 // - A listen for a wildcard communication domain, "tcp" or
kono
parents:
diff changeset
81 // "udp", with a wildcard address: If the platform supports
kono
parents:
diff changeset
82 // both IPv6 and IPv4-mapped IPv6 communication capabilities,
kono
parents:
diff changeset
83 // or does not support IPv4, we use a dual stack, AF_INET6 and
kono
parents:
diff changeset
84 // IPV6_V6ONLY=0, wildcard address listen. The dual stack
kono
parents:
diff changeset
85 // wildcard address listen may fall back to an IPv6-only,
kono
parents:
diff changeset
86 // AF_INET6 and IPV6_V6ONLY=1, wildcard address listen.
kono
parents:
diff changeset
87 // Otherwise we prefer an IPv4-only, AF_INET, wildcard address
kono
parents:
diff changeset
88 // listen.
kono
parents:
diff changeset
89 //
kono
parents:
diff changeset
90 // - A listen for a wildcard communication domain, "tcp" or
kono
parents:
diff changeset
91 // "udp", with an IPv4 wildcard address: same as above.
kono
parents:
diff changeset
92 //
kono
parents:
diff changeset
93 // - A listen for a wildcard communication domain, "tcp" or
kono
parents:
diff changeset
94 // "udp", with an IPv6 wildcard address: same as above.
kono
parents:
diff changeset
95 //
kono
parents:
diff changeset
96 // - A listen for an IPv4 communication domain, "tcp4" or "udp4",
kono
parents:
diff changeset
97 // with an IPv4 wildcard address: We use an IPv4-only, AF_INET,
kono
parents:
diff changeset
98 // wildcard address listen.
kono
parents:
diff changeset
99 //
kono
parents:
diff changeset
100 // - A listen for an IPv6 communication domain, "tcp6" or "udp6",
kono
parents:
diff changeset
101 // with an IPv6 wildcard address: We use an IPv6-only, AF_INET6
kono
parents:
diff changeset
102 // and IPV6_V6ONLY=1, wildcard address listen.
kono
parents:
diff changeset
103 //
kono
parents:
diff changeset
104 // Otherwise guess: If the addresses are IPv4 then returns AF_INET,
kono
parents:
diff changeset
105 // or else returns AF_INET6. It also returns a boolean value what
kono
parents:
diff changeset
106 // designates IPV6_V6ONLY option.
kono
parents:
diff changeset
107 //
kono
parents:
diff changeset
108 // Note that the latest DragonFly BSD and OpenBSD kernels allow
kono
parents:
diff changeset
109 // neither "net.inet6.ip6.v6only=1" change nor IPPROTO_IPV6 level
kono
parents:
diff changeset
110 // IPV6_V6ONLY socket option setting.
kono
parents:
diff changeset
111 func favoriteAddrFamily(network string, laddr, raddr sockaddr, mode string) (family int, ipv6only bool) {
kono
parents:
diff changeset
112 switch network[len(network)-1] {
kono
parents:
diff changeset
113 case '4':
kono
parents:
diff changeset
114 return syscall.AF_INET, false
kono
parents:
diff changeset
115 case '6':
kono
parents:
diff changeset
116 return syscall.AF_INET6, true
kono
parents:
diff changeset
117 }
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 if mode == "listen" && (laddr == nil || laddr.isWildcard()) {
kono
parents:
diff changeset
120 if supportsIPv4map() || !supportsIPv4() {
kono
parents:
diff changeset
121 return syscall.AF_INET6, false
kono
parents:
diff changeset
122 }
kono
parents:
diff changeset
123 if laddr == nil {
kono
parents:
diff changeset
124 return syscall.AF_INET, false
kono
parents:
diff changeset
125 }
kono
parents:
diff changeset
126 return laddr.family(), false
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 if (laddr == nil || laddr.family() == syscall.AF_INET) &&
kono
parents:
diff changeset
130 (raddr == nil || raddr.family() == syscall.AF_INET) {
kono
parents:
diff changeset
131 return syscall.AF_INET, false
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133 return syscall.AF_INET6, false
kono
parents:
diff changeset
134 }
kono
parents:
diff changeset
135
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
136 func internetSocket(ctx context.Context, net string, laddr, raddr sockaddr, sotype, proto int, mode string, ctrlFn func(string, string, syscall.RawConn) error) (fd *netFD, err error) {
111
kono
parents:
diff changeset
137 if (runtime.GOOS == "windows" || runtime.GOOS == "openbsd" || runtime.GOOS == "nacl") && mode == "dial" && raddr.isWildcard() {
kono
parents:
diff changeset
138 raddr = raddr.toLocal(net)
kono
parents:
diff changeset
139 }
kono
parents:
diff changeset
140 family, ipv6only := favoriteAddrFamily(net, laddr, raddr, mode)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
141 return socket(ctx, net, family, sotype, proto, ipv6only, laddr, raddr, ctrlFn)
111
kono
parents:
diff changeset
142 }
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 func ipToSockaddr(family int, ip IP, port int, zone string) (syscall.Sockaddr, error) {
kono
parents:
diff changeset
145 switch family {
kono
parents:
diff changeset
146 case syscall.AF_INET:
kono
parents:
diff changeset
147 if len(ip) == 0 {
kono
parents:
diff changeset
148 ip = IPv4zero
kono
parents:
diff changeset
149 }
kono
parents:
diff changeset
150 ip4 := ip.To4()
kono
parents:
diff changeset
151 if ip4 == nil {
kono
parents:
diff changeset
152 return nil, &AddrError{Err: "non-IPv4 address", Addr: ip.String()}
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154 sa := &syscall.SockaddrInet4{Port: port}
kono
parents:
diff changeset
155 copy(sa.Addr[:], ip4)
kono
parents:
diff changeset
156 return sa, nil
kono
parents:
diff changeset
157 case syscall.AF_INET6:
kono
parents:
diff changeset
158 // In general, an IP wildcard address, which is either
kono
parents:
diff changeset
159 // "0.0.0.0" or "::", means the entire IP addressing
kono
parents:
diff changeset
160 // space. For some historical reason, it is used to
kono
parents:
diff changeset
161 // specify "any available address" on some operations
kono
parents:
diff changeset
162 // of IP node.
kono
parents:
diff changeset
163 //
kono
parents:
diff changeset
164 // When the IP node supports IPv4-mapped IPv6 address,
kono
parents:
diff changeset
165 // we allow an listener to listen to the wildcard
kono
parents:
diff changeset
166 // address of both IP addressing spaces by specifying
kono
parents:
diff changeset
167 // IPv6 wildcard address.
kono
parents:
diff changeset
168 if len(ip) == 0 || ip.Equal(IPv4zero) {
kono
parents:
diff changeset
169 ip = IPv6zero
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171 // We accept any IPv6 address including IPv4-mapped
kono
parents:
diff changeset
172 // IPv6 address.
kono
parents:
diff changeset
173 ip6 := ip.To16()
kono
parents:
diff changeset
174 if ip6 == nil {
kono
parents:
diff changeset
175 return nil, &AddrError{Err: "non-IPv6 address", Addr: ip.String()}
kono
parents:
diff changeset
176 }
kono
parents:
diff changeset
177 sa := &syscall.SockaddrInet6{Port: port, ZoneId: uint32(zoneCache.index(zone))}
kono
parents:
diff changeset
178 copy(sa.Addr[:], ip6)
kono
parents:
diff changeset
179 return sa, nil
kono
parents:
diff changeset
180 }
kono
parents:
diff changeset
181 return nil, &AddrError{Err: "invalid address family", Addr: ip.String()}
kono
parents:
diff changeset
182 }