1 /* Async WINSOCK DNS services
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).
11 * - Add WSACancel* and correct handle management. (works rather well for
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.
18 * - This implementation did ignore the "NOTE:" section above (since the
19 * whole stuff did not work anyway to other changes).
25 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
34 #include <sys/ioccom.h>
35 #ifdef HAVE_SYS_SOCKIO_H
36 # include <sys/sockio.h>
41 # include <sys/so_ioctl.h>
44 #ifdef HAVE_SYS_PARAM_H
45 # include <sys/param.h>
51 #ifdef HAVE_SYS_WAIT_H
54 #ifdef HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
57 #ifdef HAVE_NETINET_IN_H
58 # include <netinet/in.h>
60 #ifdef HAVE_ARPA_INET_H
61 # include <arpa/inet.h>
66 #ifdef HAVE_SYS_ERRNO_H
67 #include <sys/errno.h>
72 #ifdef HAVE_ARPA_NAMESER_H
73 # include <arpa/nameser.h>
79 #include "wine/winbase16.h"
88 #include "wine/port.h"
89 #include "debugtools.h"
91 DEFAULT_DEBUG_CHANNEL(winsock)
93 /* ----------------------------------- helper functions - */
95 static int list_size(char** l, int item_size)
100 j += (item_size) ? item_size : strlen(l[i]) + 1;
101 j += (i + 1) * sizeof(char*); }
105 static int list_dup(char** l_src, char* ref, char* base, int item_size)
107 /* base is either either equal to ref or 0 or SEGPTR */
110 char** l_to = (char**)ref;
113 for(j=0;l_src[j];j++) ;
114 p += (j + 1) * sizeof(char*);
116 { l_to[i] = base + (p - ref);
117 k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
118 memcpy(p, l_src[i], k); p += k; }
125 static int hostent_size(struct hostent* p_he)
129 { size = sizeof(struct hostent);
130 size += strlen(p_he->h_name) + 1;
131 size += list_size(p_he->h_aliases, 0);
132 size += list_size(p_he->h_addr_list, p_he->h_length ); }
136 /* Copy hostent to p_to, fix up inside pointers using p_base (different for
137 * Win16 (linear vs. segmented). Return -neededsize on overrun.
139 static int WS_copy_he(struct ws_hostent *p_to,char *p_base,int t_size,struct hostent* p_he)
141 char* p_name,*p_aliases,*p_addr,*p;
142 int size=hostent_size(p_he)+(sizeof(struct ws_hostent)-sizeof(struct hostent));
147 p += sizeof(struct ws_hostent);
149 strcpy(p, p_he->h_name); p += strlen(p) + 1;
151 p += list_dup(p_he->h_aliases, p, p_base + (p - (char*)p_to), 0);
153 list_dup(p_he->h_addr_list, p, p_base + (p - (char*)p_to), p_he->h_length);
155 p_to->h_addrtype = (INT16)p_he->h_addrtype;
156 p_to->h_length = (INT16)p_he->h_length;
157 p_to->h_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
158 p_to->h_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
159 p_to->h_addr_list = (SEGPTR)(p_base + (p_addr - (char*)p_to));
166 static int protoent_size(struct protoent* p_pe)
170 { size = sizeof(struct protoent);
171 size += strlen(p_pe->p_name) + 1;
172 size += list_size(p_pe->p_aliases, 0); }
176 /* Copy protoent to p_to, fix up inside pointers using p_base (different for
177 * Win16 (linear vs. segmented). Return -neededsize on overrun.
179 static int WS_copy_pe(struct ws_protoent *p_to,char *p_base,int t_size,struct protoent* p_pe)
181 char* p_name,*p_aliases,*p;
182 int size=protoent_size(p_pe)+(sizeof(struct ws_protoent)-sizeof(struct protoent));
187 p += sizeof(struct ws_protoent);
189 strcpy(p, p_pe->p_name); p += strlen(p) + 1;
191 list_dup(p_pe->p_aliases, p, p_base + (p - (char*)p_to), 0);
193 p_to->p_proto = (INT16)p_pe->p_proto;
194 p_to->p_name = (SEGPTR)(p_base) + (p_name - (char*)p_to);
195 p_to->p_aliases = (SEGPTR)((p_base) + (p_aliases - (char*)p_to));
203 static int servent_size(struct servent* p_se)
207 size += sizeof(struct servent);
208 size += strlen(p_se->s_proto) + strlen(p_se->s_name) + 2;
209 size += list_size(p_se->s_aliases, 0);
214 /* Copy servent to p_to, fix up inside pointers using p_base (different for
215 * Win16 (linear vs. segmented). Return -neededsize on overrun.
217 static int WS_copy_se(struct ws_servent *p_to,char *p_base,int t_size,struct servent* p_se)
219 char* p_name,*p_aliases,*p_proto,*p;
220 int size = servent_size(p_se)+(sizeof(struct ws_servent)-sizeof(struct servent));
225 p += sizeof(struct ws_servent);
227 strcpy(p, p_se->s_name); p += strlen(p) + 1;
229 strcpy(p, p_se->s_proto); p += strlen(p) + 1;
231 list_dup(p_se->s_aliases, p, p_base + (p - (char*)p_to), 0);
233 p_to->s_port = (INT16)p_se->s_port;
234 p_to->s_name = (SEGPTR)(p_base + (p_name - (char*)p_to));
235 p_to->s_proto = (SEGPTR)(p_base + (p_proto - (char*)p_to));
236 p_to->s_aliases = (SEGPTR)(p_base + (p_aliases - (char*)p_to));
241 static HANDLE __ws_async_handle = 0xdead;
243 /* Generic async query struct. we use symbolic names for the different queries
246 typedef struct _async_query {
250 #define host_name ptr1
251 #define host_addr ptr1
252 #define serv_name ptr1
253 #define proto_name ptr1
255 #define serv_proto ptr2
257 #define host_len int1
258 #define proto_number int1
259 #define serv_port int1
261 #define host_type int2
265 HANDLE16 async_handle;
269 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
274 #define AQ_GETPROTO 1
280 /****************************************************************************
281 * The async query function.
283 * It is either called as a thread startup routine or directly. It has
284 * to free the passed arg from the process heap and PostMessageA the async
285 * result or the error code.
288 * - errorhandling not verified.
290 static DWORD WINAPI _async_queryfun(LPVOID arg) {
291 async_query *aq = (async_query*)arg;
294 char *targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:(char*)PTR_SEG_TO_LIN(aq->sbuf));
296 switch (aq->flags & AQ_GETMASK) {
299 struct ws_hostent *wshe = (struct ws_hostent*)targetptr;
301 he = (aq->flags & AQ_NAME) ?
302 gethostbyname(aq->host_name):
303 gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);
305 size = WS_copy_he(wshe,(char*)aq->sbuf,aq->sbuflen,he);
317 struct ws_protoent *wspe = (struct ws_protoent*)targetptr;
318 pe = (aq->flags & AQ_NAME)?
319 getprotobyname(aq->proto_name) :
320 getprotobynumber(aq->proto_number);
322 size = WS_copy_pe(wspe,(char*)aq->sbuf,aq->sbuflen,pe);
334 struct ws_servent *wsse = (struct ws_servent*)targetptr;
335 se = (aq->flags & AQ_NAME)?
336 getservbyname(aq->serv_name,aq->serv_proto) :
337 getservbyport(aq->serv_port,aq->serv_proto);
339 size = WS_copy_se(wsse,(char*)aq->sbuf,aq->sbuflen,se);
350 PostMessageA(aq->hWnd,aq->uMsg,aq->async_handle,size|(fail<<16));
351 HeapFree(GetProcessHeap(),0,arg);
355 /****************************************************************************
356 * The main async help function.
358 * It either starts a thread or just calls the function directly for platforms
359 * with no thread support. This relies on the fact that PostMessage() does
360 * not actually call the windowproc before the function returns.
362 static HANDLE16 __WSAsyncDBQuery(
363 HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,
364 void *sbuf, INT sbuflen, UINT flags
366 async_query *aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query));
375 aq->async_handle = ++__ws_async_handle;
377 aq->sbuf = (SEGPTR)sbuf;
378 aq->sbuflen = sbuflen;
380 hthread = CreateThread(NULL,0,_async_queryfun,aq,0,NULL);
381 if (hthread==INVALID_HANDLE_VALUE)
384 return __ws_async_handle;
388 /***********************************************************************
389 * WSAAsyncGetHostByAddr() (WINSOCK.102)
391 HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
392 INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
394 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
395 hWnd, uMsg, (unsigned)addr , len );
396 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,(void*)sbuf,buflen,AQ_NUMBER|AQ_WIN16|AQ_GETHOST);
399 /***********************************************************************
400 * WSAAsyncGetHostByAddr() (WSOCK32.102)
402 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
403 INT len, INT type, LPSTR sbuf, INT buflen)
405 TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",
406 hWnd, uMsg, (unsigned)addr , len );
407 return __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,AQ_NUMBER|AQ_WIN32|AQ_GETHOST);
410 /***********************************************************************
411 * WSAAsyncGetHostByName() (WINSOCK.103)
413 HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
414 SEGPTR sbuf, INT16 buflen)
416 TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n", hWnd, uMsg, (name)?name:"<null>", (int)buflen );
418 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_NAME|AQ_WIN16|AQ_GETHOST);
421 /***********************************************************************
422 * WSAAsyncGetHostByName() (WSOCK32.103)
424 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
425 LPSTR sbuf, INT buflen)
427 TRACE("hwnd %04x, msg %08x, host %s, buffer %i\n",
428 (HWND16)hWnd, uMsg, (name)?name:"<null>", (int)buflen );
429 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_NAME|AQ_WIN32|AQ_GETHOST);
432 /***********************************************************************
433 * WSAAsyncGetProtoByName() (WINSOCK.105)
435 HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
436 SEGPTR sbuf, INT16 buflen)
438 TRACE("hwnd %04x, msg %08x, protocol %s\n",
439 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
440 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN16);
443 /***********************************************************************
444 * WSAAsyncGetProtoByName() (WSOCK32.105)
446 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
447 LPSTR sbuf, INT buflen)
449 TRACE("hwnd %04x, msg %08x, protocol %s\n",
450 (HWND16)hWnd, uMsg, (name)?name:"<null>" );
451 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NAME|AQ_WIN32);
455 /***********************************************************************
456 * WSAAsyncGetProtoByNumber() (WINSOCK.104)
458 HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
459 SEGPTR sbuf, INT16 buflen)
461 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
462 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,(void*)sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);
465 /***********************************************************************
466 * WSAAsyncGetProtoByNumber() (WSOCK32.104)
468 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
469 LPSTR sbuf, INT buflen)
471 TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );
473 return __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,AQ_GETPROTO|AQ_NUMBER|AQ_WIN32);
476 /***********************************************************************
477 * WSAAsyncGetServByName() (WINSOCK.107)
479 HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
480 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
482 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
483 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
485 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN16);
488 /***********************************************************************
489 * WSAAsyncGetServByName() (WSOCK32.107)
491 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
492 LPCSTR proto, LPSTR sbuf, INT buflen)
494 TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",
495 hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>" );
496 return __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NAME|AQ_WIN32);
499 /***********************************************************************
500 * WSAAsyncGetServByPort() (WINSOCK.106)
502 HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
503 LPCSTR proto, SEGPTR sbuf, INT16 buflen)
505 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
506 hWnd, uMsg, port, (proto)?proto:"<null>" );
507 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,(void*)sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN16);
510 /***********************************************************************
511 * WSAAsyncGetServByPort() (WSOCK32.106)
513 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
514 LPCSTR proto, LPSTR sbuf, INT buflen)
516 TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",
517 hWnd, uMsg, port, (proto)?proto:"<null>" );
518 return __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,AQ_GETSERV|AQ_NUMBER|AQ_WIN32);
521 /***********************************************************************
522 * WSACancelAsyncRequest() (WINSOCK.108)(WSOCK32.109)
524 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
526 FIXME("(%08x),stub\n", hAsyncTaskHandle);
530 INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
532 return (HANDLE16)WSACancelAsyncRequest((HANDLE)hAsyncTaskHandle);