1 /***********************************************************************\
2 *                               mswsock.d                               *
3 *                                                                       *
4 *                       Windows API header module                       *
5 *                                                                       *
6 *                 Translated from MinGW Windows headers                 *
7 *                             by Daniel Keep                            *
8 *                                                                       *
9 *                       Placed into public domain                       *
10 \***********************************************************************/
11 module win32.mswsock;
12 
13 import win32.winbase, win32.windef;
14 private import win32.basetyps, win32.w32api;
15 
16 // FIXME: clean up Windows version support
17 
18 version (Win32_Winsock2) pragma(msg,
19  "Version Win32_Winsock2 is deprecated; Winsock2 is now imported by default");
20 
21 // Pull in Winsock1 if the user has put "Win32_Winsock1" on the compile
22 // line; otherwise, default to Winsock2.
23 version (Win32_Winsock1) {
24 	import win32.winsock;
25 } else {
26 	import win32.winsock2;
27 }
28 
29 static if (_WIN32_WINNT_ONLY) {
30 	static if (_WIN32_WINNT >= 0x500) {
31 		enum {
32 			/* WinNT5+:
33 			   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_options.htm */
34 			SO_MAXDG             = 0x7009,
35 			SO_MAXPATHDG         = 0x700A,
36 		}
37 	}
38 
39 	enum {
40 		/* WinNT4+:
41 		   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_options_for_windows_nt_4_0_2.htm */
42 		SO_CONNDATA              = 0x7000,
43 		SO_CONNOPT               = 0x7001,
44 		SO_DISCDATA              = 0x7002,
45 		SO_DISCOPT               = 0x7003,
46 		SO_CONNDATALEN           = 0x7004,
47 		SO_CONNOPTLEN            = 0x7005,
48 		SO_DISCDATALEN           = 0x7006,
49 		SO_DISCOPTLEN            = 0x7007,
50 
51 		/* WinNT4:
52 		   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_options.htm */
53 		SO_UPDATE_ACCEPT_CONTENT = 0x700B,
54 	}
55 }
56 
57 enum {
58 	/* Win95+, WinNT4+ but apparently shouldn't used: mark as deprecated?
59 	   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_options.htm */
60 	SO_OPENTYPE                  = 0x7008,
61 
62 	/* Win95+; these two are passed to the SO_OPENTYPE option as arguments,
63 	   so would they be deprecated as well?
64 	   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_options.htm */
65 	SO_SYNCHRONOUS_ALERT         = 0x0010,
66 	SO_SYNCHRONOUS_NONALERT      = 0x0020,
67 
68 	/* Win95:
69 	   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/socket_options.htm */
70 	SO_CONNECT_TIME              = 0x700C,
71 }
72 
73 
74 static if (_WIN32_WINNT_ONLY) {
75 	enum {
76 		TCP_BSDURGENT = 0x7000,
77 	}
78 
79 	/* These *appear* to be constants for passing to the TransmitFile /
80 	   TransmitPackets functions, which are available in WinNT3.51+
81 	   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/transmitfile_2.htm */
82 	enum {
83 		TF_DISCONNECT         = 1,
84 		TF_REUSE_SOCKET       = 2,
85 		TF_WRITE_BEHIND       = 4,
86 		TF_USE_DEFAULT_WORKER = 0,
87 		TF_USE_SYSTEM_THREAD  = 16,
88 		TF_USE_KERNEL_APC     = 32
89 	}
90 }
91 
92 /* Win95+, WinNT3.51+
93    ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/transmit_file_buffers_2.htm */
94 struct TRANSMIT_FILE_BUFFERS {
95 	PVOID Head;
96 	DWORD HeadLength;
97 	PVOID Tail;
98 	DWORD TailLength;
99 }
100 alias TRANSMIT_FILE_BUFFERS* PTRANSMIT_FILE_BUFFERS, LPTRANSMIT_FILE_BUFFERS;
101 
102 extern(Windows) {
103 	/* Win95+, WinNT3.51+
104 	   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsarecvex_2.htm */
105 	int WSARecvEx(SOCKET, char*, int, int*);
106 
107 	/* Win95+, WinNT3.51+
108 	   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/getacceptexSOCKADDRs_2.htm */
109 	VOID GetAcceptExSockaddrs(PVOID, DWORD, DWORD, DWORD, SOCKADDR**, LPINT, SOCKADDR**, LPINT);
110 
111 	static if (_WIN32_WINNT_ONLY) {
112 		/* WinNT3.51+
113 		   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/transmitfile_2.htm */
114 		BOOL TransmitFile(SOCKET, HANDLE, DWORD, DWORD, LPOVERLAPPED, LPTRANSMIT_FILE_BUFFERS, DWORD);
115 
116 		/* WinNT3.51+
117 		   ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/acceptex_2.htm */
118 		alias BOOL function(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED) LPFN_ACCEPTEX;
119         const GUID WSAID_ACCEPTEX = {0xb5367df1,0xcbac,0x11cf,[0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92]};
120 
121         alias BOOL function(SOCKET, SOCKADDR*, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED) LPFN_CONNECTEX;
122         const GUID WSAID_CONNECTEX = {0x25a207b9,0xddf3,0x4660,[0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e]};
123 	}
124 }
125 
126 version(Win32_Winsock1) {
127 } else {
128 	static if (WINVER > 0x501) {
129 		/*	These appear to be constants for the TRANSMIT_PACKETS_ELEMENT
130 		 *	structure below, so I've given them the same minimum version
131 		 */
132 		enum {
133 			TP_ELEMENT_FILE   = 1,
134 			TP_ELEMENT_MEMORY = 2,
135 			TP_ELEMENT_EOP    = 4
136 		}
137 
138 		/*	WinXP+, Srv2k3+
139 		 *	ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/transmit_packets_element_2.htm
140 		 */
141 		struct TRANSMIT_PACKETS_ELEMENT {
142 			ULONG dwElFlags;
143 			ULONG cLength;
144 			union {
145 				struct {
146 					LARGE_INTEGER nFileOffset;
147 					HANDLE        hFile;
148 				}
149 				PVOID pBuffer;
150 			}
151 		}
152 
153 		/*	WinXP+, Server2003+:
154 		 *	ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsamsg_2.htm
155 		 */
156 		struct WSAMSG {
157 			LPSOCKADDR name;
158 			INT        namelen;
159 			LPWSABUF   lpBuffers;
160 			DWORD      dwBufferCount;
161 			WSABUF     Control;
162 			DWORD      dwFlags;
163 		}
164 
165 		alias WSAMSG* PWSAMSG, LPWSAMSG;
166 
167 		/* According to MSDN docs, the WSAMSG.Control buffer starts with a
168 		   cmsghdr header of the following form.  See also RFC 2292. */
169 		/* DK: Confirmed.  So I suppose these should get the same version as
170 		   WSAMSG... */
171 		struct WSACMSGHDR {
172 			UINT cmsg_len;
173 			INT  cmsg_level;
174 			INT  cmsg_type;
175 			// followed by UCHAR cmsg_data[];
176 		}
177 
178 		/* TODO: Standard Posix.1g macros as per RFC 2292, with WSA_uglification. */
179 		/* DK: MinGW doesn't define these, and neither does the MSDN docs.  Might have
180 		   to actually look up RFC 2292... */
181 		/+
182 		#if 0
183 		#define WSA_CMSG_FIRSTHDR(mhdr)
184 		#define WSA_CMSG_NXTHDR(mhdr, cmsg)
185 		#define WSA_CMSG_SPACE(length)
186 		#define WSA_CMSG_LEN(length)
187 		#endif
188 		+/
189 
190 		extern(Windows)
191 		{
192 			/*	WinXP+, Srv2k3+
193 			 *	ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/disconnectex_2.htm
194 			 */
195 			BOOL DisconnectEx(SOCKET, LPOVERLAPPED, DWORD, DWORD);
196 
197 			/*	WinXP+, Srv2k3+
198 			 *	ms-help://MS.MSDNQTR.2003FEB.1033/winsock/winsock/wsarecvmsg_2.htm
199 			 */
200 			int WSARecvMsg(SOCKET, LPWSAMSG, LPDWORD, LPWSAOVERLAPPED, LPWSAOVERLAPPED_COMPLETION_ROUTINE);
201 		}
202 	}
203 }