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
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
25 # include <sys/so_ioctl.h>
27 #ifdef HAVE_SYS_PARAM_H
28 # include <sys/param.h>
30 #ifdef HAVE_SYS_FILIO_H
31 # include <sys/filio.h>
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
40 #ifdef HAVE_ARPA_NAMESER_H
41 # include <arpa/nameser.h>
47 #include "wine/winuser16.h"
56 static void WINSOCK_async_handler(int unixfd
,void *private);
58 /* async DNS op control struct */
73 extern HANDLE16
__ws_gethandle( void* ptr
);
74 extern void* __ws_memalloc( int size
);
75 extern void __ws_memfree( void* ptr
);
77 /* NOTE: ws_async_op list is traversed inside the SIGIO handler! */
78 static ws_async_op
* __async_op_list
= NULL
;
80 static void fixup_wshe(struct ws_hostent
* p_wshe
, void* base
);
81 static void fixup_wspe(struct ws_protoent
* p_wspe
, void* base
);
82 static void fixup_wsse(struct ws_servent
* p_wsse
, void* base
);
84 /* ----------------------------------- async/non-blocking I/O */
86 int WINSOCK_unblock_io(int fd
, int noblock
)
90 fd_flags
= fcntl(fd
, F_GETFL
, 0);
91 if (fcntl(fd
, F_SETFL
, (noblock
)? fd_flags
| O_NONBLOCK
92 : fd_flags
& ~O_NONBLOCK
) != -1) return 0;
96 int WINSOCK_check_async_op(ws_async_op
* p_aop
)
98 ws_async_op
* p
= __async_op_list
;
99 while( p
) if( p
== p_aop
) return 1;
104 int WINSOCK_cancel_async_op(ws_async_op
* p_aop
)
108 if( WINSOCK_check_async_op(p_aop
) )
110 if( !(p_aop
->flags
& WSMSG_DEAD_AOP
) )
112 kill(p_aop
->pid
, SIGKILL
);
113 waitpid(p_aop
->pid
, NULL
, 0); /* just in case */
116 WINSOCK_unlink_async_op(p_aop
);
117 EVENT_DeleteIO( p_aop
->fd
[0], EVENT_IO_READ
);
119 p_aop
->hWnd
= p_aop
->uMsg
= 0;
125 void WINSOCK_cancel_task_aops(HTASK16 hTask
, void (*__opfree
)(void*))
127 /* SIGIO safe, hTask == 0 cancels all outstanding async ops */
129 int num
= 0, num_dead
= 0;
130 ws_async_op
* p
, *next
;
132 TRACE(winsock
," cancelling async DNS requests... \n");
134 SIGNAL_MaskAsyncEvents( TRUE
);
135 next
= __async_op_list
;
138 HTASK16 hWndTask
= GetWindowTask16(p
->hWnd
);
141 if(!hTask
|| !hWndTask
|| (hTask
== hWndTask
))
144 if( p
->flags
& WSMSG_DEAD_AOP
)
147 WINSOCK_cancel_async_op(p
);
148 if( __opfree
) __opfree(p
);
151 SIGNAL_MaskAsyncEvents( FALSE
);
152 TRACE(winsock
," -> %i total (%i active)\n", num
, num
- num_dead
);
155 void WINSOCK_link_async_op(ws_async_op
* p_aop
)
160 SIGNAL_MaskAsyncEvents( TRUE
);
161 if( __async_op_list
)
163 ws_async_op
* p
= __async_op_list
;
164 __async_op_list
->prev
= p_aop
;
166 /* traverse the list and retire dead ops created
167 * by the signal handler (see below). */
171 if( p
->flags
& WSMSG_DEAD_AOP
)
173 ws_async_op
* dead
= p
;
175 TRACE(winsock
,"\treaping dead aop [%08x]\n", (unsigned)p
);
178 WINSOCK_unlink_async_op( dead
);
179 __ws_memfree( dead
);
185 p_aop
->next
= __async_op_list
;
186 __async_op_list
= p_aop
;
188 SIGNAL_MaskAsyncEvents( FALSE
);
190 ASYNC_RegisterFD(p_aop
->fd
[0],WINSOCK_async_handler
,p_aop
);
193 void WINSOCK_unlink_async_op(ws_async_op
* p_aop
)
197 if( p_aop
== __async_op_list
) __async_op_list
= p_aop
->next
;
199 p_aop
->prev
->next
= p_aop
->next
;
200 if( p_aop
->next
) p_aop
->next
->prev
= p_aop
->prev
;
202 ASYNC_UnregisterFD(p_aop
->fd
[0],WINSOCK_async_handler
);
205 /* ----------------------------------- SIGIO handler -
207 * link_async_op/unlink_async_op allow to install generic
208 * async IO handlers (provided that aop_control function is defined).
210 * Note: pipe-based handlers must raise explicit SIGIO with kill(2).
213 static void WINSOCK_async_handler(int unixfd
,void *private)
215 ws_async_op
* p_aop
= (ws_async_op
*)private;
217 if( p_aop
->aop_control(p_aop
, AOP_IO
) == AOP_CONTROL_REMOVE
)
219 /* NOTE: memory management is signal-unsafe, therefore
220 * we can only set a flag to remove this p_aop later on.
222 p_aop
->flags
= WSMSG_DEAD_AOP
;
226 kill(p_aop
->pid
, SIGKILL
);
227 waitpid(p_aop
->pid
, NULL
, WNOHANG
);
233 /* ----------------------------------- getXbyY requests */
235 /* child process control struct */
236 static ws_async_ctl async_ctl
;
238 static int aop_control(ws_async_op
* p_aop
, int flag
)
242 /* success: LOWORD(lLength) has the length of the struct
244 * failure: LOWORD(lLength) is zero, HIWORD(lLength) contains
248 read(p_aop
->fd
[0], &lLength
, sizeof(unsigned));
249 if( LOWORD(lLength
) )
251 if( (int)LOWORD(lLength
) <= p_aop
->buflen
)
253 char* buffer
= (p_aop
->flags
& WSMSG_WIN32_AOP
)
254 ? p_aop
->b
.lin_base
: (char*)PTR_SEG_TO_LIN(p_aop
->b
.seg_base
);
256 read(p_aop
->fd
[0], buffer
, LOWORD(lLength
));
257 switch( p_aop
->flags
& WSMSG_ASYNC_RQMASK
)
259 case WSMSG_ASYNC_HOSTBYNAME
:
260 case WSMSG_ASYNC_HOSTBYADDR
:
261 fixup_wshe((struct ws_hostent
*)buffer
, p_aop
->b
.ptr_base
); break;
262 case WSMSG_ASYNC_PROTOBYNAME
:
263 case WSMSG_ASYNC_PROTOBYNUM
:
264 fixup_wspe((struct ws_protoent
*)buffer
, p_aop
->b
.ptr_base
); break;
265 case WSMSG_ASYNC_SERVBYNAME
:
266 case WSMSG_ASYNC_SERVBYPORT
:
267 fixup_wsse((struct ws_servent
*)buffer
, p_aop
->b
.ptr_base
); break;
269 if( p_aop
->flags
) WARN(winsock
,"Received unknown async request!\n");
270 return AOP_CONTROL_REMOVE
;
273 else lLength
= ((UINT
)LOWORD(lLength
)) | ((unsigned)WSAENOBUFS
<< 16);
276 /* was a __WS_ASYNC_DEBUG statement */
277 TRACE(winsock
, "DNS aop completed: hWnd [%04x], uMsg [%04x], "
278 "aop [%04x], event [%08lx]\n",
279 p_aop
->hWnd
, p_aop
->uMsg
, __ws_gethandle(p_aop
), (LPARAM
)lLength
);
281 /* FIXME: update num_async_rq */
282 EVENT_DeleteIO( p_aop
->fd
[0], EVENT_IO_READ
);
283 PostMessageA( p_aop
->hWnd
, p_aop
->uMsg
, __ws_gethandle(p_aop
), (LPARAM
)lLength
);
285 return AOP_CONTROL_REMOVE
; /* one-shot request */
289 HANDLE16
__WSAsyncDBQuery(LPWSINFO pwsi
, HWND hWnd
, UINT uMsg
, INT type
,
290 LPCSTR init
, INT len
, LPCSTR proto
, void* sbuf
, INT buflen
, UINT flag
)
292 /* queue 'flag' request and fork off its handler */
294 async_ctl
.ws_aop
= (ws_async_op
*)__ws_memalloc(sizeof(ws_async_op
));
296 if( async_ctl
.ws_aop
)
298 HANDLE16 handle
= __ws_gethandle(async_ctl
.ws_aop
);
300 if( pipe(async_ctl
.ws_aop
->fd
) == 0 )
302 async_ctl
.rq
.init
= (char*)init
;
303 async_ctl
.ilength
= len
;
304 async_ctl
.buffer
= (char*)proto
;
305 async_ctl
.type
= type
;
307 async_ctl
.ws_aop
->hWnd
= hWnd
;
308 async_ctl
.ws_aop
->uMsg
= uMsg
;
309 async_ctl
.ws_aop
->b
.ptr_base
= sbuf
;
310 async_ctl
.ws_aop
->buflen
= buflen
;
311 async_ctl
.ws_aop
->flags
= flag
;
312 async_ctl
.ws_aop
->aop_control
= &aop_control
;
314 WINSOCK_link_async_op( async_ctl
.ws_aop
);
316 EVENT_AddIO( async_ctl
.ws_aop
->fd
[0], EVENT_IO_READ
);
317 pwsi
->num_async_rq
++;
319 async_ctl
.ws_aop
->pid
= fork();
320 if( async_ctl
.ws_aop
->pid
)
322 TRACE(winsock
, "\tasync_op = %04x (child %i)\n",
323 handle
, async_ctl
.ws_aop
->pid
);
325 close(async_ctl
.ws_aop
->fd
[1]); /* write endpoint */
326 if( async_ctl
.ws_aop
->pid
> 0 )
327 return __ws_gethandle(async_ctl
.ws_aop
);
331 pwsi
->num_async_rq
--;
332 EVENT_DeleteIO( async_ctl
.ws_aop
->fd
[0], EVENT_IO_READ
);
333 close(async_ctl
.ws_aop
->fd
[0]);
334 pwsi
->err
= WSAEWOULDBLOCK
;
338 extern BOOL THREAD_InitDone
;
340 THREAD_InitDone
= FALSE
;
343 close(async_ctl
.ws_aop
->fd
[0]); /* read endpoint */
344 switch( flag
& WSMSG_ASYNC_RQMASK
)
346 case WSMSG_ASYNC_HOSTBYADDR
:
347 case WSMSG_ASYNC_HOSTBYNAME
:
348 WS_do_async_gethost(pwsi
, flag
);
350 case WSMSG_ASYNC_PROTOBYNUM
:
351 case WSMSG_ASYNC_PROTOBYNAME
:
352 WS_do_async_getproto(pwsi
, flag
);
354 case WSMSG_ASYNC_SERVBYPORT
:
355 case WSMSG_ASYNC_SERVBYNAME
:
356 WS_do_async_getserv(pwsi
, flag
);
359 _exit(0); /* skip atexit()'ed cleanup */
362 else pwsi
->err
= wsaErrno(); /* failed to create pipe */
364 __ws_memfree((void*)async_ctl
.ws_aop
);
365 } else pwsi
->err
= WSAEWOULDBLOCK
;
369 static int _async_notify()
371 /* use half-duplex pipe to send variable length packets
372 * to the parent process */
374 write(async_ctl
.ws_aop
->fd
[1], &async_ctl
.ilength
, sizeof(unsigned));
375 write(async_ctl
.ws_aop
->fd
[1], async_ctl
.buffer
, async_ctl
.ilength
);
378 kill(getppid(), SIGIO
); /* simulate async I/O */
381 /* was a __WS_ASYNC_DEBUG statement */
382 TRACE(winsock
, "handler - notify aop [%d, buf %d]\n",
383 async_ctl
.ilength
, async_ctl
.ws_aop
->buflen
);
387 static void _async_fail()
389 /* write a DWORD with error code (low word is zero) */
392 (h_errno
< 0) ? (unsigned)WSAMAKEASYNCREPLY( 0, wsaErrno() )
393 : (unsigned)WSAMAKEASYNCREPLY( 0, wsaHerrno() );
394 write(async_ctl
.ws_aop
->fd
[1], &async_ctl
.ilength
, sizeof(unsigned) );
396 kill(getppid(), SIGIO
); /* simulate async I/O */
399 /* was a __WS_ASYNC_DEBUG statement */
400 TRACE(winsock
, "handler - failed aop [%d, buf %d]\n",
401 async_ctl
.ilength
, async_ctl
.ws_aop
->buflen
);
404 void dump_ws_hostent_offset(struct ws_hostent
* wshe
)
407 char* base
= (char*)wshe
;
410 DUMP("h_name = %08x\t[%s]\n",
411 (unsigned)wshe
->h_name
, base
+ (unsigned)wshe
->h_name
);
412 DUMP("h_aliases = %08x\t[%08x]\n",
413 (unsigned)wshe
->h_aliases
, (unsigned)(base
+(unsigned)wshe
->h_aliases
));
414 ptr
= (unsigned*)(base
+ (unsigned)wshe
->h_aliases
);
415 for(i
= 0; ptr
[i
]; i
++ )
417 DUMP("%i - %08x [%s]\n", i
+ 1, ptr
[i
], ((char*)base
) + ptr
[i
]);
419 DUMP("h_length = %i\n", wshe
->h_length
);
422 void WS_do_async_gethost(LPWSINFO pwsi
, unsigned flag
)
425 struct hostent
* p_he
;
427 close(async_ctl
.ws_aop
->fd
[0]);
429 p_he
= (flag
& WSMSG_ASYNC_HOSTBYNAME
)
430 ? gethostbyname(async_ctl
.rq
.name
)
431 : gethostbyaddr(async_ctl
.rq
.name
,
432 async_ctl
.ilength
, async_ctl
.type
);
434 TRACE(winsock
,"DNS: got hostent for [%s]\n", async_ctl
.rq
.name
);
436 if( p_he
) /* convert to the Winsock format with internal pointers as offsets */
437 size
= WS_dup_he(pwsi
, p_he
, WS_DUP_OFFSET
|
438 ((flag
& WSMSG_WIN32_AOP
) ? WS_DUP_LINEAR
: WS_DUP_SEGPTR
) );
441 async_ctl
.buffer
= (char*)pwsi
->he
;
442 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
443 _async_notify( flag
);
448 void WS_do_async_getproto(LPWSINFO pwsi
, unsigned flag
)
451 struct protoent
* p_pe
;
453 close(async_ctl
.ws_aop
->fd
[0]);
454 p_pe
= (flag
& WSMSG_ASYNC_PROTOBYNAME
)
455 ? getprotobyname(async_ctl
.rq
.name
)
456 : getprotobynumber(async_ctl
.type
);
458 TRACE(winsock
,"DNS: got protoent for [%s]\n", async_ctl
.rq
.name
);
460 if( p_pe
) /* convert to the Winsock format with internal pointers as offsets */
461 size
= WS_dup_pe(pwsi
, p_pe
, WS_DUP_OFFSET
|
462 ((flag
& WSMSG_WIN32_AOP
) ? WS_DUP_LINEAR
: WS_DUP_SEGPTR
) );
465 async_ctl
.buffer
= (char*)pwsi
->pe
;
466 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
467 _async_notify( flag
);
472 void WS_do_async_getserv(LPWSINFO pwsi
, unsigned flag
)
475 struct servent
* p_se
;
477 close(async_ctl
.ws_aop
->fd
[0]);
478 p_se
= (flag
& WSMSG_ASYNC_SERVBYNAME
)
479 ? getservbyname(async_ctl
.rq
.name
, async_ctl
.buffer
)
480 : getservbyport(async_ctl
.type
, async_ctl
.buffer
);
482 if( p_se
) /* convert to the Winsock format with internal pointers as offsets */
483 size
= WS_dup_se(pwsi
, p_se
, WS_DUP_OFFSET
|
484 ((flag
& WSMSG_WIN32_AOP
) ? WS_DUP_LINEAR
: WS_DUP_SEGPTR
) );
487 async_ctl
.buffer
= (char*)pwsi
->se
;
488 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
489 _async_notify( flag
);
494 /* ----------------------------------- helper functions -
496 * Raw results from the pipe contain internal pointers stored as
497 * offsets relative to the beginning of the buffer and we need
498 * to apply a fixup before passing them to applications.
500 * NOTE: It is possible to exploit the fact that fork() doesn't
501 * change the buffer address by storing fixed up pointers right
502 * in the handler. However, this will get in the way if we ever
503 * get around to implementing DNS helper daemon a-la Netscape 4.x.
506 void fixup_wshe(struct ws_hostent
* p_wshe
, void* base
)
508 /* add 'base' to ws_hostent pointers to convert them from offsets */
511 unsigned* p_aliases
,*p_addr
;
513 p_aliases
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_aliases
);
514 p_addr
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_addr_list
);
515 ((unsigned)(p_wshe
->h_name
)) += (unsigned)base
;
516 ((unsigned)(p_wshe
->h_aliases
)) += (unsigned)base
;
517 ((unsigned)(p_wshe
->h_addr_list
)) += (unsigned)base
;
518 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
519 for(i
=0;p_addr
[i
];i
++) p_addr
[i
] += (unsigned)base
;
522 void fixup_wspe(struct ws_protoent
* p_wspe
, void* base
)
525 unsigned* p_aliases
= (unsigned*)((char*)p_wspe
+ (unsigned)p_wspe
->p_aliases
);
526 ((unsigned)(p_wspe
->p_name
)) += (unsigned)base
;
527 ((unsigned)(p_wspe
->p_aliases
)) += (unsigned)base
;
528 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
531 void fixup_wsse(struct ws_servent
* p_wsse
, void* base
)
534 unsigned* p_aliases
= (unsigned*)((char*)p_wsse
+ (unsigned)p_wsse
->s_aliases
);
535 ((unsigned)(p_wsse
->s_name
)) += (unsigned)base
;
536 ((p_wsse
->s_proto
)) += (unsigned)base
;
537 ((p_wsse
->s_aliases
)) += (unsigned)base
;
538 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;