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>
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_FILIO_H
29 # include <sys/filio.h>
32 #include <sys/ioccom.h>
33 #include <sys/sockio.h>
37 # include <sys/so_ioctl.h>
40 #ifdef HAVE_SYS_PARAM_H
41 # include <sys/param.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
52 #include <sys/errno.h>
56 #ifdef HAVE_ARPA_NAMESER_H
57 # include <arpa/nameser.h>
63 #include "wine/winbase16.h"
72 DEFAULT_DEBUG_CHANNEL(winsock
)
74 /* ----------------------------------- helper functions - */
76 static int list_size(char** l
, int item_size
)
81 j
+= (item_size
) ? item_size
: strlen(l
[i
]) + 1;
82 j
+= (i
+ 1) * sizeof(char*); }
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 */
91 char** l_to
= (char**)ref
;
94 for(j
=0;l_src
[j
];j
++) ;
95 p
+= (j
+ 1) * sizeof(char*);
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
; }
106 static int hostent_size(struct hostent
* 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
); }
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
));
128 p
+= sizeof(struct ws_hostent
);
130 strcpy(p
, p_he
->h_name
); p
+= strlen(p
) + 1;
132 p
+= list_dup(p_he
->h_aliases
, p
, p_base
+ (p
- (char*)p_to
), 0);
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
));
147 static int protoent_size(struct protoent
* p_pe
)
151 { size
= sizeof(struct protoent
);
152 size
+= strlen(p_pe
->p_name
) + 1;
153 size
+= list_size(p_pe
->p_aliases
, 0); }
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
));
168 p
+= sizeof(struct ws_protoent
);
170 strcpy(p
, p_pe
->p_name
); p
+= strlen(p
) + 1;
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
));
184 static int servent_size(struct servent
* 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);
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
));
206 p
+= sizeof(struct ws_servent
);
208 strcpy(p
, p_se
->s_name
); p
+= strlen(p
) + 1;
210 strcpy(p
, p_se
->s_proto
); p
+= strlen(p
) + 1;
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
));
222 static HANDLE __ws_async_handle
= 0xdead;
224 /* Generic async query struct. we use symbolic names for the different queries
227 typedef struct _async_query
{
231 #define host_name ptr1
232 #define host_addr ptr1
233 #define serv_name ptr1
234 #define proto_name ptr1
236 #define serv_proto ptr2
238 #define host_len int1
239 #define proto_number int1
240 #define serv_port int1
242 #define host_type int2
246 HANDLE16 async_handle
;
250 #define HB_WIN32(hb) (hb->flags & AQ_WIN32)
255 #define AQ_GETPROTO 1
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.
269 * - errorhandling not verified.
271 static DWORD WINAPI
_async_queryfun(LPVOID arg
) {
272 async_query
*aq
= (async_query
*)arg
;
275 char *targetptr
= (HB_WIN32(aq
)?(char*)aq
->sbuf
:(char*)PTR_SEG_TO_LIN(aq
->sbuf
));
277 switch (aq
->flags
& AQ_GETMASK
) {
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
);
286 size
= WS_copy_he(wshe
,(char*)aq
->sbuf
,aq
->sbuflen
,he
);
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
);
303 size
= WS_copy_pe(wspe
,(char*)aq
->sbuf
,aq
->sbuflen
,pe
);
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
);
320 size
= WS_copy_se(wsse
,(char*)aq
->sbuf
,aq
->sbuflen
,se
);
331 PostMessageA(aq
->hWnd
,aq
->uMsg
,aq
->async_handle
,size
|(fail
<<16));
332 HeapFree(GetProcessHeap(),0,arg
);
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
));
356 aq
->async_handle
= ++__ws_async_handle
;
358 aq
->sbuf
= (SEGPTR
)sbuf
;
359 aq
->sbuflen
= sbuflen
;
361 hthread
= CreateThread(NULL
,0,_async_queryfun
,aq
,0,NULL
);
362 if (hthread
==INVALID_HANDLE_VALUE
)
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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "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(winsock
, "(%08x),stub\n", hAsyncTaskHandle
);
511 INT16 WINAPI
WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle
)
513 return (HANDLE16
)WSACancelAsyncRequest((HANDLE
)hAsyncTaskHandle
);