forked from database64128/tfo-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tfo_windows.go
143 lines (118 loc) · 3.43 KB
/
tfo_windows.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
package tfo
import (
"context"
"errors"
"net"
"os"
"runtime"
"syscall"
"unsafe"
"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) {
ltsa := (*tcpSockaddr)(laddr)
rtsa := (*tcpSockaddr)(raddr)
family, ipv6only := favoriteAddrFamily(network, ltsa, rtsa, "dial")
var (
ip net.IP
port int
zone string
)
if laddr != nil {
ip = laddr.IP
port = laddr.Port
zone = laddr.Zone
}
lsa, err := ipToSockaddr(family, ip, port, zone)
if err != nil {
return nil, err
}
rsa, err := rtsa.sockaddr(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(syscall.Handle(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, wrapSyscallError("setsockopt(IPV6_V6ONLY)", err)
}
if err = setNoDelay(handle, 1); err != nil {
fd.Close()
return nil, wrapSyscallError("setsockopt(TCP_NODELAY)", err)
}
if err = setTFODialer(uintptr(handle)); err != nil {
if !d.Fallback || !errors.Is(err, ErrUnsupported) {
fd.Close()
return nil, wrapSyscallError("setsockopt(TCP_FASTOPEN)", err)
}
runtimeDialTFOSupport.storeNone()
}
if ctrlCtxFn != nil {
rawConn, _ := fd.SyscallConn()
if err = ctrlCtxFn(ctx, fd.ctrlNetwork(), raddr.String(), rawConn); err != nil {
fd.Close()
return nil, err
}
}
if err = syscall.Bind(syscall.Handle(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 os.NewSyscallError("connectex", err)
}
if err = setUpdateConnectContext(handle); err != nil {
return wrapSyscallError("setsockopt(SO_UPDATE_CONNECT_CONTEXT)", err)
}
lsa, err = syscall.Getsockname(syscall.Handle(handle))
if err != nil {
return wrapSyscallError("getsockname", err)
}
fd.laddr = sockaddrToTCP(lsa)
rsa, err = syscall.Getpeername(syscall.Handle(handle))
if err != nil {
return wrapSyscallError("getpeername", err)
}
fd.raddr = sockaddrToTCP(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
}