3 * - filter which does resolving, not part of the library
7 * Copyright (C) 1999 Ian Jackson <ian@davenant.greenend.org.uk>
9 * It is part of adns, which is
10 * Copyright (C) 1997-1999 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/fcntl.h>
44 struct outqueuenode
*next
, *back
;
48 struct timeval printbefore
;
49 struct treething
*addr
;
52 static int bracket
, forever
;
53 static unsigned long timeout
=100;
54 static adns_rrtype rrt
= adns_r_ptr
;
56 static int outblocked
, inputeof
;
57 static struct { struct outqueuenode
*head
, *tail
; } outqueue
;
58 static int peroutqueuenode
, outqueuelen
;
60 static struct sockaddr_in sa
;
61 static adns_state ads
;
63 static char addrtextbuf
[14];
64 static int cbyte
, inbyte
, inbuf
;
65 static unsigned char bytes
[4];
66 static struct timeval printbefore
;
69 unsigned char bytes
[4];
74 static struct treething
*newthing
;
75 static void *treeroot
;
77 static int nonblock(int fd
, int isnonblock
) {
82 r
= fcntl(fd
,F_SETFL
, isnonblock ? r
|O_NONBLOCK
: r
&~O_NONBLOCK
);
87 static void quit(int exitstatus
) NONRETURNING
;
88 static void quit(int exitstatus
) {
94 static void sysfail(const char *what
) NONRETURNING
;
95 static void sysfail(const char *what
) {
96 fprintf(stderr
,"adnsresfilter: system call failed: %s: %s\n",what
,strerror(errno
));
100 static void *xmalloc(size_t sz
) {
102 r
= malloc(sz
); if (r
) return r
;
106 static void outputerr(void) NONRETURNING
;
107 static void outputerr(void) { sysfail("write to stdout"); }
109 static void usage(void) {
110 if (printf("usage: adnsresfilter [<options ...>]\n"
111 " adnsresfilter -h|--help\n"
112 "options: -b|--brackets\n"
114 " -t<timeout>|--timeout <milliseconds>\n"
119 static void usageerr(const char *why
) NONRETURNING
;
120 static void usageerr(const char *why
) {
121 fprintf(stderr
,"adnsresfilter: bad usage: %s\n",why
);
126 static void adnsfail(const char *what
, int e
) NONRETURNING
;
127 static void adnsfail(const char *what
, int e
) {
128 fprintf(stderr
,"adnsresfilter: adns call failed: %s: %s\n",what
,strerror(e
));
132 static void settimeout(const char *arg
) {
134 timeout
= strtoul(arg
,&ep
,0);
135 if (*ep
) usageerr("invalid timeout");
138 static void parseargs(const char *const *argv
) {
142 while ((arg
= *++argv
)) {
143 if (arg
[0] != '-') usageerr("no non-option arguments are allowed");
145 if (!strcmp(arg
,"--brackets")) {
147 } else if (!strcmp(arg
,"--unchecked")) {
149 } else if (!strcmp(arg
,"--wait")) {
151 } else if (!strcmp(arg
,"--help")) {
153 } else if (!strcmp(arg
,"--timeout")) {
154 if (!(arg
= *++argv
)) usageerr("--timeout needs a value");
158 usageerr("unknown long option");
161 while ((c
= *++arg
)) {
176 if (*++arg
) settimeout(arg
);
177 else if ((arg
= *++argv
)) settimeout(arg
);
178 else usageerr("-t needs a value");
183 usageerr("unknown short option");
190 static void queueoutchar(int c
) {
191 struct outqueuenode
*entry
;
193 entry
= outqueue
.tail
;
194 if (!entry
|| entry
->addr
|| entry
->textlen
>= peroutqueuenode
) {
195 peroutqueuenode
= !peroutqueuenode
|| !entry
|| entry
->addr ?
128 :
196 peroutqueuenode
>= 1024 ?
4096 : peroutqueuenode
<<2;
197 entry
= xmalloc(sizeof(*entry
));
198 entry
->buffer
= xmalloc(peroutqueuenode
);
199 entry
->textp
= entry
->buffer
;
202 LIST_LINK_TAIL(outqueue
,entry
);
205 entry
->textp
[entry
->textlen
++]= c
;
208 static void queueoutstr(const char *str
, int len
) {
209 while (len
-- > 0) queueoutchar(*str
++);
212 static void writestdout(struct outqueuenode
*entry
) {
215 while (entry
->textlen
) {
216 r
= write(1, entry
->textp
, entry
->textlen
);
218 if (errno
== EINTR
) continue;
219 if (errno
== EAGAIN
) { outblocked
= 1; break; }
220 sysfail("write stdout");
222 assert(r
<= entry
->textlen
);
226 if (!entry
->textlen
) {
227 LIST_UNLINK(outqueue
,entry
);
234 static void replacetextwithname(struct outqueuenode
*entry
) {
237 entry
->textp
= entry
->addr
->ans
->rrs
.str
[0];
238 entry
->textlen
= strlen(entry
->textp
);
241 static void checkadnsqueries(void) {
245 struct treething
*foundthing
;
249 qu
= 0; context
= 0; ans
= 0;
250 r
= adns_check(ads
,&qu
,&ans
,&context
);
251 if (r
== ESRCH
|| r
== EAGAIN
) break;
254 foundthing
->ans
= ans
;
259 static void restartbuf(void) {
260 if (inbuf
>0) queueoutstr(addrtextbuf
,inbuf
);
264 static int comparer(const void *a
, const void *b
) {
265 return memcmp(a
,b
,4);
268 static void procaddr(void) {
269 struct treething
*foundthing
;
271 struct outqueuenode
*entry
;
275 newthing
= xmalloc(sizeof(struct treething
));
280 memcpy(newthing
->bytes
,bytes
,4);
281 searchfound
= tsearch(newthing
,&treeroot
,comparer
);
282 if (!searchfound
) sysfail("tsearch");
283 foundthing
= *searchfound
;
285 if (foundthing
== newthing
) {
287 memcpy(&sa
.sin_addr
,bytes
,4);
288 r
= adns_submit_reverse(ads
, (const struct sockaddr
*)&sa
,
289 rrt
,0,foundthing
,&foundthing
->qu
);
290 if (r
) adnsfail("submit",r
);
292 entry
= xmalloc(sizeof(*entry
));
293 entry
->buffer
= xmalloc(inbuf
);
294 entry
->textp
= entry
->buffer
;
295 memcpy(entry
->textp
,addrtextbuf
,inbuf
);
296 entry
->textlen
= inbuf
;
297 entry
->addr
= foundthing
;
298 entry
->printbefore
= printbefore
;
299 LIST_LINK_TAIL(outqueue
,entry
);
305 static void startaddr(void) {
310 static void readstdin(void) {
311 char readbuf
[512], *p
;
314 while ((r
= read(0,readbuf
,sizeof(readbuf
))) <= 0) {
315 if (r
== 0) { inputeof
= 1; return; }
316 if (r
== EAGAIN
) return;
317 if (r
!= EINTR
) sysfail("read stdin");
319 for (p
=readbuf
; r
>0; r
--,p
++) {
321 if (cbyte
==-1 && bracket
&& c
=='[') {
322 addrtextbuf
[inbuf
++]= c
;
324 } else if (cbyte
==-1 && !bracket
&& !isalnum(c
)) {
327 } else if (cbyte
>=0 && inbyte
<3 && c
>='0' && c
<='9' &&
328 (nbyte
= bytes
[cbyte
]*10 + (c
-'0')) <= 255) {
330 addrtextbuf
[inbuf
++]= c
;
332 } else if (cbyte
>=0 && cbyte
<3 && inbyte
>0 && c
=='.') {
334 addrtextbuf
[inbuf
++]= c
;
336 } else if (cbyte
==3 && inbyte
>0 && bracket
&& c
==']') {
337 addrtextbuf
[inbuf
++]= c
;
339 } else if (cbyte
==3 && inbyte
>0 && !bracket
&& !isalnum(c
)) {
347 if (!bracket
&& !isalnum(c
)) startaddr();
350 if (cbyte
==3 && inbyte
>0 && !bracket
) procaddr();
353 static void startup(void) {
356 if (nonblock(0,1)) sysfail("set stdin to nonblocking mode");
357 if (nonblock(1,1)) sysfail("set stdout to nonblocking mode");
358 memset(&sa
,0,sizeof(sa
));
359 sa
.sin_family
= AF_INET
;
360 r
= adns_init(&ads
,0,0); if (r
) adnsfail("init",r
);
364 if (!bracket
) startaddr();
367 int main(int argc
, const char *const *argv
) {
369 fd_set readfds
, writefds
, exceptfds
;
370 struct outqueuenode
*entry
;
371 struct timeval
*tv
, tvbuf
, now
;
376 while (!inputeof
|| outqueue
.head
) {
379 FD_ZERO(&readfds
); FD_ZERO(&writefds
); FD_ZERO(&exceptfds
);
380 if ((entry
= outqueue
.head
) && !outblocked
) {
385 if (entry
->addr
->ans
) {
386 if (entry
->addr
->ans
->nrrs
)
387 replacetextwithname(entry
);
391 r
= gettimeofday(&now
,0); if (r
) sysfail("gettimeofday");
394 } else if (!timercmp(&now
,&entry
->printbefore
,<)) {
398 tvbuf
.tv_sec
= entry
->printbefore
.tv_sec
- now
.tv_sec
- 1;
399 tvbuf
.tv_usec
= entry
->printbefore
.tv_usec
- now
.tv_usec
+ 1000000;
400 tvbuf
.tv_sec
+= tvbuf
.tv_usec
/ 1000000;
401 tvbuf
.tv_usec
%= 1000000;
404 adns_beforeselect(ads
,&maxfd
,&readfds
,&writefds
,&exceptfds
,
407 if (outblocked
) FD_SET(1,&writefds
);
408 if (!inputeof
&& outqueuelen
<1024) FD_SET(0,&readfds
);
410 r
= select(maxfd
,&readfds
,&writefds
,&exceptfds
,tv
);
411 if (r
< 0) { if (r
== EINTR
) continue; else sysfail("select"); }
413 r
= gettimeofday(&now
,0); if (r
) sysfail("gettimeofday");
414 adns_afterselect(ads
,maxfd
,&readfds
,&writefds
,&exceptfds
,&now
);
417 if (FD_ISSET(0,&readfds
)) {
420 timevaladd(&printbefore
,timeout
);
423 } else if (FD_ISSET(1,&writefds
)) {
427 if (nonblock(0,0)) sysfail("un-nonblock stdin");
428 if (nonblock(1,0)) sysfail("un-nonblock stdout");
429 if (ferror(stdin
) || fclose(stdin
)) sysfail("read stdin");
430 if (fclose(stdout
)) sysfail("close stdout");