Release 980329
[wine/multimedia.git] / misc / winsock_dns.c
blob8a42a792a4dcd6774fba5b3ba6777874ace680f1
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 <unistd.h>
13 #include <string.h>
14 #include <signal.h>
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
17 #include <sys/ipc.h>
18 #include <sys/msg.h>
19 #include <sys/wait.h>
20 #include <errno.h>
21 #ifdef __EMX__
22 #include <sys/so_ioctl.h>
23 #include <sys/param.h>
24 #endif
25 #ifdef __svr4__
26 #include <sys/file.h>
27 #include <sys/filio.h>
28 #endif
30 extern int h_errno;
32 #include "winsock.h"
33 #include "windows.h"
34 #include "heap.h"
35 #include "ldt.h"
36 #include "message.h"
37 #include "selectors.h"
38 #include "miscemu.h"
39 #include "sig_context.h"
40 #include "debug.h"
42 #ifndef FASYNC
43 #define FASYNC FIOASYNC
44 #endif
46 typedef struct /* async DNS op control struct */
48 ws_async_op* ws_aop;
49 char* buffer;
50 int type;
51 union
53 char* init;
54 char* name;
55 char* addr;
56 } rq;
57 unsigned ilength;
58 } ws_async_ctl;
60 extern HANDLE16 __ws_gethandle( void* ptr );
61 extern void* __ws_memalloc( int size );
62 extern void __ws_memfree( void* ptr );
64 /* NOTE: ws_async_op list is traversed inside the SIGIO handler! */
66 static int __async_io_max_fd = 0;
67 static fd_set __async_io_fdset;
68 static ws_async_op* __async_op_list = NULL;
70 static void fixup_wshe(struct ws_hostent* p_wshe, void* base);
71 static void fixup_wspe(struct ws_protoent* p_wspe, void* base);
72 static void fixup_wsse(struct ws_servent* p_wsse, void* base);
74 extern void EVENT_AddIO( int fd, unsigned flag );
75 extern void EVENT_DeleteIO( int fd, unsigned flag );
77 /* ----------------------------------- async/non-blocking I/O */
79 int WINSOCK_async_io(int fd, int async)
81 int fd_flags;
83 #ifndef __EMX__
84 fcntl(fd, F_SETOWN, getpid());
85 #endif
87 fd_flags = fcntl(fd, F_GETFL, 0);
88 if (fcntl(fd, F_SETFL, (async)? fd_flags | FASYNC
89 : fd_flags & ~FASYNC ) != -1) return 0;
90 return -1;
93 int WINSOCK_unblock_io(int fd, int noblock)
95 int fd_flags;
97 fd_flags = fcntl(fd, F_GETFL, 0);
98 if (fcntl(fd, F_SETFL, (noblock)? fd_flags | O_NONBLOCK
99 : fd_flags & ~O_NONBLOCK ) != -1) return 0;
100 return -1;
103 int WINSOCK_check_async_op(ws_async_op* p_aop)
105 ws_async_op* p = __async_op_list;
106 while( p ) if( p == p_aop ) return 1;
107 else p = p->next;
108 return 0;
111 int WINSOCK_cancel_async_op(ws_async_op* p_aop)
113 /* SIGIO unsafe! */
115 if( WINSOCK_check_async_op(p_aop) )
117 if( !(p_aop->flags & WSMSG_DEAD_AOP) )
119 kill(p_aop->pid, SIGKILL);
120 waitpid(p_aop->pid, NULL, 0); /* just in case */
121 close(p_aop->fd[0]);
123 WINSOCK_unlink_async_op(p_aop);
124 EVENT_DeleteIO( p_aop->fd[0], EVENT_IO_READ );
125 p_aop->flags = 0;
126 p_aop->hWnd = p_aop->uMsg = 0;
127 return 1;
129 return 0;
132 void WINSOCK_cancel_task_aops(HTASK16 hTask, void (*__opfree)(void*))
134 /* SIGIO safe, hTask == 0 cancels all outstanding async ops */
136 int num = 0, num_dead = 0;
137 ws_async_op* p, *next;
139 TRACE(winsock," cancelling async DNS requests... \n");
141 SIGNAL_MaskAsyncEvents( TRUE );
142 next = __async_op_list;
143 while( (p = next) )
145 HTASK16 hWndTask = GetWindowTask16(p->hWnd);
147 next = p->next;
148 if(!hTask || !hWndTask || (hTask == hWndTask))
150 num++;
151 if( p->flags & WSMSG_DEAD_AOP )
152 num_dead++;
154 WINSOCK_cancel_async_op(p);
155 if( __opfree ) __opfree(p);
158 SIGNAL_MaskAsyncEvents( FALSE );
159 TRACE(winsock," -> %i total (%i active)\n", num, num - num_dead );
162 void WINSOCK_link_async_op(ws_async_op* p_aop)
164 /* SIGIO safe */
166 p_aop->prev = NULL;
167 SIGNAL_MaskAsyncEvents( TRUE );
168 if( __async_op_list )
170 ws_async_op* p = __async_op_list;
171 __async_op_list->prev = p_aop;
173 /* traverse the list and retire dead ops created
174 * by the signal handler (see below). */
176 while( p )
178 if( p->flags & WSMSG_DEAD_AOP )
180 ws_async_op* dead = p;
182 TRACE(winsock,"\treaping dead aop [%08x]\n", (unsigned)p );
184 p = p->next;
185 WINSOCK_unlink_async_op( dead );
186 __ws_memfree( dead );
187 continue;
189 p = p->next;
192 else FD_ZERO(&__async_io_fdset);
193 p_aop->next = __async_op_list;
194 __async_op_list = p_aop;
195 SIGNAL_MaskAsyncEvents( FALSE );
197 FD_SET(p_aop->fd[0], &__async_io_fdset);
198 if( p_aop->fd[0] > __async_io_max_fd )
199 __async_io_max_fd = p_aop->fd[0];
202 void WINSOCK_unlink_async_op(ws_async_op* p_aop)
204 /* SIGIO unsafe! */
206 if( p_aop == __async_op_list ) __async_op_list = p_aop->next;
207 else
208 p_aop->prev->next = p_aop->next;
209 if( p_aop->next ) p_aop->next->prev = p_aop->prev;
211 FD_CLR(p_aop->fd[0], &__async_io_fdset);
212 if( p_aop->fd[0] == __async_io_max_fd )
213 __async_io_max_fd--;
216 /* ----------------------------------- SIGIO handler -
218 * link_async_op/unlink_async_op allow to install generic
219 * async IO handlers (provided that aop_control function is defined).
221 * Note: pipe-based handlers must raise explicit SIGIO with kill(2).
224 HANDLER_DEF(WINSOCK_sigio)
226 struct timeval timeout;
227 fd_set check_set;
228 ws_async_op* p_aop;
230 HANDLER_INIT();
232 check_set = __async_io_fdset;
233 memset(&timeout, 0, sizeof(timeout));
235 while( select(__async_io_max_fd + 1,
236 &check_set, NULL, NULL, &timeout) > 0)
238 for( p_aop = __async_op_list;
239 p_aop ; p_aop = p_aop->next )
240 if( FD_ISSET(p_aop->fd[0], &check_set) )
241 if( p_aop->aop_control(p_aop, AOP_IO) == AOP_CONTROL_REMOVE )
243 /* NOTE: memory management is signal-unsafe, therefore
244 * we can only set a flag to remove this p_aop later on.
247 p_aop->flags = WSMSG_DEAD_AOP;
248 close(p_aop->fd[0]);
249 FD_CLR(p_aop->fd[0],&__async_io_fdset);
250 if( p_aop->fd[0] == __async_io_max_fd )
251 __async_io_max_fd = p_aop->fd[0];
252 if( p_aop->pid )
254 kill(p_aop->pid, SIGKILL);
255 waitpid(p_aop->pid, NULL, WNOHANG);
256 p_aop->pid = 0;
259 check_set = __async_io_fdset;
263 /* ----------------------------------- getXbyY requests */
265 static ws_async_ctl async_ctl; /* child process control struct */
267 static int aop_control(ws_async_op* p_aop, int flag )
269 unsigned lLength;
271 /* success: LOWORD(lLength) has the length of the struct
272 * to read.
273 * failure: LOWORD(lLength) is zero, HIWORD(lLength) contains
274 * the error code.
277 read(p_aop->fd[0], &lLength, sizeof(unsigned));
278 if( LOWORD(lLength) )
280 if( (int)LOWORD(lLength) <= p_aop->buflen )
282 char* buffer = (p_aop->flags & WSMSG_WIN32_AOP)
283 ? p_aop->b.lin_base : (char*)PTR_SEG_TO_LIN(p_aop->b.seg_base);
285 read(p_aop->fd[0], buffer, LOWORD(lLength));
286 switch( p_aop->flags & WSMSG_ASYNC_RQMASK )
288 case WSMSG_ASYNC_HOSTBYNAME:
289 case WSMSG_ASYNC_HOSTBYADDR:
290 fixup_wshe((struct ws_hostent*)buffer, p_aop->b.ptr_base); break;
291 case WSMSG_ASYNC_PROTOBYNAME:
292 case WSMSG_ASYNC_PROTOBYNUM:
293 fixup_wspe((struct ws_protoent*)buffer, p_aop->b.ptr_base); break;
294 case WSMSG_ASYNC_SERVBYNAME:
295 case WSMSG_ASYNC_SERVBYPORT:
296 fixup_wsse((struct ws_servent*)buffer, p_aop->b.ptr_base); break;
297 default:
298 if( p_aop->flags ) fprintf(stderr,"Received unknown async request!\n");
299 return AOP_CONTROL_REMOVE;
302 else lLength = ((UINT32)LOWORD(lLength)) | ((unsigned)WSAENOBUFS << 16);
303 } /* failure */
305 /* was a __WS_ASYNC_DEBUG statement */
306 TRACE(winsock, "DNS aop completed: hWnd [%04x], uMsg [%04x], "
307 "aop [%04x], event [%08lx]\n",
308 p_aop->hWnd, p_aop->uMsg, __ws_gethandle(p_aop), (LPARAM)lLength);
310 /* FIXME: update num_async_rq */
311 EVENT_DeleteIO( p_aop->fd[0], EVENT_IO_READ );
312 PostMessage32A( p_aop->hWnd, p_aop->uMsg, __ws_gethandle(p_aop), (LPARAM)lLength );
314 return AOP_CONTROL_REMOVE; /* one-shot request */
318 HANDLE16 __WSAsyncDBQuery(LPWSINFO pwsi, HWND32 hWnd, UINT32 uMsg, INT32 type, LPSTR init,
319 INT32 len, LPSTR proto, void* sbuf, INT32 buflen, UINT32 flag)
321 /* queue 'flag' request and fork off its handler */
323 async_ctl.ws_aop = (ws_async_op*)__ws_memalloc(sizeof(ws_async_op));
325 if( async_ctl.ws_aop )
327 HANDLE16 handle = __ws_gethandle(async_ctl.ws_aop);
329 if( pipe(async_ctl.ws_aop->fd) == 0 )
331 async_ctl.rq.init = (char*)init;
332 async_ctl.ilength = len;
333 async_ctl.buffer = proto;
334 async_ctl.type = type;
336 async_ctl.ws_aop->hWnd = hWnd;
337 async_ctl.ws_aop->uMsg = uMsg;
338 async_ctl.ws_aop->b.ptr_base = sbuf;
339 async_ctl.ws_aop->buflen = buflen;
340 async_ctl.ws_aop->flags = flag;
341 async_ctl.ws_aop->aop_control = &aop_control;
343 WINSOCK_link_async_op( async_ctl.ws_aop );
345 EVENT_AddIO( async_ctl.ws_aop->fd[0], EVENT_IO_READ );
346 pwsi->num_async_rq++;
348 async_ctl.ws_aop->pid = fork();
349 if( async_ctl.ws_aop->pid )
351 TRACE(winsock, "\tasync_op = %04x (child %i)\n",
352 handle, async_ctl.ws_aop->pid);
354 close(async_ctl.ws_aop->fd[1]); /* write endpoint */
355 if( async_ctl.ws_aop->pid > 0 )
356 return __ws_gethandle(async_ctl.ws_aop);
358 /* fork() failed */
360 pwsi->num_async_rq--;
361 EVENT_DeleteIO( async_ctl.ws_aop->fd[0], EVENT_IO_READ );
362 close(async_ctl.ws_aop->fd[0]);
363 pwsi->err = WSAEWOULDBLOCK;
365 else
367 /* child process */
369 close(async_ctl.ws_aop->fd[0]); /* read endpoint */
370 switch( flag & WSMSG_ASYNC_RQMASK )
372 case WSMSG_ASYNC_HOSTBYADDR:
373 case WSMSG_ASYNC_HOSTBYNAME:
374 WS_do_async_gethost(pwsi, flag);
375 break;
376 case WSMSG_ASYNC_PROTOBYNUM:
377 case WSMSG_ASYNC_PROTOBYNAME:
378 WS_do_async_getproto(pwsi, flag);
379 break;
380 case WSMSG_ASYNC_SERVBYPORT:
381 case WSMSG_ASYNC_SERVBYNAME:
382 WS_do_async_getserv(pwsi, flag);
383 break;
385 _exit(0); /* skip atexit()'ed cleanup */
388 else pwsi->err = wsaErrno(); /* failed to create pipe */
390 __ws_memfree((void*)async_ctl.ws_aop);
391 } else pwsi->err = WSAEWOULDBLOCK;
392 return 0;
395 static int _async_notify()
397 /* use half-duplex pipe to send variable length packets
398 * to the parent process */
400 write(async_ctl.ws_aop->fd[1], &async_ctl.ilength, sizeof(unsigned));
401 write(async_ctl.ws_aop->fd[1], async_ctl.buffer, async_ctl.ilength );
403 #ifndef __EMX__
404 kill(getppid(), SIGIO); /* simulate async I/O */
405 #endif
407 /* was a __WS_ASYNC_DEBUG statement */
408 TRACE(winsock, "handler - notify aop [%d, buf %d]\n",
409 async_ctl.ilength, async_ctl.ws_aop->buflen);
410 return 1;
413 static void _async_fail()
415 /* write a DWORD with error code (low word is zero) */
417 async_ctl.ilength =
418 (h_errno < 0) ? (unsigned)WSAMAKEASYNCREPLY( 0, wsaErrno() )
419 : (unsigned)WSAMAKEASYNCREPLY( 0, wsaHerrno() );
420 write(async_ctl.ws_aop->fd[1], &async_ctl.ilength, sizeof(unsigned) );
421 #ifndef __EMX__
422 kill(getppid(), SIGIO); /* simulate async I/O */
423 #endif
425 /* was a __WS_ASYNC_DEBUG statement */
426 TRACE(winsock, "handler - failed aop [%d, buf %d]\n",
427 async_ctl.ilength, async_ctl.ws_aop->buflen);
430 void dump_ws_hostent_offset(struct ws_hostent* wshe)
432 int i;
433 char* base = (char*)wshe;
434 unsigned* ptr;
436 DUMP("h_name = %08x\t[%s]\n",
437 (unsigned)wshe->h_name, base + (unsigned)wshe->h_name);
438 DUMP("h_aliases = %08x\t[%08x]\n",
439 (unsigned)wshe->h_aliases, (unsigned)(base+(unsigned)wshe->h_aliases));
440 ptr = (unsigned*)(base + (unsigned)wshe->h_aliases);
441 for(i = 0; ptr[i]; i++ )
443 DUMP("%i - %08x [%s]\n", i + 1, ptr[i], ((char*)base) + ptr[i]);
445 DUMP("h_length = %i\n", wshe->h_length);
448 void WS_do_async_gethost(LPWSINFO pwsi, unsigned flag )
450 int size = 0;
451 struct hostent* p_he;
453 close(async_ctl.ws_aop->fd[0]);
455 p_he = (flag & WSMSG_ASYNC_HOSTBYNAME)
456 ? gethostbyname(async_ctl.rq.name)
457 : gethostbyaddr(async_ctl.rq.name,
458 async_ctl.ilength, async_ctl.type);
460 TRACE(winsock,"DNS: got hostent for [%s]\n", async_ctl.rq.name );
462 if( p_he ) /* convert to the Winsock format with internal pointers as offsets */
463 size = WS_dup_he(pwsi, p_he, WS_DUP_OFFSET |
464 ((flag & WSMSG_WIN32_AOP) ? WS_DUP_LINEAR : WS_DUP_SEGPTR) );
465 if( size )
467 async_ctl.buffer = pwsi->buffer;
468 async_ctl.ilength = (unsigned)WSAMAKEASYNCREPLY( (UINT16)size, 0 );
469 _async_notify( flag );
471 else _async_fail();
474 void WS_do_async_getproto(LPWSINFO pwsi, unsigned flag )
476 int size = 0;
477 struct protoent* p_pe;
479 close(async_ctl.ws_aop->fd[0]);
480 p_pe = (flag & WSMSG_ASYNC_PROTOBYNAME)
481 ? getprotobyname(async_ctl.rq.name)
482 : getprotobynumber(async_ctl.type);
484 TRACE(winsock,"DNS: got protoent for [%s]\n", async_ctl.rq.name );
486 if( p_pe ) /* convert to the Winsock format with internal pointers as offsets */
487 size = WS_dup_pe(pwsi, p_pe, WS_DUP_OFFSET |
488 ((flag & WSMSG_WIN32_AOP) ? WS_DUP_LINEAR : WS_DUP_SEGPTR) );
489 if( size )
491 async_ctl.buffer = pwsi->buffer;
492 async_ctl.ilength = (unsigned)WSAMAKEASYNCREPLY( (UINT16)size, 0 );
493 _async_notify( flag );
495 else _async_fail();
498 void WS_do_async_getserv(LPWSINFO pwsi, unsigned flag )
500 int size = 0;
501 struct servent* p_se;
503 close(async_ctl.ws_aop->fd[0]);
504 p_se = (flag & WSMSG_ASYNC_SERVBYNAME)
505 ? getservbyname(async_ctl.rq.name, async_ctl.buffer)
506 : getservbyport(async_ctl.type, async_ctl.buffer);
508 if( p_se ) /* convert to the Winsock format with internal pointers as offsets */
509 size = WS_dup_se(pwsi, p_se, WS_DUP_OFFSET |
510 ((flag & WSMSG_WIN32_AOP) ? WS_DUP_LINEAR : WS_DUP_SEGPTR) );
511 if( size )
513 async_ctl.buffer = pwsi->buffer;
514 async_ctl.ilength = (unsigned)WSAMAKEASYNCREPLY( (UINT16)size, 0 );
515 _async_notify( flag );
517 else _async_fail();
520 /* ----------------------------------- helper functions -
522 * Raw results from the pipe contain internal pointers stored as
523 * offsets relative to the beginning of the buffer and we need
524 * to apply a fixup before passing them to applications.
526 * NOTE: It is possible to exploit the fact that fork() doesn't
527 * change the buffer address by storing fixed up pointers right
528 * in the handler. However, this will get in the way if we ever
529 * get around to implementing DNS helper daemon a-la Netscape 4.x.
532 void fixup_wshe(struct ws_hostent* p_wshe, void* base)
534 /* add 'base' to ws_hostent pointers to convert them from offsets */
536 int i;
537 unsigned* p_aliases,*p_addr;
539 p_aliases = (unsigned*)((char*)p_wshe + (unsigned)p_wshe->h_aliases);
540 p_addr = (unsigned*)((char*)p_wshe + (unsigned)p_wshe->h_addr_list);
541 ((unsigned)(p_wshe->h_name)) += (unsigned)base;
542 ((unsigned)(p_wshe->h_aliases)) += (unsigned)base;
543 ((unsigned)(p_wshe->h_addr_list)) += (unsigned)base;
544 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
545 for(i=0;p_addr[i];i++) p_addr[i] += (unsigned)base;
548 void fixup_wspe(struct ws_protoent* p_wspe, void* base)
550 int i;
551 unsigned* p_aliases = (unsigned*)((char*)p_wspe + (unsigned)p_wspe->p_aliases);
552 ((unsigned)(p_wspe->p_name)) += (unsigned)base;
553 ((unsigned)(p_wspe->p_aliases)) += (unsigned)base;
554 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;
557 void fixup_wsse(struct ws_servent* p_wsse, void* base)
559 int i;
560 unsigned* p_aliases = (unsigned*)((char*)p_wsse + (unsigned)p_wsse->s_aliases);
561 ((unsigned)(p_wsse->s_name)) += (unsigned)base;
562 ((p_wsse->s_proto)) += (unsigned)base;
563 ((p_wsse->s_aliases)) += (unsigned)base;
564 for(i=0;p_aliases[i];i++) p_aliases[i] += (unsigned)base;