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>
44 #include "wine/winuser16.h"
53 static void WINSOCK_async_handler(int unixfd
,void *private);
55 /* async DNS op control struct */
70 extern HANDLE16
__ws_gethandle( void* ptr
);
71 extern void* __ws_memalloc( int size
);
72 extern void __ws_memfree( void* ptr
);
74 /* NOTE: ws_async_op list is traversed inside the SIGIO handler! */
75 static ws_async_op
* __async_op_list
= NULL
;
77 static void fixup_wshe(struct ws_hostent
* p_wshe
, void* base
);
78 static void fixup_wspe(struct ws_protoent
* p_wspe
, void* base
);
79 static void fixup_wsse(struct ws_servent
* p_wsse
, void* base
);
81 /* ----------------------------------- async/non-blocking I/O */
83 int WINSOCK_unblock_io(int fd
, int noblock
)
87 fd_flags
= fcntl(fd
, F_GETFL
, 0);
88 if (fcntl(fd
, F_SETFL
, (noblock
)? fd_flags
| O_NONBLOCK
89 : fd_flags
& ~O_NONBLOCK
) != -1) return 0;
93 int WINSOCK_check_async_op(ws_async_op
* p_aop
)
95 ws_async_op
* p
= __async_op_list
;
96 while( p
) if( p
== p_aop
) return 1;
101 int WINSOCK_cancel_async_op(ws_async_op
* p_aop
)
105 if( WINSOCK_check_async_op(p_aop
) )
107 if( !(p_aop
->flags
& WSMSG_DEAD_AOP
) )
109 kill(p_aop
->pid
, SIGKILL
);
110 waitpid(p_aop
->pid
, NULL
, 0); /* just in case */
113 WINSOCK_unlink_async_op(p_aop
);
114 EVENT_DeleteIO( p_aop
->fd
[0], EVENT_IO_READ
);
116 p_aop
->hWnd
= p_aop
->uMsg
= 0;
122 void WINSOCK_cancel_task_aops(HTASK16 hTask
, void (*__opfree
)(void*))
124 /* SIGIO safe, hTask == 0 cancels all outstanding async ops */
126 int num
= 0, num_dead
= 0;
127 ws_async_op
* p
, *next
;
129 TRACE(winsock
," cancelling async DNS requests... \n");
131 SIGNAL_MaskAsyncEvents( TRUE
);
132 next
= __async_op_list
;
135 HTASK16 hWndTask
= GetWindowTask16(p
->hWnd
);
138 if(!hTask
|| !hWndTask
|| (hTask
== hWndTask
))
141 if( p
->flags
& WSMSG_DEAD_AOP
)
144 WINSOCK_cancel_async_op(p
);
145 if( __opfree
) __opfree(p
);
148 SIGNAL_MaskAsyncEvents( FALSE
);
149 TRACE(winsock
," -> %i total (%i active)\n", num
, num
- num_dead
);
152 void WINSOCK_link_async_op(ws_async_op
* p_aop
)
157 SIGNAL_MaskAsyncEvents( TRUE
);
158 if( __async_op_list
)
160 ws_async_op
* p
= __async_op_list
;
161 __async_op_list
->prev
= p_aop
;
163 /* traverse the list and retire dead ops created
164 * by the signal handler (see below). */
168 if( p
->flags
& WSMSG_DEAD_AOP
)
170 ws_async_op
* dead
= p
;
172 TRACE(winsock
,"\treaping dead aop [%08x]\n", (unsigned)p
);
175 WINSOCK_unlink_async_op( dead
);
176 __ws_memfree( dead
);
182 p_aop
->next
= __async_op_list
;
183 __async_op_list
= p_aop
;
185 SIGNAL_MaskAsyncEvents( FALSE
);
187 ASYNC_RegisterFD(p_aop
->fd
[0],WINSOCK_async_handler
,p_aop
);
190 void WINSOCK_unlink_async_op(ws_async_op
* p_aop
)
194 if( p_aop
== __async_op_list
) __async_op_list
= p_aop
->next
;
196 p_aop
->prev
->next
= p_aop
->next
;
197 if( p_aop
->next
) p_aop
->next
->prev
= p_aop
->prev
;
199 ASYNC_UnregisterFD(p_aop
->fd
[0],WINSOCK_async_handler
);
202 /* ----------------------------------- SIGIO handler -
204 * link_async_op/unlink_async_op allow to install generic
205 * async IO handlers (provided that aop_control function is defined).
207 * Note: pipe-based handlers must raise explicit SIGIO with kill(2).
210 static void WINSOCK_async_handler(int unixfd
,void *private)
212 ws_async_op
* p_aop
= (ws_async_op
*)private;
214 if( p_aop
->aop_control(p_aop
, AOP_IO
) == AOP_CONTROL_REMOVE
)
216 /* NOTE: memory management is signal-unsafe, therefore
217 * we can only set a flag to remove this p_aop later on.
219 p_aop
->flags
= WSMSG_DEAD_AOP
;
223 kill(p_aop
->pid
, SIGKILL
);
224 waitpid(p_aop
->pid
, NULL
, WNOHANG
);
230 /* ----------------------------------- getXbyY requests */
232 /* child process control struct */
233 static ws_async_ctl async_ctl
;
235 static int aop_control(ws_async_op
* p_aop
, int flag
)
239 /* success: LOWORD(lLength) has the length of the struct
241 * failure: LOWORD(lLength) is zero, HIWORD(lLength) contains
245 read(p_aop
->fd
[0], &lLength
, sizeof(unsigned));
246 if( LOWORD(lLength
) )
248 if( (int)LOWORD(lLength
) <= p_aop
->buflen
)
250 char* buffer
= (p_aop
->flags
& WSMSG_WIN32_AOP
)
251 ? p_aop
->b
.lin_base
: (char*)PTR_SEG_TO_LIN(p_aop
->b
.seg_base
);
253 read(p_aop
->fd
[0], buffer
, LOWORD(lLength
));
254 switch( p_aop
->flags
& WSMSG_ASYNC_RQMASK
)
256 case WSMSG_ASYNC_HOSTBYNAME
:
257 case WSMSG_ASYNC_HOSTBYADDR
:
258 fixup_wshe((struct ws_hostent
*)buffer
, p_aop
->b
.ptr_base
); break;
259 case WSMSG_ASYNC_PROTOBYNAME
:
260 case WSMSG_ASYNC_PROTOBYNUM
:
261 fixup_wspe((struct ws_protoent
*)buffer
, p_aop
->b
.ptr_base
); break;
262 case WSMSG_ASYNC_SERVBYNAME
:
263 case WSMSG_ASYNC_SERVBYPORT
:
264 fixup_wsse((struct ws_servent
*)buffer
, p_aop
->b
.ptr_base
); break;
266 if( p_aop
->flags
) WARN(winsock
,"Received unknown async request!\n");
267 return AOP_CONTROL_REMOVE
;
270 else lLength
= ((UINT
)LOWORD(lLength
)) | ((unsigned)WSAENOBUFS
<< 16);
273 /* was a __WS_ASYNC_DEBUG statement */
274 TRACE(winsock
, "DNS aop completed: hWnd [%04x], uMsg [%04x], "
275 "aop [%04x], event [%08lx]\n",
276 p_aop
->hWnd
, p_aop
->uMsg
, __ws_gethandle(p_aop
), (LPARAM
)lLength
);
278 /* FIXME: update num_async_rq */
279 EVENT_DeleteIO( p_aop
->fd
[0], EVENT_IO_READ
);
280 PostMessageA( p_aop
->hWnd
, p_aop
->uMsg
, __ws_gethandle(p_aop
), (LPARAM
)lLength
);
282 return AOP_CONTROL_REMOVE
; /* one-shot request */
286 HANDLE16
__WSAsyncDBQuery(LPWSINFO pwsi
, HWND hWnd
, UINT uMsg
, INT type
,
287 LPCSTR init
, INT len
, LPCSTR proto
, void* sbuf
, INT buflen
, UINT flag
)
289 /* queue 'flag' request and fork off its handler */
291 async_ctl
.ws_aop
= (ws_async_op
*)__ws_memalloc(sizeof(ws_async_op
));
293 if( async_ctl
.ws_aop
)
295 HANDLE16 handle
= __ws_gethandle(async_ctl
.ws_aop
);
297 if( pipe(async_ctl
.ws_aop
->fd
) == 0 )
299 async_ctl
.rq
.init
= (char*)init
;
300 async_ctl
.ilength
= len
;
301 async_ctl
.buffer
= (char*)proto
;
302 async_ctl
.type
= type
;
304 async_ctl
.ws_aop
->hWnd
= hWnd
;
305 async_ctl
.ws_aop
->uMsg
= uMsg
;
306 async_ctl
.ws_aop
->b
.ptr_base
= sbuf
;
307 async_ctl
.ws_aop
->buflen
= buflen
;
308 async_ctl
.ws_aop
->flags
= flag
;
309 async_ctl
.ws_aop
->aop_control
= &aop_control
;
311 WINSOCK_link_async_op( async_ctl
.ws_aop
);
313 EVENT_AddIO( async_ctl
.ws_aop
->fd
[0], EVENT_IO_READ
);
314 pwsi
->num_async_rq
++;
316 async_ctl
.ws_aop
->pid
= fork();
317 if( async_ctl
.ws_aop
->pid
)
319 TRACE(winsock
, "\tasync_op = %04x (child %i)\n",
320 handle
, async_ctl
.ws_aop
->pid
);
322 close(async_ctl
.ws_aop
->fd
[1]); /* write endpoint */
323 if( async_ctl
.ws_aop
->pid
> 0 )
324 return __ws_gethandle(async_ctl
.ws_aop
);
328 pwsi
->num_async_rq
--;
329 EVENT_DeleteIO( async_ctl
.ws_aop
->fd
[0], EVENT_IO_READ
);
330 close(async_ctl
.ws_aop
->fd
[0]);
331 pwsi
->err
= WSAEWOULDBLOCK
;
335 extern BOOL THREAD_InitDone
;
337 THREAD_InitDone
= FALSE
;
340 close(async_ctl
.ws_aop
->fd
[0]); /* read endpoint */
341 switch( flag
& WSMSG_ASYNC_RQMASK
)
343 case WSMSG_ASYNC_HOSTBYADDR
:
344 case WSMSG_ASYNC_HOSTBYNAME
:
345 WS_do_async_gethost(pwsi
, flag
);
347 case WSMSG_ASYNC_PROTOBYNUM
:
348 case WSMSG_ASYNC_PROTOBYNAME
:
349 WS_do_async_getproto(pwsi
, flag
);
351 case WSMSG_ASYNC_SERVBYPORT
:
352 case WSMSG_ASYNC_SERVBYNAME
:
353 WS_do_async_getserv(pwsi
, flag
);
356 _exit(0); /* skip atexit()'ed cleanup */
359 else pwsi
->err
= wsaErrno(); /* failed to create pipe */
361 __ws_memfree((void*)async_ctl
.ws_aop
);
362 } else pwsi
->err
= WSAEWOULDBLOCK
;
366 static int _async_notify()
368 /* use half-duplex pipe to send variable length packets
369 * to the parent process */
371 write(async_ctl
.ws_aop
->fd
[1], &async_ctl
.ilength
, sizeof(unsigned));
372 write(async_ctl
.ws_aop
->fd
[1], async_ctl
.buffer
, async_ctl
.ilength
);
375 kill(getppid(), SIGIO
); /* simulate async I/O */
378 /* was a __WS_ASYNC_DEBUG statement */
379 TRACE(winsock
, "handler - notify aop [%d, buf %d]\n",
380 async_ctl
.ilength
, async_ctl
.ws_aop
->buflen
);
384 static void _async_fail()
386 /* write a DWORD with error code (low word is zero) */
389 (h_errno
< 0) ? (unsigned)WSAMAKEASYNCREPLY( 0, wsaErrno() )
390 : (unsigned)WSAMAKEASYNCREPLY( 0, wsaHerrno() );
391 write(async_ctl
.ws_aop
->fd
[1], &async_ctl
.ilength
, sizeof(unsigned) );
393 kill(getppid(), SIGIO
); /* simulate async I/O */
396 /* was a __WS_ASYNC_DEBUG statement */
397 TRACE(winsock
, "handler - failed aop [%d, buf %d]\n",
398 async_ctl
.ilength
, async_ctl
.ws_aop
->buflen
);
401 void dump_ws_hostent_offset(struct ws_hostent
* wshe
)
404 char* base
= (char*)wshe
;
407 DUMP("h_name = %08x\t[%s]\n",
408 (unsigned)wshe
->h_name
, base
+ (unsigned)wshe
->h_name
);
409 DUMP("h_aliases = %08x\t[%08x]\n",
410 (unsigned)wshe
->h_aliases
, (unsigned)(base
+(unsigned)wshe
->h_aliases
));
411 ptr
= (unsigned*)(base
+ (unsigned)wshe
->h_aliases
);
412 for(i
= 0; ptr
[i
]; i
++ )
414 DUMP("%i - %08x [%s]\n", i
+ 1, ptr
[i
], ((char*)base
) + ptr
[i
]);
416 DUMP("h_length = %i\n", wshe
->h_length
);
419 void WS_do_async_gethost(LPWSINFO pwsi
, unsigned flag
)
422 struct hostent
* p_he
;
424 close(async_ctl
.ws_aop
->fd
[0]);
426 p_he
= (flag
& WSMSG_ASYNC_HOSTBYNAME
)
427 ? gethostbyname(async_ctl
.rq
.name
)
428 : gethostbyaddr(async_ctl
.rq
.name
,
429 async_ctl
.ilength
, async_ctl
.type
);
431 TRACE(winsock
,"DNS: got hostent for [%s]\n", async_ctl
.rq
.name
);
433 if( p_he
) /* convert to the Winsock format with internal pointers as offsets */
434 size
= WS_dup_he(pwsi
, p_he
, WS_DUP_OFFSET
|
435 ((flag
& WSMSG_WIN32_AOP
) ? WS_DUP_LINEAR
: WS_DUP_SEGPTR
) );
438 async_ctl
.buffer
= (char*)pwsi
->he
;
439 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
440 _async_notify( flag
);
445 void WS_do_async_getproto(LPWSINFO pwsi
, unsigned flag
)
448 struct protoent
* p_pe
;
450 close(async_ctl
.ws_aop
->fd
[0]);
451 p_pe
= (flag
& WSMSG_ASYNC_PROTOBYNAME
)
452 ? getprotobyname(async_ctl
.rq
.name
)
453 : getprotobynumber(async_ctl
.type
);
455 TRACE(winsock
,"DNS: got protoent for [%s]\n", async_ctl
.rq
.name
);
457 if( p_pe
) /* convert to the Winsock format with internal pointers as offsets */
458 size
= WS_dup_pe(pwsi
, p_pe
, WS_DUP_OFFSET
|
459 ((flag
& WSMSG_WIN32_AOP
) ? WS_DUP_LINEAR
: WS_DUP_SEGPTR
) );
462 async_ctl
.buffer
= (char*)pwsi
->pe
;
463 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
464 _async_notify( flag
);
469 void WS_do_async_getserv(LPWSINFO pwsi
, unsigned flag
)
472 struct servent
* p_se
;
474 close(async_ctl
.ws_aop
->fd
[0]);
475 p_se
= (flag
& WSMSG_ASYNC_SERVBYNAME
)
476 ? getservbyname(async_ctl
.rq
.name
, async_ctl
.buffer
)
477 : getservbyport(async_ctl
.type
, async_ctl
.buffer
);
479 if( p_se
) /* convert to the Winsock format with internal pointers as offsets */
480 size
= WS_dup_se(pwsi
, p_se
, WS_DUP_OFFSET
|
481 ((flag
& WSMSG_WIN32_AOP
) ? WS_DUP_LINEAR
: WS_DUP_SEGPTR
) );
484 async_ctl
.buffer
= (char*)pwsi
->se
;
485 async_ctl
.ilength
= (unsigned)WSAMAKEASYNCREPLY( (UINT16
)size
, 0 );
486 _async_notify( flag
);
491 /* ----------------------------------- helper functions -
493 * Raw results from the pipe contain internal pointers stored as
494 * offsets relative to the beginning of the buffer and we need
495 * to apply a fixup before passing them to applications.
497 * NOTE: It is possible to exploit the fact that fork() doesn't
498 * change the buffer address by storing fixed up pointers right
499 * in the handler. However, this will get in the way if we ever
500 * get around to implementing DNS helper daemon a-la Netscape 4.x.
503 void fixup_wshe(struct ws_hostent
* p_wshe
, void* base
)
505 /* add 'base' to ws_hostent pointers to convert them from offsets */
508 unsigned* p_aliases
,*p_addr
;
510 p_aliases
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_aliases
);
511 p_addr
= (unsigned*)((char*)p_wshe
+ (unsigned)p_wshe
->h_addr_list
);
512 ((unsigned)(p_wshe
->h_name
)) += (unsigned)base
;
513 ((unsigned)(p_wshe
->h_aliases
)) += (unsigned)base
;
514 ((unsigned)(p_wshe
->h_addr_list
)) += (unsigned)base
;
515 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
516 for(i
=0;p_addr
[i
];i
++) p_addr
[i
] += (unsigned)base
;
519 void fixup_wspe(struct ws_protoent
* p_wspe
, void* base
)
522 unsigned* p_aliases
= (unsigned*)((char*)p_wspe
+ (unsigned)p_wspe
->p_aliases
);
523 ((unsigned)(p_wspe
->p_name
)) += (unsigned)base
;
524 ((unsigned)(p_wspe
->p_aliases
)) += (unsigned)base
;
525 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;
528 void fixup_wsse(struct ws_servent
* p_wsse
, void* base
)
531 unsigned* p_aliases
= (unsigned*)((char*)p_wsse
+ (unsigned)p_wsse
->s_aliases
);
532 ((unsigned)(p_wsse
->s_name
)) += (unsigned)base
;
533 ((p_wsse
->s_proto
)) += (unsigned)base
;
534 ((p_wsse
->s_aliases
)) += (unsigned)base
;
535 for(i
=0;p_aliases
[i
];i
++) p_aliases
[i
] += (unsigned)base
;