3 * - filter which does resolving, not part of the library
6 * This file is part of adns, which is
7 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
8 * Copyright (C) 1999-2000,2003,2006 Tony Finch
9 * Copyright (C) 1991 Massachusetts Institute of Technology
10 * (See the file INSTALL for full details.)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <sys/types.h>
45 #ifdef ADNS_REGRESS_TEST
46 # include "hredirect.h"
50 struct outqueuenode
*next
, *back
;
53 struct timeval printbefore
;
54 struct treething
*addr
;
57 static int bracket
, forever
, address
;
58 static unsigned long timeout
= 1000;
59 static adns_rrtype rrt
= adns_r_ptr
;
60 static adns_initflags initflags
= 0;
61 static const char *config_text
;
63 static int outblocked
, inputeof
;
64 static struct { struct outqueuenode
*head
, *tail
; } outqueue
;
65 static int peroutqueuenode
, outqueuelen
;
67 static struct sockaddr_in sa
;
68 static adns_state ads
;
70 static char addrtextbuf
[14];
71 static int cbyte
, inbyte
, inbuf
;
72 static unsigned char bytes
[4];
73 static struct timeval printbefore
;
76 unsigned char bytes
[4];
81 static struct treething
*newthing
;
82 static void *treeroot
;
84 static int nonblock(int fd
, int isnonblock
) {
89 r
= fcntl(fd
,F_SETFL
, isnonblock ? r
|O_NONBLOCK
: r
&~O_NONBLOCK
);
94 void quitnow(int exitstatus
) {
100 static void sysfail(const char *what
) NONRETURNING
;
101 static void sysfail(const char *what
) {
102 fprintf(stderr
,"adnsresfilter: system call failed: %s: %s\n",what
,strerror(errno
));
106 static void *xmalloc(size_t sz
) {
108 r
= malloc(sz
); if (r
) return r
;
112 static void outputerr(void) NONRETURNING
;
113 static void outputerr(void) { sysfail("write to stdout"); }
115 static void usage(void) {
116 if (printf("usage: adnsresfilter [<options ...>]\n"
117 " adnsresfilter -h|--help | --version\n"
118 "options: -t<milliseconds>|--timeout <milliseconds>\n"
119 " -w|--wait (always wait for queries to time out or fail)\n"
120 " -b|--brackets (require [...] around IP addresses)\n"
121 " -a|--address (always include [address] in output)\n"
122 " -u|--unchecked (do not forward map for checking)\n"
123 " --config <text> (use this instead of resolv.conf)\n"
124 " --debug (turn on adns resolver debugging)\n"
125 "Timeout is the maximum amount to delay any particular bit of output for.\n"
126 "Lookups will go on in the background. Default timeout = 1000 (ms).\n")
128 if (fflush(stdout
)) sysfail("flush stdout");
131 static void usageerr(const char *why
) NONRETURNING
;
132 static void usageerr(const char *why
) {
133 fprintf(stderr
,"adnsresfilter: bad usage: %s\n",why
);
138 static void adnsfail(const char *what
, int e
) NONRETURNING
;
139 static void adnsfail(const char *what
, int e
) {
140 fprintf(stderr
,"adnsresfilter: adns call failed: %s: %s\n",what
,strerror(e
));
144 static void settimeout(const char *arg
) {
146 timeout
= strtoul(arg
,&ep
,0);
147 if (*ep
) usageerr("invalid timeout");
150 static void parseargs(const char *const *argv
) {
154 while ((arg
= *++argv
)) {
155 if (arg
[0] != '-') usageerr("no non-option arguments are allowed");
157 if (!strcmp(arg
,"--timeout")) {
158 if (!(arg
= *++argv
)) usageerr("--timeout needs a value");
161 } else if (!strcmp(arg
,"--wait")) {
163 } else if (!strcmp(arg
,"--brackets")) {
165 } else if (!strcmp(arg
,"--address")) {
167 } else if (!strcmp(arg
,"--unchecked")) {
169 } else if (!strcmp(arg
,"--config")) {
170 if (!(arg
= *++argv
)) usageerr("--config needs a value");
172 } else if (!strcmp(arg
,"--debug")) {
173 initflags
|= adns_if_debug
;
174 } else if (!strcmp(arg
,"--help")) {
176 } else if (!strcmp(arg
,"--version")) {
177 VERSION_PRINT_QUIT("adnsresfilter"); quitnow(0);
179 usageerr("unknown long option");
182 while ((c
= *++arg
)) {
185 if (*++arg
) settimeout(arg
);
186 else if ((arg
= *++argv
)) settimeout(arg
);
187 else usageerr("-t needs a value");
207 usageerr("unknown short option");
214 static void queueoutchar(int c
) {
215 struct outqueuenode
*entry
;
217 entry
= outqueue
.tail
;
218 if (!entry
|| entry
->addr
||
219 entry
->textlen
>= peroutqueuenode
- (entry
->textp
- entry
->buffer
)) {
220 peroutqueuenode
= !peroutqueuenode
|| !entry
|| entry
->addr ?
128 :
221 peroutqueuenode
>= 1024 ?
4096 : peroutqueuenode
<<2;
222 entry
= xmalloc(sizeof(*entry
));
223 entry
->buffer
= xmalloc(peroutqueuenode
);
224 entry
->textp
= entry
->buffer
;
227 LIST_LINK_TAIL(outqueue
,entry
);
230 entry
->textp
[entry
->textlen
++]= c
;
233 static void queueoutstr(const char *str
, int len
) {
234 while (len
-- > 0) queueoutchar(*str
++);
237 static void writestdout(struct outqueuenode
*entry
) {
240 while (entry
->textlen
) {
241 r
= write(1, entry
->textp
, entry
->textlen
);
243 if (errno
== EINTR
) continue;
244 if (errno
== EAGAIN
) { outblocked
= 1; break; }
245 sysfail("write stdout");
247 assert(r
<= entry
->textlen
);
251 if (!entry
->textlen
) {
252 LIST_UNLINK(outqueue
,entry
);
259 static void replacetextwithname(struct outqueuenode
*entry
) {
263 name
= entry
->addr
->ans
->rrs
.str
[0];
264 namelen
= strlen(name
);
269 entry
->textlen
= namelen
;
271 newlen
= entry
->textlen
+ namelen
+ (bracket ?
0 : 2);
272 newbuf
= xmalloc(newlen
+ 1);
273 sprintf(newbuf
, bracket ?
"%s%.*s" : "%s[%.*s]", name
, entry
->textlen
, entry
->textp
);
275 entry
->buffer
= entry
->textp
= newbuf
;
276 entry
->textlen
= newlen
;
280 static void checkadnsqueries(void) {
284 struct treething
*foundthing
;
288 qu
= 0; context
= 0; ans
= 0;
289 r
= adns_check(ads
,&qu
,&ans
,&context
);
290 if (r
== ESRCH
|| r
== EAGAIN
) break;
293 foundthing
->ans
= ans
;
298 static void restartbuf(void) {
299 if (inbuf
>0) queueoutstr(addrtextbuf
,inbuf
);
303 static int comparer(const void *a
, const void *b
) {
304 return memcmp(a
,b
,4);
307 static void procaddr(void) {
308 struct treething
*foundthing
;
310 struct outqueuenode
*entry
;
314 newthing
= xmalloc(sizeof(struct treething
));
319 memcpy(newthing
->bytes
,bytes
,4);
320 searchfound
= tsearch(newthing
,&treeroot
,comparer
);
321 if (!searchfound
) sysfail("tsearch");
322 foundthing
= *searchfound
;
324 if (foundthing
== newthing
) {
326 memcpy(&sa
.sin_addr
,bytes
,4);
327 r
= adns_submit_reverse(ads
, (const struct sockaddr
*)&sa
,
328 rrt
,0,foundthing
,&foundthing
->qu
);
329 if (r
) adnsfail("submit",r
);
331 entry
= xmalloc(sizeof(*entry
));
332 entry
->buffer
= xmalloc(inbuf
);
333 entry
->textp
= entry
->buffer
;
334 memcpy(entry
->textp
,addrtextbuf
,inbuf
);
335 entry
->textlen
= inbuf
;
336 entry
->addr
= foundthing
;
337 entry
->printbefore
= printbefore
;
338 LIST_LINK_TAIL(outqueue
,entry
);
344 static void startaddr(void) {
349 static void readstdin(void) {
350 char readbuf
[512], *p
;
353 while ((r
= read(0,readbuf
,sizeof(readbuf
))) <= 0) {
354 if (r
== 0) { inputeof
= 1; return; }
355 if (r
== EAGAIN
) return;
356 if (r
!= EINTR
) sysfail("read stdin");
358 for (p
=readbuf
; r
>0; r
--,p
++) {
360 if (cbyte
==-1 && bracket
&& c
=='[') {
361 addrtextbuf
[inbuf
++]= c
;
363 } else if (cbyte
==-1 && !bracket
&& !isalnum(c
)) {
366 } else if (cbyte
>=0 && inbyte
<3 && c
>='0' && c
<='9' &&
367 (nbyte
= bytes
[cbyte
]*10 + (c
-'0')) <= 255) {
369 addrtextbuf
[inbuf
++]= c
;
371 } else if (cbyte
>=0 && cbyte
<3 && inbyte
>0 && c
=='.') {
373 addrtextbuf
[inbuf
++]= c
;
375 } else if (cbyte
==3 && inbyte
>0 && bracket
&& c
==']') {
376 addrtextbuf
[inbuf
++]= c
;
378 } else if (cbyte
==3 && inbyte
>0 && !bracket
&& !isalnum(c
)) {
386 if (!bracket
&& !isalnum(c
)) startaddr();
391 static void startup(void) {
394 if (nonblock(0,1)) sysfail("set stdin to nonblocking mode");
395 if (nonblock(1,1)) sysfail("set stdout to nonblocking mode");
396 memset(&sa
,0,sizeof(sa
));
397 sa
.sin_family
= AF_INET
;
399 r
= adns_init_strcfg(&ads
,initflags
,stderr
,config_text
);
401 r
= adns_init(&ads
,initflags
,0);
403 if (r
) adnsfail("init",r
);
407 if (!bracket
) startaddr();
410 int main(int argc
, const char *const *argv
) {
412 fd_set readfds
, writefds
, exceptfds
;
413 struct outqueuenode
*entry
;
414 struct timeval
*tv
, tvbuf
, now
;
419 while (!inputeof
|| outqueue
.head
) {
422 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
423 if ((entry
= outqueue
.head
) && !outblocked
) {
428 if (entry
->addr
->ans
) {
429 if (entry
->addr
->ans
->nrrs
)
430 replacetextwithname(entry
);
434 r
= gettimeofday(&now
,0); if (r
) sysfail("gettimeofday");
437 } else if (!timercmp(&now
,&entry
->printbefore
,<)) {
441 tvbuf
.tv_sec
= entry
->printbefore
.tv_sec
- now
.tv_sec
- 1;
442 tvbuf
.tv_usec
= entry
->printbefore
.tv_usec
- now
.tv_usec
+ 1000000;
443 tvbuf
.tv_sec
+= tvbuf
.tv_usec
/ 1000000;
444 tvbuf
.tv_usec
%= 1000000;
447 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,
450 if (outblocked
) FD_SET(1,&writefds
);
451 if (!inputeof
&& outqueuelen
<1024) FD_SET(0,&readfds
);
453 r
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tv
);
454 if (r
< 0) { if (r
== EINTR
) continue; else sysfail("select"); }
456 r
= gettimeofday(&now
,0); if (r
) sysfail("gettimeofday");
457 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,&now
);
460 if (FD_ISSET(0,&readfds
)) {
463 timevaladd(&printbefore
,timeout
);
466 } else if (FD_ISSET(1,&writefds
)) {
470 if (nonblock(0,0)) sysfail("un-nonblock stdin");
471 if (nonblock(1,0)) sysfail("un-nonblock stdout");