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