1// Copyright 2011 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
5package windows
6
7import "syscall"
8
9const (
10	// Windows errors.
11	ERROR_FILE_NOT_FOUND         syscall.Errno = 2
12	ERROR_PATH_NOT_FOUND         syscall.Errno = 3
13	ERROR_ACCESS_DENIED          syscall.Errno = 5
14	ERROR_NO_MORE_FILES          syscall.Errno = 18
15	ERROR_HANDLE_EOF             syscall.Errno = 38
16	ERROR_NETNAME_DELETED        syscall.Errno = 64
17	ERROR_FILE_EXISTS            syscall.Errno = 80
18	ERROR_BROKEN_PIPE            syscall.Errno = 109
19	ERROR_BUFFER_OVERFLOW        syscall.Errno = 111
20	ERROR_INSUFFICIENT_BUFFER    syscall.Errno = 122
21	ERROR_MOD_NOT_FOUND          syscall.Errno = 126
22	ERROR_PROC_NOT_FOUND         syscall.Errno = 127
23	ERROR_ALREADY_EXISTS         syscall.Errno = 183
24	ERROR_ENVVAR_NOT_FOUND       syscall.Errno = 203
25	ERROR_MORE_DATA              syscall.Errno = 234
26	ERROR_OPERATION_ABORTED      syscall.Errno = 995
27	ERROR_IO_PENDING             syscall.Errno = 997
28	ERROR_SERVICE_SPECIFIC_ERROR syscall.Errno = 1066
29	ERROR_NOT_FOUND              syscall.Errno = 1168
30	ERROR_PRIVILEGE_NOT_HELD     syscall.Errno = 1314
31	WSAEACCES                    syscall.Errno = 10013
32	WSAECONNRESET                syscall.Errno = 10054
33)
34
35const (
36	// Invented values to support what package os expects.
37	O_RDONLY   = 0x00000
38	O_WRONLY   = 0x00001
39	O_RDWR     = 0x00002
40	O_CREAT    = 0x00040
41	O_EXCL     = 0x00080
42	O_NOCTTY   = 0x00100
43	O_TRUNC    = 0x00200
44	O_NONBLOCK = 0x00800
45	O_APPEND   = 0x00400
46	O_SYNC     = 0x01000
47	O_ASYNC    = 0x02000
48	O_CLOEXEC  = 0x80000
49)
50
51const (
52	// More invented values for signals
53	SIGHUP  = Signal(0x1)
54	SIGINT  = Signal(0x2)
55	SIGQUIT = Signal(0x3)
56	SIGILL  = Signal(0x4)
57	SIGTRAP = Signal(0x5)
58	SIGABRT = Signal(0x6)
59	SIGBUS  = Signal(0x7)
60	SIGFPE  = Signal(0x8)
61	SIGKILL = Signal(0x9)
62	SIGSEGV = Signal(0xb)
63	SIGPIPE = Signal(0xd)
64	SIGALRM = Signal(0xe)
65	SIGTERM = Signal(0xf)
66)
67
68var signals = [...]string{
69	1:  "hangup",
70	2:  "interrupt",
71	3:  "quit",
72	4:  "illegal instruction",
73	5:  "trace/breakpoint trap",
74	6:  "aborted",
75	7:  "bus error",
76	8:  "floating point exception",
77	9:  "killed",
78	10: "user defined signal 1",
79	11: "segmentation fault",
80	12: "user defined signal 2",
81	13: "broken pipe",
82	14: "alarm clock",
83	15: "terminated",
84}
85
86const (
87	GENERIC_READ    = 0x80000000
88	GENERIC_WRITE   = 0x40000000
89	GENERIC_EXECUTE = 0x20000000
90	GENERIC_ALL     = 0x10000000
91
92	FILE_LIST_DIRECTORY   = 0x00000001
93	FILE_APPEND_DATA      = 0x00000004
94	FILE_WRITE_ATTRIBUTES = 0x00000100
95
96	FILE_SHARE_READ              = 0x00000001
97	FILE_SHARE_WRITE             = 0x00000002
98	FILE_SHARE_DELETE            = 0x00000004
99	FILE_ATTRIBUTE_READONLY      = 0x00000001
100	FILE_ATTRIBUTE_HIDDEN        = 0x00000002
101	FILE_ATTRIBUTE_SYSTEM        = 0x00000004
102	FILE_ATTRIBUTE_DIRECTORY     = 0x00000010
103	FILE_ATTRIBUTE_ARCHIVE       = 0x00000020
104	FILE_ATTRIBUTE_NORMAL        = 0x00000080
105	FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400
106
107	INVALID_FILE_ATTRIBUTES = 0xffffffff
108
109	CREATE_NEW        = 1
110	CREATE_ALWAYS     = 2
111	OPEN_EXISTING     = 3
112	OPEN_ALWAYS       = 4
113	TRUNCATE_EXISTING = 5
114
115	FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
116	FILE_FLAG_BACKUP_SEMANTICS   = 0x02000000
117	FILE_FLAG_OVERLAPPED         = 0x40000000
118
119	HANDLE_FLAG_INHERIT    = 0x00000001
120	STARTF_USESTDHANDLES   = 0x00000100
121	STARTF_USESHOWWINDOW   = 0x00000001
122	DUPLICATE_CLOSE_SOURCE = 0x00000001
123	DUPLICATE_SAME_ACCESS  = 0x00000002
124
125	STD_INPUT_HANDLE  = -10 & (1<<32 - 1)
126	STD_OUTPUT_HANDLE = -11 & (1<<32 - 1)
127	STD_ERROR_HANDLE  = -12 & (1<<32 - 1)
128
129	FILE_BEGIN   = 0
130	FILE_CURRENT = 1
131	FILE_END     = 2
132
133	LANG_ENGLISH       = 0x09
134	SUBLANG_ENGLISH_US = 0x01
135
136	FORMAT_MESSAGE_ALLOCATE_BUFFER = 256
137	FORMAT_MESSAGE_IGNORE_INSERTS  = 512
138	FORMAT_MESSAGE_FROM_STRING     = 1024
139	FORMAT_MESSAGE_FROM_HMODULE    = 2048
140	FORMAT_MESSAGE_FROM_SYSTEM     = 4096
141	FORMAT_MESSAGE_ARGUMENT_ARRAY  = 8192
142	FORMAT_MESSAGE_MAX_WIDTH_MASK  = 255
143
144	MAX_PATH      = 260
145	MAX_LONG_PATH = 32768
146
147	MAX_COMPUTERNAME_LENGTH = 15
148
149	TIME_ZONE_ID_UNKNOWN  = 0
150	TIME_ZONE_ID_STANDARD = 1
151
152	TIME_ZONE_ID_DAYLIGHT = 2
153	IGNORE                = 0
154	INFINITE              = 0xffffffff
155
156	WAIT_TIMEOUT   = 258
157	WAIT_ABANDONED = 0x00000080
158	WAIT_OBJECT_0  = 0x00000000
159	WAIT_FAILED    = 0xFFFFFFFF
160
161	CREATE_NEW_PROCESS_GROUP   = 0x00000200
162	CREATE_UNICODE_ENVIRONMENT = 0x00000400
163
164	PROCESS_TERMINATE         = 1
165	PROCESS_QUERY_INFORMATION = 0x00000400
166	SYNCHRONIZE               = 0x00100000
167
168	FILE_MAP_COPY    = 0x01
169	FILE_MAP_WRITE   = 0x02
170	FILE_MAP_READ    = 0x04
171	FILE_MAP_EXECUTE = 0x20
172
173	CTRL_C_EVENT     = 0
174	CTRL_BREAK_EVENT = 1
175
176	// Windows reserves errors >= 1<<29 for application use.
177	APPLICATION_ERROR = 1 << 29
178)
179
180const (
181	// flags for CreateToolhelp32Snapshot
182	TH32CS_SNAPHEAPLIST = 0x01
183	TH32CS_SNAPPROCESS  = 0x02
184	TH32CS_SNAPTHREAD   = 0x04
185	TH32CS_SNAPMODULE   = 0x08
186	TH32CS_SNAPMODULE32 = 0x10
187	TH32CS_SNAPALL      = TH32CS_SNAPHEAPLIST | TH32CS_SNAPMODULE | TH32CS_SNAPPROCESS | TH32CS_SNAPTHREAD
188	TH32CS_INHERIT      = 0x80000000
189)
190
191const (
192	// filters for ReadDirectoryChangesW
193	FILE_NOTIFY_CHANGE_FILE_NAME   = 0x001
194	FILE_NOTIFY_CHANGE_DIR_NAME    = 0x002
195	FILE_NOTIFY_CHANGE_ATTRIBUTES  = 0x004
196	FILE_NOTIFY_CHANGE_SIZE        = 0x008
197	FILE_NOTIFY_CHANGE_LAST_WRITE  = 0x010
198	FILE_NOTIFY_CHANGE_LAST_ACCESS = 0x020
199	FILE_NOTIFY_CHANGE_CREATION    = 0x040
200	FILE_NOTIFY_CHANGE_SECURITY    = 0x100
201)
202
203const (
204	// do not reorder
205	FILE_ACTION_ADDED = iota + 1
206	FILE_ACTION_REMOVED
207	FILE_ACTION_MODIFIED
208	FILE_ACTION_RENAMED_OLD_NAME
209	FILE_ACTION_RENAMED_NEW_NAME
210)
211
212const (
213	// wincrypt.h
214	PROV_RSA_FULL                    = 1
215	PROV_RSA_SIG                     = 2
216	PROV_DSS                         = 3
217	PROV_FORTEZZA                    = 4
218	PROV_MS_EXCHANGE                 = 5
219	PROV_SSL                         = 6
220	PROV_RSA_SCHANNEL                = 12
221	PROV_DSS_DH                      = 13
222	PROV_EC_ECDSA_SIG                = 14
223	PROV_EC_ECNRA_SIG                = 15
224	PROV_EC_ECDSA_FULL               = 16
225	PROV_EC_ECNRA_FULL               = 17
226	PROV_DH_SCHANNEL                 = 18
227	PROV_SPYRUS_LYNKS                = 20
228	PROV_RNG                         = 21
229	PROV_INTEL_SEC                   = 22
230	PROV_REPLACE_OWF                 = 23
231	PROV_RSA_AES                     = 24
232	CRYPT_VERIFYCONTEXT              = 0xF0000000
233	CRYPT_NEWKEYSET                  = 0x00000008
234	CRYPT_DELETEKEYSET               = 0x00000010
235	CRYPT_MACHINE_KEYSET             = 0x00000020
236	CRYPT_SILENT                     = 0x00000040
237	CRYPT_DEFAULT_CONTAINER_OPTIONAL = 0x00000080
238
239	USAGE_MATCH_TYPE_AND = 0
240	USAGE_MATCH_TYPE_OR  = 1
241
242	X509_ASN_ENCODING   = 0x00000001
243	PKCS_7_ASN_ENCODING = 0x00010000
244
245	CERT_STORE_PROV_MEMORY = 2
246
247	CERT_STORE_ADD_ALWAYS = 4
248
249	CERT_STORE_DEFER_CLOSE_UNTIL_LAST_FREE_FLAG = 0x00000004
250
251	CERT_TRUST_NO_ERROR                          = 0x00000000
252	CERT_TRUST_IS_NOT_TIME_VALID                 = 0x00000001
253	CERT_TRUST_IS_REVOKED                        = 0x00000004
254	CERT_TRUST_IS_NOT_SIGNATURE_VALID            = 0x00000008
255	CERT_TRUST_IS_NOT_VALID_FOR_USAGE            = 0x00000010
256	CERT_TRUST_IS_UNTRUSTED_ROOT                 = 0x00000020
257	CERT_TRUST_REVOCATION_STATUS_UNKNOWN         = 0x00000040
258	CERT_TRUST_IS_CYCLIC                         = 0x00000080
259	CERT_TRUST_INVALID_EXTENSION                 = 0x00000100
260	CERT_TRUST_INVALID_POLICY_CONSTRAINTS        = 0x00000200
261	CERT_TRUST_INVALID_BASIC_CONSTRAINTS         = 0x00000400
262	CERT_TRUST_INVALID_NAME_CONSTRAINTS          = 0x00000800
263	CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT = 0x00001000
264	CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT   = 0x00002000
265	CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT = 0x00004000
266	CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT      = 0x00008000
267	CERT_TRUST_IS_OFFLINE_REVOCATION             = 0x01000000
268	CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY          = 0x02000000
269	CERT_TRUST_IS_EXPLICIT_DISTRUST              = 0x04000000
270	CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT    = 0x08000000
271
272	CERT_CHAIN_POLICY_BASE              = 1
273	CERT_CHAIN_POLICY_AUTHENTICODE      = 2
274	CERT_CHAIN_POLICY_AUTHENTICODE_TS   = 3
275	CERT_CHAIN_POLICY_SSL               = 4
276	CERT_CHAIN_POLICY_BASIC_CONSTRAINTS = 5
277	CERT_CHAIN_POLICY_NT_AUTH           = 6
278	CERT_CHAIN_POLICY_MICROSOFT_ROOT    = 7
279	CERT_CHAIN_POLICY_EV                = 8
280
281	CERT_E_EXPIRED       = 0x800B0101
282	CERT_E_ROLE          = 0x800B0103
283	CERT_E_PURPOSE       = 0x800B0106
284	CERT_E_UNTRUSTEDROOT = 0x800B0109
285	CERT_E_CN_NO_MATCH   = 0x800B010F
286
287	AUTHTYPE_CLIENT = 1
288	AUTHTYPE_SERVER = 2
289)
290
291var (
292	OID_PKIX_KP_SERVER_AUTH = []byte("1.3.6.1.5.5.7.3.1\x00")
293	OID_SERVER_GATED_CRYPTO = []byte("1.3.6.1.4.1.311.10.3.3\x00")
294	OID_SGC_NETSCAPE        = []byte("2.16.840.1.113730.4.1\x00")
295)
296
297// Invented values to support what package os expects.
298type Timeval struct {
299	Sec  int32
300	Usec int32
301}
302
303func (tv *Timeval) Nanoseconds() int64 {
304	return (int64(tv.Sec)*1e6 + int64(tv.Usec)) * 1e3
305}
306
307func NsecToTimeval(nsec int64) (tv Timeval) {
308	tv.Sec = int32(nsec / 1e9)
309	tv.Usec = int32(nsec % 1e9 / 1e3)
310	return
311}
312
313type SecurityAttributes struct {
314	Length             uint32
315	SecurityDescriptor uintptr
316	InheritHandle      uint32
317}
318
319type Overlapped struct {
320	Internal     uintptr
321	InternalHigh uintptr
322	Offset       uint32
323	OffsetHigh   uint32
324	HEvent       Handle
325}
326
327type FileNotifyInformation struct {
328	NextEntryOffset uint32
329	Action          uint32
330	FileNameLength  uint32
331	FileName        uint16
332}
333
334type Filetime struct {
335	LowDateTime  uint32
336	HighDateTime uint32
337}
338
339// Nanoseconds returns Filetime ft in nanoseconds
340// since Epoch (00:00:00 UTC, January 1, 1970).
341func (ft *Filetime) Nanoseconds() int64 {
342	// 100-nanosecond intervals since January 1, 1601
343	nsec := int64(ft.HighDateTime)<<32 + int64(ft.LowDateTime)
344	// change starting time to the Epoch (00:00:00 UTC, January 1, 1970)
345	nsec -= 116444736000000000
346	// convert into nanoseconds
347	nsec *= 100
348	return nsec
349}
350
351func NsecToFiletime(nsec int64) (ft Filetime) {
352	// convert into 100-nanosecond
353	nsec /= 100
354	// change starting time to January 1, 1601
355	nsec += 116444736000000000
356	// split into high / low
357	ft.LowDateTime = uint32(nsec & 0xffffffff)
358	ft.HighDateTime = uint32(nsec >> 32 & 0xffffffff)
359	return ft
360}
361
362type Win32finddata struct {
363	FileAttributes    uint32
364	CreationTime      Filetime
365	LastAccessTime    Filetime
366	LastWriteTime     Filetime
367	FileSizeHigh      uint32
368	FileSizeLow       uint32
369	Reserved0         uint32
370	Reserved1         uint32
371	FileName          [MAX_PATH - 1]uint16
372	AlternateFileName [13]uint16
373}
374
375// This is the actual system call structure.
376// Win32finddata is what we committed to in Go 1.
377type win32finddata1 struct {
378	FileAttributes    uint32
379	CreationTime      Filetime
380	LastAccessTime    Filetime
381	LastWriteTime     Filetime
382	FileSizeHigh      uint32
383	FileSizeLow       uint32
384	Reserved0         uint32
385	Reserved1         uint32
386	FileName          [MAX_PATH]uint16
387	AlternateFileName [14]uint16
388}
389
390func copyFindData(dst *Win32finddata, src *win32finddata1) {
391	dst.FileAttributes = src.FileAttributes
392	dst.CreationTime = src.CreationTime
393	dst.LastAccessTime = src.LastAccessTime
394	dst.LastWriteTime = src.LastWriteTime
395	dst.FileSizeHigh = src.FileSizeHigh
396	dst.FileSizeLow = src.FileSizeLow
397	dst.Reserved0 = src.Reserved0
398	dst.Reserved1 = src.Reserved1
399
400	// The src is 1 element bigger than dst, but it must be NUL.
401	copy(dst.FileName[:], src.FileName[:])
402	copy(dst.AlternateFileName[:], src.AlternateFileName[:])
403}
404
405type ByHandleFileInformation struct {
406	FileAttributes     uint32
407	CreationTime       Filetime
408	LastAccessTime     Filetime
409	LastWriteTime      Filetime
410	VolumeSerialNumber uint32
411	FileSizeHigh       uint32
412	FileSizeLow        uint32
413	NumberOfLinks      uint32
414	FileIndexHigh      uint32
415	FileIndexLow       uint32
416}
417
418const (
419	GetFileExInfoStandard = 0
420	GetFileExMaxInfoLevel = 1
421)
422
423type Win32FileAttributeData struct {
424	FileAttributes uint32
425	CreationTime   Filetime
426	LastAccessTime Filetime
427	LastWriteTime  Filetime
428	FileSizeHigh   uint32
429	FileSizeLow    uint32
430}
431
432// ShowWindow constants
433const (
434	// winuser.h
435	SW_HIDE            = 0
436	SW_NORMAL          = 1
437	SW_SHOWNORMAL      = 1
438	SW_SHOWMINIMIZED   = 2
439	SW_SHOWMAXIMIZED   = 3
440	SW_MAXIMIZE        = 3
441	SW_SHOWNOACTIVATE  = 4
442	SW_SHOW            = 5
443	SW_MINIMIZE        = 6
444	SW_SHOWMINNOACTIVE = 7
445	SW_SHOWNA          = 8
446	SW_RESTORE         = 9
447	SW_SHOWDEFAULT     = 10
448	SW_FORCEMINIMIZE   = 11
449)
450
451type StartupInfo struct {
452	Cb            uint32
453	_             *uint16
454	Desktop       *uint16
455	Title         *uint16
456	X             uint32
457	Y             uint32
458	XSize         uint32
459	YSize         uint32
460	XCountChars   uint32
461	YCountChars   uint32
462	FillAttribute uint32
463	Flags         uint32
464	ShowWindow    uint16
465	_             uint16
466	_             *byte
467	StdInput      Handle
468	StdOutput     Handle
469	StdErr        Handle
470}
471
472type ProcessInformation struct {
473	Process   Handle
474	Thread    Handle
475	ProcessId uint32
476	ThreadId  uint32
477}
478
479type ProcessEntry32 struct {
480	Size            uint32
481	Usage           uint32
482	ProcessID       uint32
483	DefaultHeapID   uintptr
484	ModuleID        uint32
485	Threads         uint32
486	ParentProcessID uint32
487	PriClassBase    int32
488	Flags           uint32
489	ExeFile         [MAX_PATH]uint16
490}
491
492type Systemtime struct {
493	Year         uint16
494	Month        uint16
495	DayOfWeek    uint16
496	Day          uint16
497	Hour         uint16
498	Minute       uint16
499	Second       uint16
500	Milliseconds uint16
501}
502
503type Timezoneinformation struct {
504	Bias         int32
505	StandardName [32]uint16
506	StandardDate Systemtime
507	StandardBias int32
508	DaylightName [32]uint16
509	DaylightDate Systemtime
510	DaylightBias int32
511}
512
513// Socket related.
514
515const (
516	AF_UNSPEC  = 0
517	AF_UNIX    = 1
518	AF_INET    = 2
519	AF_INET6   = 23
520	AF_NETBIOS = 17
521
522	SOCK_STREAM    = 1
523	SOCK_DGRAM     = 2
524	SOCK_RAW       = 3
525	SOCK_SEQPACKET = 5
526
527	IPPROTO_IP   = 0
528	IPPROTO_IPV6 = 0x29
529	IPPROTO_TCP  = 6
530	IPPROTO_UDP  = 17
531
532	SOL_SOCKET                = 0xffff
533	SO_REUSEADDR              = 4
534	SO_KEEPALIVE              = 8
535	SO_DONTROUTE              = 16
536	SO_BROADCAST              = 32
537	SO_LINGER                 = 128
538	SO_RCVBUF                 = 0x1002
539	SO_SNDBUF                 = 0x1001
540	SO_UPDATE_ACCEPT_CONTEXT  = 0x700b
541	SO_UPDATE_CONNECT_CONTEXT = 0x7010
542
543	IOC_OUT                            = 0x40000000
544	IOC_IN                             = 0x80000000
545	IOC_VENDOR                         = 0x18000000
546	IOC_INOUT                          = IOC_IN | IOC_OUT
547	IOC_WS2                            = 0x08000000
548	SIO_GET_EXTENSION_FUNCTION_POINTER = IOC_INOUT | IOC_WS2 | 6
549	SIO_KEEPALIVE_VALS                 = IOC_IN | IOC_VENDOR | 4
550	SIO_UDP_CONNRESET                  = IOC_IN | IOC_VENDOR | 12
551
552	// cf. http://support.microsoft.com/default.aspx?scid=kb;en-us;257460
553
554	IP_TOS             = 0x3
555	IP_TTL             = 0x4
556	IP_MULTICAST_IF    = 0x9
557	IP_MULTICAST_TTL   = 0xa
558	IP_MULTICAST_LOOP  = 0xb
559	IP_ADD_MEMBERSHIP  = 0xc
560	IP_DROP_MEMBERSHIP = 0xd
561
562	IPV6_V6ONLY         = 0x1b
563	IPV6_UNICAST_HOPS   = 0x4
564	IPV6_MULTICAST_IF   = 0x9
565	IPV6_MULTICAST_HOPS = 0xa
566	IPV6_MULTICAST_LOOP = 0xb
567	IPV6_JOIN_GROUP     = 0xc
568	IPV6_LEAVE_GROUP    = 0xd
569
570	SOMAXCONN = 0x7fffffff
571
572	TCP_NODELAY = 1
573
574	SHUT_RD   = 0
575	SHUT_WR   = 1
576	SHUT_RDWR = 2
577
578	WSADESCRIPTION_LEN = 256
579	WSASYS_STATUS_LEN  = 128
580)
581
582type WSABuf struct {
583	Len uint32
584	Buf *byte
585}
586
587// Invented values to support what package os expects.
588const (
589	S_IFMT   = 0x1f000
590	S_IFIFO  = 0x1000
591	S_IFCHR  = 0x2000
592	S_IFDIR  = 0x4000
593	S_IFBLK  = 0x6000
594	S_IFREG  = 0x8000
595	S_IFLNK  = 0xa000
596	S_IFSOCK = 0xc000
597	S_ISUID  = 0x800
598	S_ISGID  = 0x400
599	S_ISVTX  = 0x200
600	S_IRUSR  = 0x100
601	S_IWRITE = 0x80
602	S_IWUSR  = 0x80
603	S_IXUSR  = 0x40
604)
605
606const (
607	FILE_TYPE_CHAR    = 0x0002
608	FILE_TYPE_DISK    = 0x0001
609	FILE_TYPE_PIPE    = 0x0003
610	FILE_TYPE_REMOTE  = 0x8000
611	FILE_TYPE_UNKNOWN = 0x0000
612)
613
614type Hostent struct {
615	Name     *byte
616	Aliases  **byte
617	AddrType uint16
618	Length   uint16
619	AddrList **byte
620}
621
622type Protoent struct {
623	Name    *byte
624	Aliases **byte
625	Proto   uint16
626}
627
628const (
629	DNS_TYPE_A       = 0x0001
630	DNS_TYPE_NS      = 0x0002
631	DNS_TYPE_MD      = 0x0003
632	DNS_TYPE_MF      = 0x0004
633	DNS_TYPE_CNAME   = 0x0005
634	DNS_TYPE_SOA     = 0x0006
635	DNS_TYPE_MB      = 0x0007
636	DNS_TYPE_MG      = 0x0008
637	DNS_TYPE_MR      = 0x0009
638	DNS_TYPE_NULL    = 0x000a
639	DNS_TYPE_WKS     = 0x000b
640	DNS_TYPE_PTR     = 0x000c
641	DNS_TYPE_HINFO   = 0x000d
642	DNS_TYPE_MINFO   = 0x000e
643	DNS_TYPE_MX      = 0x000f
644	DNS_TYPE_TEXT    = 0x0010
645	DNS_TYPE_RP      = 0x0011
646	DNS_TYPE_AFSDB   = 0x0012
647	DNS_TYPE_X25     = 0x0013
648	DNS_TYPE_ISDN    = 0x0014
649	DNS_TYPE_RT      = 0x0015
650	DNS_TYPE_NSAP    = 0x0016
651	DNS_TYPE_NSAPPTR = 0x0017
652	DNS_TYPE_SIG     = 0x0018
653	DNS_TYPE_KEY     = 0x0019
654	DNS_TYPE_PX      = 0x001a
655	DNS_TYPE_GPOS    = 0x001b
656	DNS_TYPE_AAAA    = 0x001c
657	DNS_TYPE_LOC     = 0x001d
658	DNS_TYPE_NXT     = 0x001e
659	DNS_TYPE_EID     = 0x001f
660	DNS_TYPE_NIMLOC  = 0x0020
661	DNS_TYPE_SRV     = 0x0021
662	DNS_TYPE_ATMA    = 0x0022
663	DNS_TYPE_NAPTR   = 0x0023
664	DNS_TYPE_KX      = 0x0024
665	DNS_TYPE_CERT    = 0x0025
666	DNS_TYPE_A6      = 0x0026
667	DNS_TYPE_DNAME   = 0x0027
668	DNS_TYPE_SINK    = 0x0028
669	DNS_TYPE_OPT     = 0x0029
670	DNS_TYPE_DS      = 0x002B
671	DNS_TYPE_RRSIG   = 0x002E
672	DNS_TYPE_NSEC    = 0x002F
673	DNS_TYPE_DNSKEY  = 0x0030
674	DNS_TYPE_DHCID   = 0x0031
675	DNS_TYPE_UINFO   = 0x0064
676	DNS_TYPE_UID     = 0x0065
677	DNS_TYPE_GID     = 0x0066
678	DNS_TYPE_UNSPEC  = 0x0067
679	DNS_TYPE_ADDRS   = 0x00f8
680	DNS_TYPE_TKEY    = 0x00f9
681	DNS_TYPE_TSIG    = 0x00fa
682	DNS_TYPE_IXFR    = 0x00fb
683	DNS_TYPE_AXFR    = 0x00fc
684	DNS_TYPE_MAILB   = 0x00fd
685	DNS_TYPE_MAILA   = 0x00fe
686	DNS_TYPE_ALL     = 0x00ff
687	DNS_TYPE_ANY     = 0x00ff
688	DNS_TYPE_WINS    = 0xff01
689	DNS_TYPE_WINSR   = 0xff02
690	DNS_TYPE_NBSTAT  = 0xff01
691)
692
693const (
694	DNS_INFO_NO_RECORDS = 0x251D
695)
696
697const (
698	// flags inside DNSRecord.Dw
699	DnsSectionQuestion   = 0x0000
700	DnsSectionAnswer     = 0x0001
701	DnsSectionAuthority  = 0x0002
702	DnsSectionAdditional = 0x0003
703)
704
705type DNSSRVData struct {
706	Target   *uint16
707	Priority uint16
708	Weight   uint16
709	Port     uint16
710	Pad      uint16
711}
712
713type DNSPTRData struct {
714	Host *uint16
715}
716
717type DNSMXData struct {
718	NameExchange *uint16
719	Preference   uint16
720	Pad          uint16
721}
722
723type DNSTXTData struct {
724	StringCount uint16
725	StringArray [1]*uint16
726}
727
728type DNSRecord struct {
729	Next     *DNSRecord
730	Name     *uint16
731	Type     uint16
732	Length   uint16
733	Dw       uint32
734	Ttl      uint32
735	Reserved uint32
736	Data     [40]byte
737}
738
739const (
740	TF_DISCONNECT         = 1
741	TF_REUSE_SOCKET       = 2
742	TF_WRITE_BEHIND       = 4
743	TF_USE_DEFAULT_WORKER = 0
744	TF_USE_SYSTEM_THREAD  = 16
745	TF_USE_KERNEL_APC     = 32
746)
747
748type TransmitFileBuffers struct {
749	Head       uintptr
750	HeadLength uint32
751	Tail       uintptr
752	TailLength uint32
753}
754
755const (
756	IFF_UP           = 1
757	IFF_BROADCAST    = 2
758	IFF_LOOPBACK     = 4
759	IFF_POINTTOPOINT = 8
760	IFF_MULTICAST    = 16
761)
762
763const SIO_GET_INTERFACE_LIST = 0x4004747F
764
765// TODO(mattn): SockaddrGen is union of sockaddr/sockaddr_in/sockaddr_in6_old.
766// will be fixed to change variable type as suitable.
767
768type SockaddrGen [24]byte
769
770type InterfaceInfo struct {
771	Flags            uint32
772	Address          SockaddrGen
773	BroadcastAddress SockaddrGen
774	Netmask          SockaddrGen
775}
776
777type IpAddressString struct {
778	String [16]byte
779}
780
781type IpMaskString IpAddressString
782
783type IpAddrString struct {
784	Next      *IpAddrString
785	IpAddress IpAddressString
786	IpMask    IpMaskString
787	Context   uint32
788}
789
790const MAX_ADAPTER_NAME_LENGTH = 256
791const MAX_ADAPTER_DESCRIPTION_LENGTH = 128
792const MAX_ADAPTER_ADDRESS_LENGTH = 8
793
794type IpAdapterInfo struct {
795	Next                *IpAdapterInfo
796	ComboIndex          uint32
797	AdapterName         [MAX_ADAPTER_NAME_LENGTH + 4]byte
798	Description         [MAX_ADAPTER_DESCRIPTION_LENGTH + 4]byte
799	AddressLength       uint32
800	Address             [MAX_ADAPTER_ADDRESS_LENGTH]byte
801	Index               uint32
802	Type                uint32
803	DhcpEnabled         uint32
804	CurrentIpAddress    *IpAddrString
805	IpAddressList       IpAddrString
806	GatewayList         IpAddrString
807	DhcpServer          IpAddrString
808	HaveWins            bool
809	PrimaryWinsServer   IpAddrString
810	SecondaryWinsServer IpAddrString
811	LeaseObtained       int64
812	LeaseExpires        int64
813}
814
815const MAXLEN_PHYSADDR = 8
816const MAX_INTERFACE_NAME_LEN = 256
817const MAXLEN_IFDESCR = 256
818
819type MibIfRow struct {
820	Name            [MAX_INTERFACE_NAME_LEN]uint16
821	Index           uint32
822	Type            uint32
823	Mtu             uint32
824	Speed           uint32
825	PhysAddrLen     uint32
826	PhysAddr        [MAXLEN_PHYSADDR]byte
827	AdminStatus     uint32
828	OperStatus      uint32
829	LastChange      uint32
830	InOctets        uint32
831	InUcastPkts     uint32
832	InNUcastPkts    uint32
833	InDiscards      uint32
834	InErrors        uint32
835	InUnknownProtos uint32
836	OutOctets       uint32
837	OutUcastPkts    uint32
838	OutNUcastPkts   uint32
839	OutDiscards     uint32
840	OutErrors       uint32
841	OutQLen         uint32
842	DescrLen        uint32
843	Descr           [MAXLEN_IFDESCR]byte
844}
845
846type CertContext struct {
847	EncodingType uint32
848	EncodedCert  *byte
849	Length       uint32
850	CertInfo     uintptr
851	Store        Handle
852}
853
854type CertChainContext struct {
855	Size                       uint32
856	TrustStatus                CertTrustStatus
857	ChainCount                 uint32
858	Chains                     **CertSimpleChain
859	LowerQualityChainCount     uint32
860	LowerQualityChains         **CertChainContext
861	HasRevocationFreshnessTime uint32
862	RevocationFreshnessTime    uint32
863}
864
865type CertSimpleChain struct {
866	Size                       uint32
867	TrustStatus                CertTrustStatus
868	NumElements                uint32
869	Elements                   **CertChainElement
870	TrustListInfo              uintptr
871	HasRevocationFreshnessTime uint32
872	RevocationFreshnessTime    uint32
873}
874
875type CertChainElement struct {
876	Size              uint32
877	CertContext       *CertContext
878	TrustStatus       CertTrustStatus
879	RevocationInfo    *CertRevocationInfo
880	IssuanceUsage     *CertEnhKeyUsage
881	ApplicationUsage  *CertEnhKeyUsage
882	ExtendedErrorInfo *uint16
883}
884
885type CertRevocationInfo struct {
886	Size             uint32
887	RevocationResult uint32
888	RevocationOid    *byte
889	OidSpecificInfo  uintptr
890	HasFreshnessTime uint32
891	FreshnessTime    uint32
892	CrlInfo          uintptr // *CertRevocationCrlInfo
893}
894
895type CertTrustStatus struct {
896	ErrorStatus uint32
897	InfoStatus  uint32
898}
899
900type CertUsageMatch struct {
901	Type  uint32
902	Usage CertEnhKeyUsage
903}
904
905type CertEnhKeyUsage struct {
906	Length           uint32
907	UsageIdentifiers **byte
908}
909
910type CertChainPara struct {
911	Size                         uint32
912	RequestedUsage               CertUsageMatch
913	RequstedIssuancePolicy       CertUsageMatch
914	URLRetrievalTimeout          uint32
915	CheckRevocationFreshnessTime uint32
916	RevocationFreshnessTime      uint32
917	CacheResync                  *Filetime
918}
919
920type CertChainPolicyPara struct {
921	Size            uint32
922	Flags           uint32
923	ExtraPolicyPara uintptr
924}
925
926type SSLExtraCertChainPolicyPara struct {
927	Size       uint32
928	AuthType   uint32
929	Checks     uint32
930	ServerName *uint16
931}
932
933type CertChainPolicyStatus struct {
934	Size              uint32
935	Error             uint32
936	ChainIndex        uint32
937	ElementIndex      uint32
938	ExtraPolicyStatus uintptr
939}
940
941const (
942	// do not reorder
943	HKEY_CLASSES_ROOT = 0x80000000 + iota
944	HKEY_CURRENT_USER
945	HKEY_LOCAL_MACHINE
946	HKEY_USERS
947	HKEY_PERFORMANCE_DATA
948	HKEY_CURRENT_CONFIG
949	HKEY_DYN_DATA
950
951	KEY_QUERY_VALUE        = 1
952	KEY_SET_VALUE          = 2
953	KEY_CREATE_SUB_KEY     = 4
954	KEY_ENUMERATE_SUB_KEYS = 8
955	KEY_NOTIFY             = 16
956	KEY_CREATE_LINK        = 32
957	KEY_WRITE              = 0x20006
958	KEY_EXECUTE            = 0x20019
959	KEY_READ               = 0x20019
960	KEY_WOW64_64KEY        = 0x0100
961	KEY_WOW64_32KEY        = 0x0200
962	KEY_ALL_ACCESS         = 0xf003f
963)
964
965const (
966	// do not reorder
967	REG_NONE = iota
968	REG_SZ
969	REG_EXPAND_SZ
970	REG_BINARY
971	REG_DWORD_LITTLE_ENDIAN
972	REG_DWORD_BIG_ENDIAN
973	REG_LINK
974	REG_MULTI_SZ
975	REG_RESOURCE_LIST
976	REG_FULL_RESOURCE_DESCRIPTOR
977	REG_RESOURCE_REQUIREMENTS_LIST
978	REG_QWORD_LITTLE_ENDIAN
979	REG_DWORD = REG_DWORD_LITTLE_ENDIAN
980	REG_QWORD = REG_QWORD_LITTLE_ENDIAN
981)
982
983type AddrinfoW struct {
984	Flags     int32
985	Family    int32
986	Socktype  int32
987	Protocol  int32
988	Addrlen   uintptr
989	Canonname *uint16
990	Addr      uintptr
991	Next      *AddrinfoW
992}
993
994const (
995	AI_PASSIVE     = 1
996	AI_CANONNAME   = 2
997	AI_NUMERICHOST = 4
998)
999
1000type GUID struct {
1001	Data1 uint32
1002	Data2 uint16
1003	Data3 uint16
1004	Data4 [8]byte
1005}
1006
1007var WSAID_CONNECTEX = GUID{
1008	0x25a207b9,
1009	0xddf3,
1010	0x4660,
1011	[8]byte{0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e},
1012}
1013
1014const (
1015	FILE_SKIP_COMPLETION_PORT_ON_SUCCESS = 1
1016	FILE_SKIP_SET_EVENT_ON_HANDLE        = 2
1017)
1018
1019const (
1020	WSAPROTOCOL_LEN    = 255
1021	MAX_PROTOCOL_CHAIN = 7
1022	BASE_PROTOCOL      = 1
1023	LAYERED_PROTOCOL   = 0
1024
1025	XP1_CONNECTIONLESS           = 0x00000001
1026	XP1_GUARANTEED_DELIVERY      = 0x00000002
1027	XP1_GUARANTEED_ORDER         = 0x00000004
1028	XP1_MESSAGE_ORIENTED         = 0x00000008
1029	XP1_PSEUDO_STREAM            = 0x00000010
1030	XP1_GRACEFUL_CLOSE           = 0x00000020
1031	XP1_EXPEDITED_DATA           = 0x00000040
1032	XP1_CONNECT_DATA             = 0x00000080
1033	XP1_DISCONNECT_DATA          = 0x00000100
1034	XP1_SUPPORT_BROADCAST        = 0x00000200
1035	XP1_SUPPORT_MULTIPOINT       = 0x00000400
1036	XP1_MULTIPOINT_CONTROL_PLANE = 0x00000800
1037	XP1_MULTIPOINT_DATA_PLANE    = 0x00001000
1038	XP1_QOS_SUPPORTED            = 0x00002000
1039	XP1_UNI_SEND                 = 0x00008000
1040	XP1_UNI_RECV                 = 0x00010000
1041	XP1_IFS_HANDLES              = 0x00020000
1042	XP1_PARTIAL_MESSAGE          = 0x00040000
1043	XP1_SAN_SUPPORT_SDP          = 0x00080000
1044
1045	PFL_MULTIPLE_PROTO_ENTRIES  = 0x00000001
1046	PFL_RECOMMENDED_PROTO_ENTRY = 0x00000002
1047	PFL_HIDDEN                  = 0x00000004
1048	PFL_MATCHES_PROTOCOL_ZERO   = 0x00000008
1049	PFL_NETWORKDIRECT_PROVIDER  = 0x00000010
1050)
1051
1052type WSAProtocolInfo struct {
1053	ServiceFlags1     uint32
1054	ServiceFlags2     uint32
1055	ServiceFlags3     uint32
1056	ServiceFlags4     uint32
1057	ProviderFlags     uint32
1058	ProviderId        GUID
1059	CatalogEntryId    uint32
1060	ProtocolChain     WSAProtocolChain
1061	Version           int32
1062	AddressFamily     int32
1063	MaxSockAddr       int32
1064	MinSockAddr       int32
1065	SocketType        int32
1066	Protocol          int32
1067	ProtocolMaxOffset int32
1068	NetworkByteOrder  int32
1069	SecurityScheme    int32
1070	MessageSize       uint32
1071	ProviderReserved  uint32
1072	ProtocolName      [WSAPROTOCOL_LEN + 1]uint16
1073}
1074
1075type WSAProtocolChain struct {
1076	ChainLen     int32
1077	ChainEntries [MAX_PROTOCOL_CHAIN]uint32
1078}
1079
1080type TCPKeepalive struct {
1081	OnOff    uint32
1082	Time     uint32
1083	Interval uint32
1084}
1085
1086type symbolicLinkReparseBuffer struct {
1087	SubstituteNameOffset uint16
1088	SubstituteNameLength uint16
1089	PrintNameOffset      uint16
1090	PrintNameLength      uint16
1091	Flags                uint32
1092	PathBuffer           [1]uint16
1093}
1094
1095type mountPointReparseBuffer struct {
1096	SubstituteNameOffset uint16
1097	SubstituteNameLength uint16
1098	PrintNameOffset      uint16
1099	PrintNameLength      uint16
1100	PathBuffer           [1]uint16
1101}
1102
1103type reparseDataBuffer struct {
1104	ReparseTag        uint32
1105	ReparseDataLength uint16
1106	Reserved          uint16
1107
1108	// GenericReparseBuffer
1109	reparseBuffer byte
1110}
1111
1112const (
1113	FSCTL_GET_REPARSE_POINT          = 0x900A8
1114	MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16 * 1024
1115	IO_REPARSE_TAG_MOUNT_POINT       = 0xA0000003
1116	IO_REPARSE_TAG_SYMLINK           = 0xA000000C
1117	SYMBOLIC_LINK_FLAG_DIRECTORY     = 0x1
1118)
1119
1120const (
1121	ComputerNameNetBIOS                   = 0
1122	ComputerNameDnsHostname               = 1
1123	ComputerNameDnsDomain                 = 2
1124	ComputerNameDnsFullyQualified         = 3
1125	ComputerNamePhysicalNetBIOS           = 4
1126	ComputerNamePhysicalDnsHostname       = 5
1127	ComputerNamePhysicalDnsDomain         = 6
1128	ComputerNamePhysicalDnsFullyQualified = 7
1129	ComputerNameMax                       = 8
1130)
1131
1132const (
1133	MOVEFILE_REPLACE_EXISTING      = 0x1
1134	MOVEFILE_COPY_ALLOWED          = 0x2
1135	MOVEFILE_DELAY_UNTIL_REBOOT    = 0x4
1136	MOVEFILE_WRITE_THROUGH         = 0x8
1137	MOVEFILE_CREATE_HARDLINK       = 0x10
1138	MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x20
1139)
1140
1141const GAA_FLAG_INCLUDE_PREFIX = 0x00000010
1142
1143const (
1144	IF_TYPE_OTHER              = 1
1145	IF_TYPE_ETHERNET_CSMACD    = 6
1146	IF_TYPE_ISO88025_TOKENRING = 9
1147	IF_TYPE_PPP                = 23
1148	IF_TYPE_SOFTWARE_LOOPBACK  = 24
1149	IF_TYPE_ATM                = 37
1150	IF_TYPE_IEEE80211          = 71
1151	IF_TYPE_TUNNEL             = 131
1152	IF_TYPE_IEEE1394           = 144
1153)
1154
1155type SocketAddress struct {
1156	Sockaddr       *syscall.RawSockaddrAny
1157	SockaddrLength int32
1158}
1159
1160type IpAdapterUnicastAddress struct {
1161	Length             uint32
1162	Flags              uint32
1163	Next               *IpAdapterUnicastAddress
1164	Address            SocketAddress
1165	PrefixOrigin       int32
1166	SuffixOrigin       int32
1167	DadState           int32
1168	ValidLifetime      uint32
1169	PreferredLifetime  uint32
1170	LeaseLifetime      uint32
1171	OnLinkPrefixLength uint8
1172}
1173
1174type IpAdapterAnycastAddress struct {
1175	Length  uint32
1176	Flags   uint32
1177	Next    *IpAdapterAnycastAddress
1178	Address SocketAddress
1179}
1180
1181type IpAdapterMulticastAddress struct {
1182	Length  uint32
1183	Flags   uint32
1184	Next    *IpAdapterMulticastAddress
1185	Address SocketAddress
1186}
1187
1188type IpAdapterDnsServerAdapter struct {
1189	Length   uint32
1190	Reserved uint32
1191	Next     *IpAdapterDnsServerAdapter
1192	Address  SocketAddress
1193}
1194
1195type IpAdapterPrefix struct {
1196	Length       uint32
1197	Flags        uint32
1198	Next         *IpAdapterPrefix
1199	Address      SocketAddress
1200	PrefixLength uint32
1201}
1202
1203type IpAdapterAddresses struct {
1204	Length                uint32
1205	IfIndex               uint32
1206	Next                  *IpAdapterAddresses
1207	AdapterName           *byte
1208	FirstUnicastAddress   *IpAdapterUnicastAddress
1209	FirstAnycastAddress   *IpAdapterAnycastAddress
1210	FirstMulticastAddress *IpAdapterMulticastAddress
1211	FirstDnsServerAddress *IpAdapterDnsServerAdapter
1212	DnsSuffix             *uint16
1213	Description           *uint16
1214	FriendlyName          *uint16
1215	PhysicalAddress       [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
1216	PhysicalAddressLength uint32
1217	Flags                 uint32
1218	Mtu                   uint32
1219	IfType                uint32
1220	OperStatus            uint32
1221	Ipv6IfIndex           uint32
1222	ZoneIndices           [16]uint32
1223	FirstPrefix           *IpAdapterPrefix
1224	/* more fields might be present here. */
1225}
1226
1227const (
1228	IfOperStatusUp             = 1
1229	IfOperStatusDown           = 2
1230	IfOperStatusTesting        = 3
1231	IfOperStatusUnknown        = 4
1232	IfOperStatusDormant        = 5
1233	IfOperStatusNotPresent     = 6
1234	IfOperStatusLowerLayerDown = 7
1235)
1236
1237// Console related constants used for the mode parameter to SetConsoleMode. See
1238// https://docs.microsoft.com/en-us/windows/console/setconsolemode for details.
1239
1240const (
1241	ENABLE_PROCESSED_INPUT        = 0x1
1242	ENABLE_LINE_INPUT             = 0x2
1243	ENABLE_ECHO_INPUT             = 0x4
1244	ENABLE_WINDOW_INPUT           = 0x8
1245	ENABLE_MOUSE_INPUT            = 0x10
1246	ENABLE_INSERT_MODE            = 0x20
1247	ENABLE_QUICK_EDIT_MODE        = 0x40
1248	ENABLE_EXTENDED_FLAGS         = 0x80
1249	ENABLE_AUTO_POSITION          = 0x100
1250	ENABLE_VIRTUAL_TERMINAL_INPUT = 0x200
1251
1252	ENABLE_PROCESSED_OUTPUT            = 0x1
1253	ENABLE_WRAP_AT_EOL_OUTPUT          = 0x2
1254	ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
1255	DISABLE_NEWLINE_AUTO_RETURN        = 0x8
1256	ENABLE_LVB_GRID_WORLDWIDE          = 0x10
1257)
1258
1259type Coord struct {
1260	X int16
1261	Y int16
1262}
1263
1264type SmallRect struct {
1265	Left   int16
1266	Top    int16
1267	Right  int16
1268	Bottom int16
1269}
1270
1271// Used with GetConsoleScreenBuffer to retreive information about a console
1272// screen buffer. See
1273// https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str
1274// for details.
1275
1276type ConsoleScreenBufferInfo struct {
1277	Size              Coord
1278	CursorPosition    Coord
1279	Attributes        uint16
1280	Window            SmallRect
1281	MaximumWindowSize Coord
1282}
1283