2 * asynchronous DNS services
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
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
22 #include <sys/so_ioctl.h>
23 #include <sys/param.h>
27 #include <sys/filio.h>
37 #include "selectors.h"
39 #include "sig_context.h"
43 #define FASYNC FIOASYNC
46 typedef struct /* async DNS op control struct */
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
)
84 fcntl(fd
, F_SETOWN
, getpid());
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;
93 int WINSOCK_unblock_io(int fd
, int noblock
)
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;
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;
111 int WINSOCK_cancel_async_op(ws_async_op
* p_aop
)
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 */
123 WINSOCK_unlink_async_op(p_aop
);
124 EVENT_DeleteIO( p_aop
->fd
[0], EVENT_IO_READ
);
126 p_aop
->hWnd
= p_aop
->uMsg
= 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
;
145 HTASK16 hWndTask
= GetWindowTask16(p
->hWnd
);
148 if(!hTask
|| !hWndTask
|| (hTask
== hWndTask
))
151 if( p
->flags
& WSMSG_DEAD_AOP
)
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
)
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). */
178 if( p
->flags
& WSMSG_DEAD_AOP
)
180 ws_async_op
* dead
= p
;
182 TRACE(winsock
,"\treaping dead aop [%08x]\n", (unsigned)p
);
185 WINSOCK_unlink_async_op( dead
);
186 __ws_memfree( dead
);
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
)
206 if( p_aop
== __async_op_list
) __async_op_list
= p_aop
->next
;
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
)
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
;
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
;
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];
254 kill(p_aop
->pid
, SIGKILL
);
255 waitpid(p_aop
->pid
, NULL
, WNOHANG
);
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
)
271 /* success: LOWORD(lLength) has the length of the struct
273 * failure: LOWORD(lLength) is zero, HIWORD(lLength) contains
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;
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);
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
);
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
;
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
);
376 case WSMSG_ASYNC_PROTOBYNUM
:
377 case WSMSG_ASYNC_PROTOBYNAME
:
378 WS_do_async_getproto(pwsi
, flag
);
380 case WSMSG_ASYNC_SERVBYPORT
:
381 case WSMSG_ASYNC_SERVBYNAME
:
382 WS_do_async_getserv(pwsi
, flag
);
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
;
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
);
404 kill(getppid(), SIGIO
); /* simulate async I/O */
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
);
413 static void _async_fail()
415 /* write a DWORD with error code (low word is zero) */
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) );
422 kill(getppid(), SIGIO
); /* simulate async I/O */
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
)
433 char* base
= (char*)wshe
;
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
)
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
) );
467 async_ctl
.buffer
= pwsi
->buffer
;
468 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
469 _async_notify( flag
);
474 void WS_do_async_getproto(LPWSINFO pwsi
, unsigned flag
)
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
) );
491 async_ctl
.buffer
= pwsi
->buffer
;
492 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
493 _async_notify( flag
);
498 void WS_do_async_getserv(LPWSINFO pwsi
, unsigned flag
)
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
) );
513 async_ctl
.buffer
= pwsi
->buffer
;
514 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
515 _async_notify( flag
);
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 */
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
)
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
)
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
;