Debian Codegraph @

pkg/hvsock/zsyscall_windows.go

raw
1 package hvsock
2
3 /*
4 Most of this code was derived from: https://github.com/Microsoft/go-winio
5 which has the following license:
6
7 The MIT License (MIT)
8
9 Copyright (c) 2015 Microsoft
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29
30 import (
31 "syscall"
32 "unsafe"
33 )
34
35 var (
36 modws2_32 = syscall.NewLazyDLL("ws2_32.dll")
37 modwinmm = syscall.NewLazyDLL("winmm.dll")
38 modkernel32 = syscall.NewLazyDLL("kernel32.dll")
39
40 procConnect = modws2_32.NewProc("connect")
41 procBind = modws2_32.NewProc("bind")
42 procAccept = modws2_32.NewProc("accept")
43
44 procCancelIoEx = modkernel32.NewProc("CancelIoEx")
45 procCreateIoCompletionPort = modkernel32.NewProc("CreateIoCompletionPort")
46 procGetQueuedCompletionStatus = modkernel32.NewProc("GetQueuedCompletionStatus")
47 procSetFileCompletionNotificationModes = modkernel32.NewProc("SetFileCompletionNotificationModes")
48 proctimeBeginPeriod = modwinmm.NewProc("timeBeginPeriod")
49 )
50
51 // Do the interface allocations only once for common
52 // Errno values.
53 const (
54 errnoERROR_IO_PENDING = 997
55 socketError = uintptr(^uint32(0))
56
57 cFILE_SKIP_COMPLETION_PORT_ON_SUCCESS = 1
58 cFILE_SKIP_SET_EVENT_ON_HANDLE = 2
59 )
60
61 var (
62 errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING)
63 )
64
65 // errnoErr returns common boxed Errno values, to prevent
66 // allocations at runtime.
67 func errnoErr(e syscall.Errno) error {
68 switch e {
69 case 0:
70 return nil
71 case errnoERROR_IO_PENDING:
72 return errERROR_IO_PENDING
73 }
74 // TODO: add more here, after collecting data on the common
75 // error values see on Windows. (perhaps when running
76 // all.bat?)
77 return e
78 }
79
80 func sys_connect(s syscall.Handle, name unsafe.Pointer, namelen int32) (err error) {
81 r1, _, e1 := syscall.Syscall(procConnect.Addr(), 3, uintptr(s), uintptr(name), uintptr(namelen))
82 if r1 == socketError {
83 if e1 != 0 {
84 err = errnoErr(e1)
85 } else {
86 err = syscall.EINVAL
87 }
88 }
89
90 return
91 }
92
93 func sys_bind(s syscall.Handle, name unsafe.Pointer, namelen int32) (err error) {
94 r1, _, e1 := syscall.Syscall(procBind.Addr(), 3, uintptr(s), uintptr(name), uintptr(namelen))
95 if r1 == socketError {
96 if e1 != 0 {
97 err = errnoErr(e1)
98 } else {
99 err = syscall.EINVAL
100 }
101 }
102 return
103 }
104
105 func sys_accept(s syscall.Handle, rsa *rawSockaddrHyperv, addrlen *int32) (handle syscall.Handle, err error) {
106 r1, _, e1 := syscall.Syscall(procAccept.Addr(), 3, uintptr(s), uintptr(unsafe.Pointer(rsa)), uintptr(unsafe.Pointer(addrlen)))
107 handle = syscall.Handle(r1)
108 if r1 == socketError {
109 if e1 != 0 {
110 err = errnoErr(e1)
111 } else {
112 err = syscall.EINVAL
113 }
114 }
115 return
116 }
117
118 func cancelIoEx(file syscall.Handle, o *syscall.Overlapped) (err error) {
119 r1, _, e1 := syscall.Syscall(procCancelIoEx.Addr(), 2, uintptr(file), uintptr(unsafe.Pointer(o)), 0)
120 if r1 == 0 {
121 if e1 != 0 {
122 err = errnoErr(e1)
123 } else {
124 err = syscall.EINVAL
125 }
126 }
127 return
128 }
129
130 func createIoCompletionPort(file syscall.Handle, port syscall.Handle, key uintptr, threadCount uint32) (newport syscall.Handle, err error) {
131 r0, _, e1 := syscall.Syscall6(procCreateIoCompletionPort.Addr(), 4, uintptr(file), uintptr(port), uintptr(key), uintptr(threadCount), 0, 0)
132 newport = syscall.Handle(r0)
133 if newport == 0 {
134 if e1 != 0 {
135 err = errnoErr(e1)
136 } else {
137 err = syscall.EINVAL
138 }
139 }
140 return
141 }
142
143 func getQueuedCompletionStatus(port syscall.Handle, bytes *uint32, key *uintptr, o **ioOperation, timeout uint32) (err error) {
144 r1, _, e1 := syscall.Syscall6(procGetQueuedCompletionStatus.Addr(), 5, uintptr(port), uintptr(unsafe.Pointer(bytes)), uintptr(unsafe.Pointer(key)), uintptr(unsafe.Pointer(o)), uintptr(timeout), 0)
145 if r1 == 0 {
146 if e1 != 0 {
147 err = errnoErr(e1)
148 } else {
149 err = syscall.EINVAL
150 }
151 }
152 return
153 }
154
155 func setFileCompletionNotificationModes(h syscall.Handle, flags uint8) (err error) {
156 r1, _, e1 := syscall.Syscall(procSetFileCompletionNotificationModes.Addr(), 2, uintptr(h), uintptr(flags), 0)
157 if r1 == 0 {
158 if e1 != 0 {
159 err = errnoErr(e1)
160 } else {
161 err = syscall.EINVAL
162 }
163 }
164 return
165 }
166
167 func timeBeginPeriod(period uint32) (n int32) {
168 r0, _, _ := syscall.Syscall(proctimeBeginPeriod.Addr(), 1, uintptr(period), 0, 0)
169 n = int32(r0)
170 return
171 }