Made GetSystemTimeAsFileTime return value more precise.
[wine/multimedia.git] / misc / winsock_async.c
blobdf88bd79749c6ccc45553e44a7963af0b547b212
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 #ifdef HAVE_SYS_IPC_H
27 # include <sys/ipc.h>
28 #endif
29 #include <sys/ioctl.h>
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
32 #endif
33 #if defined(__svr4__)
34 #include <sys/ioccom.h>
35 #ifdef HAVE_SYS_SOCKIO_H
36 # include <sys/sockio.h>
37 #endif
38 #endif
40 #if defined(__EMX__)
41 # include <sys/so_ioctl.h>
42 #endif
44 #ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
46 #endif
48 #ifdef HAVE_SYS_MSG_H
49 # include <sys/msg.h>
50 #endif
51 #ifdef HAVE_SYS_WAIT_H
52 #include <sys/wait.h>
53 #endif
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
56 #endif
57 #ifdef HAVE_NETINET_IN_H
58 # include <netinet/in.h>
59 #endif
60 #ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
62 #endif
63 #include <ctype.h>
64 #include <fcntl.h>
65 #include <errno.h>
66 #ifdef HAVE_SYS_ERRNO_H
67 #include <sys/errno.h>
68 #endif
69 #include <netdb.h>
70 #include <unistd.h>
71 #include <stdlib.h>
72 #ifdef HAVE_ARPA_NAMESER_H
73 # include <arpa/nameser.h>
74 #endif
75 #ifdef HAVE_RESOLV_H
76 # include <resolv.h>
77 #endif
79 #include "wine/winbase16.h"
80 #include "winsock.h"
81 #include "winnt.h"
82 #include "heap.h"
83 #include "task.h"
84 #include "message.h"
85 #include "miscemu.h"
86 #include "wine/port.h"
87 #include "debugtools.h"
89 DEFAULT_DEBUG_CHANNEL(winsock)
91 /* ----------------------------------- helper functions - */
93 static int list_size(char** l, int item_size)
95 int i,j = 0;
96 if(l)
97 { for(i=0;l[i];i++)
98 j += (item_size) ? item_size : strlen(l[i]) + 1;
99 j += (i + 1) * sizeof(char*); }
100 return j;
103 static int list_dup(char** l_src, char* ref, char* base, int item_size)
105 /* base is either either equal to ref or 0 or SEGPTR */
107 char* p = ref;
108 char** l_to = (char**)ref;
109 int i,j,k;
111 for(j=0;l_src[j];j++) ;
112 p += (j + 1) * sizeof(char*);
113 for(i=0;i<j;i++)
114 { l_to[i] = base + (p - ref);
115 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
116 memcpy(p, l_src[i], k); p += k; }
117 l_to[i] = NULL;
118 return (p - ref);
121 /* ----- hostent */
123 static int hostent_size(struct hostent* p_he)
125 int size = 0;
126 if( p_he )
127 { size = sizeof(struct hostent);
128 size += strlen(p_he->h_name) + 1;
129 size += list_size(p_he->h_aliases, 0);
130 size += list_size(p_he->h_addr_list, p_he->h_length ); }
131 return size;
134 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
135 * Win16 (linear vs. segmented). Return -neededsize on overrun.
137 static int WS_copy_he(struct ws_hostent *p_to,char *p_base,int t_size,struct hostent* p_he)
139 char* p_name,*p_aliases,*p_addr,*p;
140 int size=hostent_size(p_he)+(sizeof(struct ws_hostent)-sizeof(struct hostent));
142 if (t_size < size)
143 return -size;
144 p = (char*)p_to;
145 p += sizeof(struct ws_hostent);
146 p_name = p;
147 strcpy(p, p_he->h_name); p += strlen(p) + 1;
148 p_aliases = p;
149 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
150 p_addr = p;
151 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
153 p_to->h_addrtype = (INT16)p_he->h_addrtype;
154 p_to->h_length = (INT16)p_he->h_length;
155 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
156 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
157 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)p_to));
159 return size;
162 /* ----- protoent */
164 static int protoent_size(struct protoent* p_pe)
166 int size = 0;
167 if( p_pe )
168 { size = sizeof(struct protoent);
169 size += strlen(p_pe->p_name) + 1;
170 size += list_size(p_pe->p_aliases, 0); }
171 return size;
174 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
175 * Win16 (linear vs. segmented). Return -neededsize on overrun.
177 static int WS_copy_pe(struct ws_protoent *p_to,char *p_base,int t_size,struct protoent* p_pe)
179 char* p_name,*p_aliases,*p;
180 int size=protoent_size(p_pe)+(sizeof(struct ws_protoent)-sizeof(struct protoent));
182 if (t_size < size)
183 return -size;
184 p = (char*)p_to;
185 p += sizeof(struct ws_protoent);
186 p_name = p;
187 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
188 p_aliases = p;
189 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
191 p_to->p_proto = (INT16)p_pe->p_proto;
192 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)p_to);
193 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)p_to));
196 return size;
199 /* ----- servent */
201 static int servent_size(struct servent* p_se)
203 int size = 0;
204 if( p_se ) {
205 size += sizeof(struct servent);
206 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
207 size += list_size(p_se->s_aliases, 0);
209 return size;
212 /* Copy servent to p_to, fix up inside pointers using p_base (different for
213 * Win16 (linear vs. segmented). Return -neededsize on overrun.
215 static int WS_copy_se(struct ws_servent *p_to,char *p_base,int t_size,struct servent* p_se)
217 char* p_name,*p_aliases,*p_proto,*p;
218 int size = servent_size(p_se)+(sizeof(struct ws_servent)-sizeof(struct servent));
220 if (t_size < size )
221 return -size;
222 p = (char*)p_to;
223 p += sizeof(struct ws_servent);
224 p_name = p;
225 strcpy(p, p_se->s_name); p += strlen(p) + 1;
226 p_proto = p;
227 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
228 p_aliases = p;
229 list_dup(p_se->s_aliases, p, p_base + (p - (char*)p_to), 0);
231 p_to->s_port = (INT16)p_se->s_port;
232 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
233 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)p_to));
234 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
236 return size;
239 static HANDLE __ws_async_handle = 0xdead;
241 /* Generic async query struct. we use symbolic names for the different queries
242 * for readability.
244 typedef struct _async_query {
245 HWND16 hWnd;
246 UINT16 uMsg;
247 LPCSTR ptr1;
248 #define host_name ptr1
249 #define host_addr ptr1
250 #define serv_name ptr1
251 #define proto_name ptr1
252 LPCSTR ptr2;
253 #define serv_proto ptr2
254 int int1;
255 #define host_len int1
256 #define proto_number int1
257 #define serv_port int1
258 int int2;
259 #define host_type int2
260 SEGPTR sbuf;
261 INT16 sbuflen;
263 HANDLE16 async_handle;
264 int flags;
265 #define AQ_WIN16 0
266 #define AQ_WIN32 4
267 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
268 #define AQ_NUMBER 0
269 #define AQ_NAME 8
271 #define AQ_GETHOST 0
272 #define AQ_GETPROTO 1
273 #define AQ_GETSERV 2
274 #define AQ_GETMASK 3
275 int qt;
276 } async_query;
278 /****************************************************************************
279 * The async query function.
281 * It is either called as a thread startup routine or directly. It has
282 * to free the passed arg from the process heap and PostMessageA the async
283 * result or the error code.
285 * FIXME:
286 * - errorhandling not verified.
288 static DWORD WINAPI _async_queryfun(LPVOID arg) {
289 async_query *aq = (async_query*)arg;
290 int size = 0;
291 WORD fail = 0;
292 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)PTR_SEG_TO_LIN(aq->sbuf));
294 switch (aq->flags & AQ_GETMASK) {
295 case AQ_GETHOST: {
296 struct hostent *he;
297 struct ws_hostent *wshe = (struct ws_hostent*)targetptr;
299 he = (aq->flags & AQ_NAME) ?
300 gethostbyname(aq->host_name):
301 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
302 if (he) {
303 size = WS_copy_he(wshe,(char*)aq->sbuf,aq->sbuflen,he);
304 if (size < 0) {
305 fail = WSAENOBUFS;
306 size = -size;
308 } else {
309 fail = WSAENOBUFS;
312 break;
313 case AQ_GETPROTO: {
314 struct protoent *pe;
315 struct ws_protoent *wspe = (struct ws_protoent*)targetptr;
316 pe = (aq->flags & AQ_NAME)?
317 getprotobyname(aq->proto_name) :
318 getprotobynumber(aq->proto_number);
319 if (pe) {
320 size = WS_copy_pe(wspe,(char*)aq->sbuf,aq->sbuflen,pe);
321 if (size < 0) {
322 fail = WSAENOBUFS;
323 size = -size;
325 } else {
326 fail = WSAENOBUFS;
329 break;
330 case AQ_GETSERV: {
331 struct servent *se;
332 struct ws_servent *wsse = (struct ws_servent*)targetptr;
333 se = (aq->flags & AQ_NAME)?
334 getservbyname(aq->serv_name,aq->serv_proto) :
335 getservbyport(aq->serv_port,aq->serv_proto);
336 if (se) {
337 size = WS_copy_se(wsse,(char*)aq->sbuf,aq->sbuflen,se);
338 if (size < 0) {
339 fail = WSAENOBUFS;
340 size = -size;
342 } else {
343 fail = WSAENOBUFS;
346 break;
348 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
349 HeapFree(GetProcessHeap(),0,arg);
350 return 0;
353 /****************************************************************************
354 * The main async help function.
356 * It either starts a thread or just calls the function directly for platforms
357 * with no thread support. This relies on the fact that PostMessage() does
358 * not actually call the windowproc before the function returns.
360 static HANDLE16 __WSAsyncDBQuery(
361 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
362 void *sbuf, INT sbuflen, UINT flags
364 async_query *aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query));
365 HANDLE hthread;
367 aq->hWnd = hWnd;
368 aq->uMsg = uMsg;
369 aq->int1 = int1;
370 aq->ptr1 = ptr1;
371 aq->int2 = int2;
372 aq->ptr2 = ptr2;
373 aq->async_handle = ++__ws_async_handle;
374 aq->flags = flags;
375 aq->sbuf = (SEGPTR)sbuf;
376 aq->sbuflen = sbuflen;
377 #if 1
378 hthread = CreateThread(NULL,0,_async_queryfun,aq,0,NULL);
379 if (hthread==INVALID_HANDLE_VALUE)
380 #endif
381 _async_queryfun(aq);
382 return __ws_async_handle;
386 /***********************************************************************
387 * WSAAsyncGetHostByAddr() (WINSOCK.102)
389 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
390 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
392 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
393 hWnd, uMsg, (unsigned)addr , len );
394 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,AQ_NUMBER|AQ_WIN16|AQ_GETHOST);
397 /***********************************************************************
398 * WSAAsyncGetHostByAddr() (WSOCK32.102)
400 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
401 INT len, INT type, LPSTR sbuf, INT buflen)
403 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
404 hWnd, uMsg, (unsigned)addr , len );
405 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,AQ_NUMBER|AQ_WIN32|AQ_GETHOST);
408 /***********************************************************************
409 * WSAAsyncGetHostByName() (WINSOCK.103)
411 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
412 SEGPTR sbuf, INT16 buflen)
414 TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n", hWnd, uMsg, (name)?name:"<null>", (int)buflen );
416 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_NAME|AQ_WIN16|AQ_GETHOST);
419 /***********************************************************************
420 * WSAAsyncGetHostByName32() (WSOCK32.103)
422 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
423 LPSTR sbuf, INT buflen)
425 TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n",
426 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
427 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_NAME|AQ_WIN32|AQ_GETHOST);
430 /***********************************************************************
431 * WSAAsyncGetProtoByName() (WINSOCK.105)
433 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
434 SEGPTR sbuf, INT16 buflen)
436 TRACE("hwnd %04x, msg %08x, protocol %s\n",
437 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
438 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN16);
441 /***********************************************************************
442 * WSAAsyncGetProtoByName() (WSOCK32.105)
444 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
445 LPSTR sbuf, INT buflen)
447 TRACE("hwnd %04x, msg %08x, protocol %s\n",
448 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
449 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN32);
453 /***********************************************************************
454 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
456 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
457 SEGPTR sbuf, INT16 buflen)
459 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
460 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
463 /***********************************************************************
464 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
466 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
467 LPSTR sbuf, INT buflen)
469 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
471 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
474 /***********************************************************************
475 * WSAAsyncGetServByName() (WINSOCK.107)
477 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
478 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
480 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
481 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
483 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN16);
486 /***********************************************************************
487 * WSAAsyncGetServByName() (WSOCK32.107)
489 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
490 LPCSTR proto, LPSTR sbuf, INT buflen)
492 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
493 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
494 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN32);
497 /***********************************************************************
498 * WSAAsyncGetServByPort() (WINSOCK.106)
500 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
501 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
503 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
504 hWnd, uMsg, port, (proto)?proto:"<null>" );
505 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN16);
508 /***********************************************************************
509 * WSAAsyncGetServByPort() (WSOCK32.106)
511 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
512 LPCSTR proto, LPSTR sbuf, INT buflen)
514 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
515 hWnd, uMsg, port, (proto)?proto:"<null>" );
516 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN32);
519 /***********************************************************************
520 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
522 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
524 FIXME("(%08x),stub\n", hAsyncTaskHandle);
525 return 0;
528 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
530 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);