Problems reported by Donald Page, PR 105-107.
[wine/gsoc_dplay.git] / misc / winsock_dns.c
blob9dfde85dbec43d3465c76418495994efe5413bce
1 /*
2 * asynchronous DNS services
3 *
4 * (C) 1996,1997 Alex Korobka.
6 * TODO: Fork dns lookup helper during the startup (with a pipe
7 * for communication) and make it fork for a database request
8 * instead of forking the main process (i.e. something like
9 * Netscape 4.0).
12 #include "config.h"
14 #include <assert.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <signal.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/ipc.h>
21 #include <sys/msg.h>
22 #include <sys/wait.h>
23 #include <errno.h>
24 #ifdef __EMX__
25 # include <sys/so_ioctl.h>
26 #endif
27 #ifdef HAVE_SYS_PARAM_H
28 # include <sys/param.h>
29 #endif
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
32 #endif
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
35 #endif
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <netdb.h>
40 #ifdef HAVE_RESOLV_H
41 # include <resolv.h>
42 #endif
44 #include "winsock.h"
45 #include "heap.h"
46 #include "ldt.h"
47 #include "message.h"
48 #include "miscemu.h"
49 #include "async.h"
50 #include "debug.h"
52 static void WINSOCK_async_handler(int unixfd,void *private);
54 /* async DNS op control struct */
55 typedef struct
57 ws_async_op* ws_aop;
58 char* buffer;
59 int type;
60 union
62 char* init;
63 char* name;
64 char* addr;
65 } rq;
66 unsigned ilength;
67 } ws_async_ctl;
69 extern HANDLE16 __ws_gethandle( void* ptr );
70 extern void* __ws_memalloc( int size );
71 extern void __ws_memfree( void* ptr );
73 /* NOTE: ws_async_op list is traversed inside the SIGIO handler! */
74 static ws_async_op* __async_op_list = NULL;
76 static void fixup_wshe(struct ws_hostent* p_wshe, void* base);
77 static void fixup_wspe(struct ws_protoent* p_wspe, void* base);
78 static void fixup_wsse(struct ws_servent* p_wsse, void* base);
80 /* ----------------------------------- async/non-blocking I/O */
82 int WINSOCK_unblock_io(int fd, int noblock)
84 int fd_flags;
86 fd_flags = fcntl(fd, F_GETFL, 0);
87 if (fcntl(fd, F_SETFL, (noblock)? fd_flags | O_NONBLOCK
88 : fd_flags & ~O_NONBLOCK ) != -1) return 0;
89 return -1;
92 int WINSOCK_check_async_op(ws_async_op* p_aop)
94 ws_async_op* p = __async_op_list;
95 while( p ) if( p == p_aop ) return 1;
96 else p = p->next;
97 return 0;
100 int WINSOCK_cancel_async_op(ws_async_op* p_aop)
102 /* SIGIO unsafe! */
104 if( WINSOCK_check_async_op(p_aop) )
106 if( !(p_aop->flags & WSMSG_DEAD_AOP) )
108 kill(p_aop->pid, SIGKILL);
109 waitpid(p_aop->pid, NULL, 0); /* just in case */
110 close(p_aop->fd[0]);
112 WINSOCK_unlink_async_op(p_aop);
113 EVENT_DeleteIO( p_aop->fd[0], EVENT_IO_READ );
114 p_aop->flags = 0;
115 p_aop->hWnd = p_aop->uMsg = 0;
116 return 1;
118 return 0;
121 void WINSOCK_cancel_task_aops(HTASK16 hTask, void (*__opfree)(void*))
123 /* SIGIO safe, hTask == 0 cancels all outstanding async ops */
125 int num = 0, num_dead = 0;
126 ws_async_op* p, *next;
128 TRACE(winsock," cancelling async DNS requests... \n");
130 SIGNAL_MaskAsyncEvents( TRUE );
131 next = __async_op_list;
132 while( (p = next) )
134 HTASK16 hWndTask = GetWindowTask16(p->hWnd);
136 next = p->next;
137 if(!hTask || !hWndTask || (hTask == hWndTask))
139 num++;
140 if( p->flags & WSMSG_DEAD_AOP )
141 num_dead++;
143 WINSOCK_cancel_async_op(p);
144 if( __opfree ) __opfree(p);
147 SIGNAL_MaskAsyncEvents( FALSE );
148 TRACE(winsock," -> %i total (%i active)\n", num, num - num_dead );
151 void WINSOCK_link_async_op(ws_async_op* p_aop)
153 /* SIGIO safe */
155 p_aop->prev = NULL;
156 SIGNAL_MaskAsyncEvents( TRUE );
157 if( __async_op_list )
159 ws_async_op* p = __async_op_list;
160 __async_op_list->prev = p_aop;
162 /* traverse the list and retire dead ops created
163 * by the signal handler (see below). */
165 while( p )
167 if( p->flags & WSMSG_DEAD_AOP )
169 ws_async_op* dead = p;
171 TRACE(winsock,"\treaping dead aop [%08x]\n", (unsigned)p );
173 p = p->next;
174 WINSOCK_unlink_async_op( dead );
175 __ws_memfree( dead );
176 continue;
178 p = p->next;
181 p_aop->next = __async_op_list;
182 __async_op_list = p_aop;
184 SIGNAL_MaskAsyncEvents( FALSE );
186 ASYNC_RegisterFD(p_aop->fd[0],WINSOCK_async_handler,p_aop);
189 void WINSOCK_unlink_async_op(ws_async_op* p_aop)
191 /* SIGIO unsafe! */
193 if( p_aop == __async_op_list ) __async_op_list = p_aop->next;
194 else
195 p_aop->prev->next = p_aop->next;
196 if( p_aop->next ) p_aop->next->prev = p_aop->prev;
198 ASYNC_UnregisterFD(p_aop->fd[0],WINSOCK_async_handler);
201 /* ----------------------------------- SIGIO handler -
203 * link_async_op/unlink_async_op allow to install generic
204 * async IO handlers (provided that aop_control function is defined).
206 * Note: pipe-based handlers must raise explicit SIGIO with kill(2).
209 static void WINSOCK_async_handler(int unixfd,void *private)
211 ws_async_op* p_aop = (ws_async_op*)private;
213 if( p_aop->aop_control(p_aop, AOP_IO) == AOP_CONTROL_REMOVE )
215 /* NOTE: memory management is signal-unsafe, therefore
216 * we can only set a flag to remove this p_aop later on.
218 p_aop->flags = WSMSG_DEAD_AOP;
219 close(p_aop->fd[0]);
220 if( p_aop->pid )
222 kill(p_aop->pid, SIGKILL);
223 waitpid(p_aop->pid, NULL, WNOHANG);
224 p_aop->pid = 0;
229 /* ----------------------------------- getXbyY requests */
231 /* child process control struct */
232 static ws_async_ctl async_ctl;
234 static int aop_control(ws_async_op* p_aop, int flag )
236 unsigned lLength;
238 /* success: LOWORD(lLength) has the length of the struct
239 * to read.
240 * failure: LOWORD(lLength) is zero, HIWORD(lLength) contains
241 * the error code.
244 read(p_aop->fd[0], &lLength, sizeof(unsigned));
245 if( LOWORD(lLength) )
247 if( (int)LOWORD(lLength) <= p_aop->buflen )
249 char* buffer = (p_aop->flags & WSMSG_WIN32_AOP)
250 ? p_aop->b.lin_base : (char*)PTR_SEG_TO_LIN(p_aop->b.seg_base);
252 read(p_aop->fd[0], buffer, LOWORD(lLength));
253 switch( p_aop->flags & WSMSG_ASYNC_RQMASK )
255 case WSMSG_ASYNC_HOSTBYNAME:
256 case WSMSG_ASYNC_HOSTBYADDR:
257 fixup_wshe((struct ws_hostent*)buffer, p_aop->b.ptr_base); break;
258 case WSMSG_ASYNC_PROTOBYNAME:
259 case WSMSG_ASYNC_PROTOBYNUM:
260 fixup_wspe((struct ws_protoent*)buffer, p_aop->b.ptr_base); break;
261 case WSMSG_ASYNC_SERVBYNAME:
262 case WSMSG_ASYNC_SERVBYPORT:
263 fixup_wsse((struct ws_servent*)buffer, p_aop->b.ptr_base); break;
264 default:
265 if( p_aop->flags ) WARN(winsock,"Received unknown async request!\n");
266 return AOP_CONTROL_REMOVE;
269 else lLength = ((UINT32)LOWORD(lLength)) | ((unsigned)WSAENOBUFS << 16);
270 } /* failure */
272 /* was a __WS_ASYNC_DEBUG statement */
273 TRACE(winsock, "DNS aop completed: hWnd [%04x], uMsg [%04x], "
274 "aop [%04x], event [%08lx]\n",
275 p_aop->hWnd, p_aop->uMsg, __ws_gethandle(p_aop), (LPARAM)lLength);
277 /* FIXME: update num_async_rq */
278 EVENT_DeleteIO( p_aop->fd[0], EVENT_IO_READ );
279 PostMessage32A( p_aop->hWnd, p_aop->uMsg, __ws_gethandle(p_aop), (LPARAM)lLength );
281 return AOP_CONTROL_REMOVE; /* one-shot request */
285 HANDLE16 __WSAsyncDBQuery(LPWSINFO pwsi, HWND32 hWnd, UINT32 uMsg, INT32 type,
286 LPCSTR init, INT32 len, LPCSTR proto, void* sbuf, INT32 buflen, UINT32 flag)
288 /* queue 'flag' request and fork off its handler */
290 async_ctl.ws_aop = (ws_async_op*)__ws_memalloc(sizeof(ws_async_op));
292 if( async_ctl.ws_aop )
294 HANDLE16 handle = __ws_gethandle(async_ctl.ws_aop);
296 if( pipe(async_ctl.ws_aop->fd) == 0 )
298 async_ctl.rq.init = (char*)init;
299 async_ctl.ilength = len;
300 async_ctl.buffer = (char*)proto;
301 async_ctl.type = type;
303 async_ctl.ws_aop->hWnd = hWnd;
304 async_ctl.ws_aop->uMsg = uMsg;
305 async_ctl.ws_aop->b.ptr_base = sbuf;
306 async_ctl.ws_aop->buflen = buflen;
307 async_ctl.ws_aop->flags = flag;
308 async_ctl.ws_aop->aop_control = &aop_control;
310 WINSOCK_link_async_op( async_ctl.ws_aop );
312 EVENT_AddIO( async_ctl.ws_aop->fd[0], EVENT_IO_READ );
313 pwsi->num_async_rq++;
315 async_ctl.ws_aop->pid = fork();
316 if( async_ctl.ws_aop->pid )
318 TRACE(winsock, "\tasync_op = %04x (child %i)\n",
319 handle, async_ctl.ws_aop->pid);
321 close(async_ctl.ws_aop->fd[1]); /* write endpoint */
322 if( async_ctl.ws_aop->pid > 0 )
323 return __ws_gethandle(async_ctl.ws_aop);
325 /* fork() failed */
327 pwsi->num_async_rq--;
328 EVENT_DeleteIO( async_ctl.ws_aop->fd[0], EVENT_IO_READ );
329 close(async_ctl.ws_aop->fd[0]);
330 pwsi->err = WSAEWOULDBLOCK;
332 else
334 extern BOOL32 THREAD_InitDone;
336 THREAD_InitDone = FALSE;
337 /* child process */
339 close(async_ctl.ws_aop->fd[0]); /* read endpoint */
340 switch( flag & WSMSG_ASYNC_RQMASK )
342 case WSMSG_ASYNC_HOSTBYADDR:
343 case WSMSG_ASYNC_HOSTBYNAME:
344 WS_do_async_gethost(pwsi, flag);
345 break;
346 case WSMSG_ASYNC_PROTOBYNUM:
347 case WSMSG_ASYNC_PROTOBYNAME:
348 WS_do_async_getproto(pwsi, flag);
349 break;
350 case WSMSG_ASYNC_SERVBYPORT:
351 case WSMSG_ASYNC_SERVBYNAME:
352 WS_do_async_getserv(pwsi, flag);
353 break;
355 _exit(0); /* skip atexit()'ed cleanup */
358 else pwsi->err = wsaErrno(); /* failed to create pipe */
360 __ws_memfree((void*)async_ctl.ws_aop);
361 } else pwsi->err = WSAEWOULDBLOCK;
362 return 0;
365 static int _async_notify()
367 /* use half-duplex pipe to send variable length packets
368 * to the parent process */
370 write(async_ctl.ws_aop->fd[1], &async_ctl.ilength, sizeof(unsigned));
371 write(async_ctl.ws_aop->fd[1], async_ctl.buffer, async_ctl.ilength );
373 #ifndef __EMX__
374 kill(getppid(), SIGIO); /* simulate async I/O */
375 #endif
377 /* was a __WS_ASYNC_DEBUG statement */
378 TRACE(winsock, "handler - notify aop [%d, buf %d]\n",
379 async_ctl.ilength, async_ctl.ws_aop->buflen);
380 return 1;
383 static void _async_fail()
385 /* write a DWORD with error code (low word is zero) */
387 async_ctl.ilength =
388 (h_errno < 0) ? (unsigned)WSAMAKEASYNCREPLY( 0, wsaErrno() )
389 : (unsigned)WSAMAKEASYNCREPLY( 0, wsaHerrno() );
390 write(async_ctl.ws_aop->fd[1], &async_ctl.ilength, sizeof(unsigned) );
391 #ifndef __EMX__
392 kill(getppid(), SIGIO); /* simulate async I/O */
393 #endif
395 /* was a __WS_ASYNC_DEBUG statement */
396 TRACE(winsock, "handler - failed aop [%d, buf %d]\n",
397 async_ctl.ilength, async_ctl.ws_aop->buflen);
400 void dump_ws_hostent_offset(struct ws_hostent* wshe)
402 int i;
403 char* base = (char*)wshe;
404 unsigned* ptr;
406 DUMP("h_name = %08x\t[%s]\n",
407 (unsigned)wshe->h_name, base + (unsigned)wshe->h_name);
408 DUMP("h_aliases = %08x\t[%08x]\n",
409 (unsigned)wshe->h_aliases, (unsigned)(base+(unsigned)wshe->h_aliases));
410 ptr = (unsigned*)(base + (unsigned)wshe->h_aliases);
411 for(i = 0; ptr[i]; i++ )
413 DUMP("%i - %08x [%s]\n", i + 1, ptr[i], ((char*)base) + ptr[i]);
415 DUMP("h_length = %i\n", wshe->h_length);
418 void WS_do_async_gethost(LPWSINFO pwsi, unsigned flag )
420 int size = 0;
421 struct hostent* p_he;
423 close(async_ctl.ws_aop->fd[0]);
425 p_he = (flag & WSMSG_ASYNC_HOSTBYNAME)
426 ? gethostbyname(async_ctl.rq.name)
427 : gethostbyaddr(async_ctl.rq.name,
428 async_ctl.ilength, async_ctl.type);
430 TRACE(winsock,"DNS: got hostent for [%s]\n", async_ctl.rq.name );
432 if( p_he ) /* convert to the Winsock format with internal pointers as offsets */
433 size = WS_dup_he(pwsi, p_he, WS_DUP_OFFSET |
434 ((flag & WSMSG_WIN32_AOP) ? WS_DUP_LINEAR : WS_DUP_SEGPTR) );
435 if( size )
437 async_ctl.buffer = (char*)pwsi->he;
438 async_ctl.ilength = (unsigned)WSAMAKEASYNCREPLY( (UINT16)size, 0 );
439 _async_notify( flag );
441 else _async_fail();
444 void WS_do_async_getproto(LPWSINFO pwsi, unsigned flag )
446 int size = 0;
447 struct protoent* p_pe;
449 close(async_ctl.ws_aop->fd[0]);
450 p_pe = (flag & WSMSG_ASYNC_PROTOBYNAME)
451 ? getprotobyname(async_ctl.rq.name)
452 : getprotobynumber(async_ctl.type);
454 TRACE(winsock,"DNS: got protoent for [%s]\n", async_ctl.rq.name );
456 if( p_pe ) /* convert to the Winsock format with internal pointers as offsets */
457 size = WS_dup_pe(pwsi, p_pe, WS_DUP_OFFSET |
458 ((flag & WSMSG_WIN32_AOP) ? WS_DUP_LINEAR : WS_DUP_SEGPTR) );
459 if( size )
461 async_ctl.buffer = (char*)pwsi->pe;
462 async_ctl.ilength = (unsigned)WSAMAKEASYNCREPLY( (UINT16)size, 0 );
463 _async_notify( flag );
465 else _async_fail();
468 void WS_do_async_getserv(LPWSINFO pwsi, unsigned flag )
470 int size = 0;
471 struct servent* p_se;
473 close(async_ctl.ws_aop->fd[0]);
474 p_se = (flag & WSMSG_ASYNC_SERVBYNAME)
475 ? getservbyname(async_ctl.rq.name, async_ctl.buffer)
476 : getservbyport(async_ctl.type, async_ctl.buffer);
478 if( p_se ) /* convert to the Winsock format with internal pointers as offsets */
479 size = WS_dup_se(pwsi, p_se, WS_DUP_OFFSET |
480 ((flag & WSMSG_WIN32_AOP) ? WS_DUP_LINEAR : WS_DUP_SEGPTR) );
481 if( size )
483 async_ctl.buffer = (char*)pwsi->se;
484 async_ctl.ilength = (unsigned)WSAMAKEASYNCREPLY( (UINT16)size, 0 );
485 _async_notify( flag );
487 else _async_fail();
490 /* ----------------------------------- helper functions -
492 * Raw results from the pipe contain internal pointers stored as
493 * offsets relative to the beginning of the buffer and we need
494 * to apply a fixup before passing them to applications.
496 * NOTE: It is possible to exploit the fact that fork() doesn't
497 * change the buffer address by storing fixed up pointers right
498 * in the handler. However, this will get in the way if we ever
499 * get around to implementing DNS helper daemon a-la Netscape 4.x.
502 void fixup_wshe(struct ws_hostent* p_wshe, void* base)
504 /* add 'base' to ws_hostent pointers to convert them from offsets */
506 int i;
507 unsigned* p_aliases,*p_addr;
509 p_aliases = (unsigned*)((char*)p_wshe + (unsigned)p_wshe->h_aliases);
510 p_addr = (unsigned*)((char*)p_wshe + (unsigned)p_wshe->h_addr_list);
511 ((unsigned)(p_wshe->h_name)) += (unsigned)base;
512 ((unsigned)(p_wshe->h_aliases)) += (unsigned)base;
513 ((unsigned)(p_wshe->h_addr_list)) += (unsigned)base;
514 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
515 for(i=0;p_addr[i];i++) p_addr[i] += (unsigned)base;
518 void fixup_wspe(struct ws_protoent* p_wspe, void* base)
520 int i;
521 unsigned* p_aliases = (unsigned*)((char*)p_wspe + (unsigned)p_wspe->p_aliases);
522 ((unsigned)(p_wspe->p_name)) += (unsigned)base;
523 ((unsigned)(p_wspe->p_aliases)) += (unsigned)base;
524 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
527 void fixup_wsse(struct ws_servent* p_wsse, void* base)
529 int i;
530 unsigned* p_aliases = (unsigned*)((char*)p_wsse + (unsigned)p_wsse->s_aliases);
531 ((unsigned)(p_wsse->s_name)) += (unsigned)base;
532 ((p_wsse->s_proto)) += (unsigned)base;
533 ((p_wsse->s_aliases)) += (unsigned)base;
534 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;