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.
39 static void sysfail(const char *what) NONRETURNING;
40 static void sysfail(const char *what) {
41 fprintf(stderr,"adnsresfilter: system call failed: %s: %s\n",what,strerror(errno));
45 static void outputerr(void) NONRETURNING;
46 static void outputerr(void) { sysfail("write to stdout"); }
48 static void usage(void) {
49 if (printf("usage: adnsresfilter [<options ...>]\n"
50 " adnsresfilter -h|--help\n"
51 "options: -b|--brackets\n"
57 static void usageerr(const char *why) NONRETURNING;
58 static void usageerr(const char *why) {
59 fprintf(stderr,"adnsresfilter: bad usage: %s\n",why);
64 static void adnsfail(const char *what, int e) NONRETURNING;
65 static void adnsfail(const char *what, int e) {
66 fprintf(stderr,"adnsresfilter: adns call failed: %s: %s\n",what,strerror(e));
70 static int bracket, forever;
71 static adns_rrtype rrt= adns_r_ptr;
73 static struct sockaddr_in sa;
74 static adns_state ads;
77 static int c, cbyte, inbyte, inbuf;
78 static unsigned char bytes[4];
81 unsigned char bytes[4];
86 static struct treething *newthing;
87 static void *treeroot;
89 static int comparer(const void *a, const void *b) {
93 static void restartbuf(void) {
96 if (fputs(buf,stdout) < 0) outputerr();
101 static void procaddr(void) {
102 struct treething *foundthing;
103 void *expectreturn, **searchfound;
107 newthing= malloc(sizeof(struct treething));
108 if (!newthing) sysfail("malloc");
113 memcpy(newthing->bytes,bytes,4);
114 searchfound= tsearch(newthing,&treeroot,comparer);
115 if (!searchfound) sysfail("tsearch");
116 foundthing= *searchfound;
118 if (foundthing == newthing) {
120 memcpy(&sa.sin_addr,bytes,4);
121 r= adns_submit_reverse(ads, (const struct sockaddr*)&sa,
122 rrt,0,foundthing,&foundthing->qu);
123 if (r) adnsfail("submit",r);
125 if (!forever && foundthing->ans && foundthing->ans->status == adns_s_timeout) {
126 free(foundthing->ans);
129 if (!foundthing->ans) {
130 expectreturn= foundthing;
131 r= (forever ? adns_wait : adns_check)
132 (ads,&foundthing->qu,&foundthing->ans,&expectreturn);
133 assert(r==EAGAIN || (!r && foundthing->ans && expectreturn==foundthing));
135 if (foundthing->ans && foundthing->ans->nrrs > 0) {
136 if (fputs(foundthing->ans->rrs.str[0],stdout) < 0) outputerr();
144 static void startaddr(void) {
149 static void mustputchar(int c) {
150 if (putchar(c) == EOF) outputerr();
153 int main(int argc, const char *const *argv) {
157 while ((arg= *++argv)) {
158 if (arg[0] != '-') usageerr("no non-option arguments are allowed");
160 if (!strcmp(arg,"--brackets")) {
162 } else if (!strcmp(arg,"--unchecked")) {
164 } else if (!strcmp(arg,"--wait")) {
166 } else if (!strcmp(arg,"--help")) {
169 usageerr("unknown long option");
172 while ((c= *++arg)) {
186 usageerr("unknown short option");
191 if (setvbuf(stdout,0,_IOLBF,0)) sysfail("setvbuf stdout");
193 memset(&sa,0,sizeof(sa));
194 sa.sin_family= AF_INET;
196 r= adns_init(&ads,0,0); if (r) adnsfail("init",r);
201 if (!bracket) startaddr();
202 while ((c= getchar()) != EOF) {
203 if (cbyte==-1 && bracket && c=='[') {
206 } else if (cbyte==-1 && !bracket && !isalnum(c)) {
209 } else if (cbyte>=0 && inbyte<3 && c>='0' && c<='9' &&
210 (nbyte= bytes[cbyte]*10 + (c-'0')) <= 255) {
214 } else if (cbyte>=0 && cbyte<3 && inbyte>0 && c=='.') {
218 } else if (cbyte==3 && inbyte>0 && bracket && c==']') {
221 } else if (cbyte==3 && inbyte>0 && !bracket && !isalnum(c)) {
229 if (!bracket && !isalnum(c)) startaddr();
232 if (ferror(stdin) || fclose(stdin)) sysfail("read stdin");
233 if (cbyte==3 && inbyte>0 && !bracket) procaddr();
234 if (fclose(stdout)) sysfail("close stdout");