Winspool DocumentProperties and DeviceCapabilities should now work on
[wine.git] / misc / winsock_async.c
blobf18e21bb4e6db707a286d7961216f06b606ab12c
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 "debugtools.h"
72 DEFAULT_DEBUG_CHANNEL(winsock)
74 /* ----------------------------------- helper functions - */
76 static int list_size(char** l, int item_size)
78 int i,j = 0;
79 if(l)
80 { for(i=0;l[i];i++)
81 j += (item_size) ? item_size : strlen(l[i]) + 1;
82 j += (i + 1) * sizeof(char*); }
83 return j;
86 static int list_dup(char** l_src, char* ref, char* base, int item_size)
88 /* base is either either equal to ref or 0 or SEGPTR */
90 char* p = ref;
91 char** l_to = (char**)ref;
92 int i,j,k;
94 for(j=0;l_src[j];j++) ;
95 p += (j + 1) * sizeof(char*);
96 for(i=0;i<j;i++)
97 { l_to[i] = base + (p - ref);
98 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
99 memcpy(p, l_src[i], k); p += k; }
100 l_to[i] = NULL;
101 return (p - ref);
104 /* ----- hostent */
106 static int hostent_size(struct hostent* p_he)
108 int size = 0;
109 if( p_he )
110 { size = sizeof(struct hostent);
111 size += strlen(p_he->h_name) + 1;
112 size += list_size(p_he->h_aliases, 0);
113 size += list_size(p_he->h_addr_list, p_he->h_length ); }
114 return size;
117 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
118 * Win16 (linear vs. segmented). Return -neededsize on overrun.
120 static int WS_copy_he(struct ws_hostent *p_to,char *p_base,int t_size,struct hostent* p_he)
122 char* p_name,*p_aliases,*p_addr,*p;
123 int size=hostent_size(p_he)+(sizeof(struct ws_hostent)-sizeof(struct hostent));
125 if (t_size < size)
126 return -size;
127 p = (char*)p_to;
128 p += sizeof(struct ws_hostent);
129 p_name = p;
130 strcpy(p, p_he->h_name); p += strlen(p) + 1;
131 p_aliases = p;
132 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
133 p_addr = p;
134 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
136 p_to->h_addrtype = (INT16)p_he->h_addrtype;
137 p_to->h_length = (INT16)p_he->h_length;
138 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
139 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
140 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)p_to));
142 return size;
145 /* ----- protoent */
147 static int protoent_size(struct protoent* p_pe)
149 int size = 0;
150 if( p_pe )
151 { size = sizeof(struct protoent);
152 size += strlen(p_pe->p_name) + 1;
153 size += list_size(p_pe->p_aliases, 0); }
154 return size;
157 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
158 * Win16 (linear vs. segmented). Return -neededsize on overrun.
160 static int WS_copy_pe(struct ws_protoent *p_to,char *p_base,int t_size,struct protoent* p_pe)
162 char* p_name,*p_aliases,*p;
163 int size=protoent_size(p_pe)+(sizeof(struct ws_protoent)-sizeof(struct protoent));
165 if (t_size < size)
166 return -size;
167 p = (char*)p_to;
168 p += sizeof(struct ws_protoent);
169 p_name = p;
170 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
171 p_aliases = p;
172 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
174 p_to->p_proto = (INT16)p_pe->p_proto;
175 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)p_to);
176 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)p_to));
179 return size;
182 /* ----- servent */
184 static int servent_size(struct servent* p_se)
186 int size = 0;
187 if( p_se ) {
188 size += sizeof(struct servent);
189 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
190 size += list_size(p_se->s_aliases, 0);
192 return size;
195 /* Copy servent to p_to, fix up inside pointers using p_base (different for
196 * Win16 (linear vs. segmented). Return -neededsize on overrun.
198 static int WS_copy_se(struct ws_servent *p_to,char *p_base,int t_size,struct servent* p_se)
200 char* p_name,*p_aliases,*p_proto,*p;
201 int size = servent_size(p_se)+(sizeof(struct ws_servent)-sizeof(struct servent));
203 if (t_size < size )
204 return -size;
205 p = (char*)p_to;
206 p += sizeof(struct ws_servent);
207 p_name = p;
208 strcpy(p, p_se->s_name); p += strlen(p) + 1;
209 p_proto = p;
210 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
211 p_aliases = p;
212 list_dup(p_se->s_aliases, p, p_base + (p - (char*)p_to), 0);
214 p_to->s_port = (INT16)p_se->s_port;
215 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
216 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)p_to));
217 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
219 return size;
222 static HANDLE __ws_async_handle = 0xdead;
224 /* Generic async query struct. we use symbolic names for the different queries
225 * for readability.
227 typedef struct _async_query {
228 HWND16 hWnd;
229 UINT16 uMsg;
230 LPCSTR ptr1;
231 #define host_name ptr1
232 #define host_addr ptr1
233 #define serv_name ptr1
234 #define proto_name ptr1
235 LPCSTR ptr2;
236 #define serv_proto ptr2
237 int int1;
238 #define host_len int1
239 #define proto_number int1
240 #define serv_port int1
241 int int2;
242 #define host_type int2
243 SEGPTR sbuf;
244 INT16 sbuflen;
246 HANDLE16 async_handle;
247 int flags;
248 #define AQ_WIN16 0
249 #define AQ_WIN32 4
250 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
251 #define AQ_NUMBER 0
252 #define AQ_NAME 8
254 #define AQ_GETHOST 0
255 #define AQ_GETPROTO 1
256 #define AQ_GETSERV 2
257 #define AQ_GETMASK 3
258 int qt;
259 } async_query;
261 /****************************************************************************
262 * The async query function.
264 * It is either called as a thread startup routine or directly. It has
265 * to free the passed arg from the process heap and PostMessageA the async
266 * result or the error code.
268 * FIXME:
269 * - errorhandling not verified.
271 static DWORD WINAPI _async_queryfun(LPVOID arg) {
272 async_query *aq = (async_query*)arg;
273 int size = 0;
274 WORD fail = 0;
275 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)PTR_SEG_TO_LIN(aq->sbuf));
277 switch (aq->flags & AQ_GETMASK) {
278 case AQ_GETHOST: {
279 struct hostent *he;
280 struct ws_hostent *wshe = (struct ws_hostent*)targetptr;
282 he = (aq->flags & AQ_NAME) ?
283 gethostbyname(aq->host_name):
284 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
285 if (he) {
286 size = WS_copy_he(wshe,(char*)aq->sbuf,aq->sbuflen,he);
287 if (size < 0) {
288 fail = WSAENOBUFS;
289 size = -size;
291 } else {
292 fail = WSAENOBUFS;
295 break;
296 case AQ_GETPROTO: {
297 struct protoent *pe;
298 struct ws_protoent *wspe = (struct ws_protoent*)targetptr;
299 pe = (aq->flags & AQ_NAME)?
300 getprotobyname(aq->proto_name) :
301 getprotobynumber(aq->proto_number);
302 if (pe) {
303 size = WS_copy_pe(wspe,(char*)aq->sbuf,aq->sbuflen,pe);
304 if (size < 0) {
305 fail = WSAENOBUFS;
306 size = -size;
308 } else {
309 fail = WSAENOBUFS;
312 break;
313 case AQ_GETSERV: {
314 struct servent *se;
315 struct ws_servent *wsse = (struct ws_servent*)targetptr;
316 se = (aq->flags & AQ_NAME)?
317 getservbyname(aq->serv_name,aq->serv_proto) :
318 getservbyport(aq->serv_port,aq->serv_proto);
319 if (se) {
320 size = WS_copy_se(wsse,(char*)aq->sbuf,aq->sbuflen,se);
321 if (size < 0) {
322 fail = WSAENOBUFS;
323 size = -size;
325 } else {
326 fail = WSAENOBUFS;
329 break;
331 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
332 HeapFree(GetProcessHeap(),0,arg);
333 return 0;
336 /****************************************************************************
337 * The main async help function.
339 * It either starts a thread or just calls the function directly for platforms
340 * with no thread support. This relies on the fact that PostMessage() does
341 * not actually call the windowproc before the function returns.
343 static HANDLE16 __WSAsyncDBQuery(
344 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
345 void *sbuf, INT sbuflen, UINT flags
347 async_query *aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query));
348 HANDLE hthread;
350 aq->hWnd = hWnd;
351 aq->uMsg = uMsg;
352 aq->int1 = int1;
353 aq->ptr1 = ptr1;
354 aq->int2 = int2;
355 aq->ptr2 = ptr2;
356 aq->async_handle = ++__ws_async_handle;
357 aq->flags = flags;
358 aq->sbuf = (SEGPTR)sbuf;
359 aq->sbuflen = sbuflen;
360 #if 1
361 hthread = CreateThread(NULL,0,_async_queryfun,aq,0,NULL);
362 if (hthread==INVALID_HANDLE_VALUE)
363 #endif
364 _async_queryfun(aq);
365 return __ws_async_handle;
369 /***********************************************************************
370 * WSAAsyncGetHostByAddr() (WINSOCK.102)
372 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
373 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
375 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
376 hWnd, uMsg, (unsigned)addr , len );
377 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,AQ_NUMBER|AQ_WIN16|AQ_GETHOST);
380 /***********************************************************************
381 * WSAAsyncGetHostByAddr() (WSOCK32.102)
383 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
384 INT len, INT type, LPSTR sbuf, INT buflen)
386 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
387 hWnd, uMsg, (unsigned)addr , len );
388 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,AQ_NUMBER|AQ_WIN32|AQ_GETHOST);
391 /***********************************************************************
392 * WSAAsyncGetHostByName() (WINSOCK.103)
394 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
395 SEGPTR sbuf, INT16 buflen)
397 TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n", hWnd, uMsg, (name)?name:"<null>", (int)buflen );
399 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_NAME|AQ_WIN16|AQ_GETHOST);
402 /***********************************************************************
403 * WSAAsyncGetHostByName32() (WSOCK32.103)
405 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
406 LPSTR sbuf, INT buflen)
408 TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n",
409 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
410 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_NAME|AQ_WIN32|AQ_GETHOST);
413 /***********************************************************************
414 * WSAAsyncGetProtoByName() (WINSOCK.105)
416 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
417 SEGPTR sbuf, INT16 buflen)
419 TRACE("hwnd %04x, msg %08x, protocol %s\n",
420 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
421 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN16);
424 /***********************************************************************
425 * WSAAsyncGetProtoByName() (WSOCK32.105)
427 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
428 LPSTR sbuf, INT buflen)
430 TRACE("hwnd %04x, msg %08x, protocol %s\n",
431 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
432 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN32);
436 /***********************************************************************
437 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
439 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
440 SEGPTR sbuf, INT16 buflen)
442 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
443 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
446 /***********************************************************************
447 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
449 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
450 LPSTR sbuf, INT buflen)
452 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
454 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
457 /***********************************************************************
458 * WSAAsyncGetServByName() (WINSOCK.107)
460 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
461 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
463 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
464 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
466 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN16);
469 /***********************************************************************
470 * WSAAsyncGetServByName() (WSOCK32.107)
472 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
473 LPCSTR proto, LPSTR sbuf, INT buflen)
475 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
476 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
477 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN32);
480 /***********************************************************************
481 * WSAAsyncGetServByPort() (WINSOCK.106)
483 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
484 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
486 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
487 hWnd, uMsg, port, (proto)?proto:"<null>" );
488 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN16);
491 /***********************************************************************
492 * WSAAsyncGetServByPort() (WSOCK32.106)
494 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
495 LPCSTR proto, LPSTR sbuf, INT buflen)
497 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
498 hWnd, uMsg, port, (proto)?proto:"<null>" );
499 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN32);
502 /***********************************************************************
503 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
505 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
507 FIXME("(%08x),stub\n", hAsyncTaskHandle);
508 return 0;
511 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
513 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);