-
Notifications
You must be signed in to change notification settings - Fork 9
/
tfo_windows_checklinkname0.go
185 lines (159 loc) · 4.65 KB
/
tfo_windows_checklinkname0.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
//go:build windows && (!go1.23 || (go1.23 && tfogo_checklinkname0))
package tfo
import (
"context"
"errors"
"net"
"os"
"runtime"
"syscall"
"unsafe"
"github.com/database64128/netx-go"
"golang.org/x/sys/windows"
)
func setIPv6Only(fd windows.Handle, family int, ipv6only bool) error {
if family == windows.AF_INET6 {
// Allow both IP versions even if the OS default
// is otherwise. Note that some operating systems
// never admit this option.
return windows.SetsockoptInt(fd, windows.IPPROTO_IPV6, windows.IPV6_V6ONLY, boolint(ipv6only))
}
return nil
}
func setNoDelay(fd windows.Handle, noDelay int) error {
return windows.SetsockoptInt(fd, windows.IPPROTO_TCP, windows.TCP_NODELAY, noDelay)
}
func setUpdateConnectContext(fd windows.Handle) error {
return windows.Setsockopt(fd, windows.SOL_SOCKET, windows.SO_UPDATE_CONNECT_CONTEXT, nil, 0)
}
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)
lsa, err := windowsSockaddrFromTCPAddr(laddr, family)
if err != nil {
return nil, err
}
if lsa == nil {
// ConnectEx requires a bound socket.
switch family {
case windows.AF_INET:
lsa = &windows.SockaddrInet4{}
case windows.AF_INET6:
lsa = &windows.SockaddrInet6{}
}
}
rsa, err := windowsSockaddrFromTCPAddr(raddr, family)
if err != nil {
return nil, err
}
handle, err := windows.WSASocket(int32(family), windows.SOCK_STREAM, windows.IPPROTO_TCP, nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
if err != nil {
return nil, os.NewSyscallError("WSASocket", err)
}
fd, err := newFD(handle, family, windows.SOCK_STREAM, network)
if err != nil {
windows.Closesocket(handle)
return nil, err
}
if err = setIPv6Only(handle, family, ipv6only); err != nil {
fd.Close()
return nil, os.NewSyscallError("setsockopt(IPV6_V6ONLY)", err)
}
if err = setNoDelay(handle, 1); err != nil {
fd.Close()
return nil, os.NewSyscallError("setsockopt(TCP_NODELAY)", err)
}
if err = setTFODialer(uintptr(handle)); err != nil {
if !d.Fallback || !errors.Is(err, errors.ErrUnsupported) {
fd.Close()
return nil, os.NewSyscallError("setsockopt(TCP_FASTOPEN)", err)
}
runtimeDialTFOSupport.storeNone()
}
if ctrlCtxFn != nil {
if err = ctrlCtxFn(ctx, fd.ctrlNetwork(), raddr.String(), newRawConn(fd)); err != nil {
fd.Close()
return nil, err
}
}
if err = windows.Bind(handle, lsa); err != nil {
fd.Close()
return nil, wrapSyscallError("bind", err)
}
if err = fd.init(); err != nil {
fd.Close()
return nil, err
}
if err = connWriteFunc(ctx, fd, func(fd *netFD) error {
n, err := fd.pfd.ConnectEx(rsa, b)
if err != nil {
return wrapSyscallError("connectex", err)
}
if err = setUpdateConnectContext(handle); err != nil {
return os.NewSyscallError("setsockopt(SO_UPDATE_CONNECT_CONTEXT)", err)
}
lsa, err = windows.Getsockname(handle)
if err != nil {
return wrapSyscallError("getsockname", err)
}
fd.laddr = tcpAddrFromWindowsSockaddr(lsa)
rsa, err = windows.Getpeername(handle)
if err != nil {
return wrapSyscallError("getpeername", err)
}
fd.raddr = tcpAddrFromWindowsSockaddr(rsa)
if n < len(b) {
if _, err = fd.Write(b[n:]); err != nil {
return err
}
}
return nil
}); err != nil {
fd.Close()
return nil, err
}
runtime.SetFinalizer(fd, netFDClose)
return (*net.TCPConn)(unsafe.Pointer(&fd)), nil
}
func windowsSockaddrFromTCPAddr(a *net.TCPAddr, family int) (windows.Sockaddr, error) {
if a == nil {
return nil, nil
}
ip := a.IP
switch family {
case windows.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 &windows.SockaddrInet4{
Port: a.Port,
Addr: [4]byte(ip4),
}, nil
case windows.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 &windows.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 tcpAddrFromWindowsSockaddr(sa windows.Sockaddr) *net.TCPAddr {
switch sa := sa.(type) {
case *windows.SockaddrInet4:
return &net.TCPAddr{IP: sa.Addr[0:], Port: sa.Port}
case *windows.SockaddrInet6:
return &net.TCPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: netx.ZoneCache.Name(int(sa.ZoneId))}
}
return nil
}