| 1 | /* $Id: asyncapi.cpp,v 1.1 2000-03-22 20:01:04 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | *
|
|---|
| 5 | * Win32 SOCK32 for OS/2 (Async apis)
|
|---|
| 6 | *
|
|---|
| 7 | * Copyright (C) 2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 8 | *
|
|---|
| 9 | * Based on Wine code: (dlls\winsock\async.c)
|
|---|
| 10 | * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
|
|---|
| 11 | * (C) 1999 Marcus Meissner
|
|---|
| 12 | *
|
|---|
| 13 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 14 | *
|
|---|
| 15 | */
|
|---|
| 16 | #define INCL_BASE
|
|---|
| 17 | #include <os2wrap.h>
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <string.h>
|
|---|
| 20 | #include <odin.h>
|
|---|
| 21 | #include <odinwrap.h>
|
|---|
| 22 | #include <os2sel.h>
|
|---|
| 23 | #include <misc.h>
|
|---|
| 24 | #include <win32api.h>
|
|---|
| 25 | #include "wsock32.h"
|
|---|
| 26 | #include "asyncthread.h"
|
|---|
| 27 | #include "wsastruct.h"
|
|---|
| 28 |
|
|---|
| 29 | #define DBG_LOCALLOG DBG_async
|
|---|
| 30 | #include "dbglocal.h"
|
|---|
| 31 |
|
|---|
| 32 | ODINDEBUGCHANNEL(WSOCK32-ASYNC)
|
|---|
| 33 |
|
|---|
| 34 | //******************************************************************************
|
|---|
| 35 | //******************************************************************************
|
|---|
| 36 | void ASYNCCNV WSAsyncThreadProc(void *pparm)
|
|---|
| 37 | {
|
|---|
| 38 | PASYNCTHREADPARM pThreadParm = (PASYNCTHREADPARM)pparm;
|
|---|
| 39 | LPARAM lParam;
|
|---|
| 40 | int size = 0;
|
|---|
| 41 | int fail = NO_ERROR;
|
|---|
| 42 |
|
|---|
| 43 | if(!pThreadParm->fCancelled)
|
|---|
| 44 | {
|
|---|
| 45 | switch(pThreadParm->request)
|
|---|
| 46 | {
|
|---|
| 47 | case ASYNC_GETHOSTBYNAME:
|
|---|
| 48 | case ASYNC_GETHOSTBYADDR:
|
|---|
| 49 | {
|
|---|
| 50 | struct hostent *ret;
|
|---|
| 51 |
|
|---|
| 52 | if(pThreadParm->request == ASYNC_GETHOSTBYNAME) {
|
|---|
| 53 | ret = gethostbyname(pThreadParm->u.gethostbyname.name);
|
|---|
| 54 |
|
|---|
| 55 | free(pThreadParm->u.gethostbyname.name);
|
|---|
| 56 | pThreadParm->u.gethostbyname.name = 0;
|
|---|
| 57 | }
|
|---|
| 58 | else {
|
|---|
| 59 | ret = gethostbyaddr(pThreadParm->u.gethostbyaddr.addr,
|
|---|
| 60 | pThreadParm->u.gethostbyaddr.len,
|
|---|
| 61 | pThreadParm->u.gethostbyaddr.type);
|
|---|
| 62 |
|
|---|
| 63 | free(pThreadParm->u.gethostbyaddr.addr);
|
|---|
| 64 | pThreadParm->u.gethostbyaddr.addr = 0;
|
|---|
| 65 | }
|
|---|
| 66 | if (ret != NULL) {
|
|---|
| 67 | size = WS_copy_he((struct ws_hostent *)pThreadParm->buf,
|
|---|
| 68 | pThreadParm->buf,
|
|---|
| 69 | pThreadParm->buflen,
|
|---|
| 70 | ret);
|
|---|
| 71 | if(size < 0) {
|
|---|
| 72 | fail = WSAENOBUFS;
|
|---|
| 73 | size = -size;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | else fail = WSANO_DATA;
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | case ASYNC_GETPROTOBYNAME:
|
|---|
| 81 | case ASYNC_GETPROTOBYNUMBER:
|
|---|
| 82 | {
|
|---|
| 83 | struct protoent *ret;
|
|---|
| 84 |
|
|---|
| 85 | if(pThreadParm->request == ASYNC_GETPROTOBYNAME) {
|
|---|
| 86 | ret = getprotobyname(pThreadParm->u.getprotobyname.name);
|
|---|
| 87 |
|
|---|
| 88 | free(pThreadParm->u.getprotobyname.name);
|
|---|
| 89 | pThreadParm->u.getprotobyname.name = 0;
|
|---|
| 90 | }
|
|---|
| 91 | else ret = getprotobynumber(pThreadParm->u.getprotobynumber.number);
|
|---|
| 92 |
|
|---|
| 93 | if (ret != NULL) {
|
|---|
| 94 | size = WS_copy_pe((struct ws_protoent *)pThreadParm->buf,
|
|---|
| 95 | pThreadParm->buf,
|
|---|
| 96 | pThreadParm->buflen,
|
|---|
| 97 | ret);
|
|---|
| 98 | if(size < 0) {
|
|---|
| 99 | fail = WSAENOBUFS;
|
|---|
| 100 | size = -size;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | else fail = WSANO_DATA;
|
|---|
| 104 |
|
|---|
| 105 | break;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | case ASYNC_GETSERVBYNAME:
|
|---|
| 109 | case ASYNC_GETSERVBYPORT:
|
|---|
| 110 | {
|
|---|
| 111 | struct servent *ret;
|
|---|
| 112 |
|
|---|
| 113 | if(pThreadParm->request == ASYNC_GETSERVBYNAME) {
|
|---|
| 114 | ret = getservbyname(pThreadParm->u.getservbyname.name, pThreadParm->u.getservbyname.proto);
|
|---|
| 115 |
|
|---|
| 116 | free(pThreadParm->u.getservbyname.name);
|
|---|
| 117 | pThreadParm->u.getservbyname.name = 0;
|
|---|
| 118 |
|
|---|
| 119 | if(pThreadParm->u.getservbyname.proto) {
|
|---|
| 120 | free(pThreadParm->u.getservbyname.proto);
|
|---|
| 121 | pThreadParm->u.getservbyname.proto = 0;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | }
|
|---|
| 125 | else {
|
|---|
| 126 | ret = getservbyport(pThreadParm->u.getservbyport.port, pThreadParm->u.getservbyport.proto);
|
|---|
| 127 | if(pThreadParm->u.getservbyport.proto) {
|
|---|
| 128 | free(pThreadParm->u.getservbyport.proto);
|
|---|
| 129 | pThreadParm->u.getservbyport.proto = 0;
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if (ret != NULL) {
|
|---|
| 134 | size = WS_copy_se((struct ws_servent *)pThreadParm->buf,
|
|---|
| 135 | pThreadParm->buf,
|
|---|
| 136 | pThreadParm->buflen,
|
|---|
| 137 | ret);
|
|---|
| 138 | if(size < 0) {
|
|---|
| 139 | fail = WSAENOBUFS;
|
|---|
| 140 | size = -size;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | else fail = WSANO_DATA;
|
|---|
| 144 | break;
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | lParam = (fail << 16) | size;
|
|---|
| 149 |
|
|---|
| 150 | if(!pThreadParm->fCancelled) {
|
|---|
| 151 | PostMessageA(pThreadParm->hwnd, pThreadParm->msg,
|
|---|
| 152 | (WPARAM)pThreadParm->hAsyncTaskHandle, lParam);
|
|---|
| 153 | }
|
|---|
| 154 | RemoveFromQueue(pThreadParm);
|
|---|
| 155 | }
|
|---|
| 156 | //******************************************************************************
|
|---|
| 157 | //******************************************************************************
|
|---|
| 158 | LHANDLE WSAAsyncRequest(AsyncRequestType requesttype, HWND hwnd, int msg, char *buf,
|
|---|
| 159 | int buflen, PVOID param1, PVOID param2, PVOID param3,
|
|---|
| 160 | PVOID param4)
|
|---|
| 161 | {
|
|---|
| 162 | PASYNCTHREADPARM pThreadParm;
|
|---|
| 163 | LHANDLE hHost = 0;
|
|---|
| 164 | LPSTR tempname;
|
|---|
| 165 | LPSTR tempproto;
|
|---|
| 166 | LPSTR tempaddr;
|
|---|
| 167 |
|
|---|
| 168 | if(!fWSAInitialized)
|
|---|
| 169 | {
|
|---|
| 170 | WSASetLastError(WSANOTINITIALISED);
|
|---|
| 171 | }
|
|---|
| 172 | else
|
|---|
| 173 | if(WSAIsBlocking())
|
|---|
| 174 | {
|
|---|
| 175 | WSASetLastError(WSAEINPROGRESS); // blocking call in progress
|
|---|
| 176 | }
|
|---|
| 177 | else
|
|---|
| 178 | if(!IsWindow(hwnd))
|
|---|
| 179 | {
|
|---|
| 180 | WSASetLastError(WSAEINVAL); // invalid parameter
|
|---|
| 181 | }
|
|---|
| 182 | else
|
|---|
| 183 | if(buf == NULL || buflen == 0) {
|
|---|
| 184 | WSASetLastError(WSAENOBUFS); // invalid parameter
|
|---|
| 185 | }
|
|---|
| 186 | else
|
|---|
| 187 | {
|
|---|
| 188 | pThreadParm = (PASYNCTHREADPARM)malloc(sizeof(ASYNCTHREADPARM));
|
|---|
| 189 | if(pThreadParm == NULL) {
|
|---|
| 190 | dprintf(("WSAAsyncRequest: malloc failure!"));
|
|---|
| 191 | DebugInt3();
|
|---|
| 192 | WSASetLastError(WSAEFAULT);
|
|---|
| 193 | return 0;
|
|---|
| 194 | }
|
|---|
| 195 | pThreadParm->request= requesttype;
|
|---|
| 196 | pThreadParm->hwnd = hwnd;
|
|---|
| 197 | pThreadParm->msg = msg;
|
|---|
| 198 | pThreadParm->buf = buf;
|
|---|
| 199 | pThreadParm->buflen = buflen;
|
|---|
| 200 |
|
|---|
| 201 | switch(requesttype) {
|
|---|
| 202 | case ASYNC_GETHOSTBYNAME:
|
|---|
| 203 | tempname = (LPSTR)malloc(strlen((char *)param1)+1);
|
|---|
| 204 | if(tempname == NULL)
|
|---|
| 205 | {
|
|---|
| 206 | dprintf(("WSAAsyncGetHostByName: malloc failure!"));
|
|---|
| 207 | DebugInt3();
|
|---|
| 208 | WSASetLastError(WSAEFAULT);
|
|---|
| 209 | return 0;
|
|---|
| 210 | }
|
|---|
| 211 | pThreadParm->u.gethostbyname.name = tempname;
|
|---|
| 212 | strcpy(pThreadParm->u.gethostbyname.name, (char *)param1);
|
|---|
| 213 | break;
|
|---|
| 214 |
|
|---|
| 215 | case ASYNC_GETHOSTBYADDR:
|
|---|
| 216 | tempaddr = (LPSTR)malloc(strlen((char *)param1)+1);
|
|---|
| 217 | if(tempaddr == NULL)
|
|---|
| 218 | {
|
|---|
| 219 | dprintf(("WSAAsyncGetHostByAddr: malloc failure!"));
|
|---|
| 220 | DebugInt3();
|
|---|
| 221 | WSASetLastError(WSAEFAULT);
|
|---|
| 222 | return 0;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | pThreadParm->u.gethostbyaddr.len = (int)param2;
|
|---|
| 226 | pThreadParm->u.gethostbyaddr.type = (int)param3;
|
|---|
| 227 | pThreadParm->u.gethostbyaddr.addr = tempaddr;
|
|---|
| 228 | strcpy(pThreadParm->u.gethostbyaddr.addr, (char *)param1);
|
|---|
| 229 | break;
|
|---|
| 230 |
|
|---|
| 231 | case ASYNC_GETPROTOBYNAME:
|
|---|
| 232 | tempname = (LPSTR)malloc(strlen((char *)param1)+1);
|
|---|
| 233 | if(tempname == NULL)
|
|---|
| 234 | {
|
|---|
| 235 | dprintf(("WSAAsyncGetProtoByName: malloc failure!"));
|
|---|
| 236 | DebugInt3();
|
|---|
| 237 | WSASetLastError(WSAEFAULT);
|
|---|
| 238 | return 0;
|
|---|
| 239 | }
|
|---|
| 240 | pThreadParm->u.getprotobyname.name = tempname;
|
|---|
| 241 | strcpy(pThreadParm->u.getprotobyname.name, (char *)param1);
|
|---|
| 242 | break;
|
|---|
| 243 |
|
|---|
| 244 | case ASYNC_GETPROTOBYNUMBER:
|
|---|
| 245 | pThreadParm->u.getprotobynumber.number = (int)param1;
|
|---|
| 246 | break;
|
|---|
| 247 |
|
|---|
| 248 | case ASYNC_GETSERVBYNAME:
|
|---|
| 249 | tempname = (LPSTR)malloc(strlen((char *)param1)+1);
|
|---|
| 250 | tempproto = NULL;
|
|---|
| 251 | if(param2) {
|
|---|
| 252 | tempproto = (LPSTR)malloc(strlen((char *)param2)+1);
|
|---|
| 253 | }
|
|---|
| 254 | if(tempname == NULL || (param2 && !tempproto))
|
|---|
| 255 | {
|
|---|
| 256 | dprintf(("WSAAsyncGetServByName: malloc failure!"));
|
|---|
| 257 | DebugInt3();
|
|---|
| 258 | WSASetLastError(WSAEFAULT);
|
|---|
| 259 | return 0;
|
|---|
| 260 | }
|
|---|
| 261 | pThreadParm->u.getservbyname.name = tempname;
|
|---|
| 262 | strcpy(pThreadParm->u.getservbyname.name, (char *)param1);
|
|---|
| 263 | pThreadParm->u.getservbyname.proto = tempproto;
|
|---|
| 264 | if(param2)
|
|---|
| 265 | strcpy(pThreadParm->u.getservbyname.proto, (char *)param2);
|
|---|
| 266 | break;
|
|---|
| 267 |
|
|---|
| 268 | case ASYNC_GETSERVBYPORT:
|
|---|
| 269 | if(param2) {
|
|---|
| 270 | tempproto = (LPSTR)malloc(strlen((char *)param2)+1);
|
|---|
| 271 | }
|
|---|
| 272 | if(param2 && !tempproto)
|
|---|
| 273 | {
|
|---|
| 274 | dprintf(("WSAAsyncGetServByPort: malloc failure!"));
|
|---|
| 275 | DebugInt3();
|
|---|
| 276 | WSASetLastError(WSAEFAULT);
|
|---|
| 277 | return 0;
|
|---|
| 278 | }
|
|---|
| 279 | pThreadParm->u.getservbyport.port = (int)param1;
|
|---|
| 280 | pThreadParm->u.getservbyport.proto = tempproto;
|
|---|
| 281 | if(param2)
|
|---|
| 282 | strcpy(pThreadParm->u.getservbyport.proto, (char *)param2);
|
|---|
| 283 | break;
|
|---|
| 284 | }
|
|---|
| 285 | return (LHANDLE)QueueAsyncJob(WSAsyncThreadProc, pThreadParm);
|
|---|
| 286 | }
|
|---|
| 287 | return 0;
|
|---|
| 288 | }
|
|---|
| 289 | //******************************************************************************
|
|---|
| 290 | //******************************************************************************
|
|---|
| 291 | ODINFUNCTION5(LHANDLE,WSAAsyncGetHostByName,
|
|---|
| 292 | HWND,hWnd,
|
|---|
| 293 | u_int,wMsg,
|
|---|
| 294 | const char *,name,
|
|---|
| 295 | char *,buf,
|
|---|
| 296 | int,buflen)
|
|---|
| 297 | {
|
|---|
| 298 | dprintf(("WSAAsyncGetHostByName %s", name));
|
|---|
| 299 |
|
|---|
| 300 | return WSAAsyncRequest(ASYNC_GETHOSTBYNAME, hWnd, wMsg, buf, buflen,
|
|---|
| 301 | (PVOID)name, 0, 0, 0);
|
|---|
| 302 | }
|
|---|
| 303 | //******************************************************************************
|
|---|
| 304 | //******************************************************************************
|
|---|
| 305 | ODINFUNCTION7(LHANDLE,WSAAsyncGetHostByAddr,
|
|---|
| 306 | HWND,hWnd,
|
|---|
| 307 | u_int,wMsg,
|
|---|
| 308 | const char *,addr,
|
|---|
| 309 | int,len,
|
|---|
| 310 | int,type,
|
|---|
| 311 | char *,buf,
|
|---|
| 312 | int,buflen)
|
|---|
| 313 | {
|
|---|
| 314 | dprintf(("WSAAsyncGetHostByAddr %s", addr));
|
|---|
| 315 |
|
|---|
| 316 | return WSAAsyncRequest(ASYNC_GETHOSTBYADDR, hWnd, wMsg, buf, buflen,
|
|---|
| 317 | (PVOID)addr, (PVOID)len, (PVOID)type, 0);
|
|---|
| 318 | }
|
|---|
| 319 | //******************************************************************************
|
|---|
| 320 | //******************************************************************************
|
|---|
| 321 | ODINFUNCTION5(LHANDLE,WSAAsyncGetProtoByName,
|
|---|
| 322 | HWND,hWnd,
|
|---|
| 323 | u_int,wMsg,
|
|---|
| 324 | const char *,name,
|
|---|
| 325 | char *,buf,
|
|---|
| 326 | int,buflen)
|
|---|
| 327 | {
|
|---|
| 328 | dprintf(("WSAAsyncGetProtoByName %s", name));
|
|---|
| 329 |
|
|---|
| 330 | return WSAAsyncRequest(ASYNC_GETPROTOBYNAME, hWnd, wMsg, buf, buflen,
|
|---|
| 331 | (PVOID)name, 0, 0, 0);
|
|---|
| 332 | }
|
|---|
| 333 | //******************************************************************************
|
|---|
| 334 | //******************************************************************************
|
|---|
| 335 | ODINFUNCTION5(LHANDLE,WSAAsyncGetProtoByNumber,
|
|---|
| 336 | HWND,hWnd,
|
|---|
| 337 | u_int,wMsg,
|
|---|
| 338 | int,number,
|
|---|
| 339 | char *,buf,
|
|---|
| 340 | int,buflen)
|
|---|
| 341 | {
|
|---|
| 342 | dprintf(("WSAAsyncGetProtoByNumber %d", number));
|
|---|
| 343 |
|
|---|
| 344 | return WSAAsyncRequest(ASYNC_GETPROTOBYNUMBER, hWnd, wMsg, buf, buflen,
|
|---|
| 345 | (PVOID)number, 0, 0, 0);
|
|---|
| 346 | }
|
|---|
| 347 | //******************************************************************************
|
|---|
| 348 | //******************************************************************************
|
|---|
| 349 | ODINFUNCTION6(LHANDLE,WSAAsyncGetServByName,
|
|---|
| 350 | HWND,hWnd,
|
|---|
| 351 | u_int,wMsg,
|
|---|
| 352 | const char *,name,
|
|---|
| 353 | const char *,proto,
|
|---|
| 354 | char *,buf,
|
|---|
| 355 | int,buflen)
|
|---|
| 356 | {
|
|---|
| 357 | dprintf(("WSAAsyncGetServByName %s", name));
|
|---|
| 358 |
|
|---|
| 359 | return WSAAsyncRequest(ASYNC_GETSERVBYNAME, hWnd, wMsg, buf, buflen,
|
|---|
| 360 | (PVOID)name, (PVOID)proto, 0, 0);
|
|---|
| 361 | }
|
|---|
| 362 | //******************************************************************************
|
|---|
| 363 | //******************************************************************************
|
|---|
| 364 | ODINFUNCTION6(LHANDLE,WSAAsyncGetServByPort,
|
|---|
| 365 | HWND,hWnd,
|
|---|
| 366 | u_int,wMsg,
|
|---|
| 367 | int,port,
|
|---|
| 368 | const char *,proto,
|
|---|
| 369 | char *,buf,
|
|---|
| 370 | int,buflen)
|
|---|
| 371 | {
|
|---|
| 372 | dprintf(("WSAAsyncGetServByPort %d %s", port, proto));
|
|---|
| 373 |
|
|---|
| 374 | return WSAAsyncRequest(ASYNC_GETSERVBYPORT, hWnd, wMsg, buf, buflen,
|
|---|
| 375 | (PVOID)port, (PVOID)proto, 0, 0);
|
|---|
| 376 | }
|
|---|
| 377 | //******************************************************************************
|
|---|
| 378 | //******************************************************************************
|
|---|
| 379 | ODINFUNCTION4(int,WSAAsyncSelect,
|
|---|
| 380 | SOCKET,s,
|
|---|
| 381 | HWND,hWnd,
|
|---|
| 382 | u_int,wMsg,
|
|---|
| 383 | long,lEvent)
|
|---|
| 384 | {
|
|---|
| 385 | return SOCKET_ERROR;
|
|---|
| 386 | }
|
|---|
| 387 | //******************************************************************************
|
|---|
| 388 | //******************************************************************************
|
|---|