+ * New adns_submit_reverse_any for eg RBL lookups, and corresponding
[adns] / client / adh-main.c
1 /*
2 * adh-main.c
3 * - useful general-purpose resolver client program
4 * main program and useful subroutines
5 */
6 /*
7 * This file is
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
9 *
10 * It is part of adns, which is
11 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
13 *
14 * This program 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, or (at your option)
17 * any later version.
18 *
19 * This program 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.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29 #include "adnshost.h"
30
31 void sysfail(const char *what, int errnoval) {
32 fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
33 exit(10);
34 }
35
36 void usageerr(const char *fmt, ...) {
37 va_list al;
38 fputs("adnshost usage error: ",stderr);
39 va_start(al,fmt);
40 vfprintf(stderr,fmt,al);
41 va_end(al);
42 putc('\n',stderr);
43 exit(11);
44 }
45
46 void outerr(void) {
47 sysfail("write to stdout",errno);
48 }
49
50 void *xmalloc(size_t sz) {
51 void *p;
52
53 p= malloc(sz); if (!p) sysfail("malloc",sz);
54 return p;
55 }
56
57 char *xstrsave(const char *str) {
58 char *p;
59
60 p= xmalloc(strlen(str)+1);
61 strcpy(p,str);
62 return p;
63 }
64
65 void of_type(const struct optioninfo *oi, const char *arg, const char *arg2) {
66 static const struct typename {
67 adns_rrtype type;
68 const char *desc;
69 } typenames[]= {
70 /* enhanced versions */
71 { adns_r_ns, "ns" },
72 { adns_r_soa, "soa" },
73 { adns_r_ptr, "ptr" },
74 { adns_r_mx, "mx" },
75 { adns_r_rp, "rp" },
76 { adns_r_addr, "addr" },
77
78 /* types with only one version */
79 { adns_r_cname, "cname" },
80 { adns_r_hinfo, "hinfo" },
81 { adns_r_txt, "txt" },
82
83 /* raw versions */
84 { adns_r_a, "a" },
85 { adns_r_ns_raw, "ns-" },
86 { adns_r_soa_raw, "soa-" },
87 { adns_r_ptr_raw, "ptr-" },
88 { adns_r_mx_raw, "mx-" },
89 { adns_r_rp_raw, "rp-" },
90
91 { adns_r_none, 0 }
92 };
93
94 const struct typename *tnp;
95
96 for (tnp=typenames;
97 tnp->type && strcmp(arg,tnp->desc);
98 tnp++);
99 if (!tnp->type) usageerr("unknown RR type %s",arg);
100 ov_type= tnp->type;
101 }
102
103 int rcode;
104
105 static void process_optarg(const char *arg,
106 const char *const **argv_p,
107 const char *value) {
108 const struct optioninfo *oip;
109 const char *arg2;
110 int invert;
111
112 if (arg[0] == '-' || arg[0] == '+') {
113 if (arg[0] == '-' && arg[1] == '-') {
114 if (!strncmp(arg,"--no-",5)) {
115 invert= 1;
116 oip= opt_findl(arg+5);
117 } else {
118 invert= 0;
119 oip= opt_findl(arg+2);
120 }
121 if (oip->type == ot_funcarg) {
122 arg= argv_p ? *++(*argv_p) : value;
123 if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
124 arg2= 0;
125 } else if (oip->type == ot_funcarg2) {
126 assert(argv_p);
127 arg= *++(*argv_p);
128 if (arg) arg2= *++(*argv_p);
129 if (!arg || !arg2)
130 usageerr("option --%s requires two more arguments", oip->lopt);
131 } else {
132 if (value) usageerr("option --%s does not take a value",oip->lopt);
133 arg= 0;
134 arg2= 0;
135 }
136 opt_do(oip,invert,arg,arg2);
137 } else if (arg[0] == '-' && arg[1] == 0) {
138 arg= argv_p ? *++(*argv_p) : value;
139 if (!arg) usageerr("option `-' must be followed by a domain");
140 query_do(arg);
141 } else { /* arg[1] != '-', != '\0' */
142 invert= (arg[0] == '+');
143 ++arg;
144 while (*arg) {
145 oip= opt_finds(&arg);
146 if (oip->type == ot_funcarg) {
147 if (!*arg) {
148 arg= argv_p ? *++(*argv_p) : value;
149 if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
150 } else {
151 if (value) usageerr("two values for option -%s given !",oip->sopt);
152 }
153 opt_do(oip,invert,arg,0);
154 arg= "";
155 } else {
156 if (value) usageerr("option -%s does not take a value",oip->sopt);
157 opt_do(oip,invert,0,0);
158 }
159 }
160 }
161 } else { /* arg[0] != '-' */
162 query_do(arg);
163 }
164 }
165
166 static void read_stdin(void) {
167 static int used, avail;
168 static char *buf;
169
170 int anydone, r;
171 char *newline, *space;
172
173 anydone= 0;
174 while (!anydone || used) {
175 while (!(newline= memchr(buf,'\n',used))) {
176 if (used == avail) {
177 avail += 20; avail <<= 1;
178 buf= realloc(buf,avail);
179 if (!buf) sysfail("realloc stdin buffer",errno);
180 }
181 do {
182 r= read(0,buf+used,avail-used);
183 } while (r < 0 && errno == EINTR);
184 if (r == 0) {
185 if (used) {
186 /* fake up final newline */
187 buf[used++]= '\n';
188 r= 1;
189 } else {
190 ov_pipe= 0;
191 return;
192 }
193 }
194 if (r < 0) sysfail("read stdin",errno);
195 used += r;
196 }
197 *newline++= 0;
198 space= strchr(buf,' ');
199 if (space) *space++= 0;
200 process_optarg(buf,0,space);
201 used -= (newline-buf);
202 memmove(buf,newline,used);
203 anydone= 1;
204 }
205 }
206
207 int main(int argc, const char *const *argv) {
208 struct timeval *tv, tvbuf;
209 adns_query qu;
210 void *qun_v;
211 adns_answer *answer;
212 int r, maxfd;
213 fd_set readfds, writefds, exceptfds;
214 const char *arg;
215
216 while ((arg= *++argv)) process_optarg(arg,&argv,0);
217
218 if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
219
220 ensure_adns_init();
221
222 for (;;) {
223 for (;;) {
224 qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
225 r= adns_check(ads,&qu,&answer,&qun_v);
226 if (r == EAGAIN) break;
227 if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
228 assert(!r);
229 query_done(qun_v,answer);
230 }
231 maxfd= 0;
232 FD_ZERO(&readfds);
233 FD_ZERO(&writefds);
234 FD_ZERO(&exceptfds);
235 if (ov_pipe) {
236 maxfd= 1;
237 FD_SET(0,&readfds);
238 }
239 tv= 0;
240 adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
241 r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
242 if (r == -1) {
243 if (errno == EINTR) continue;
244 sysfail("select",errno);
245 }
246 adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
247 if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
248 }
249 x_quit:
250 if (fclose(stdout)) outerr();
251 exit(rcode);
252 }