3 * $Id: bres.c,v 1.3 1999/07/26 23:27:22 mdw Exp $
5 * Background reverse name resolution
7 * (c) 1999 Straylight/Edgeware
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.3 1999/07/26 23:27:22 mdw
33 * Change copyright notice.
35 * Revision 1.2 1999/07/03 13:56:04 mdw
36 * Perform a forward resolution to verify result of reverse lookup.
38 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
43 /*----- Header files ------------------------------------------------------*/
51 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
61 #include <mLib/alloc.h>
62 #include <mLib/report.h>
64 #include <mLib/selbuf.h>
68 /*----- Magic numbers -----------------------------------------------------*/
70 #define BRES_MAX 5 /* Maximum number of resolvers */
71 #define BRES_IDLE 60 /* Lifetime of an idle resolver */
73 /*----- Static variables --------------------------------------------------*/
75 static bres_server servers
[BRES_MAX
]; /* Statically allocated servers */
77 #define FREE ((bres_server *)&freelist)
78 static struct { bres_server
*next
, *prev
; } freelist
= { FREE
, FREE
};
80 #define QUEUE ((bres_client *)&queue)
81 static struct { bres_client
*next
, *prev
; } queue
= { QUEUE
, QUEUE
};
83 static sel_state
*sel
;
85 /*----- Main code ---------------------------------------------------------*/
89 * Arguments: @bres_server *rs@ = pointer to server block
93 * Use: Kills a server process, reaps the losing child and makes
94 * things generally clean again.
97 static void zap(bres_server
*rs
)
99 /* --- Close the pipes, kill the child, and reap it --- */
102 selbuf_disable(&rs
->b
);
104 close(rs
->b
.reader
.fd
);
105 kill(rs
->kid
, SIGTERM
);
106 waitpid(rs
->kid
, 0, 0);
110 /* --- Move the server to the back of the list --- */
112 rs
->next
->prev
= rs
->prev
;
113 rs
->prev
->next
= rs
->next
;
115 rs
->prev
= FREE
->prev
;
116 FREE
->prev
->next
= rs
;
120 /* --- @bres_abort@ --- *
122 * Arguments: @bres_client *rc@ = pointer to client block
126 * Use: Removes a queued job.
129 void bres_abort(bres_client
*rc
)
135 rc
->next
->prev
= rc
->prev
;
136 rc
->prev
->next
= rc
->next
;
142 * Arguments: @int rfd@ = output file descriptor for resolved hostnames
143 * @int cfd@ = input file descriptor for raw addresses
147 * Use: Asynchronous name resolving process.
150 static void child(int rfd
, int cfd
)
153 FILE *fp
= fdopen(rfd
, "w");
157 int maxfd
= sysconf(_SC_OPEN_MAX
);
159 for (i
= 0; i
< maxfd
; i
++) {
160 if (i
!= rfd
&& i
!= cfd
)
166 int r
= read(cfd
, &addr
, sizeof(addr
));
172 else if (r
!= sizeof(addr
))
175 h
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET
);
179 p
= xstrdup(h
->h_name
);
180 h
= gethostbyname(p
);
184 for (pp
= h
->h_addr_list
; *pp
; pp
++) {
186 memcpy(&a
, *pp
, sizeof(a
));
187 if (a
.s_addr
== addr
.s_addr
) {
197 fprintf(fp
, "%s\n", p
);
205 * Arguments: @struct timeval *tv@ = pointer to the current time
206 * @void *vp@ = pointer to a server block
210 * Use: Kills off a child which has been idle for too long.
213 static void idle(struct timeval
*tv
, void *vp
)
215 bres_server
*rs
= vp
;
219 /* --- @answer@ --- *
221 * Arguments: @char *p@ = pointer to string read
222 * @void *vp@ = pointer to server block
226 * Use: Retrieves an answer from a name resolver process.
229 static void attach(bres_client */
*rc*/
);
231 static void answer(char *p
, void *vp
)
233 bres_server
*rs
= vp
;
234 bres_client
*rc
= rs
->rc
;
236 /* --- Report the result to my client --- */
245 /* --- Wrap up the various structures --- */
249 rs
->next
= FREE
->next
;
251 FREE
->next
->prev
= rs
;
254 /* --- Tie a timer onto the server block --- */
259 gettimeofday(&tv
, 0);
260 tv
.tv_sec
+= BRES_IDLE
;
261 sel_addtimer(sel
, &rs
->t
, &tv
, idle
, rs
);
264 /* --- If there are any clients waiting, attach one --- */
266 if (QUEUE
->next
!= QUEUE
) {
268 QUEUE
->next
= rc
->next
;
269 rc
->next
->prev
= QUEUE
;
276 * Arguments: @bres_server *rs@ = pointer to a server block
278 * Returns: Zero if OK, nonzero if something failed.
280 * Use: Starts up a child resolver process.
283 static int start(bres_server
*rs
)
288 /* --- Make the pipes --- */
295 /* --- Start up the child process --- */
297 if ((kid
= fork()) < 0)
302 child(rfd
[1], cfd
[0]);
306 /* --- Fix up everything in the server block --- */
311 selbuf_init(&rs
->b
, sel
, rfd
[0], answer
, rs
);
315 /* --- Fix up after errors --- */
327 /* --- @attach@ --- *
329 * Arguments: @bres_client *rc@ = pointer to a client block
333 * Use: Attaches a client to a spare server (which is assumed to
337 static void attach(bres_client
*rc
)
342 /* --- Fix up the server ready for the job --- *
344 * If the server has a process, remove its timer. Otherwise, fork off a
345 * new resolver process. This is also where I go if I find that the child
346 * resolver process has lost while I wasn't looking. Only one attempt at
347 * forking is performed.
355 if (lose
|| start(rs
))
360 /* --- Submit the job to the resolver --- */
363 struct sigaction sa
, osa
;
366 /* --- Ignore @SIGPIPE@ for now --- *
368 * This way I can trap @EPIPE@ and reap a losing child, if there was one.
371 sa
.sa_handler
= SIG_IGN
;
373 sigemptyset(&sa
.sa_mask
);
374 sigaction(SIGPIPE
, &sa
, &osa
);
376 /* --- Write the new job to the child --- */
379 if (write(rs
->fd
, &rc
->addr
, sizeof(rc
->addr
)) < 0)
381 sigaction(SIGPIPE
, &osa
, 0);
383 /* --- Sort out various errors --- */
392 /* --- Fiddle with lists so that everything's OK --- */
394 rs
->next
->prev
= FREE
;
395 FREE
->next
= rs
->next
;
396 rs
->next
= rs
->prev
= rs
;
405 /* --- @bres_resolve@ --- *
407 * Arguments: @bres_client *rc@ = pointer to client block
408 * @struct in_addr addr@ = address to resolve
409 * @void (*func)(const char *host, void *p)@ = handler function
410 * @void *p@ = argument for handler function
414 * Use: Adds a resolver job to the queue. The job will be processed
415 * when there's a spare resolver process to deal with it.
418 void bres_resolve(bres_client
*rc
, struct in_addr addr
,
419 void (*func
)(const char */
*host*/
, void */
*p*/
), void *p
)
421 /* --- Fill in the structure --- */
428 /* --- If there's a free server, plug it in --- */
430 if (FREE
->next
== FREE
) {
432 rc
->prev
= QUEUE
->prev
;
433 QUEUE
->prev
->next
= rc
;
439 /* --- @bres_init@ --- *
441 * Arguments: @sel_state *s@ = pointer to select multiplexor
445 * Use: Initializes the background resolver for use.
448 void bres_init(sel_state
*s
)
453 for (i
= 0; i
< BRES_MAX
; i
++) {
454 servers
[i
].next
= FREE
;
455 servers
[i
].prev
= FREE
->prev
;
458 FREE
->prev
->next
= &servers
[i
];
459 FREE
->prev
= &servers
[i
];
463 /*----- That's all, folks -------------------------------------------------*/