PageRenderTime 73ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/libgo/go/internal/syscall/windows/syscall_windows.go

https://gitlab.com/4144/gcc
Go | 307 lines | 249 code | 40 blank | 18 comment | 23 complexity | 91ba45328c0d3e8d378d0ed172ade67c MD5 | raw file
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package windows
  5. import (
  6. "sync"
  7. "syscall"
  8. "unsafe"
  9. )
  10. const (
  11. ERROR_SHARING_VIOLATION syscall.Errno = 32
  12. ERROR_LOCK_VIOLATION syscall.Errno = 33
  13. ERROR_NOT_SUPPORTED syscall.Errno = 50
  14. ERROR_CALL_NOT_IMPLEMENTED syscall.Errno = 120
  15. ERROR_INVALID_NAME syscall.Errno = 123
  16. ERROR_LOCK_FAILED syscall.Errno = 167
  17. ERROR_NO_UNICODE_TRANSLATION syscall.Errno = 1113
  18. )
  19. const GAA_FLAG_INCLUDE_PREFIX = 0x00000010
  20. const (
  21. IF_TYPE_OTHER = 1
  22. IF_TYPE_ETHERNET_CSMACD = 6
  23. IF_TYPE_ISO88025_TOKENRING = 9
  24. IF_TYPE_PPP = 23
  25. IF_TYPE_SOFTWARE_LOOPBACK = 24
  26. IF_TYPE_ATM = 37
  27. IF_TYPE_IEEE80211 = 71
  28. IF_TYPE_TUNNEL = 131
  29. IF_TYPE_IEEE1394 = 144
  30. )
  31. type SocketAddress struct {
  32. Sockaddr *syscall.RawSockaddrAny
  33. SockaddrLength int32
  34. }
  35. type IpAdapterUnicastAddress struct {
  36. Length uint32
  37. Flags uint32
  38. Next *IpAdapterUnicastAddress
  39. Address SocketAddress
  40. PrefixOrigin int32
  41. SuffixOrigin int32
  42. DadState int32
  43. ValidLifetime uint32
  44. PreferredLifetime uint32
  45. LeaseLifetime uint32
  46. OnLinkPrefixLength uint8
  47. }
  48. type IpAdapterAnycastAddress struct {
  49. Length uint32
  50. Flags uint32
  51. Next *IpAdapterAnycastAddress
  52. Address SocketAddress
  53. }
  54. type IpAdapterMulticastAddress struct {
  55. Length uint32
  56. Flags uint32
  57. Next *IpAdapterMulticastAddress
  58. Address SocketAddress
  59. }
  60. type IpAdapterDnsServerAdapter struct {
  61. Length uint32
  62. Reserved uint32
  63. Next *IpAdapterDnsServerAdapter
  64. Address SocketAddress
  65. }
  66. type IpAdapterPrefix struct {
  67. Length uint32
  68. Flags uint32
  69. Next *IpAdapterPrefix
  70. Address SocketAddress
  71. PrefixLength uint32
  72. }
  73. type IpAdapterAddresses struct {
  74. Length uint32
  75. IfIndex uint32
  76. Next *IpAdapterAddresses
  77. AdapterName *byte
  78. FirstUnicastAddress *IpAdapterUnicastAddress
  79. FirstAnycastAddress *IpAdapterAnycastAddress
  80. FirstMulticastAddress *IpAdapterMulticastAddress
  81. FirstDnsServerAddress *IpAdapterDnsServerAdapter
  82. DnsSuffix *uint16
  83. Description *uint16
  84. FriendlyName *uint16
  85. PhysicalAddress [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
  86. PhysicalAddressLength uint32
  87. Flags uint32
  88. Mtu uint32
  89. IfType uint32
  90. OperStatus uint32
  91. Ipv6IfIndex uint32
  92. ZoneIndices [16]uint32
  93. FirstPrefix *IpAdapterPrefix
  94. /* more fields might be present here. */
  95. }
  96. const (
  97. IfOperStatusUp = 1
  98. IfOperStatusDown = 2
  99. IfOperStatusTesting = 3
  100. IfOperStatusUnknown = 4
  101. IfOperStatusDormant = 5
  102. IfOperStatusNotPresent = 6
  103. IfOperStatusLowerLayerDown = 7
  104. )
  105. //sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
  106. //sys GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
  107. //sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW
  108. //sys GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
  109. const (
  110. WSA_FLAG_OVERLAPPED = 0x01
  111. WSA_FLAG_NO_HANDLE_INHERIT = 0x80
  112. WSAEMSGSIZE syscall.Errno = 10040
  113. MSG_PEEK = 0x2
  114. MSG_TRUNC = 0x0100
  115. MSG_CTRUNC = 0x0200
  116. socket_error = uintptr(^uint32(0))
  117. )
  118. var WSAID_WSASENDMSG = syscall.GUID{
  119. Data1: 0xa441e712,
  120. Data2: 0x754f,
  121. Data3: 0x43ca,
  122. Data4: [8]byte{0x84, 0xa7, 0x0d, 0xee, 0x44, 0xcf, 0x60, 0x6d},
  123. }
  124. var WSAID_WSARECVMSG = syscall.GUID{
  125. Data1: 0xf689d7c8,
  126. Data2: 0x6f1f,
  127. Data3: 0x436b,
  128. Data4: [8]byte{0x8a, 0x53, 0xe5, 0x4f, 0xe3, 0x51, 0xc3, 0x22},
  129. }
  130. var sendRecvMsgFunc struct {
  131. once sync.Once
  132. sendAddr uintptr
  133. recvAddr uintptr
  134. err error
  135. }
  136. type WSAMsg struct {
  137. Name *syscall.RawSockaddrAny
  138. Namelen int32
  139. Buffers *syscall.WSABuf
  140. BufferCount uint32
  141. Control syscall.WSABuf
  142. Flags uint32
  143. }
  144. //sys WSASocket(af int32, typ int32, protocol int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (handle syscall.Handle, err error) [failretval==syscall.InvalidHandle] = ws2_32.WSASocketW
  145. func loadWSASendRecvMsg() error {
  146. sendRecvMsgFunc.once.Do(func() {
  147. var s syscall.Handle
  148. s, sendRecvMsgFunc.err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
  149. if sendRecvMsgFunc.err != nil {
  150. return
  151. }
  152. defer syscall.CloseHandle(s)
  153. var n uint32
  154. sendRecvMsgFunc.err = syscall.WSAIoctl(s,
  155. syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
  156. (*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)),
  157. uint32(unsafe.Sizeof(WSAID_WSARECVMSG)),
  158. (*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)),
  159. uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)),
  160. &n, nil, 0)
  161. if sendRecvMsgFunc.err != nil {
  162. return
  163. }
  164. sendRecvMsgFunc.err = syscall.WSAIoctl(s,
  165. syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
  166. (*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)),
  167. uint32(unsafe.Sizeof(WSAID_WSASENDMSG)),
  168. (*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)),
  169. uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)),
  170. &n, nil, 0)
  171. })
  172. return sendRecvMsgFunc.err
  173. }
  174. func WSASendMsg(fd syscall.Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
  175. err := loadWSASendRecvMsg()
  176. if err != nil {
  177. return err
  178. }
  179. r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.sendAddr, 6, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(flags), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)))
  180. if r1 == socket_error {
  181. if e1 != 0 {
  182. err = errnoErr(e1)
  183. } else {
  184. err = syscall.EINVAL
  185. }
  186. }
  187. return err
  188. }
  189. func WSARecvMsg(fd syscall.Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
  190. err := loadWSASendRecvMsg()
  191. if err != nil {
  192. return err
  193. }
  194. r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.recvAddr, 5, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(unsafe.Pointer(bytesReceived)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)), 0)
  195. if r1 == socket_error {
  196. if e1 != 0 {
  197. err = errnoErr(e1)
  198. } else {
  199. err = syscall.EINVAL
  200. }
  201. }
  202. return err
  203. }
  204. const (
  205. ComputerNameNetBIOS = 0
  206. ComputerNameDnsHostname = 1
  207. ComputerNameDnsDomain = 2
  208. ComputerNameDnsFullyQualified = 3
  209. ComputerNamePhysicalNetBIOS = 4
  210. ComputerNamePhysicalDnsHostname = 5
  211. ComputerNamePhysicalDnsDomain = 6
  212. ComputerNamePhysicalDnsFullyQualified = 7
  213. ComputerNameMax = 8
  214. MOVEFILE_REPLACE_EXISTING = 0x1
  215. MOVEFILE_COPY_ALLOWED = 0x2
  216. MOVEFILE_DELAY_UNTIL_REBOOT = 0x4
  217. MOVEFILE_WRITE_THROUGH = 0x8
  218. MOVEFILE_CREATE_HARDLINK = 0x10
  219. MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x20
  220. )
  221. func Rename(oldpath, newpath string) error {
  222. from, err := syscall.UTF16PtrFromString(oldpath)
  223. if err != nil {
  224. return err
  225. }
  226. to, err := syscall.UTF16PtrFromString(newpath)
  227. if err != nil {
  228. return err
  229. }
  230. return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING)
  231. }
  232. //sys LockFileEx(file syscall.Handle, flags uint32, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *syscall.Overlapped) (err error) = kernel32.LockFileEx
  233. //sys UnlockFileEx(file syscall.Handle, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *syscall.Overlapped) (err error) = kernel32.UnlockFileEx
  234. const (
  235. LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
  236. LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
  237. )
  238. const MB_ERR_INVALID_CHARS = 8
  239. //sys GetACP() (acp uint32) = kernel32.GetACP
  240. //sys GetConsoleCP() (ccp uint32) = kernel32.GetConsoleCP
  241. //sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
  242. //sys GetCurrentThread() (pseudoHandle syscall.Handle, err error) = kernel32.GetCurrentThread
  243. const STYPE_DISKTREE = 0x00
  244. type SHARE_INFO_2 struct {
  245. Netname *uint16
  246. Type uint32
  247. Remark *uint16
  248. Permissions uint32
  249. MaxUses uint32
  250. CurrentUses uint32
  251. Path *uint16
  252. Passwd *uint16
  253. }
  254. //sys NetShareAdd(serverName *uint16, level uint32, buf *byte, parmErr *uint16) (neterr error) = netapi32.NetShareAdd
  255. //sys NetShareDel(serverName *uint16, netName *uint16, reserved uint32) (neterr error) = netapi32.NetShareDel
  256. const (
  257. FILE_NAME_NORMALIZED = 0x0
  258. FILE_NAME_OPENED = 0x8
  259. VOLUME_NAME_DOS = 0x0
  260. VOLUME_NAME_GUID = 0x1
  261. VOLUME_NAME_NONE = 0x4
  262. VOLUME_NAME_NT = 0x2
  263. )
  264. //sys GetFinalPathNameByHandle(file syscall.Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) = kernel32.GetFinalPathNameByHandleW
  265. func LoadGetFinalPathNameByHandle() error {
  266. return procGetFinalPathNameByHandleW.Find()
  267. }