Added MMDEVLDR DeviceIoctl(5) stub (msacm32 is a bit happier now).
[wine/hacks.git] / misc / winsock_async.c
blob03f8084802b410b9e61386c0b90eee86b7250f4d
1 /* Async WINSOCK DNS services
2 *
3 * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
4 * (C) 1999 Marcus Meissner
6 * NOTE: If you make any changes to fix a particular app, make sure
7 * they don't break something else like Netscape or telnet and ftp
8 * clients and servers (www.winsite.com got a lot of those).
9 *
10 * FIXME:
11 * - Add WSACancel* and correct handle management. (works rather well for
12 * now without it.)
13 * - Verify & Check all calls for correctness
14 * (currently only WSAGetHostByName*, WSAGetServByPort* calls)
15 * - Check error returns.
16 * - mirc/mirc32 Finger @linux.kernel.org sometimes fails in threaded mode.
17 * (not sure why)
18 * - This implementation did ignore the "NOTE:" section above (since the
19 * whole stuff did not work anyway to other changes).
22 #include "config.h"
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/ipc.h>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_FILIO_H
29 # include <sys/filio.h>
30 #endif
31 #if defined(__svr4__)
32 #include <sys/ioccom.h>
33 #include <sys/sockio.h>
34 #endif
36 #if defined(__EMX__)
37 # include <sys/so_ioctl.h>
38 #endif
40 #ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
42 #endif
44 #include <sys/msg.h>
45 #include <sys/wait.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <ctype.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <sys/errno.h>
53 #include <netdb.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #ifdef HAVE_ARPA_NAMESER_H
57 # include <arpa/nameser.h>
58 #endif
59 #ifdef HAVE_RESOLV_H
60 # include <resolv.h>
61 #endif
63 #include "wine/winbase16.h"
64 #include "winsock.h"
65 #include "winnt.h"
66 #include "heap.h"
67 #include "task.h"
68 #include "message.h"
69 #include "miscemu.h"
70 #include "debug.h"
72 DEFAULT_DEBUG_CHANNEL(winsock)
74 #pragma pack(4)
76 /* ----------------------------------- helper functions - */
78 static int list_size(char** l, int item_size)
80 int i,j = 0;
81 if(l)
82 { for(i=0;l[i];i++)
83 j += (item_size) ? item_size : strlen(l[i]) + 1;
84 j += (i + 1) * sizeof(char*); }
85 return j;
88 static int list_dup(char** l_src, char* ref, char* base, int item_size)
90 /* base is either either equal to ref or 0 or SEGPTR */
92 char* p = ref;
93 char** l_to = (char**)ref;
94 int i,j,k;
96 for(j=0;l_src[j];j++) ;
97 p += (j + 1) * sizeof(char*);
98 for(i=0;i<j;i++)
99 { l_to[i] = base + (p - ref);
100 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
101 memcpy(p, l_src[i], k); p += k; }
102 l_to[i] = NULL;
103 return (p - ref);
106 /* ----- hostent */
108 static int hostent_size(struct hostent* p_he)
110 int size = 0;
111 if( p_he )
112 { size = sizeof(struct hostent);
113 size += strlen(p_he->h_name) + 1;
114 size += list_size(p_he->h_aliases, 0);
115 size += list_size(p_he->h_addr_list, p_he->h_length ); }
116 return size;
119 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
120 * Win16 (linear vs. segmented). Return -neededsize on overrun.
122 static int WS_copy_he(struct ws_hostent *p_to,char *p_base,int t_size,struct hostent* p_he)
124 char* p_name,*p_aliases,*p_addr,*p;
125 int size=hostent_size(p_he)+(sizeof(struct ws_hostent)-sizeof(struct hostent));
127 if (t_size < size)
128 return -size;
129 p = (char*)p_to;
130 p += sizeof(struct ws_hostent);
131 p_name = p;
132 strcpy(p, p_he->h_name); p += strlen(p) + 1;
133 p_aliases = p;
134 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
135 p_addr = p;
136 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
138 p_to->h_addrtype = (INT16)p_he->h_addrtype;
139 p_to->h_length = (INT16)p_he->h_length;
140 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
141 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
142 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)p_to));
144 return size;
147 /* ----- protoent */
149 static int protoent_size(struct protoent* p_pe)
151 int size = 0;
152 if( p_pe )
153 { size = sizeof(struct protoent);
154 size += strlen(p_pe->p_name) + 1;
155 size += list_size(p_pe->p_aliases, 0); }
156 return size;
159 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
160 * Win16 (linear vs. segmented). Return -neededsize on overrun.
162 static int WS_copy_pe(struct ws_protoent *p_to,char *p_base,int t_size,struct protoent* p_pe)
164 char* p_name,*p_aliases,*p;
165 int size=protoent_size(p_pe)+(sizeof(struct ws_protoent)-sizeof(struct protoent));
167 if (t_size < size)
168 return -size;
169 p = (char*)p_to;
170 p += sizeof(struct ws_protoent);
171 p_name = p;
172 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
173 p_aliases = p;
174 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
176 p_to->p_proto = (INT16)p_pe->p_proto;
177 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)p_to);
178 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)p_to));
181 return size;
184 /* ----- servent */
186 static int servent_size(struct servent* p_se)
188 int size = 0;
189 if( p_se ) {
190 size += sizeof(struct servent);
191 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
192 size += list_size(p_se->s_aliases, 0);
194 return size;
197 /* Copy servent to p_to, fix up inside pointers using p_base (different for
198 * Win16 (linear vs. segmented). Return -neededsize on overrun.
200 static int WS_copy_se(struct ws_servent *p_to,char *p_base,int t_size,struct servent* p_se)
202 char* p_name,*p_aliases,*p_proto,*p;
203 int size = servent_size(p_se)+(sizeof(struct ws_servent)-sizeof(struct servent));
205 if (t_size < size )
206 return -size;
207 p = (char*)p_to;
208 p += sizeof(struct ws_servent);
209 p_name = p;
210 strcpy(p, p_se->s_name); p += strlen(p) + 1;
211 p_proto = p;
212 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
213 p_aliases = p;
214 list_dup(p_se->s_aliases, p, p_base + (p - (char*)p_to), 0);
216 p_to->s_port = (INT16)p_se->s_port;
217 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
218 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)p_to));
219 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
221 return size;
224 static HANDLE __ws_async_handle = 0xdead;
226 /* Generic async query struct. we use symbolic names for the different queries
227 * for readability.
229 typedef struct _async_query {
230 HWND16 hWnd;
231 UINT16 uMsg;
232 LPCSTR ptr1;
233 #define host_name ptr1
234 #define host_addr ptr1
235 #define serv_name ptr1
236 #define proto_name ptr1
237 LPCSTR ptr2;
238 #define serv_proto ptr2
239 int int1;
240 #define host_len int1
241 #define proto_number int1
242 #define serv_port int1
243 int int2;
244 #define host_type int2
245 SEGPTR sbuf;
246 INT16 sbuflen;
248 HANDLE16 async_handle;
249 int flags;
250 #define AQ_WIN16 0
251 #define AQ_WIN32 4
252 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
253 #define AQ_NUMBER 0
254 #define AQ_NAME 8
256 #define AQ_GETHOST 0
257 #define AQ_GETPROTO 1
258 #define AQ_GETSERV 2
259 #define AQ_GETMASK 3
260 int qt;
261 } async_query;
263 /****************************************************************************
264 * The async query function.
266 * It is either called as a thread startup routine or directly. It has
267 * to free the passed arg from the process heap and PostMessageA the async
268 * result or the error code.
270 * FIXME:
271 * - errorhandling not verified.
273 static DWORD WINAPI _async_queryfun(LPVOID arg) {
274 async_query *aq = (async_query*)arg;
275 int size = 0;
276 WORD fail = 0;
277 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)PTR_SEG_TO_LIN(aq->sbuf));
279 switch (aq->flags & AQ_GETMASK) {
280 case AQ_GETHOST: {
281 struct hostent *he;
282 struct ws_hostent *wshe = (struct ws_hostent*)targetptr;
284 he = (aq->flags & AQ_NAME) ?
285 gethostbyname(aq->host_name):
286 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
287 if (he) {
288 size = WS_copy_he(wshe,(char*)aq->sbuf,aq->sbuflen,he);
289 if (size < 0) {
290 fail = WSAENOBUFS;
291 size = -size;
293 } else {
294 fail = WSAENOBUFS;
297 break;
298 case AQ_GETPROTO: {
299 struct protoent *pe;
300 struct ws_protoent *wspe = (struct ws_protoent*)targetptr;
301 pe = (aq->flags & AQ_NAME)?
302 getprotobyname(aq->proto_name) :
303 getprotobynumber(aq->proto_number);
304 if (pe) {
305 size = WS_copy_pe(wspe,(char*)aq->sbuf,aq->sbuflen,pe);
306 if (size < 0) {
307 fail = WSAENOBUFS;
308 size = -size;
310 } else {
311 fail = WSAENOBUFS;
314 break;
315 case AQ_GETSERV: {
316 struct servent *se;
317 struct ws_servent *wsse = (struct ws_servent*)targetptr;
318 se = (aq->flags & AQ_NAME)?
319 getservbyname(aq->serv_name,aq->serv_proto) :
320 getservbyport(aq->serv_port,aq->serv_proto);
321 if (se) {
322 size = WS_copy_se(wsse,(char*)aq->sbuf,aq->sbuflen,se);
323 if (size < 0) {
324 fail = WSAENOBUFS;
325 size = -size;
327 } else {
328 fail = WSAENOBUFS;
331 break;
333 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
334 HeapFree(GetProcessHeap(),0,arg);
335 return 0;
338 /****************************************************************************
339 * The main async help function.
341 * It either starts a thread or just calls the function directly for platforms
342 * with no thread support. This relies on the fact that PostMessage() does
343 * not actually call the windowproc before the function returns.
345 static HANDLE16 __WSAsyncDBQuery(
346 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
347 void *sbuf, INT sbuflen, UINT flags
349 async_query *aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query));
350 HANDLE hthread;
352 aq->hWnd = hWnd;
353 aq->uMsg = uMsg;
354 aq->int1 = int1;
355 aq->ptr1 = ptr1;
356 aq->int2 = int2;
357 aq->ptr2 = ptr2;
358 aq->async_handle = ++__ws_async_handle;
359 aq->flags = flags;
360 aq->sbuf = (SEGPTR)sbuf;
361 aq->sbuflen = sbuflen;
362 #if 1
363 hthread = CreateThread(NULL,0,_async_queryfun,aq,0,NULL);
364 if (hthread==INVALID_HANDLE_VALUE)
365 #endif
366 _async_queryfun(aq);
367 return __ws_async_handle;
371 /***********************************************************************
372 * WSAAsyncGetHostByAddr() (WINSOCK.102)
374 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
375 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
377 TRACE(winsock, "hwnd %04x, msg %04x, addr %08x[%i]\n",
378 hWnd, uMsg, (unsigned)addr , len );
379 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,AQ_NUMBER|AQ_WIN16|AQ_GETHOST);
382 /***********************************************************************
383 * WSAAsyncGetHostByAddr() (WSOCK32.102)
385 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
386 INT len, INT type, LPSTR sbuf, INT buflen)
388 TRACE(winsock, "hwnd %04x, msg %04x, addr %08x[%i]\n",
389 hWnd, uMsg, (unsigned)addr , len );
390 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,AQ_NUMBER|AQ_WIN32|AQ_GETHOST);
393 /***********************************************************************
394 * WSAAsyncGetHostByName() (WINSOCK.103)
396 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
397 SEGPTR sbuf, INT16 buflen)
399 TRACE(winsock, "hwnd %04x, msg %04x, host %s, buffer %i\n", hWnd, uMsg, (name)?name:"<null>", (int)buflen );
401 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_NAME|AQ_WIN16|AQ_GETHOST);
404 /***********************************************************************
405 * WSAAsyncGetHostByName32() (WSOCK32.103)
407 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
408 LPSTR sbuf, INT buflen)
410 TRACE(winsock, "hwnd %04x, msg %08x, host %s, buffer %i\n",
411 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
412 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_NAME|AQ_WIN32|AQ_GETHOST);
415 /***********************************************************************
416 * WSAAsyncGetProtoByName() (WINSOCK.105)
418 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
419 SEGPTR sbuf, INT16 buflen)
421 TRACE(winsock, "hwnd %04x, msg %08x, protocol %s\n",
422 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
423 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN16);
426 /***********************************************************************
427 * WSAAsyncGetProtoByName() (WSOCK32.105)
429 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
430 LPSTR sbuf, INT buflen)
432 TRACE(winsock, "hwnd %04x, msg %08x, protocol %s\n",
433 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
434 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN32);
438 /***********************************************************************
439 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
441 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
442 SEGPTR sbuf, INT16 buflen)
444 TRACE(winsock, "hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
445 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
448 /***********************************************************************
449 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
451 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
452 LPSTR sbuf, INT buflen)
454 TRACE(winsock, "hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
456 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
459 /***********************************************************************
460 * WSAAsyncGetServByName() (WINSOCK.107)
462 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
463 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
465 TRACE(winsock, "hwnd %04x, msg %04x, name %s, proto %s\n",
466 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
468 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN16);
471 /***********************************************************************
472 * WSAAsyncGetServByName() (WSOCK32.107)
474 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
475 LPCSTR proto, LPSTR sbuf, INT buflen)
477 TRACE(winsock, "hwnd %04x, msg %04x, name %s, proto %s\n",
478 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
479 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN32);
482 /***********************************************************************
483 * WSAAsyncGetServByPort() (WINSOCK.106)
485 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
486 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
488 TRACE(winsock, "hwnd %04x, msg %04x, port %i, proto %s\n",
489 hWnd, uMsg, port, (proto)?proto:"<null>" );
490 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN16);
493 /***********************************************************************
494 * WSAAsyncGetServByPort() (WSOCK32.106)
496 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
497 LPCSTR proto, LPSTR sbuf, INT buflen)
499 TRACE(winsock, "hwnd %04x, msg %04x, port %i, proto %s\n",
500 hWnd, uMsg, port, (proto)?proto:"<null>" );
501 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN32);
504 /***********************************************************************
505 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
507 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
509 FIXME(winsock, "(%08x),stub\n", hAsyncTaskHandle);
510 return 0;
513 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
515 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);