This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathos_windows.go
317 lines (290 loc) · 8.11 KB
/
os_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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package dnssd
import (
"sync"
"syscall"
"unsafe"
)
var lib struct {
dll map[string]*syscall.DLL
proc map[string]map[string]*syscall.Proc
}
func getDLL(name string) (*syscall.DLL, error) {
if dll, present := lib.dll[name]; present {
return dll, nil
}
dll, err := syscall.LoadDLL(name)
if err != nil {
return nil, err
}
if lib.dll == nil {
lib.dll = make(map[string]*syscall.DLL)
}
lib.dll[name] = dll
if lib.proc == nil {
lib.proc = make(map[string]map[string]*syscall.Proc)
}
lib.proc[name] = make(map[string]*syscall.Proc)
return dll, nil
}
func getProc(dllName, procName string) (*syscall.Proc, error) {
if proc, present := lib.proc[dllName][procName]; present {
return proc, nil
}
dll, err := getDLL(dllName)
if err != nil {
return nil, err
}
proc, err := dll.FindProc(procName)
if err != nil {
return nil, err
}
lib.proc[dllName][procName] = proc
return proc, nil
}
func mustGetProc(dllName, procName string) *syscall.Proc {
proc, err := getProc(dllName, procName)
if err != nil {
panic(err)
}
return proc
}
func browseStart(ref *uintptr, flags, ifIndex uint32, typ, domain string, ctx unsafe.Pointer) error {
proc, err := getProc("dnssd.dll", "DNSServiceBrowse")
if err != nil {
return err
}
btyp, err := syscall.BytePtrFromString(typ)
if err != nil {
return err
}
bdomain, err := syscall.BytePtrFromString(domain)
if err != nil {
return err
}
r, _, _ := proc.Call(
(uintptr)(unsafe.Pointer(ref)),
uintptr(flags),
uintptr(ifIndex),
(uintptr)(unsafe.Pointer(btyp)),
(uintptr)(unsafe.Pointer(bdomain)),
syscall.NewCallback(browseCallbackWrapper),
(uintptr)(ctx),
)
return getError(int32(r))
}
func browseCallbackWrapper(sdRef unsafe.Pointer, flags, interfaceIndex uint, err int, name, stype, domain unsafe.Pointer, ctx unsafe.Pointer) uintptr {
dnssdBrowseCallback(sdRef, uint32(flags), uint32(interfaceIndex), int32(err), name, stype, domain, ctx)
return 0
}
func resolveStart(ref *uintptr, flags, ifIndex uint32, name, typ, domain string, ctx unsafe.Pointer) error {
proc, err := getProc("dnssd.dll", "DNSServiceResolve")
if err != nil {
return err
}
bname, err := syscall.BytePtrFromString(name)
if err != nil {
return err
}
btyp, err := syscall.BytePtrFromString(typ)
if err != nil {
return err
}
bdomain, err := syscall.BytePtrFromString(domain)
if err != nil {
return err
}
r, _, _ := proc.Call(
(uintptr)(unsafe.Pointer(ref)),
uintptr(flags),
uintptr(ifIndex),
uintptr(unsafe.Pointer(bname)),
uintptr(unsafe.Pointer(btyp)),
uintptr(unsafe.Pointer(bdomain)),
syscall.NewCallback(dnssdResolveCallbackWrapper),
uintptr(ctx),
)
return getError(int32(r))
}
func dnssdResolveCallbackWrapper(sdRef unsafe.Pointer, flags, interfaceIndex uint, err int, fullname, hosttarget unsafe.Pointer, port uint, txtLen uint, txtRecord, ctx unsafe.Pointer) uintptr {
dnssdResolveCallback(sdRef, uint32(flags), uint32(interfaceIndex), int32(err), fullname, hosttarget, syscall.Ntohs(uint16(port)), uint16(txtLen), txtRecord, ctx)
return 0
}
func registerStart(ref *uintptr, flags, ifIndex uint32, name, typ, domain, host string, port int, txt []byte, ctx unsafe.Pointer) error {
proc, err := getProc("dnssd.dll", "DNSServiceRegister")
if err != nil {
return err
}
bname, err := syscall.BytePtrFromString(name)
if err != nil {
return err
}
btyp, err := syscall.BytePtrFromString(typ)
if err != nil {
return err
}
bdomain, err := syscall.BytePtrFromString(domain)
if err != nil {
return err
}
bhost, err := syscall.BytePtrFromString(host)
if err != nil {
return err
}
txtLen := uintptr(len(txt))
txtPtr := unsafe.Pointer(nil)
if txtLen > 0 {
txtPtr = unsafe.Pointer(&txt[0])
}
port16 := uint16(port)
r, _, _ := proc.Call(
(uintptr)(unsafe.Pointer(ref)),
uintptr(flags),
uintptr(ifIndex),
(uintptr)(unsafe.Pointer(bname)),
(uintptr)(unsafe.Pointer(btyp)),
(uintptr)(unsafe.Pointer(bdomain)),
(uintptr)(unsafe.Pointer(bhost)),
uintptr(port16<<8|port16>>8),
txtLen,
(uintptr)(txtPtr),
syscall.NewCallback(registerCallbackWrapper),
uintptr(ctx),
)
return getError(int32(r))
}
func registerCallbackWrapper(sdRef unsafe.Pointer, flags uint, err int, name, regtype, domain, ctx unsafe.Pointer) uintptr {
dnssdRegisterCallback(sdRef, uint32(flags), int32(err), name, regtype, domain, ctx)
return 0
}
func queryStart(ref *uintptr, flags, ifIndex uint32, name string, rrtype, rrclass uint16, ctx unsafe.Pointer) error {
proc, err := getProc("dnssd.dll", "DNSServiceQueryRecord")
if err != nil {
return err
}
bname, err := syscall.BytePtrFromString(name)
if err != nil {
return err
}
r, _, _ := proc.Call(
(uintptr)(unsafe.Pointer(ref)),
uintptr(flags),
uintptr(ifIndex),
(uintptr)(unsafe.Pointer(bname)),
uintptr(rrtype),
uintptr(rrclass),
syscall.NewCallback(queryCallbackWrapper),
(uintptr)(ctx),
)
return getError(int32(r))
}
func queryCallbackWrapper(sdRef unsafe.Pointer, flags, ifIndex uint, err int, fullname unsafe.Pointer, rrtype, rrclass, rdlen uint, rdataptr unsafe.Pointer, ttl uint, ctx unsafe.Pointer) uintptr {
dnssdQueryCallback(sdRef, uint32(flags), uint32(ifIndex), int32(err), fullname, uint16(rrtype), uint16(rrclass), uint16(rdlen), rdataptr, uint32(ttl), ctx)
return 0
}
func refSockFd(ref *uintptr) int {
proc := mustGetProc("dnssd.dll", "DNSServiceRefSockFD")
fd, _, _ := proc.Call(*ref)
return int(fd)
}
func platformDeallocateRef(ref *uintptr) {
proc := mustGetProc("dnssd.dll", "DNSServiceRefDeallocate")
_, _, _ = proc.Call(*ref)
}
func createConnection(ref *uintptr) error {
proc, err := getProc("dnssd.dll", "DNSServiceCreateConnection")
if err != nil {
return err
}
e, _, _ := proc.Call((uintptr)(unsafe.Pointer(ref)))
return getError(int32(e))
}
type platformPollServerState struct {
event uintptr
once sync.Once
}
func (s *pollServerState) stopPoll() {
if s.event != 0 {
mustGetProc("ws2_32.dll", "WSASetEvent").Call(s.event)
}
}
func (s *pollServerState) startPoll() {
s.m.internal.Lock()
s.once.Do(func() {
s.event = createEvent()
})
go pollLoop(s)
}
func pollLoop(s *pollServerState) {
defer s.m.internal.Unlock()
sharedPollables, uniquePollables := s.sharedAndUniquePollables()
events := []uintptr{s.event}
for i := range uniquePollables {
events = append(events, createFdEvent(uniquePollables[i].fd))
}
if s.shared.fd > 0 {
events = append(events, createFdEvent(s.shared.fd))
}
for {
r, _, err := waitForObjects(events)
switch r {
case 0xFFFFFFFF: // WAIT_FAILED
panic(err)
case 0x00000102: // WAIT_OBJECT_TIMEOUT
case 0: // WAIT_OBJECT_0
resetEvent(events[r])
return
default: // WAIT_OBJECT_N
resetEvent(events[r])
var ref uintptr
if s.shared.ref != 0 && len(events)-1 == int(r) {
ref = s.shared.ref
} else {
ref = uniquePollables[r].ref
}
e, _, _ := mustGetProc("dnssd.dll", "DNSServiceProcessResult").Call(ref)
err := getError(int32(e))
if err != nil && ref == s.shared.ref {
// ref is no longer valid. ops using callback should have had their
// callback invoked. can call them anyway since we only pass on the first error.
s.shared.ref = 0
s.shared.fd = 0
for i := range sharedPollables {
sharedPollables[i].ref = 0
sharedPollables[i].p.handleError(err)
}
} else if err != nil {
uniquePollables[r].p.handleError(err)
}
if err != nil {
events = append(events[:r], events[r+1:]...)
}
}
}
}
func createEvent() uintptr {
event, _, err := mustGetProc("ws2_32.dll", "WSACreateEvent").Call()
if event == 0 {
panic(err)
}
return event
}
func createFdEvent(fd int) uintptr {
event := createEvent()
proc := mustGetProc("ws2_32.dll", "WSAEventSelect")
r, _, err := proc.Call(uintptr(fd), event, 1) // 1 == FD_READ
if r == ^uintptr(0) { // -1 == SOCKET_ERROR
panic(err)
}
return event
}
func resetEvent(event uintptr) {
r, _, err := mustGetProc("ws2_32.dll", "WSAResetEvent").Call(event)
if r == 0 {
panic(err)
}
}
func waitForObjects(events []uintptr) (r1, r2 uintptr, lastErr error) {
proc := mustGetProc("kernel32.dll", "WaitForMultipleObjects")
r1, r2, lastErr = proc.Call(uintptr(len(events)), uintptr(unsafe.Pointer(&events[0])), 0, 0xFFFFFFFF)
return
}