3 * $Id: bres.c,v 1.1 1999/07/01 08:56:23 mdw Exp $
5 * Background reverse name resolution
7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1999/07/01 08:56:23 mdw
37 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
55 #include <mLib/report.h>
57 #include <mLib/selbuf.h>
61 /*----- Magic numbers -----------------------------------------------------*/
63 #define BRES_MAX 5 /* Maximum number of resolvers */
64 #define BRES_IDLE 60 /* Lifetime of an idle resolver */
66 /*----- Static variables --------------------------------------------------*/
68 static bres_server servers[BRES_MAX]; /* Statically allocated servers */
70 #define FREE ((bres_server *)&freelist)
71 static struct { bres_server *next, *prev; } freelist = { FREE, FREE };
73 #define QUEUE ((bres_client *)&queue)
74 static struct { bres_client *next, *prev; } queue = { QUEUE, QUEUE };
76 static sel_state *sel;
78 /*----- Main code ---------------------------------------------------------*/
82 * Arguments: @bres_server *rs@ = pointer to server block
86 * Use: Kills a server process, reaps the losing child and makes
87 * things generally clean again.
90 static void zap(bres_server *rs)
92 /* --- Close the pipes, kill the child, and reap it --- */
95 selbuf_disable(&rs->b);
97 close(rs->b.reader.fd);
98 kill(rs->kid, SIGTERM);
99 waitpid(rs->kid, 0, 0);
103 /* --- Move the server to the back of the list --- */
105 rs->next->prev = rs->prev;
106 rs->prev->next = rs->next;
108 rs->prev = FREE->prev;
109 FREE->prev->next = rs;
113 /* --- @bres_abort@ --- *
115 * Arguments: @bres_client *rc@ = pointer to client block
119 * Use: Removes a queued job.
122 void bres_abort(bres_client *rc)
128 rc->next->prev = rc->prev;
129 rc->prev->next = rc->next;
135 * Arguments: @int rfd@ = output file descriptor for resolved hostnames
136 * @int cfd@ = input file descriptor for raw addresses
140 * Use: Asynchronous name resolving process.
143 static void child(int rfd, int cfd)
146 FILE *fp = fdopen(rfd, "w");
150 int maxfd = sysconf(_SC_OPEN_MAX);
152 for (i = 0; i < maxfd; i++) {
153 if (i != rfd && i != cfd)
159 int r = read(cfd, &addr, sizeof(addr));
165 else if (r != sizeof(addr))
168 h = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
173 fprintf(fp, "%s\n", p);
181 * Arguments: @struct timeval *tv@ = pointer to the current time
182 * @void *vp@ = pointer to a server block
186 * Use: Kills off a child which has been idle for too long.
189 static void idle(struct timeval *tv, void *vp)
191 bres_server *rs = vp;
195 /* --- @answer@ --- *
197 * Arguments: @char *p@ = pointer to string read
198 * @void *vp@ = pointer to server block
202 * Use: Retrieves an answer from a name resolver process.
205 static void attach(bres_client */*rc*/);
207 static void answer(char *p, void *vp)
209 bres_server *rs = vp;
210 bres_client *rc = rs->rc;
212 /* --- Report the result to my client --- */
221 /* --- Wrap up the various structures --- */
225 rs->next = FREE->next;
227 FREE->next->prev = rs;
230 /* --- Tie a timer onto the server block --- */
235 gettimeofday(&tv, 0);
236 tv.tv_sec += BRES_IDLE;
237 sel_addtimer(sel, &rs->t, &tv, idle, rs);
240 /* --- If there are any clients waiting, attach one --- */
242 if (QUEUE->next != QUEUE) {
244 QUEUE->next = rc->next;
245 rc->next->prev = QUEUE;
252 * Arguments: @bres_server *rs@ = pointer to a server block
254 * Returns: Zero if OK, nonzero if something failed.
256 * Use: Starts up a child resolver process.
259 static int start(bres_server *rs)
264 /* --- Make the pipes --- */
271 /* --- Start up the child process --- */
273 if ((kid = fork()) < 0)
278 child(rfd[1], cfd[0]);
282 /* --- Fix up everything in the server block --- */
287 selbuf_init(&rs->b, sel, rfd[0], answer, rs);
291 /* --- Fix up after errors --- */
303 /* --- @attach@ --- *
305 * Arguments: @bres_client *rc@ = pointer to a client block
309 * Use: Attaches a client to a spare server (which is assumed to
313 static void attach(bres_client *rc)
318 /* --- Fix up the server ready for the job --- *
320 * If the server has a process, remove its timer. Otherwise, fork off a
321 * new resolver process. This is also where I go if I find that the child
322 * resolver process has lost while I wasn't looking. Only one attempt at
323 * forking is performed.
331 if (lose || start(rs))
336 /* --- Submit the job to the resolver --- */
339 struct sigaction sa, osa;
342 /* --- Ignore @SIGPIPE@ for now --- *
344 * This way I can trap @EPIPE@ and reap a losing child, if there was one.
347 sa.sa_handler = SIG_IGN;
349 sigemptyset(&sa.sa_mask);
350 sigaction(SIGPIPE, &sa, &osa);
352 /* --- Write the new job to the child --- */
355 if (write(rs->fd, &rc->addr, sizeof(rc->addr)) < 0)
357 sigaction(SIGPIPE, &osa, 0);
359 /* --- Sort out various errors --- */
368 /* --- Fiddle with lists so that everything's OK --- */
370 rs->next->prev = FREE;
371 FREE->next = rs->next;
372 rs->next = rs->prev = rs;
381 /* --- @bres_resolve@ --- *
383 * Arguments: @bres_client *rc@ = pointer to client block
384 * @struct in_addr addr@ = address to resolve
385 * @void (*func)(const char *host, void *p)@ = handler function
386 * @void *p@ = argument for handler function
390 * Use: Adds a resolver job to the queue. The job will be processed
391 * when there's a spare resolver process to deal with it.
394 void bres_resolve(bres_client *rc, struct in_addr addr,
395 void (*func)(const char */*host*/, void */*p*/), void *p)
397 /* --- Fill in the structure --- */
404 /* --- If there's a free server, plug it in --- */
406 if (FREE->next == FREE) {
408 rc->prev = QUEUE->prev;
409 QUEUE->prev->next = rc;
415 /* --- @bres_init@ --- *
417 * Arguments: @sel_state *s@ = pointer to select multiplexor
421 * Use: Initializes the background resolver for use.
424 void bres_init(sel_state *s)
429 for (i = 0; i < BRES_MAX; i++) {
430 servers[i].next = FREE;
431 servers[i].prev = FREE->prev;
434 FREE->prev->next = &servers[i];
435 FREE->prev = &servers[i];
439 /*----- That's all, folks -------------------------------------------------*/