2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-01-19 16:56:04 +09:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package windows
|
|
|
|
|
|
2017-11-07 10:19:10 -08:00
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
"syscall"
|
2019-11-09 19:06:24 +11:00
|
|
|
"unicode/utf16"
|
2017-11-07 10:19:10 -08:00
|
|
|
"unsafe"
|
|
|
|
|
)
|
2015-01-19 16:56:04 +09:00
|
|
|
|
2019-11-09 19:06:24 +11:00
|
|
|
// UTF16PtrToString is like UTF16ToString, but takes *uint16
|
|
|
|
|
// as a parameter instead of []uint16.
|
|
|
|
|
// max is how many times p can be advanced looking for the null terminator.
|
|
|
|
|
// If max is hit, the string is truncated at that point.
|
|
|
|
|
func UTF16PtrToString(p *uint16, max int) string {
|
|
|
|
|
if p == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
// Find NUL terminator.
|
|
|
|
|
end := unsafe.Pointer(p)
|
|
|
|
|
n := 0
|
|
|
|
|
for *(*uint16)(end) != 0 && n < max {
|
|
|
|
|
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
|
|
|
|
|
return string(utf16.Decode(s))
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-21 11:19:36 +10:00
|
|
|
const (
|
2016-05-05 12:44:28 +10:00
|
|
|
ERROR_SHARING_VIOLATION syscall.Errno = 32
|
2018-10-16 16:05:51 -04:00
|
|
|
ERROR_LOCK_VIOLATION syscall.Errno = 33
|
|
|
|
|
ERROR_NOT_SUPPORTED syscall.Errno = 50
|
|
|
|
|
ERROR_CALL_NOT_IMPLEMENTED syscall.Errno = 120
|
2018-04-23 13:03:44 +09:00
|
|
|
ERROR_INVALID_NAME syscall.Errno = 123
|
2018-10-16 16:05:51 -04:00
|
|
|
ERROR_LOCK_FAILED syscall.Errno = 167
|
2016-09-21 11:19:36 +10:00
|
|
|
ERROR_NO_UNICODE_TRANSLATION syscall.Errno = 1113
|
|
|
|
|
)
|
|
|
|
|
|
2015-01-19 16:56:04 +09:00
|
|
|
const GAA_FLAG_INCLUDE_PREFIX = 0x00000010
|
|
|
|
|
|
2015-12-04 19:06:01 +09:00
|
|
|
const (
|
|
|
|
|
IF_TYPE_OTHER = 1
|
|
|
|
|
IF_TYPE_ETHERNET_CSMACD = 6
|
|
|
|
|
IF_TYPE_ISO88025_TOKENRING = 9
|
|
|
|
|
IF_TYPE_PPP = 23
|
|
|
|
|
IF_TYPE_SOFTWARE_LOOPBACK = 24
|
|
|
|
|
IF_TYPE_ATM = 37
|
|
|
|
|
IF_TYPE_IEEE80211 = 71
|
|
|
|
|
IF_TYPE_TUNNEL = 131
|
|
|
|
|
IF_TYPE_IEEE1394 = 144
|
|
|
|
|
)
|
2015-01-19 16:56:04 +09:00
|
|
|
|
|
|
|
|
type SocketAddress struct {
|
|
|
|
|
Sockaddr *syscall.RawSockaddrAny
|
|
|
|
|
SockaddrLength int32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IpAdapterUnicastAddress struct {
|
|
|
|
|
Length uint32
|
|
|
|
|
Flags uint32
|
|
|
|
|
Next *IpAdapterUnicastAddress
|
|
|
|
|
Address SocketAddress
|
|
|
|
|
PrefixOrigin int32
|
|
|
|
|
SuffixOrigin int32
|
|
|
|
|
DadState int32
|
|
|
|
|
ValidLifetime uint32
|
|
|
|
|
PreferredLifetime uint32
|
|
|
|
|
LeaseLifetime uint32
|
|
|
|
|
OnLinkPrefixLength uint8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IpAdapterAnycastAddress struct {
|
|
|
|
|
Length uint32
|
|
|
|
|
Flags uint32
|
|
|
|
|
Next *IpAdapterAnycastAddress
|
|
|
|
|
Address SocketAddress
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IpAdapterMulticastAddress struct {
|
|
|
|
|
Length uint32
|
|
|
|
|
Flags uint32
|
|
|
|
|
Next *IpAdapterMulticastAddress
|
|
|
|
|
Address SocketAddress
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IpAdapterDnsServerAdapter struct {
|
|
|
|
|
Length uint32
|
|
|
|
|
Reserved uint32
|
|
|
|
|
Next *IpAdapterDnsServerAdapter
|
|
|
|
|
Address SocketAddress
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IpAdapterPrefix struct {
|
|
|
|
|
Length uint32
|
|
|
|
|
Flags uint32
|
|
|
|
|
Next *IpAdapterPrefix
|
|
|
|
|
Address SocketAddress
|
|
|
|
|
PrefixLength uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IpAdapterAddresses struct {
|
|
|
|
|
Length uint32
|
|
|
|
|
IfIndex uint32
|
|
|
|
|
Next *IpAdapterAddresses
|
|
|
|
|
AdapterName *byte
|
|
|
|
|
FirstUnicastAddress *IpAdapterUnicastAddress
|
|
|
|
|
FirstAnycastAddress *IpAdapterAnycastAddress
|
|
|
|
|
FirstMulticastAddress *IpAdapterMulticastAddress
|
|
|
|
|
FirstDnsServerAddress *IpAdapterDnsServerAdapter
|
|
|
|
|
DnsSuffix *uint16
|
|
|
|
|
Description *uint16
|
|
|
|
|
FriendlyName *uint16
|
|
|
|
|
PhysicalAddress [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
|
|
|
|
|
PhysicalAddressLength uint32
|
|
|
|
|
Flags uint32
|
|
|
|
|
Mtu uint32
|
|
|
|
|
IfType uint32
|
|
|
|
|
OperStatus uint32
|
|
|
|
|
Ipv6IfIndex uint32
|
|
|
|
|
ZoneIndices [16]uint32
|
|
|
|
|
FirstPrefix *IpAdapterPrefix
|
|
|
|
|
/* more fields might be present here. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
IfOperStatusUp = 1
|
|
|
|
|
IfOperStatusDown = 2
|
|
|
|
|
IfOperStatusTesting = 3
|
|
|
|
|
IfOperStatusUnknown = 4
|
|
|
|
|
IfOperStatusDormant = 5
|
|
|
|
|
IfOperStatusNotPresent = 6
|
|
|
|
|
IfOperStatusLowerLayerDown = 7
|
|
|
|
|
)
|
|
|
|
|
|
2015-12-04 19:06:01 +09:00
|
|
|
//sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
|
2015-02-24 02:35:55 -08:00
|
|
|
//sys GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
|
2015-02-26 12:10:11 -08:00
|
|
|
//sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW
|
2015-11-01 04:17:34 -05:00
|
|
|
//sys GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
|
2015-02-24 02:35:55 -08:00
|
|
|
|
2017-10-23 16:23:43 +11:00
|
|
|
const (
|
|
|
|
|
WSA_FLAG_OVERLAPPED = 0x01
|
|
|
|
|
WSA_FLAG_NO_HANDLE_INHERIT = 0x80
|
2017-11-07 10:19:10 -08:00
|
|
|
|
|
|
|
|
WSAEMSGSIZE syscall.Errno = 10040
|
|
|
|
|
|
2017-11-06 23:02:21 -08:00
|
|
|
MSG_PEEK = 0x2
|
2017-11-07 10:19:10 -08:00
|
|
|
MSG_TRUNC = 0x0100
|
|
|
|
|
MSG_CTRUNC = 0x0200
|
|
|
|
|
|
|
|
|
|
socket_error = uintptr(^uint32(0))
|
2017-10-23 16:23:43 +11:00
|
|
|
)
|
|
|
|
|
|
2017-11-07 10:19:10 -08:00
|
|
|
var WSAID_WSASENDMSG = syscall.GUID{
|
|
|
|
|
Data1: 0xa441e712,
|
|
|
|
|
Data2: 0x754f,
|
|
|
|
|
Data3: 0x43ca,
|
|
|
|
|
Data4: [8]byte{0x84, 0xa7, 0x0d, 0xee, 0x44, 0xcf, 0x60, 0x6d},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var WSAID_WSARECVMSG = syscall.GUID{
|
|
|
|
|
Data1: 0xf689d7c8,
|
|
|
|
|
Data2: 0x6f1f,
|
|
|
|
|
Data3: 0x436b,
|
|
|
|
|
Data4: [8]byte{0x8a, 0x53, 0xe5, 0x4f, 0xe3, 0x51, 0xc3, 0x22},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sendRecvMsgFunc struct {
|
|
|
|
|
once sync.Once
|
|
|
|
|
sendAddr uintptr
|
|
|
|
|
recvAddr uintptr
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type WSAMsg struct {
|
2020-03-07 11:08:06 +11:00
|
|
|
Name syscall.Pointer
|
2017-11-07 10:19:10 -08:00
|
|
|
Namelen int32
|
|
|
|
|
Buffers *syscall.WSABuf
|
|
|
|
|
BufferCount uint32
|
|
|
|
|
Control syscall.WSABuf
|
|
|
|
|
Flags uint32
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 16:23:43 +11:00
|
|
|
//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
|
|
|
|
|
|
2017-11-07 10:19:10 -08:00
|
|
|
func loadWSASendRecvMsg() error {
|
|
|
|
|
sendRecvMsgFunc.once.Do(func() {
|
|
|
|
|
var s syscall.Handle
|
|
|
|
|
s, sendRecvMsgFunc.err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
|
|
|
|
|
if sendRecvMsgFunc.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer syscall.CloseHandle(s)
|
|
|
|
|
var n uint32
|
|
|
|
|
sendRecvMsgFunc.err = syscall.WSAIoctl(s,
|
|
|
|
|
syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
|
|
|
|
|
(*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)),
|
|
|
|
|
uint32(unsafe.Sizeof(WSAID_WSARECVMSG)),
|
|
|
|
|
(*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)),
|
|
|
|
|
uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)),
|
|
|
|
|
&n, nil, 0)
|
|
|
|
|
if sendRecvMsgFunc.err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sendRecvMsgFunc.err = syscall.WSAIoctl(s,
|
|
|
|
|
syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
|
|
|
|
|
(*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)),
|
|
|
|
|
uint32(unsafe.Sizeof(WSAID_WSASENDMSG)),
|
|
|
|
|
(*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)),
|
|
|
|
|
uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)),
|
|
|
|
|
&n, nil, 0)
|
|
|
|
|
})
|
|
|
|
|
return sendRecvMsgFunc.err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WSASendMsg(fd syscall.Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
|
|
|
|
|
err := loadWSASendRecvMsg()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
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)))
|
|
|
|
|
if r1 == socket_error {
|
|
|
|
|
if e1 != 0 {
|
|
|
|
|
err = errnoErr(e1)
|
|
|
|
|
} else {
|
|
|
|
|
err = syscall.EINVAL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WSARecvMsg(fd syscall.Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
|
|
|
|
|
err := loadWSASendRecvMsg()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
if r1 == socket_error {
|
|
|
|
|
if e1 != 0 {
|
|
|
|
|
err = errnoErr(e1)
|
|
|
|
|
} else {
|
|
|
|
|
err = syscall.EINVAL
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-24 02:35:55 -08:00
|
|
|
const (
|
|
|
|
|
ComputerNameNetBIOS = 0
|
|
|
|
|
ComputerNameDnsHostname = 1
|
|
|
|
|
ComputerNameDnsDomain = 2
|
|
|
|
|
ComputerNameDnsFullyQualified = 3
|
|
|
|
|
ComputerNamePhysicalNetBIOS = 4
|
|
|
|
|
ComputerNamePhysicalDnsHostname = 5
|
|
|
|
|
ComputerNamePhysicalDnsDomain = 6
|
|
|
|
|
ComputerNamePhysicalDnsFullyQualified = 7
|
|
|
|
|
ComputerNameMax = 8
|
2015-02-26 12:10:11 -08:00
|
|
|
|
|
|
|
|
MOVEFILE_REPLACE_EXISTING = 0x1
|
|
|
|
|
MOVEFILE_COPY_ALLOWED = 0x2
|
|
|
|
|
MOVEFILE_DELAY_UNTIL_REBOOT = 0x4
|
|
|
|
|
MOVEFILE_WRITE_THROUGH = 0x8
|
|
|
|
|
MOVEFILE_CREATE_HARDLINK = 0x10
|
|
|
|
|
MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x20
|
2015-02-24 02:35:55 -08:00
|
|
|
)
|
2015-02-26 12:10:11 -08:00
|
|
|
|
|
|
|
|
func Rename(oldpath, newpath string) error {
|
|
|
|
|
from, err := syscall.UTF16PtrFromString(oldpath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
to, err := syscall.UTF16PtrFromString(newpath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING)
|
|
|
|
|
}
|
2015-02-10 11:06:02 +09:00
|
|
|
|
2018-10-16 16:05:51 -04:00
|
|
|
//sys LockFileEx(file syscall.Handle, flags uint32, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *syscall.Overlapped) (err error) = kernel32.LockFileEx
|
|
|
|
|
//sys UnlockFileEx(file syscall.Handle, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *syscall.Overlapped) (err error) = kernel32.UnlockFileEx
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
|
|
|
|
|
LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
|
|
|
|
|
)
|
|
|
|
|
|
2016-09-21 11:19:36 +10:00
|
|
|
const MB_ERR_INVALID_CHARS = 8
|
|
|
|
|
|
2015-12-21 16:16:06 +11:00
|
|
|
//sys GetACP() (acp uint32) = kernel32.GetACP
|
2016-08-24 10:34:16 +09:00
|
|
|
//sys GetConsoleCP() (ccp uint32) = kernel32.GetConsoleCP
|
2015-12-21 16:16:06 +11:00
|
|
|
//sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
|
2016-07-28 10:33:26 +10:00
|
|
|
//sys GetCurrentThread() (pseudoHandle syscall.Handle, err error) = kernel32.GetCurrentThread
|
2016-10-16 13:25:53 +09:00
|
|
|
|
|
|
|
|
const STYPE_DISKTREE = 0x00
|
|
|
|
|
|
|
|
|
|
type SHARE_INFO_2 struct {
|
|
|
|
|
Netname *uint16
|
|
|
|
|
Type uint32
|
|
|
|
|
Remark *uint16
|
|
|
|
|
Permissions uint32
|
|
|
|
|
MaxUses uint32
|
|
|
|
|
CurrentUses uint32
|
|
|
|
|
Path *uint16
|
|
|
|
|
Passwd *uint16
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//sys NetShareAdd(serverName *uint16, level uint32, buf *byte, parmErr *uint16) (neterr error) = netapi32.NetShareAdd
|
|
|
|
|
//sys NetShareDel(serverName *uint16, netName *uint16, reserved uint32) (neterr error) = netapi32.NetShareDel
|
2017-08-09 11:33:40 +10:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
FILE_NAME_NORMALIZED = 0x0
|
|
|
|
|
FILE_NAME_OPENED = 0x8
|
|
|
|
|
|
|
|
|
|
VOLUME_NAME_DOS = 0x0
|
|
|
|
|
VOLUME_NAME_GUID = 0x1
|
|
|
|
|
VOLUME_NAME_NONE = 0x4
|
|
|
|
|
VOLUME_NAME_NT = 0x2
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//sys GetFinalPathNameByHandle(file syscall.Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) = kernel32.GetFinalPathNameByHandleW
|
|
|
|
|
|
|
|
|
|
func LoadGetFinalPathNameByHandle() error {
|
|
|
|
|
return procGetFinalPathNameByHandleW.Find()
|
|
|
|
|
}
|
2019-05-12 14:34:30 +02:00
|
|
|
|
|
|
|
|
//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
|
|
|
|
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|