-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdivert.go
207 lines (166 loc) · 4.83 KB
/
divert.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
206
207
//go:build windows
// +build windows
package divert
import (
"strconv"
"strings"
"sync"
"unsafe"
"golang.org/x/sys/windows"
)
var once = sync.Once{}
// GerVersionInfo is ...
func GetVersionInfo() (ver string, err error) {
h, err := Open("false", LayerNetwork, PriorityDefault, FlagDefault)
if err != nil {
return
}
defer func() {
err = h.Close()
}()
major, err := h.GetParam(VersionMajor)
if err != nil {
return
}
minor, err := h.GetParam(VersionMinor)
if err != nil {
return
}
ver = strings.Join([]string{strconv.Itoa(int(major)), strconv.Itoa(int(minor))}, ".")
return
}
func ioControlEx(h windows.Handle, code CtlCode, ioctl unsafe.Pointer, buf *byte, bufLen uint32, overlapped *windows.Overlapped) (iolen uint32, err error) {
err = windows.DeviceIoControl(h, uint32(code), (*byte)(ioctl), uint32(unsafe.Sizeof(ioCtl{})), buf, bufLen, &iolen, overlapped)
if err != windows.ERROR_IO_PENDING {
return
}
err = windows.GetOverlappedResult(h, overlapped, &iolen, true)
return
}
func ioControl(h windows.Handle, code CtlCode, ioctl unsafe.Pointer, buf *byte, bufLen uint32) (iolen uint32, err error) {
event, _ := windows.CreateEvent(nil, 0, 0, nil)
overlapped := windows.Overlapped{
HEvent: event,
}
iolen, err = ioControlEx(h, code, ioctl, buf, bufLen, &overlapped)
windows.CloseHandle(event)
return
}
// Handle is ...
type Handle struct {
sync.Mutex
windows.Handle
rOverlapped windows.Overlapped
wOverlapped windows.Overlapped
}
// Recv is ...
func (h *Handle) Recv(buffer []byte, address *Address) (uint, error) {
addrLen := uint(unsafe.Sizeof(Address{}))
recv := recv{
Addr: uint64(uintptr(unsafe.Pointer(address))),
AddrLenPtr: uint64(uintptr(unsafe.Pointer(&addrLen))),
}
iolen, err := ioControlEx(h.Handle, ioCtlRecv, unsafe.Pointer(&recv), &buffer[0], uint32(len(buffer)), &h.rOverlapped)
if err != nil {
return uint(iolen), Error(err.(windows.Errno))
}
return uint(iolen), nil
}
// RecvEx is ...
func (h *Handle) RecvEx(buffer []byte, address []Address) (uint, uint, error) {
addrLen := uint(len(address)) * uint(unsafe.Sizeof(Address{}))
recv := recv{
Addr: uint64(uintptr(unsafe.Pointer(&address[0]))),
AddrLenPtr: uint64(uintptr(unsafe.Pointer(&addrLen))),
}
iolen, err := ioControlEx(h.Handle, ioCtlRecv, unsafe.Pointer(&recv), &buffer[0], uint32(len(buffer)), &h.rOverlapped)
if err != nil {
return uint(iolen), addrLen / uint(unsafe.Sizeof(Address{})), Error(err.(windows.Errno))
}
return uint(iolen), addrLen / uint(unsafe.Sizeof(Address{})), nil
}
// Send is ...
func (h *Handle) Send(buffer []byte, address *Address) (uint, error) {
send := send{
Addr: uint64(uintptr(unsafe.Pointer(address))),
AddrLen: uint64(unsafe.Sizeof(Address{})),
}
iolen, err := ioControlEx(h.Handle, ioCtlSend, unsafe.Pointer(&send), &buffer[0], uint32(len(buffer)), &h.wOverlapped)
if err != nil {
return uint(iolen), Error(err.(windows.Errno))
}
return uint(iolen), nil
}
// SendEx is ...
func (h *Handle) SendEx(buffer []byte, address []Address) (uint, error) {
send := send{
Addr: uint64(uintptr(unsafe.Pointer(&address[0]))),
AddrLen: uint64(unsafe.Sizeof(Address{})) * uint64(len(address)),
}
iolen, err := ioControlEx(h.Handle, ioCtlSend, unsafe.Pointer(&send), &buffer[0], uint32(len(buffer)), &h.wOverlapped)
if err != nil {
return uint(iolen), Error(err.(windows.Errno))
}
return uint(iolen), nil
}
// Shutdown is ...
func (h *Handle) Shutdown(how Shutdown) error {
shutdown := shutdown{
How: uint32(how),
}
_, err := ioControl(h.Handle, ioCtlShutdown, unsafe.Pointer(&shutdown), nil, 0)
if err != nil {
return Error(err.(windows.Errno))
}
return nil
}
// Close is ...
func (h *Handle) Close() error {
windows.CloseHandle(h.rOverlapped.HEvent)
windows.CloseHandle(h.wOverlapped.HEvent)
err := windows.CloseHandle(h.Handle)
if err != nil {
return Error(err.(windows.Errno))
}
return nil
}
// GetParam is ...
func (h *Handle) GetParam(p Param) (uint64, error) {
getParam := getParam{
Param: uint32(p),
Value: 0,
}
_, err := ioControl(h.Handle, ioCtlGetParam, unsafe.Pointer(&getParam), (*byte)(unsafe.Pointer(&getParam.Value)), uint32(unsafe.Sizeof(getParam.Value)))
if err != nil {
return getParam.Value, Error(err.(windows.Errno))
}
return getParam.Value, nil
}
// SetParam is ...
func (h *Handle) SetParam(p Param, v uint64) error {
switch p {
case QueueLength:
if v < QueueLengthMin || v > QueueLengthMax {
return errQueueLength
}
case QueueTime:
if v < QueueTimeMin || v > QueueTimeMax {
return errQueueTime
}
case QueueSize:
if v < QueueSizeMin || v > QueueSizeMax {
return errQueueSize
}
default:
return errQueueParam
}
setParam := setParam{
Value: v,
Param: uint32(p),
}
_, err := ioControl(h.Handle, ioCtlSetParam, unsafe.Pointer(&setParam), nil, 0)
if err != nil {
return Error(err.(windows.Errno))
}
return nil
}