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