-
Notifications
You must be signed in to change notification settings - Fork 9
/
tfo_bsd+linux.go
205 lines (175 loc) · 4.54 KB
/
tfo_bsd+linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//go:build darwin || freebsd || linux
package tfo
import (
"context"
"errors"
"net"
"os"
"syscall"
"github.com/database64128/netx-go"
"golang.org/x/sys/unix"
)
func setIPv6Only(fd int, family int, ipv6only bool) error {
if family == unix.AF_INET6 {
// Allow both IP versions even if the OS default
// is otherwise. Note that some operating systems
// never admit this option.
return unix.SetsockoptInt(fd, unix.IPPROTO_IPV6, unix.IPV6_V6ONLY, boolint(ipv6only))
}
return nil
}
func setNoDelay(fd int, noDelay int) error {
return unix.SetsockoptInt(fd, unix.IPPROTO_TCP, unix.TCP_NODELAY, noDelay)
}
func ctrlNetwork(network string, family int) string {
if network == "tcp4" || family == unix.AF_INET {
return "tcp4"
}
return "tcp6"
}
func (d *Dialer) dialSingle(ctx context.Context, network string, laddr, raddr *net.TCPAddr, b []byte, ctrlCtxFn func(context.Context, string, string, syscall.RawConn) error) (*net.TCPConn, error) {
family, ipv6only := favoriteDialAddrFamily(network, laddr, raddr)
fd, err := d.socket(family)
if err != nil {
return nil, wrapSyscallError("socket", err)
}
if err = d.setIPv6Only(fd, family, ipv6only); err != nil {
unix.Close(fd)
return nil, os.NewSyscallError("setsockopt(IPV6_V6ONLY)", err)
}
if err = setNoDelay(fd, 1); err != nil {
unix.Close(fd)
return nil, os.NewSyscallError("setsockopt(TCP_NODELAY)", err)
}
if err = setTFODialerFromSocket(uintptr(fd)); err != nil {
if !d.Fallback || !errors.Is(err, errors.ErrUnsupported) {
unix.Close(fd)
return nil, os.NewSyscallError("setsockopt("+setTFODialerFromSocketSockoptName+")", err)
}
runtimeDialTFOSupport.storeNone()
}
f := os.NewFile(uintptr(fd), "")
defer f.Close()
rawConn, err := f.SyscallConn()
if err != nil {
return nil, err
}
if ctrlCtxFn != nil {
if err = ctrlCtxFn(ctx, ctrlNetwork(network, family), raddr.String(), rawConn); err != nil {
return nil, err
}
}
if laddr != nil {
lsa, err := unixSockaddrFromTCPAddr(laddr, family)
if err != nil {
return nil, err
}
if cErr := rawConn.Control(func(fd uintptr) {
err = unix.Bind(int(fd), lsa)
}); cErr != nil {
return nil, cErr
}
if err != nil {
return nil, wrapSyscallError("bind", err)
}
}
rsa, err := unixSockaddrFromTCPAddr(raddr, family)
if err != nil {
return nil, err
}
var (
n int
canFallback bool
)
if err = connWriteFunc(ctx, f, func(f *os.File) (err error) {
n, canFallback, err = connect(rawConn, rsa, b)
return err
}); err != nil {
if d.Fallback && canFallback {
runtimeDialTFOSupport.storeNone()
return d.dialAndWriteTCPConn(ctx, network, raddr.String(), b)
}
return nil, err
}
c, err := net.FileConn(f)
if err != nil {
return nil, err
}
if n < len(b) {
if err = netConnWriteBytes(ctx, c, b[n:]); err != nil {
c.Close()
return nil, err
}
}
return c.(*net.TCPConn), err
}
func unixSockaddrFromTCPAddr(a *net.TCPAddr, family int) (unix.Sockaddr, error) {
if a == nil {
return nil, nil
}
ip := a.IP
switch family {
case unix.AF_INET:
if len(ip) == 0 {
ip = net.IPv4zero
}
ip4 := ip.To4()
if ip4 == nil {
return nil, &net.AddrError{Err: "non-IPv4 address", Addr: ip.String()}
}
return &unix.SockaddrInet4{
Port: a.Port,
Addr: [4]byte(ip4),
}, nil
case unix.AF_INET6:
if len(ip) == 0 || ip.Equal(net.IPv4zero) {
ip = net.IPv6zero
}
ip6 := ip.To16()
if ip6 == nil {
return nil, &net.AddrError{Err: "non-IPv6 address", Addr: ip.String()}
}
return &unix.SockaddrInet6{
Port: a.Port,
ZoneId: uint32(netx.ZoneCache.Index(a.Zone)),
Addr: [16]byte(ip6),
}, nil
}
return nil, &net.AddrError{Err: "invalid address family", Addr: ip.String()}
}
func connect(rawConn syscall.RawConn, rsa unix.Sockaddr, b []byte) (n int, canFallback bool, err error) {
var done bool
if perr := rawConn.Write(func(fd uintptr) bool {
if done {
return true
}
n, err = doConnect(fd, rsa, b)
if err == unix.EINPROGRESS {
done = true
err = nil
return false
}
return true
}); perr != nil {
return 0, false, perr
}
if err != nil {
return 0, doConnectCanFallback(err), wrapSyscallError(connectSyscallName, err)
}
if perr := rawConn.Control(func(fd uintptr) {
err = getSocketError(int(fd), connectSyscallName)
}); perr != nil {
return 0, false, perr
}
return
}
func getSocketError(fd int, call string) error {
nerr, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_ERROR)
if err != nil {
return os.NewSyscallError("getsockopt", err)
}
if nerr != 0 {
return os.NewSyscallError(call, syscall.Errno(nerr))
}
return nil
}