3 * - useful general-purpose resolver client program
4 * main program and useful subroutines
7 * This file is part of adns, which is Copyright Ian Jackson
8 * and contributors (see the file INSTALL for full details).
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation.
27 const char *config_text;
29 static int used, avail;
32 void quitnow(int rc) {
33 if (ads) adns_finish(ads);
39 void sysfail(const char *what, int errnoval) {
40 fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
44 void usageerr(const char *fmt, ...) {
46 fputs("adnshost usage error: ",stderr);
48 vfprintf(stderr,fmt,al);
55 sysfail("write to stdout",errno);
58 void *xmalloc(size_t sz) {
61 p= malloc(sz); if (!p) sysfail("malloc",sz);
65 char *xstrsave(const char *str) {
68 p= xmalloc(strlen(str)+1);
73 void of_config(const struct optioninfo *oi, const char *arg, const char *arg2) {
77 void of_type(const struct optioninfo *oi, const char *arg, const char *arg2) {
78 static const struct typename {
82 /* enhanced versions */
84 { adns_r_soa, "soa" },
85 { adns_r_ptr, "ptr" },
88 { adns_r_srv, "srv" },
89 { adns_r_addr, "addr" },
91 /* types with only one version */
92 { adns_r_cname, "cname" },
93 { adns_r_hinfo, "hinfo" },
94 { adns_r_txt, "txt" },
98 { adns_r_aaaa, "aaaa" },
99 { adns_r_ns_raw, "ns-" },
100 { adns_r_soa_raw, "soa-" },
101 { adns_r_ptr_raw, "ptr-" },
102 { adns_r_mx_raw, "mx-" },
103 { adns_r_rp_raw, "rp-" },
104 { adns_r_srv_raw, "srv-" },
109 const struct typename *tnp;
110 unsigned long unknowntype;
113 if (strlen(arg) > 4 && !memcmp(arg,"type",4) &&
114 (unknowntype= strtoul(arg+4, &ep, 10), !*ep) && unknowntype < 65536) {
115 ov_type= unknowntype | adns_r_unknown;
120 tnp->type && strcmp(arg,tnp->desc);
122 if (!tnp->type) usageerr("unknown RR type %s",arg);
126 static void process_optarg(const char *arg,
127 const char *const **argv_p,
129 const struct optioninfo *oip;
133 if (arg[0] == '-' || arg[0] == '+') {
134 if (arg[0] == '-' && arg[1] == '-') {
135 if (!strncmp(arg,"--no-",5)) {
137 oip= opt_findl(arg+5);
140 oip= opt_findl(arg+2);
142 if (oip->type == ot_funcarg) {
143 arg= argv_p ? *++(*argv_p) : value;
144 if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
146 } else if (oip->type == ot_funcarg2) {
149 arg2= arg ? *++(*argv_p) : 0;
152 char *space= strchr(value,' ');
153 if (space) *space++= 0;
159 usageerr("option --%s requires two more arguments", oip->lopt);
161 if (value) usageerr("option --%s does not take a value",oip->lopt);
165 opt_do(oip,invert,arg,arg2);
166 } else if (arg[0] == '-' && arg[1] == 0) {
167 arg= argv_p ? *++(*argv_p) : value;
168 if (!arg) usageerr("option `-' must be followed by a domain");
170 } else { /* arg[1] != '-', != '\0' */
171 invert= (arg[0] == '+');
174 oip= opt_finds(&arg);
175 if (oip->type == ot_funcarg) {
177 arg= argv_p ? *++(*argv_p) : value;
178 if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
180 if (value) usageerr("two values for option -%s given !",oip->sopt);
182 opt_do(oip,invert,arg,0);
185 if (value) usageerr("option -%s does not take a value",oip->sopt);
186 opt_do(oip,invert,0,0);
190 } else { /* arg[0] != '-' */
195 static void read_stdin(void) {
197 char *newline, *space;
200 while (!anydone || used) {
201 while (!(newline= memchr(buf,'\n',used))) {
203 avail += 20; avail <<= 1;
204 buf= realloc(buf,avail);
205 if (!buf) sysfail("realloc stdin buffer",errno);
208 r= read(0,buf+used,avail-used);
209 } while (r < 0 && errno == EINTR);
212 /* fake up final newline */
220 if (r < 0) sysfail("read stdin",errno);
224 space= strchr(buf,' ');
225 if (space) *space++= 0;
226 process_optarg(buf,0,space);
227 used -= (newline-buf);
228 memmove(buf,newline,used);
233 int main(int argc, const char *const *argv) {
234 struct timeval *tv, tvbuf;
239 fd_set readfds, writefds, exceptfds;
242 while ((arg= *++argv)) process_optarg(arg,&argv,0);
244 if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
250 qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
251 r= adns_check(ads,&qu,&answer,&qun_v);
252 if (r == EAGAIN) break;
253 if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
255 query_done(qun_v,answer);
266 adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
267 r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
269 if (errno == EINTR) continue;
270 sysfail("select",errno);
272 adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
273 if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
276 if (fclose(stdout)) outerr();