1 | #include <windows.h>
|
---|
2 | #include "d_winsock.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | typedef SOCKET (PASCAL FAR *accept_Type)(SOCKET s, struct sockaddr FAR *addr,
|
---|
6 | int FAR *addrlen);
|
---|
7 | typedef int (PASCAL FAR *bind_Type)(SOCKET s, const struct sockaddr FAR *addr, int namelen);
|
---|
8 | typedef int (PASCAL FAR *closesocket_Type)(SOCKET s);
|
---|
9 | typedef int (PASCAL FAR *connect_Type)(SOCKET s, const struct sockaddr FAR *name,
|
---|
10 | int namelen);
|
---|
11 | typedef int (PASCAL FAR *ioctlsocket_Type)(SOCKET s, long cmd, u_long FAR *argp);
|
---|
12 | typedef int (PASCAL FAR *getpeername_Type)(SOCKET s, struct sockaddr FAR *name,
|
---|
13 | int FAR * namelen);
|
---|
14 | typedef int (PASCAL FAR *getsockname_Type)(SOCKET s, struct sockaddr FAR *name,
|
---|
15 | int FAR * namelen);
|
---|
16 | typedef int (PASCAL FAR *getsockopt_Type)(SOCKET s, int level, int optname,
|
---|
17 | char FAR * optval, int FAR *optlen);
|
---|
18 | typedef u_long (PASCAL FAR *htonl_Type)(u_long hostlong);
|
---|
19 | typedef u_short (PASCAL FAR *htons_Type)(u_short hostshort);
|
---|
20 | typedef unsigned long (PASCAL FAR *inet_addr_Type)(const char FAR * cp);
|
---|
21 | typedef char FAR * (PASCAL FAR *inet_ntoa_Type)(struct in_addr in);
|
---|
22 | typedef int (PASCAL FAR *listen_Type)(SOCKET s, int backlog);
|
---|
23 | typedef u_long (PASCAL FAR *ntohl_Type)(u_long netlong);
|
---|
24 | typedef u_short (PASCAL FAR *ntohs_Type)(u_short netshort);
|
---|
25 | typedef int (PASCAL FAR *recv_Type)(SOCKET s, char FAR * buf, int len, int flags);
|
---|
26 | typedef int (PASCAL FAR *recvfrom_Type)(SOCKET s, char FAR * buf, int len, int flags,
|
---|
27 | struct sockaddr FAR *from, int FAR * fromlen);
|
---|
28 | typedef int (PASCAL FAR *select_Type)(int nfds, fd_set FAR *readfds, fd_set FAR *writefds,
|
---|
29 | fd_set FAR *exceptfds, const struct timeval FAR *timeout);
|
---|
30 | typedef int (PASCAL FAR *send_Type)(SOCKET s, const char FAR * buf, int len, int flags);
|
---|
31 | typedef int (PASCAL FAR *sendto_Type)(SOCKET s, const char FAR * buf, int len, int flags,
|
---|
32 | const struct sockaddr FAR *to, int tolen);
|
---|
33 | typedef int (PASCAL FAR *setsockopt_Type)(SOCKET s, int level, int optname,
|
---|
34 | const char FAR * optval, int optlen);
|
---|
35 | typedef int (PASCAL FAR *shutdown_Type)(SOCKET s, int how);
|
---|
36 | typedef SOCKET (PASCAL FAR *socket_Type)(int af, int type, int protocol);
|
---|
37 | typedef struct hostent FAR * (PASCAL FAR *gethostbyaddr_Type)(const char FAR * addr,
|
---|
38 | int len, int type);
|
---|
39 | typedef struct hostent FAR * (PASCAL FAR *gethostbyname_Type)(const char FAR * name);
|
---|
40 | typedef int (PASCAL FAR *gethostname_Type) (char FAR * name, int namelen);
|
---|
41 | typedef struct servent FAR * (PASCAL FAR *getservbyport_Type)(int port, const char FAR * proto);
|
---|
42 | typedef struct servent FAR * (PASCAL FAR *getservbyname_Type)(const char FAR * name,
|
---|
43 | const char FAR * proto);
|
---|
44 | typedef struct protoent FAR * (PASCAL FAR *getprotobynumber_Type)(int proto);
|
---|
45 | typedef struct protoent FAR * (PASCAL FAR *getprotobyname_Type)(const char FAR * name);
|
---|
46 | typedef int (PASCAL FAR *WSAStartup_Type)(WORD wVersionRequired, LPWSADATA lpWSAData);
|
---|
47 | typedef int (PASCAL FAR *WSACleanup_Type)(void);
|
---|
48 | typedef void (PASCAL FAR *WSASetLastError_Type)(int iError);
|
---|
49 | typedef int (PASCAL FAR *WSAGetLastError_Type)(void);
|
---|
50 | typedef BOOL (PASCAL FAR *WSAIsBlocking_Type)(void);
|
---|
51 | typedef int (PASCAL FAR *WSAUnhookBlockingHook_Type)(void);
|
---|
52 | typedef FARPROC (PASCAL FAR *WSASetBlockingHook_Type)(FARPROC lpBlockFunc);
|
---|
53 | typedef int (PASCAL FAR *WSACancelBlockingCall_Type)(void);
|
---|
54 | typedef HANDLE (PASCAL FAR *WSAAsyncGetServByName_Type)(HWND hWnd, u_int wMsg,
|
---|
55 | const char FAR * name,
|
---|
56 | const char FAR * proto,
|
---|
57 | char FAR * buf, int buflen);
|
---|
58 | typedef HANDLE (PASCAL FAR *WSAAsyncGetServByPort_Type)(HWND hWnd, u_int wMsg, int port,
|
---|
59 | const char FAR * proto, char FAR * buf,
|
---|
60 | int buflen);
|
---|
61 | typedef HANDLE (PASCAL FAR *WSAAsyncGetProtoByName_Type)(HWND hWnd, u_int wMsg,
|
---|
62 | const char FAR * name, char FAR * buf,
|
---|
63 | int buflen);
|
---|
64 | typedef HANDLE (PASCAL FAR *WSAAsyncGetProtoByNumber_Type)(HWND hWnd, u_int wMsg,
|
---|
65 | int number, char FAR * buf,
|
---|
66 | int buflen);
|
---|
67 | typedef HANDLE (PASCAL FAR *WSAAsyncGetHostByName_Type)(HWND hWnd, u_int wMsg,
|
---|
68 | const char FAR * name, char FAR * buf,
|
---|
69 | int buflen);
|
---|
70 | typedef HANDLE (PASCAL FAR *WSAAsyncGetHostByAddr_Type)(HWND hWnd, u_int wMsg,
|
---|
71 | const char FAR * addr, int len, int type,
|
---|
72 | char FAR * buf, int buflen);
|
---|
73 | typedef int (PASCAL FAR *WSACancelAsyncRequest_Type)(HANDLE hAsyncTaskHandle);
|
---|
74 | typedef int (PASCAL FAR *WSAAsyncSelect_Type)(SOCKET s, HWND hWnd, u_int wMsg,
|
---|
75 | long lEvent);
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | static accept_Type accept_Ptr = NULL;
|
---|
80 | static bind_Type bind_Ptr = NULL;
|
---|
81 | static closesocket_Type closesocket_Ptr = NULL;
|
---|
82 | static connect_Type connect_Ptr = NULL;
|
---|
83 | static ioctlsocket_Type ioctlsocket_Ptr = NULL;
|
---|
84 | static getpeername_Type getpeername_Ptr = NULL;
|
---|
85 | static getsockname_Type getsockname_Ptr = NULL;
|
---|
86 | static getsockopt_Type getsockopt_Ptr = NULL;
|
---|
87 | static htonl_Type htonl_Ptr = NULL;
|
---|
88 | static htons_Type htons_Ptr = NULL;
|
---|
89 | static inet_addr_Type inet_addr_Ptr = NULL;
|
---|
90 | static inet_ntoa_Type inet_ntoa_Ptr = NULL;
|
---|
91 | static listen_Type listen_Ptr = NULL;
|
---|
92 | static ntohl_Type ntohl_Ptr = NULL;
|
---|
93 | static ntohs_Type ntohs_Ptr = NULL;
|
---|
94 | static recv_Type recv_Ptr = NULL;
|
---|
95 | static recvfrom_Type recvfrom_Ptr = NULL;
|
---|
96 | static select_Type select_Ptr = NULL;
|
---|
97 | static send_Type send_Ptr = NULL;
|
---|
98 | static sendto_Type sendto_Ptr = NULL;
|
---|
99 | static setsockopt_Type setsockopt_Ptr = NULL;
|
---|
100 | static shutdown_Type shutdown_Ptr = NULL;
|
---|
101 | static socket_Type socket_Ptr = NULL;
|
---|
102 | static gethostbyaddr_Type gethostbyaddr_Ptr = NULL;
|
---|
103 | static gethostbyname_Type gethostbyname_Ptr = NULL;
|
---|
104 | static gethostname_Type gethostname_Ptr = NULL;
|
---|
105 | static getservbyport_Type getservbyport_Ptr = NULL;
|
---|
106 | static getservbyname_Type getservbyname_Ptr = NULL;
|
---|
107 | static getprotobynumber_Type getprotobynumber_Ptr = NULL;
|
---|
108 | static getprotobyname_Type getprotobyname_Ptr = NULL;
|
---|
109 | static WSAStartup_Type WSAStartup_Ptr = NULL;
|
---|
110 | static WSACleanup_Type WSACleanup_Ptr = NULL;
|
---|
111 | static WSASetLastError_Type WSASetLastError_Ptr = NULL;
|
---|
112 | static WSAGetLastError_Type WSAGetLastError_Ptr = NULL;
|
---|
113 | static WSAIsBlocking_Type WSAIsBlocking_Ptr = NULL;
|
---|
114 | static WSAUnhookBlockingHook_Type WSAUnhookBlockingHook_Ptr = NULL;
|
---|
115 | static WSASetBlockingHook_Type WSASetBlockingHook_Ptr = NULL;
|
---|
116 | static WSACancelBlockingCall_Type WSACancelBlockingCall_Ptr = NULL;
|
---|
117 | static WSAAsyncGetServByName_Type WSAAsyncGetServByName_Ptr = NULL;
|
---|
118 | static WSAAsyncGetServByPort_Type WSAAsyncGetServByPort_Ptr = NULL;
|
---|
119 | static WSAAsyncGetProtoByName_Type WSAAsyncGetProtoByName_Ptr = NULL;
|
---|
120 | static WSAAsyncGetProtoByNumber_Type WSAAsyncGetProtoByNumber_Ptr = NULL;
|
---|
121 | static WSAAsyncGetHostByName_Type WSAAsyncGetHostByName_Ptr = NULL;
|
---|
122 | static WSAAsyncGetHostByAddr_Type WSAAsyncGetHostByAddr_Ptr = NULL;
|
---|
123 | static WSACancelAsyncRequest_Type WSACancelAsyncRequest_Ptr = NULL;
|
---|
124 | static WSAAsyncSelect_Type WSAAsyncSelect_Ptr = NULL;
|
---|
125 |
|
---|
126 |
|
---|
127 | static HINSTANCE wsock32lib = NULL;
|
---|
128 | static HINSTANCE winsocklib = NULL;
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 | // set all the pointers back to null
|
---|
133 | static void ResetWSPtrs () {
|
---|
134 | accept_Ptr = NULL;
|
---|
135 | bind_Ptr = NULL;
|
---|
136 | closesocket_Ptr = NULL;
|
---|
137 | connect_Ptr = NULL;
|
---|
138 | ioctlsocket_Ptr = NULL;
|
---|
139 | getpeername_Ptr = NULL;
|
---|
140 | getsockname_Ptr = NULL;
|
---|
141 | getsockopt_Ptr = NULL;
|
---|
142 | htonl_Ptr = NULL;
|
---|
143 | htons_Ptr = NULL;
|
---|
144 | inet_addr_Ptr = NULL;
|
---|
145 | inet_ntoa_Ptr = NULL;
|
---|
146 | listen_Ptr = NULL;
|
---|
147 | ntohl_Ptr = NULL;
|
---|
148 | ntohs_Ptr = NULL;
|
---|
149 | recv_Ptr = NULL;
|
---|
150 | recvfrom_Ptr = NULL;
|
---|
151 | select_Ptr = NULL;
|
---|
152 | send_Ptr = NULL;
|
---|
153 | sendto_Ptr = NULL;
|
---|
154 | setsockopt_Ptr = NULL;
|
---|
155 | shutdown_Ptr = NULL;
|
---|
156 | socket_Ptr = NULL;
|
---|
157 | gethostbyaddr_Ptr = NULL;
|
---|
158 | gethostbyname_Ptr = NULL;
|
---|
159 | gethostname_Ptr = NULL;
|
---|
160 | getservbyport_Ptr = NULL;
|
---|
161 | getservbyname_Ptr = NULL;
|
---|
162 | getprotobynumber_Ptr = NULL;
|
---|
163 | getprotobyname_Ptr = NULL;
|
---|
164 | WSAStartup_Ptr = NULL;
|
---|
165 | WSACleanup_Ptr = NULL;
|
---|
166 | WSASetLastError_Ptr = NULL;
|
---|
167 | WSAGetLastError_Ptr = NULL;
|
---|
168 | WSAIsBlocking_Ptr = NULL;
|
---|
169 | WSAUnhookBlockingHook_Ptr = NULL;
|
---|
170 | WSASetBlockingHook_Ptr = NULL;
|
---|
171 | WSACancelBlockingCall_Ptr = NULL;
|
---|
172 | WSAAsyncGetServByName_Ptr = NULL;
|
---|
173 | WSAAsyncGetServByPort_Ptr = NULL;
|
---|
174 | WSAAsyncGetProtoByName_Ptr = NULL;
|
---|
175 | WSAAsyncGetProtoByNumber_Ptr = NULL;
|
---|
176 | WSAAsyncGetHostByName_Ptr = NULL;
|
---|
177 | WSAAsyncGetHostByAddr_Ptr = NULL;
|
---|
178 | WSACancelAsyncRequest_Ptr = NULL;
|
---|
179 | WSAAsyncSelect_Ptr = NULL;
|
---|
180 | }
|
---|
181 |
|
---|
182 | // get the pointers to all the socket functions
|
---|
183 | static void SetupWSPtrs (HINSTANCE socketlib) {
|
---|
184 | if (socketlib == NULL) return;
|
---|
185 |
|
---|
186 | accept_Ptr = (accept_Type)GetProcAddress(socketlib,"accept");
|
---|
187 | bind_Ptr = (bind_Type)GetProcAddress(socketlib,"bind");
|
---|
188 | closesocket_Ptr = (closesocket_Type)GetProcAddress(socketlib,"closesocket");
|
---|
189 | connect_Ptr = (connect_Type)GetProcAddress(socketlib,"connect");
|
---|
190 | ioctlsocket_Ptr = (ioctlsocket_Type)GetProcAddress(socketlib,"ioctlsocket");
|
---|
191 | getpeername_Ptr = (getpeername_Type)GetProcAddress(socketlib,"getpeername");
|
---|
192 | getsockname_Ptr = (getsockname_Type)GetProcAddress(socketlib,"getsockname");
|
---|
193 | getsockopt_Ptr = (getsockopt_Type)GetProcAddress(socketlib,"getsockopt");
|
---|
194 | htonl_Ptr = (htonl_Type)GetProcAddress(socketlib,"htonl");
|
---|
195 | htons_Ptr = (htons_Type)GetProcAddress(socketlib,"htons");
|
---|
196 | inet_addr_Ptr = (inet_addr_Type)GetProcAddress(socketlib,"inet_addr");
|
---|
197 | inet_ntoa_Ptr = (inet_ntoa_Type)GetProcAddress(socketlib,"inet_ntoa");
|
---|
198 | listen_Ptr = (listen_Type)GetProcAddress(socketlib,"listen");
|
---|
199 | ntohl_Ptr = (ntohl_Type)GetProcAddress(socketlib,"ntohl");
|
---|
200 | ntohs_Ptr = (ntohs_Type)GetProcAddress(socketlib,"ntohs");
|
---|
201 | recv_Ptr = (recv_Type)GetProcAddress(socketlib,"recv");
|
---|
202 | recvfrom_Ptr = (recvfrom_Type)GetProcAddress(socketlib,"recvfrom");
|
---|
203 | select_Ptr = (select_Type)GetProcAddress(socketlib,"select");
|
---|
204 | send_Ptr = (send_Type)GetProcAddress(socketlib,"send");
|
---|
205 | sendto_Ptr = (sendto_Type)GetProcAddress(socketlib,"sendto");
|
---|
206 | setsockopt_Ptr = (setsockopt_Type)GetProcAddress(socketlib,"setsockopt");
|
---|
207 | shutdown_Ptr = (shutdown_Type)GetProcAddress(socketlib,"shutdown");
|
---|
208 | socket_Ptr = (socket_Type)GetProcAddress(socketlib,"socket");
|
---|
209 | gethostbyaddr_Ptr = (gethostbyaddr_Type)GetProcAddress(socketlib,"gethostbyaddr");
|
---|
210 | gethostbyname_Ptr = (gethostbyname_Type)GetProcAddress(socketlib,"gethostbyname");
|
---|
211 | gethostname_Ptr = (gethostname_Type)GetProcAddress(socketlib,"gethostname");
|
---|
212 | getservbyport_Ptr = (getservbyport_Type)GetProcAddress(socketlib,"getservbyport");
|
---|
213 | getservbyname_Ptr = (getservbyname_Type)GetProcAddress(socketlib,"getservbyname");
|
---|
214 | getprotobynumber_Ptr = (getprotobynumber_Type)GetProcAddress(socketlib,"getprotobynumber");
|
---|
215 | getprotobyname_Ptr = (getprotobyname_Type)GetProcAddress(socketlib,"getprotobyname");
|
---|
216 | WSAStartup_Ptr = (WSAStartup_Type)GetProcAddress(socketlib,"WSAStartup");
|
---|
217 | WSACleanup_Ptr = (WSACleanup_Type)GetProcAddress(socketlib,"WSACleanup");
|
---|
218 | WSASetLastError_Ptr = (WSASetLastError_Type)GetProcAddress(socketlib,"WSASetLastError");
|
---|
219 | WSAGetLastError_Ptr = (WSAGetLastError_Type)GetProcAddress(socketlib,"WSAGetLastError");
|
---|
220 | WSAIsBlocking_Ptr = (WSAIsBlocking_Type)GetProcAddress(socketlib,"WSAIsBlocking");
|
---|
221 | WSAUnhookBlockingHook_Ptr =
|
---|
222 | (WSAUnhookBlockingHook_Type)GetProcAddress(socketlib,"WSAUnhookBlockingHook");
|
---|
223 | WSASetBlockingHook_Ptr =
|
---|
224 | (WSASetBlockingHook_Type)GetProcAddress(socketlib,"WSASetBlockingHook");
|
---|
225 | WSACancelBlockingCall_Ptr =
|
---|
226 | (WSACancelBlockingCall_Type)GetProcAddress(socketlib,"WSACancelBlockingCall");
|
---|
227 | WSAAsyncGetServByName_Ptr =
|
---|
228 | (WSAAsyncGetServByName_Type)GetProcAddress(socketlib,"WSAAsyncGetServByName");
|
---|
229 | WSAAsyncGetServByPort_Ptr =
|
---|
230 | (WSAAsyncGetServByPort_Type)GetProcAddress(socketlib,"WSAAsyncGetServByPort");
|
---|
231 | WSAAsyncGetProtoByName_Ptr =
|
---|
232 | (WSAAsyncGetProtoByName_Type)GetProcAddress(socketlib,"WSAAsyncGetProtoByName");
|
---|
233 | WSAAsyncGetProtoByNumber_Ptr =
|
---|
234 | (WSAAsyncGetProtoByNumber_Type)GetProcAddress(socketlib,"WSAAsyncGetProtoByNumber");
|
---|
235 | WSAAsyncGetHostByName_Ptr =
|
---|
236 | (WSAAsyncGetHostByName_Type)GetProcAddress(socketlib,"WSAAsyncGetHostByName");
|
---|
237 | WSAAsyncGetHostByAddr_Ptr =
|
---|
238 | (WSAAsyncGetHostByAddr_Type)GetProcAddress(socketlib,"WSAAsyncGetHostByAddr");
|
---|
239 | WSACancelAsyncRequest_Ptr =
|
---|
240 | (WSACancelAsyncRequest_Type)GetProcAddress(socketlib,"WSACancelAsyncRequest");
|
---|
241 | WSAAsyncSelect_Ptr = (WSAAsyncSelect_Type)GetProcAddress(socketlib,"WSAAsyncSelect");
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 |
|
---|
246 | // expects path to be NULL, a empty string, or a path which ends in a slash
|
---|
247 | // returns D_NOERROR on success,
|
---|
248 | // D_ERR_UNEXPECTED or D_ERR_LOADFAILED on failure
|
---|
249 | int d_LoadWinsock (char *path) {
|
---|
250 | char dllname[2048]; // HARD LIMIT!!!!
|
---|
251 | OSVERSIONINFO osver;
|
---|
252 |
|
---|
253 | if ((wsock32lib != NULL) || (winsocklib != NULL)) return D_ERR_UNEXPECTED;
|
---|
254 |
|
---|
255 | // set the error mode so that no nasty dialog boxes come
|
---|
256 | // up when things aren't installed
|
---|
257 | int olderrormode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
|
---|
258 |
|
---|
259 | // get the operating system version information
|
---|
260 | memset(&osver, 0, sizeof(OSVERSIONINFO));
|
---|
261 | osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
---|
262 | if (!GetVersionEx(&osver)) {
|
---|
263 | SetErrorMode(olderrormode);
|
---|
264 | return D_ERR_UNEXPECTED;
|
---|
265 | }
|
---|
266 |
|
---|
267 | // see if we should load winsock before wsock32
|
---|
268 | if ((osver.dwPlatformId == VER_PLATFORM_WIN32s) && (path != NULL)) {
|
---|
269 | strcpy (dllname, path);
|
---|
270 | strcat (dllname, "winsock.dll");
|
---|
271 |
|
---|
272 | // attempt to load the library
|
---|
273 | winsocklib = LoadLibrary (dllname);
|
---|
274 | if (winsocklib == NULL) {
|
---|
275 | SetErrorMode(olderrormode);
|
---|
276 | return D_ERR_LOADFAILED;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | // attempt to load wsock32
|
---|
281 | int err = D_NOERROR;
|
---|
282 | dllname[0] = '\0';
|
---|
283 | if ((osver.dwPlatformId != VER_PLATFORM_WIN32s) && (path != NULL)) {
|
---|
284 | strcpy (dllname, path);
|
---|
285 | }
|
---|
286 | strcat (dllname, "wsock32.dll");
|
---|
287 | wsock32lib = LoadLibrary (dllname);
|
---|
288 | if (wsock32lib == NULL) err = D_ERR_LOADFAILED;
|
---|
289 | else SetupWSPtrs (wsock32lib);
|
---|
290 |
|
---|
291 |
|
---|
292 | // perform a few tests
|
---|
293 |
|
---|
294 | // first try starting-up (and check the szDescription field)
|
---|
295 | if (err == D_NOERROR) {
|
---|
296 | WSADATA Data;
|
---|
297 | memset(&Data, 0, sizeof(WSADATA));
|
---|
298 | if ((d_WSAStartup(257, &Data) != 0) ||
|
---|
299 | (Data.szDescription == NULL) ||
|
---|
300 | (Data.szDescription[0] == '\0'))
|
---|
301 | err = D_ERR_LOADFAILED;
|
---|
302 | }
|
---|
303 |
|
---|
304 | // next try setting and getting an error
|
---|
305 | if (err == D_NOERROR) {
|
---|
306 | d_WSASetLastError (WSAENOTSOCK);
|
---|
307 | if (d_WSAGetLastError () != WSAENOTSOCK) err = D_ERR_LOADFAILED;
|
---|
308 | d_WSASetLastError (0);
|
---|
309 | }
|
---|
310 |
|
---|
311 | // last try cleaning up (if an error occurs here we might
|
---|
312 | // not be able to unload anything)
|
---|
313 | if ((err == D_NOERROR) && (d_WSACleanup() == SOCKET_ERROR))
|
---|
314 | err = D_ERR_LOADFAILED;
|
---|
315 |
|
---|
316 | // if an error occurred then we need to unload everything
|
---|
317 | if (err != D_NOERROR) d_UnloadWinsock ();
|
---|
318 |
|
---|
319 | SetErrorMode(olderrormode);
|
---|
320 | return err;
|
---|
321 | }
|
---|
322 |
|
---|
323 | // returns D_NOERROR on success,
|
---|
324 | // D_ERR_FREELIBFAILED on failure
|
---|
325 | int d_UnloadWinsock () {
|
---|
326 | int err = D_NOERROR;
|
---|
327 |
|
---|
328 | // unload wsock32
|
---|
329 | if ((wsock32lib != NULL) && !FreeLibrary (wsock32lib)) {
|
---|
330 | err = D_ERR_FREELIBFAILED;
|
---|
331 | }
|
---|
332 | wsock32lib = NULL;
|
---|
333 |
|
---|
334 | // unload winsock
|
---|
335 | if ((winsocklib != NULL) && !FreeLibrary (winsocklib)) {
|
---|
336 | err = D_ERR_FREELIBFAILED;
|
---|
337 | }
|
---|
338 | winsocklib = NULL;
|
---|
339 |
|
---|
340 | // reset all the function pointers
|
---|
341 | ResetWSPtrs ();
|
---|
342 |
|
---|
343 | return err;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 |
|
---|
348 |
|
---|
349 | // socket functions, these just call the dynamically loaded version
|
---|
350 |
|
---|
351 | SOCKET d_accept (SOCKET s, struct sockaddr *addr, int *addrlen) {
|
---|
352 | if (accept_Ptr != NULL)
|
---|
353 | return (*accept_Ptr)(s, addr, addrlen);
|
---|
354 | return INVALID_SOCKET; // error winsock not loaded
|
---|
355 | }
|
---|
356 |
|
---|
357 | int d_bind (SOCKET s, const struct sockaddr *addr, int namelen) {
|
---|
358 | if (bind_Ptr != NULL)
|
---|
359 | return (*bind_Ptr)(s, addr, namelen);
|
---|
360 | return WSANOTINITIALISED; // error winsock not loaded
|
---|
361 | }
|
---|
362 |
|
---|
363 | int d_closesocket (SOCKET s) {
|
---|
364 | if (closesocket_Ptr != NULL)
|
---|
365 | return (*closesocket_Ptr)(s);
|
---|
366 | return SOCKET_ERROR; // error winsock not loaded
|
---|
367 | }
|
---|
368 |
|
---|
369 | int d_connect (SOCKET s, const struct sockaddr *name, int namelen) {
|
---|
370 | if (connect_Ptr != NULL)
|
---|
371 | return (*connect_Ptr)(s, name, namelen);
|
---|
372 | return SOCKET_ERROR; // error winsock not loaded
|
---|
373 | }
|
---|
374 |
|
---|
375 | int d_ioctlsocket (SOCKET s, long cmd, u_long *argp) {
|
---|
376 | if (ioctlsocket_Ptr != NULL)
|
---|
377 | return (*ioctlsocket_Ptr)(s, cmd, argp);
|
---|
378 | return SOCKET_ERROR; // error winsock not loaded
|
---|
379 | }
|
---|
380 |
|
---|
381 | int d_getpeername (SOCKET s, struct sockaddr *name, int *namelen) {
|
---|
382 | if (getpeername_Ptr != NULL)
|
---|
383 | return (*getpeername_Ptr)(s, name, namelen);
|
---|
384 | return SOCKET_ERROR; // error winsock not loaded
|
---|
385 | }
|
---|
386 |
|
---|
387 | int d_getsockname (SOCKET s, struct sockaddr *name, int *namelen) {
|
---|
388 | if (getsockname_Ptr != NULL)
|
---|
389 | return (*getsockname_Ptr)(s, name, namelen);
|
---|
390 | return SOCKET_ERROR; // error winsock not loaded
|
---|
391 | }
|
---|
392 |
|
---|
393 | int d_getsockopt (SOCKET s, int level, int optname, char *optval, int *optlen) {
|
---|
394 | if (getsockopt_Ptr != NULL)
|
---|
395 | return (*getsockopt_Ptr)(s, level, optname, optval, optlen);
|
---|
396 | return SOCKET_ERROR; // error winsock not loaded
|
---|
397 | }
|
---|
398 |
|
---|
399 | u_long d_htonl (u_long hostlong) {
|
---|
400 | if (htonl_Ptr != NULL)
|
---|
401 | return (*htonl_Ptr)(hostlong);
|
---|
402 | return hostlong; // error winsock not loaded
|
---|
403 | }
|
---|
404 |
|
---|
405 | u_short d_htons (u_short hostshort) {
|
---|
406 | if (htons_Ptr != NULL)
|
---|
407 | return (*htons_Ptr)(hostshort);
|
---|
408 | return hostshort; // error winsock not loaded
|
---|
409 | }
|
---|
410 |
|
---|
411 | unsigned long d_inet_addr (const char *cp) {
|
---|
412 | if (inet_addr_Ptr != NULL)
|
---|
413 | return (*inet_addr_Ptr)(cp);
|
---|
414 | return INADDR_NONE; // error winsock not loaded
|
---|
415 | }
|
---|
416 |
|
---|
417 | char *d_inet_ntoa (struct in_addr in) {
|
---|
418 | if (inet_ntoa_Ptr != NULL)
|
---|
419 | return (*inet_ntoa_Ptr)(in);
|
---|
420 | return NULL; // error winsock not loaded
|
---|
421 | }
|
---|
422 |
|
---|
423 | int d_listen (SOCKET s, int backlog) {
|
---|
424 | if (listen_Ptr != NULL)
|
---|
425 | return (*listen_Ptr)(s, backlog);
|
---|
426 | return SOCKET_ERROR; // error winsock not loaded
|
---|
427 | }
|
---|
428 |
|
---|
429 | u_long d_ntohl (u_long netlong) {
|
---|
430 | if (ntohl_Ptr != NULL)
|
---|
431 | return (*ntohl_Ptr)(netlong);
|
---|
432 | return netlong; // error winsock not loaded
|
---|
433 | }
|
---|
434 |
|
---|
435 | u_short d_ntohs (u_short netshort) {
|
---|
436 | if (ntohs_Ptr != NULL)
|
---|
437 | return (*ntohs_Ptr)(netshort);
|
---|
438 | return netshort; // error winsock not loaded
|
---|
439 | }
|
---|
440 |
|
---|
441 | int d_recv (SOCKET s, char *buf, int len, int flags) {
|
---|
442 | if (recv_Ptr != NULL)
|
---|
443 | return (*recv_Ptr)(s, buf, len, flags);
|
---|
444 | return SOCKET_ERROR; // error winsock not loaded
|
---|
445 | }
|
---|
446 |
|
---|
447 | int d_recvfrom (SOCKET s, char *buf, int len, int flags,
|
---|
448 | struct sockaddr *from, int *fromlen) {
|
---|
449 | if (recvfrom_Ptr != NULL)
|
---|
450 | return (*recvfrom_Ptr)(s, buf, len, flags, from, fromlen);
|
---|
451 | return SOCKET_ERROR; // error winsock not loaded
|
---|
452 | }
|
---|
453 |
|
---|
454 | int d_select (int nfds, fd_set *readfds, fd_set *writefds,
|
---|
455 | fd_set *exceptfds, const struct timeval *timeout) {
|
---|
456 | if (select_Ptr != NULL)
|
---|
457 | return (*select_Ptr)(nfds, readfds, writefds, exceptfds, timeout);
|
---|
458 | return SOCKET_ERROR; // error winsock not loaded
|
---|
459 | }
|
---|
460 |
|
---|
461 | int d_send (SOCKET s, const char *buf, int len, int flags) {
|
---|
462 | if (send_Ptr != NULL)
|
---|
463 | return (*send_Ptr)(s, buf, len, flags);
|
---|
464 | return SOCKET_ERROR; // error winsock not loaded
|
---|
465 | }
|
---|
466 |
|
---|
467 | int d_sendto (SOCKET s, const char *buf, int len, int flags,
|
---|
468 | const struct sockaddr *to, int tolen) {
|
---|
469 | if (sendto_Ptr != NULL)
|
---|
470 | return (*sendto_Ptr)(s, buf, len, flags, to, tolen);
|
---|
471 | return SOCKET_ERROR; // error winsock not loaded
|
---|
472 | }
|
---|
473 |
|
---|
474 | int d_setsockopt (SOCKET s, int level, int optname,
|
---|
475 | const char *optval, int optlen) {
|
---|
476 | if (setsockopt_Ptr != NULL)
|
---|
477 | return (*setsockopt_Ptr)(s, level, optname, optval, optlen);
|
---|
478 | return SOCKET_ERROR; // error winsock not loaded
|
---|
479 | }
|
---|
480 |
|
---|
481 | int d_shutdown (SOCKET s, int how) {
|
---|
482 | if (shutdown_Ptr != NULL)
|
---|
483 | return (*shutdown_Ptr)(s, how);
|
---|
484 | return SOCKET_ERROR; // error winsock not loaded
|
---|
485 | }
|
---|
486 |
|
---|
487 | SOCKET d_socket (int af, int type, int protocol) {
|
---|
488 | if (socket_Ptr != NULL)
|
---|
489 | return (*socket_Ptr)(af, type, protocol);
|
---|
490 | return INVALID_SOCKET; // error winsock not loaded
|
---|
491 | }
|
---|
492 |
|
---|
493 | hostent *d_gethostbyaddr(const char *addr, int len, int type) {
|
---|
494 | if (gethostbyaddr_Ptr != NULL)
|
---|
495 | return (*gethostbyaddr_Ptr)(addr, len, type);
|
---|
496 | return NULL; // error winsock not loaded
|
---|
497 | }
|
---|
498 |
|
---|
499 | hostent *d_gethostbyname(const char *name) {
|
---|
500 | if (gethostbyname_Ptr != NULL)
|
---|
501 | return (*gethostbyname_Ptr)(name);
|
---|
502 | return NULL; // error winsock not loaded
|
---|
503 | }
|
---|
504 |
|
---|
505 | int d_gethostname (char *name, int namelen) {
|
---|
506 | if (gethostname_Ptr != NULL)
|
---|
507 | return (*gethostname_Ptr)(name, namelen);
|
---|
508 | return SOCKET_ERROR; // error winsock not loaded
|
---|
509 | }
|
---|
510 |
|
---|
511 | servent *d_getservbyport(int port, const char *proto) {
|
---|
512 | if (getservbyport_Ptr != NULL)
|
---|
513 | return (*getservbyport_Ptr)(port, proto);
|
---|
514 | return NULL; // error winsock not loaded
|
---|
515 | }
|
---|
516 |
|
---|
517 | servent *d_getservbyname(const char *name, const char *proto) {
|
---|
518 | if (getservbyname_Ptr != NULL)
|
---|
519 | return (*getservbyname_Ptr)(name, proto);
|
---|
520 | return NULL; // error winsock not loaded
|
---|
521 | }
|
---|
522 |
|
---|
523 | protoent *d_getprotobynumber(int proto) {
|
---|
524 | if (getprotobynumber_Ptr != NULL)
|
---|
525 | return (*getprotobynumber_Ptr)(proto);
|
---|
526 | return NULL; // error winsock not loaded
|
---|
527 | }
|
---|
528 |
|
---|
529 | protoent *d_getprotobyname(const char *name) {
|
---|
530 | if (getprotobyname_Ptr != NULL)
|
---|
531 | return (*getprotobyname_Ptr)(name);
|
---|
532 | return NULL; // error winsock not loaded
|
---|
533 | }
|
---|
534 |
|
---|
535 | int d_WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData) {
|
---|
536 | if (WSAStartup_Ptr != NULL)
|
---|
537 | return (*WSAStartup_Ptr)(wVersionRequired, lpWSAData);
|
---|
538 | return WSASYSNOTREADY; // error winsock not loaded
|
---|
539 | }
|
---|
540 |
|
---|
541 | int d_WSACleanup(void) {
|
---|
542 | if (WSACleanup_Ptr != NULL)
|
---|
543 | return (*WSACleanup_Ptr)();
|
---|
544 | return WSANOTINITIALISED; // error winsock not loaded
|
---|
545 | }
|
---|
546 |
|
---|
547 | void d_WSASetLastError(int iError) {
|
---|
548 | if (WSASetLastError_Ptr != NULL) (*WSASetLastError_Ptr)(iError);
|
---|
549 | }
|
---|
550 |
|
---|
551 | int d_WSAGetLastError(void) {
|
---|
552 | if (WSAGetLastError_Ptr != NULL)
|
---|
553 | return (*WSAGetLastError_Ptr)();
|
---|
554 | return WSANOTINITIALISED; // error winsock not loaded
|
---|
555 | }
|
---|
556 |
|
---|
557 | BOOL d_WSAIsBlocking(void) {
|
---|
558 | if (WSAIsBlocking_Ptr != NULL)
|
---|
559 | return (*WSAIsBlocking_Ptr)();
|
---|
560 | return FALSE; // error winsock not loaded
|
---|
561 | }
|
---|
562 |
|
---|
563 | int d_WSAUnhookBlockingHook(void) {
|
---|
564 | if (WSAUnhookBlockingHook_Ptr != NULL)
|
---|
565 | return (*WSAUnhookBlockingHook_Ptr)();
|
---|
566 | return SOCKET_ERROR; // error winsock not loaded
|
---|
567 | }
|
---|
568 |
|
---|
569 | FARPROC d_WSASetBlockingHook(FARPROC lpBlockFunc) {
|
---|
570 | if (WSASetBlockingHook_Ptr != NULL)
|
---|
571 | return (*WSASetBlockingHook_Ptr)(lpBlockFunc);
|
---|
572 | return NULL; // error winsock not loaded
|
---|
573 | }
|
---|
574 |
|
---|
575 | int d_WSACancelBlockingCall(void) {
|
---|
576 | if (WSACancelBlockingCall_Ptr != NULL)
|
---|
577 | return (*WSACancelBlockingCall_Ptr)();
|
---|
578 | return SOCKET_ERROR; // error winsock not loaded
|
---|
579 | }
|
---|
580 |
|
---|
581 | HANDLE d_WSAAsyncGetServByName(HWND hWnd, u_int wMsg,
|
---|
582 | const char *name,
|
---|
583 | const char *proto,
|
---|
584 | char *buf, int buflen) {
|
---|
585 | if (WSAAsyncGetServByName_Ptr != NULL)
|
---|
586 | return (*WSAAsyncGetServByName_Ptr)(hWnd, wMsg, name, proto, buf, buflen);
|
---|
587 | return NULL; // error winsock not loaded
|
---|
588 | }
|
---|
589 |
|
---|
590 | HANDLE d_WSAAsyncGetServByPort(HWND hWnd, u_int wMsg, int port,
|
---|
591 | const char *proto, char *buf,
|
---|
592 | int buflen) {
|
---|
593 | if (WSAAsyncGetServByPort_Ptr != NULL)
|
---|
594 | return (*WSAAsyncGetServByPort_Ptr)(hWnd, wMsg, port, proto, buf, buflen);
|
---|
595 | return NULL; // error winsock not loaded
|
---|
596 | }
|
---|
597 |
|
---|
598 | HANDLE d_WSAAsyncGetProtoByName(HWND hWnd, u_int wMsg,
|
---|
599 | const char *name, char *buf,
|
---|
600 | int buflen) {
|
---|
601 | if (WSAAsyncGetProtoByName_Ptr != NULL)
|
---|
602 | return (*WSAAsyncGetProtoByName_Ptr)(hWnd, wMsg, name, buf, buflen);
|
---|
603 | return NULL; // error winsock not loaded
|
---|
604 | }
|
---|
605 |
|
---|
606 | HANDLE d_WSAAsyncGetProtoByNumber(HWND hWnd, u_int wMsg,
|
---|
607 | int number, char *buf,
|
---|
608 | int buflen) {
|
---|
609 | if (WSAAsyncGetProtoByNumber_Ptr != NULL)
|
---|
610 | return (*WSAAsyncGetProtoByNumber_Ptr)(hWnd, wMsg, number, buf, buflen);
|
---|
611 | return NULL; // error winsock not loaded
|
---|
612 | }
|
---|
613 |
|
---|
614 | HANDLE d_WSAAsyncGetHostByName(HWND hWnd, u_int wMsg,
|
---|
615 | const char *name, char *buf,
|
---|
616 | int buflen) {
|
---|
617 | if (WSAAsyncGetHostByName_Ptr != NULL)
|
---|
618 | return (*WSAAsyncGetHostByName_Ptr)(hWnd, wMsg, name, buf, buflen);
|
---|
619 | return NULL; // error winsock not loaded
|
---|
620 | }
|
---|
621 |
|
---|
622 | HANDLE d_WSAAsyncGetHostByAddr(HWND hWnd, u_int wMsg,
|
---|
623 | const char *addr, int len, int type,
|
---|
624 | char *buf, int buflen) {
|
---|
625 | if (WSAAsyncGetHostByAddr_Ptr != NULL)
|
---|
626 | return (*WSAAsyncGetHostByAddr_Ptr)(hWnd, wMsg, addr, len, type, buf, buflen);
|
---|
627 | return NULL; // error winsock not loaded
|
---|
628 | }
|
---|
629 |
|
---|
630 | int d_WSACancelAsyncRequest(HANDLE hAsyncTaskHandle) {
|
---|
631 | if (WSACancelAsyncRequest_Ptr != NULL)
|
---|
632 | return (*WSACancelAsyncRequest_Ptr)(hAsyncTaskHandle);
|
---|
633 | return SOCKET_ERROR; // error winsock not loaded
|
---|
634 | }
|
---|
635 |
|
---|
636 | int d_WSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg,
|
---|
637 | long lEvent) {
|
---|
638 | if (WSAAsyncSelect_Ptr != NULL)
|
---|
639 | return (*WSAAsyncSelect_Ptr)(s, hWnd, wMsg, lEvent);
|
---|
640 | return SOCKET_ERROR; // error winsock not loaded
|
---|
641 | }
|
---|
642 |
|
---|