...
Run Format

Source file src/internal/syscall/windows/syscall_windows.go

Documentation: internal/syscall/windows

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

View as plain text