Define __fastcall.
[wine] / dlls / winsock / async.c
1 /* Async WINSOCK DNS services
2  * 
3  * (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
4  * (C) 1999 Marcus Meissner
5  *
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).
20  */
21  
22 #include "config.h"
23
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
39
40 #if defined(__EMX__)
41 # include <sys/so_ioctl.h>
42 #endif
43
44 #ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
46 #endif
47
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
78
79 #include "wine/winbase16.h"
80 #include "wingdi.h"
81 #include "winuser.h"
82 #include "winsock.h"
83 #include "winnt.h"
84 #include "heap.h"
85 #include "task.h"
86 #include "message.h"
87 #include "miscemu.h"
88 #include "wine/port.h"
89 #include "debugtools.h"
90
91 DEFAULT_DEBUG_CHANNEL(winsock)
92
93 /* protoptypes of some functions in socket.c
94  */
95 UINT16 wsaErrno(void);
96 UINT16 wsaHerrno(void);
97
98 /* ----------------------------------- helper functions - */
99
100 static int list_size(char** l, int item_size)
101 {
102   int i,j = 0;
103   if(l)
104   { for(i=0;l[i];i++) 
105         j += (item_size) ? item_size : strlen(l[i]) + 1;
106     j += (i + 1) * sizeof(char*); }
107   return j;
108 }
109
110 static int list_dup(char** l_src, char* ref, char* base, int item_size)
111
112    /* base is either either equal to ref or 0 or SEGPTR */
113
114    char*                p = ref;
115    char**               l_to = (char**)ref;
116    int                  i,j,k;
117
118    for(j=0;l_src[j];j++) ;
119    p += (j + 1) * sizeof(char*);
120    for(i=0;i<j;i++)
121    { l_to[i] = base + (p - ref);
122      k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
123      memcpy(p, l_src[i], k); p += k; }
124    l_to[i] = NULL;
125    return (p - ref);
126 }
127
128 /* ----- hostent */
129
130 static int hostent_size(struct hostent* p_he)
131 {
132   int size = 0;
133   if( p_he )
134   { size  = sizeof(struct hostent); 
135     size += strlen(p_he->h_name) + 1;
136     size += list_size(p_he->h_aliases, 0);  
137     size += list_size(p_he->h_addr_list, p_he->h_length ); }
138   return size;
139 }
140
141 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
142  * Win16 (linear vs. segmented). Return -neededsize on overrun.
143  */
144 static int WS_copy_he(struct ws_hostent *p_to,char *p_base,int t_size,struct hostent* p_he)
145 {
146         char* p_name,*p_aliases,*p_addr,*p;
147         int     size=hostent_size(p_he)+(sizeof(struct ws_hostent)-sizeof(struct hostent));
148
149         if (t_size < size)
150                 return -size;
151         p = (char*)p_to;
152         p += sizeof(struct ws_hostent);
153         p_name = p;
154         strcpy(p, p_he->h_name); p += strlen(p) + 1;
155         p_aliases = p;
156         p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
157         p_addr = p;
158         list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
159
160         p_to->h_addrtype = (INT16)p_he->h_addrtype; 
161         p_to->h_length = (INT16)p_he->h_length;
162         p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
163         p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
164         p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)p_to));
165
166         return size;
167 }
168
169 /* ----- protoent */
170
171 static int protoent_size(struct protoent* p_pe)
172 {
173   int size = 0;
174   if( p_pe )
175   { size  = sizeof(struct protoent);
176     size += strlen(p_pe->p_name) + 1;
177     size += list_size(p_pe->p_aliases, 0); }
178   return size;
179 }
180
181 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
182  * Win16 (linear vs. segmented). Return -neededsize on overrun.
183  */
184 static int WS_copy_pe(struct ws_protoent *p_to,char *p_base,int t_size,struct protoent* p_pe)
185 {
186         char* p_name,*p_aliases,*p;
187         int     size=protoent_size(p_pe)+(sizeof(struct ws_protoent)-sizeof(struct protoent));
188
189         if (t_size < size)
190                 return -size;
191         p = (char*)p_to;
192         p += sizeof(struct ws_protoent);
193         p_name = p;
194         strcpy(p, p_pe->p_name); p += strlen(p) + 1;
195         p_aliases = p;
196         list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
197
198         p_to->p_proto = (INT16)p_pe->p_proto;
199         p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)p_to);
200         p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)p_to)); 
201
202
203         return size;
204 }
205
206 /* ----- servent */
207
208 static int servent_size(struct servent* p_se)
209 {
210         int size = 0;
211         if( p_se ) {
212                 size += sizeof(struct servent);
213                 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
214                 size += list_size(p_se->s_aliases, 0);
215         }
216         return size;
217 }
218
219 /* Copy servent to p_to, fix up inside pointers using p_base (different for
220  * Win16 (linear vs. segmented). Return -neededsize on overrun.
221  */
222 static int WS_copy_se(struct ws_servent *p_to,char *p_base,int t_size,struct servent* p_se)
223 {
224         char* p_name,*p_aliases,*p_proto,*p;
225         int     size = servent_size(p_se)+(sizeof(struct ws_servent)-sizeof(struct servent));
226
227         if (t_size < size )
228                 return -size;
229         p = (char*)p_to;
230         p += sizeof(struct ws_servent);
231         p_name = p;
232         strcpy(p, p_se->s_name); p += strlen(p) + 1;
233         p_proto = p;
234         strcpy(p, p_se->s_proto); p += strlen(p) + 1;
235         p_aliases = p;
236         list_dup(p_se->s_aliases, p, p_base + (p - (char*)p_to), 0);
237
238         p_to->s_port = (INT16)p_se->s_port;
239         p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
240         p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)p_to));
241         p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to)); 
242
243         return size;
244 }
245
246 static HANDLE __ws_async_handle = 0xdead;
247
248 /* Generic async query struct. we use symbolic names for the different queries
249  * for readability.
250  */
251 typedef struct _async_query {
252         HWND16          hWnd;
253         UINT16          uMsg;
254         LPCSTR          ptr1;
255 #define host_name       ptr1
256 #define host_addr       ptr1
257 #define serv_name       ptr1
258 #define proto_name      ptr1
259         LPCSTR          ptr2;
260 #define serv_proto      ptr2
261         int             int1;
262 #define host_len        int1
263 #define proto_number    int1
264 #define serv_port       int1
265         int             int2;
266 #define host_type       int2
267         SEGPTR          sbuf;
268         INT16           sbuflen;
269
270         HANDLE16        async_handle;
271         int             flags;
272 #define AQ_WIN16        0
273 #define AQ_WIN32        4
274 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
275 #define AQ_NUMBER       0
276 #define AQ_NAME         8
277
278 #define AQ_GETHOST      0
279 #define AQ_GETPROTO     1
280 #define AQ_GETSERV      2
281 #define AQ_GETMASK      3
282         int             qt;
283 } async_query;
284
285 /****************************************************************************
286  * The async query function.
287  *
288  * It is either called as a thread startup routine or directly. It has
289  * to free the passed arg from the process heap and PostMessageA the async
290  * result or the error code.
291  * 
292  * FIXME:
293  *      - errorhandling not verified.
294  */
295 static DWORD WINAPI _async_queryfun(LPVOID arg) {
296         async_query     *aq = (async_query*)arg;
297         int             size = 0;
298         WORD            fail = 0;
299         char            *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)PTR_SEG_TO_LIN(aq->sbuf));
300
301         switch (aq->flags & AQ_GETMASK) {
302         case AQ_GETHOST: {
303                         struct hostent          *he;
304                         struct ws_hostent       *wshe = (struct ws_hostent*)targetptr;
305
306                         he = (aq->flags & AQ_NAME) ?
307                                 gethostbyname(aq->host_name):
308                                 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
309                         if (he) {
310                                 size = WS_copy_he(wshe,(char*)aq->sbuf,aq->sbuflen,he);
311                                 if (size < 0) {
312                                         fail = WSAENOBUFS;
313                                         size = -size;
314                                 }
315                         } else {
316                                 fail = ((h_errno < 0) ? wsaErrno() : wsaHerrno());
317                         }
318                 }
319                 break;
320         case AQ_GETPROTO: {
321                         struct  protoent        *pe;
322                         struct ws_protoent      *wspe = (struct ws_protoent*)targetptr;
323                         pe = (aq->flags & AQ_NAME)?
324                                 getprotobyname(aq->proto_name) : 
325                                 getprotobynumber(aq->proto_number);
326                         if (pe) {
327                                 size = WS_copy_pe(wspe,(char*)aq->sbuf,aq->sbuflen,pe);
328                                 if (size < 0) {
329                                         fail = WSAENOBUFS;
330                                         size = -size;
331                                 }
332                         } else {
333                 if (aq->flags & AQ_NAME)
334                     MESSAGE("protocol %s not found; You might want to add "
335                             "this to /etc/protocols\n", debugstr_a(aq->proto_name) );
336                 else
337                     MESSAGE("protocol number %d not found; You might want to add "
338                             "this to /etc/protocols\n", aq->proto_number );
339                 fail = WSANO_DATA;
340                         }
341                 }
342                 break;
343         case AQ_GETSERV: {
344                         struct  servent *se;
345                         struct ws_servent       *wsse = (struct ws_servent*)targetptr;
346                         se = (aq->flags & AQ_NAME)?
347                                 getservbyname(aq->serv_name,aq->serv_proto) : 
348                                 getservbyport(aq->serv_port,aq->serv_proto);
349                         if (se) {
350                                 size = WS_copy_se(wsse,(char*)aq->sbuf,aq->sbuflen,se);
351                                 if (size < 0) {
352                                         fail = WSAENOBUFS;
353                                         size = -size;
354                                 }
355                         } else {
356                 if (aq->flags & AQ_NAME)
357                     MESSAGE("service %s protocol %s not found; You might want to add "
358                             "this to /etc/services\n", debugstr_a(aq->serv_name) ,
359                             aq->serv_proto ? debugstr_a(aq->serv_proto ):"*"); 
360                 else
361                     MESSAGE("service on port %d protocol %s not found; You might want to add "
362                             "this to /etc/services\n", aq->serv_port,
363                             aq->serv_proto ? debugstr_a(aq->serv_proto ):"*"); 
364                 fail = WSANO_DATA;
365                         }
366                 }
367                 break;
368         }
369         PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
370         HeapFree(GetProcessHeap(),0,arg);
371         return 0;
372 }
373
374 /****************************************************************************
375  * The main async help function. 
376  * 
377  * It either starts a thread or just calls the function directly for platforms
378  * with no thread support. This relies on the fact that PostMessage() does
379  * not actually call the windowproc before the function returns.
380  */
381 static HANDLE16 __WSAsyncDBQuery(
382         HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
383         void *sbuf, INT sbuflen, UINT flags
384 ) {
385         async_query     *aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query));
386         HANDLE  hthread;
387
388         aq->hWnd        = hWnd;
389         aq->uMsg        = uMsg;
390         aq->int1        = int1;
391         aq->ptr1        = ptr1;
392         aq->int2        = int2;
393         aq->ptr2        = ptr2;
394         aq->async_handle = ++__ws_async_handle;
395         aq->flags       = flags;
396         aq->sbuf        = (SEGPTR)sbuf;
397         aq->sbuflen     = sbuflen;
398 #if 1
399         hthread = CreateThread(NULL,0,_async_queryfun,aq,0,NULL);
400         if (hthread==INVALID_HANDLE_VALUE)
401 #endif
402                 _async_queryfun(aq);
403         return __ws_async_handle;
404 }
405
406
407 /***********************************************************************
408  *       WSAAsyncGetHostByAddr()        (WINSOCK.102)
409  */
410 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
411                                INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
412 {
413         TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
414                hWnd, uMsg, (unsigned)addr , len );
415         return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,AQ_NUMBER|AQ_WIN16|AQ_GETHOST);
416 }
417
418 /***********************************************************************
419  *       WSAAsyncGetHostByAddr()        (WSOCK32.102)
420  */
421 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
422                                INT len, INT type, LPSTR sbuf, INT buflen)
423 {
424         TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
425                hWnd, uMsg, (unsigned)addr , len );
426         return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,AQ_NUMBER|AQ_WIN32|AQ_GETHOST);
427 }
428
429 /***********************************************************************
430  *       WSAAsyncGetHostByName()        (WINSOCK.103)
431  */
432 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name, 
433                                       SEGPTR sbuf, INT16 buflen)
434 {
435         TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n", hWnd, uMsg, (name)?name:"<null>", (int)buflen );
436
437         return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_NAME|AQ_WIN16|AQ_GETHOST);
438 }                     
439
440 /***********************************************************************
441  *       WSAAsyncGetHostByName()        (WSOCK32.103)
442  */
443 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name, 
444                                         LPSTR sbuf, INT buflen)
445 {
446         TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n", 
447                (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
448         return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_NAME|AQ_WIN32|AQ_GETHOST);
449 }
450
451 /***********************************************************************
452  *       WSAAsyncGetProtoByName()       (WINSOCK.105)
453  */
454 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name, 
455                                          SEGPTR sbuf, INT16 buflen)
456 {
457         TRACE("hwnd %04x, msg %08x, protocol %s\n",
458                (HWND16)hWnd, uMsg, (name)?name:"<null>" );
459         return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN16);
460 }
461
462 /***********************************************************************
463  *       WSAAsyncGetProtoByName()       (WSOCK32.105)
464  */
465 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
466                                          LPSTR sbuf, INT buflen)
467 {
468         TRACE("hwnd %04x, msg %08x, protocol %s\n",
469                (HWND16)hWnd, uMsg, (name)?name:"<null>" );
470         return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN32);
471 }
472
473
474 /***********************************************************************
475  *       WSAAsyncGetProtoByNumber()     (WINSOCK.104)
476  */
477 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
478                                            SEGPTR sbuf, INT16 buflen)
479 {
480         TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
481         return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
482 }
483
484 /***********************************************************************
485  *       WSAAsyncGetProtoByNumber()     (WSOCK32.104)
486  */
487 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
488                                            LPSTR sbuf, INT buflen)
489 {
490         TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
491
492         return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
493 }
494
495 /***********************************************************************
496  *       WSAAsyncGetServByName()        (WINSOCK.107)
497  */
498 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name, 
499                                         LPCSTR proto, SEGPTR sbuf, INT16 buflen)
500 {
501         TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
502                hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
503
504         return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN16);
505 }
506
507 /***********************************************************************
508  *       WSAAsyncGetServByName()        (WSOCK32.107)
509  */
510 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
511                                         LPCSTR proto, LPSTR sbuf, INT buflen)
512 {
513         TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
514                hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
515         return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN32);
516 }
517
518 /***********************************************************************
519  *       WSAAsyncGetServByPort()        (WINSOCK.106)
520  */
521 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port, 
522                                         LPCSTR proto, SEGPTR sbuf, INT16 buflen)
523 {
524         TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
525                hWnd, uMsg, port, (proto)?proto:"<null>" );
526         return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN16);
527 }
528
529 /***********************************************************************
530  *       WSAAsyncGetServByPort()        (WSOCK32.106)
531  */
532 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
533                                         LPCSTR proto, LPSTR sbuf, INT buflen)
534 {
535         TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
536                hWnd, uMsg, port, (proto)?proto:"<null>" );
537         return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN32);
538 }
539
540 /***********************************************************************
541  *       WSACancelAsyncRequest()        (WINSOCK.108)(WSOCK32.109)
542  */
543 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
544 {
545     FIXME("(%08x),stub\n", hAsyncTaskHandle);
546     return 0;
547 }
548
549 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
550 {
551     return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);
552 }