3 * - filter which does resolving, not part of the library
7 * Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9 * It is part of adns, which is
10 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
11 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include <sys/types.h>
45 #ifdef ADNS_REGRESS_TEST
46 # include "hredirect.h"
50 struct outqueuenode
*next
, *back
;
54 struct timeval printbefore
;
55 struct treething
*addr
;
58 static int bracket
, forever
, address
;
59 static unsigned long timeout
= 1000;
60 static adns_rrtype rrt
= adns_r_ptr
;
61 static adns_initflags initflags
= 0;
62 static const char *config_text
;
64 static int outblocked
, inputeof
;
65 static struct { struct outqueuenode
*head
, *tail
; } outqueue
;
66 static int peroutqueuenode
, outqueuelen
;
68 static struct sockaddr_in sa
;
69 static adns_state ads
;
71 static char addrtextbuf
[14];
72 static int cbyte
, inbyte
, inbuf
;
73 static unsigned char bytes
[4];
74 static struct timeval printbefore
;
77 unsigned char bytes
[4];
82 static struct treething
*newthing
;
83 static void *treeroot
;
85 static int nonblock(int fd
, int isnonblock
) {
90 r
= fcntl(fd
,F_SETFL
, isnonblock ? r
|O_NONBLOCK
: r
&~O_NONBLOCK
);
95 static void quit(int exitstatus
) NONRETURNING
;
96 static void quit(int exitstatus
) {
102 static void sysfail(const char *what
) NONRETURNING
;
103 static void sysfail(const char *what
) {
104 fprintf(stderr
,"adnsresfilter: system call failed: %s: %s\n",what
,strerror(errno
));
108 static void *xmalloc(size_t sz
) {
110 r
= malloc(sz
); if (r
) return r
;
114 static void outputerr(void) NONRETURNING
;
115 static void outputerr(void) { sysfail("write to stdout"); }
117 static void usage(void) {
118 if (printf("usage: adnsresfilter [<options ...>]\n"
119 " adnsresfilter -h|--help\n"
120 "options: -t<milliseconds>|--timeout <milliseconds>\n"
121 " -w|--wait (always wait for queries to time out or fail)\n"
122 " -b|--brackets (require [...] around IP addresses)\n"
123 " -a|--address (always include [address] in output)\n"
124 " -u|--unchecked (do not forward map for checking)\n"
125 " --config <text> (use this instead of resolv.conf)\n"
126 " --debug (turn on adns resolver debugging)\n"
127 "Timeout is the maximum amount to delay any particular bit of output for.\n"
128 "Lookups will go on in the background. Default timeout = 1000 (ms).\n")
132 static void usageerr(const char *why
) NONRETURNING
;
133 static void usageerr(const char *why
) {
134 fprintf(stderr
,"adnsresfilter: bad usage: %s\n",why
);
139 static void adnsfail(const char *what
, int e
) NONRETURNING
;
140 static void adnsfail(const char *what
, int e
) {
141 fprintf(stderr
,"adnsresfilter: adns call failed: %s: %s\n",what
,strerror(e
));
145 static void settimeout(const char *arg
) {
147 timeout
= strtoul(arg
,&ep
,0);
148 if (*ep
) usageerr("invalid timeout");
151 static void parseargs(const char *const *argv
) {
155 while ((arg
= *++argv
)) {
156 if (arg
[0] != '-') usageerr("no non-option arguments are allowed");
158 if (!strcmp(arg
,"--timeout")) {
159 if (!(arg
= *++argv
)) usageerr("--timeout needs a value");
162 } else if (!strcmp(arg
,"--wait")) {
164 } else if (!strcmp(arg
,"--brackets")) {
166 } else if (!strcmp(arg
,"--address")) {
168 } else if (!strcmp(arg
,"--unchecked")) {
170 } else if (!strcmp(arg
,"--config")) {
171 if (!(arg
= *++argv
)) usageerr("--config needs a value");
173 } else if (!strcmp(arg
,"--debug")) {
174 initflags
|= adns_if_debug
;
175 } else if (!strcmp(arg
,"--help")) {
178 usageerr("unknown long option");
181 while ((c
= *++arg
)) {
184 if (*++arg
) settimeout(arg
);
185 else if ((arg
= *++argv
)) settimeout(arg
);
186 else usageerr("-t needs a value");
206 usageerr("unknown short option");
213 static void queueoutchar(int c
) {
214 struct outqueuenode
*entry
;
216 entry
= outqueue
.tail
;
217 if (!entry
|| entry
->addr
|| entry
->textlen
>= peroutqueuenode
) {
218 peroutqueuenode
= !peroutqueuenode
|| !entry
|| entry
->addr ?
128 :
219 peroutqueuenode
>= 1024 ?
4096 : peroutqueuenode
<<2;
220 entry
= xmalloc(sizeof(*entry
));
221 entry
->buffer
= xmalloc(peroutqueuenode
);
222 entry
->textp
= entry
->buffer
;
225 LIST_LINK_TAIL(outqueue
,entry
);
228 entry
->textp
[entry
->textlen
++]= c
;
231 static void queueoutstr(const char *str
, int len
) {
232 while (len
-- > 0) queueoutchar(*str
++);
235 static void writestdout(struct outqueuenode
*entry
) {
238 while (entry
->textlen
) {
239 r
= write(1, entry
->textp
, entry
->textlen
);
241 if (errno
== EINTR
) continue;
242 if (errno
== EAGAIN
) { outblocked
= 1; break; }
243 sysfail("write stdout");
245 assert(r
<= entry
->textlen
);
249 if (!entry
->textlen
) {
250 LIST_UNLINK(outqueue
,entry
);
257 static void replacetextwithname(struct outqueuenode
*entry
) {
261 name
= entry
->addr
->ans
->rrs
.str
[0];
262 namelen
= strlen(name
);
267 entry
->textlen
= namelen
;
269 newlen
= entry
->textlen
+ namelen
+ (bracket ?
0 : 2);
270 newbuf
= xmalloc(newlen
+ 1);
271 sprintf(newbuf
, bracket ?
"%s%.*s" : "%s[%.*s]", name
, entry
->textlen
, entry
->textp
);
273 entry
->buffer
= entry
->textp
= newbuf
;
274 entry
->textlen
= newlen
;
278 static void checkadnsqueries(void) {
282 struct treething
*foundthing
;
286 qu
= 0; context
= 0; ans
= 0;
287 r
= adns_check(ads
,&qu
,&ans
,&context
);
288 if (r
== ESRCH
|| r
== EAGAIN
) break;
291 foundthing
->ans
= ans
;
296 static void restartbuf(void) {
297 if (inbuf
>0) queueoutstr(addrtextbuf
,inbuf
);
301 static int comparer(const void *a
, const void *b
) {
302 return memcmp(a
,b
,4);
305 static void procaddr(void) {
306 struct treething
*foundthing
;
308 struct outqueuenode
*entry
;
312 newthing
= xmalloc(sizeof(struct treething
));
317 memcpy(newthing
->bytes
,bytes
,4);
318 searchfound
= tsearch(newthing
,&treeroot
,comparer
);
319 if (!searchfound
) sysfail("tsearch");
320 foundthing
= *searchfound
;
322 if (foundthing
== newthing
) {
324 memcpy(&sa
.sin_addr
,bytes
,4);
325 r
= adns_submit_reverse(ads
, (const struct sockaddr
*)&sa
,
326 rrt
,0,foundthing
,&foundthing
->qu
);
327 if (r
) adnsfail("submit",r
);
329 entry
= xmalloc(sizeof(*entry
));
330 entry
->buffer
= xmalloc(inbuf
);
331 entry
->textp
= entry
->buffer
;
332 memcpy(entry
->textp
,addrtextbuf
,inbuf
);
333 entry
->textlen
= inbuf
;
334 entry
->addr
= foundthing
;
335 entry
->printbefore
= printbefore
;
336 LIST_LINK_TAIL(outqueue
,entry
);
342 static void startaddr(void) {
347 static void readstdin(void) {
348 char readbuf
[512], *p
;
351 while ((r
= read(0,readbuf
,sizeof(readbuf
))) <= 0) {
352 if (r
== 0) { inputeof
= 1; return; }
353 if (r
== EAGAIN
) return;
354 if (r
!= EINTR
) sysfail("read stdin");
356 for (p
=readbuf
; r
>0; r
--,p
++) {
358 if (cbyte
==-1 && bracket
&& c
=='[') {
359 addrtextbuf
[inbuf
++]= c
;
361 } else if (cbyte
==-1 && !bracket
&& !isalnum(c
)) {
364 } else if (cbyte
>=0 && inbyte
<3 && c
>='0' && c
<='9' &&
365 (nbyte
= bytes
[cbyte
]*10 + (c
-'0')) <= 255) {
367 addrtextbuf
[inbuf
++]= c
;
369 } else if (cbyte
>=0 && cbyte
<3 && inbyte
>0 && c
=='.') {
371 addrtextbuf
[inbuf
++]= c
;
373 } else if (cbyte
==3 && inbyte
>0 && bracket
&& c
==']') {
374 addrtextbuf
[inbuf
++]= c
;
376 } else if (cbyte
==3 && inbyte
>0 && !bracket
&& !isalnum(c
)) {
384 if (!bracket
&& !isalnum(c
)) startaddr();
389 static void startup(void) {
392 if (nonblock(0,1)) sysfail("set stdin to nonblocking mode");
393 if (nonblock(1,1)) sysfail("set stdout to nonblocking mode");
394 memset(&sa
,0,sizeof(sa
));
395 sa
.sin_family
= AF_INET
;
397 r
= adns_init_strcfg(&ads
,initflags
,stderr
,config_text
);
399 r
= adns_init(&ads
,initflags
,0);
401 if (r
) adnsfail("init",r
);
405 if (!bracket
) startaddr();
408 int main(int argc
, const char *const *argv
) {
410 fd_set readfds
, writefds
, exceptfds
;
411 struct outqueuenode
*entry
;
412 struct timeval
*tv
, tvbuf
, now
;
417 while (!inputeof
|| outqueue
.head
) {
420 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
421 if ((entry
= outqueue
.head
) && !outblocked
) {
426 if (entry
->addr
->ans
) {
427 if (entry
->addr
->ans
->nrrs
)
428 replacetextwithname(entry
);
432 r
= gettimeofday(&now
,0); if (r
) sysfail("gettimeofday");
435 } else if (!timercmp(&now
,&entry
->printbefore
,<)) {
439 tvbuf
.tv_sec
= entry
->printbefore
.tv_sec
- now
.tv_sec
- 1;
440 tvbuf
.tv_usec
= entry
->printbefore
.tv_usec
- now
.tv_usec
+ 1000000;
441 tvbuf
.tv_sec
+= tvbuf
.tv_usec
/ 1000000;
442 tvbuf
.tv_usec
%= 1000000;
445 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,
448 if (outblocked
) FD_SET(1,&writefds
);
449 if (!inputeof
&& outqueuelen
<1024) FD_SET(0,&readfds
);
451 r
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tv
);
452 if (r
< 0) { if (r
== EINTR
) continue; else sysfail("select"); }
454 r
= gettimeofday(&now
,0); if (r
) sysfail("gettimeofday");
455 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,&now
);
458 if (FD_ISSET(0,&readfds
)) {
461 timevaladd(&printbefore
,timeout
);
464 } else if (FD_ISSET(1,&writefds
)) {
468 if (nonblock(0,0)) sysfail("un-nonblock stdin");
469 if (nonblock(1,0)) sysfail("un-nonblock stdout");
470 if (ferror(stdin
) || fclose(stdin
)) sysfail("read stdin");
471 if (fclose(stdout
)) sysfail("close stdout");