3 * - useful general-purpose resolver client program
4 * main program and useful subroutines
7 * This file is part of adns, which is
8 * Copyright (C) 1997-2000,2003,2006,2014-2016,2020 Ian Jackson
9 * Copyright (C) 2014 Mark Wooding
10 * Copyright (C) 1999-2000,2003,2006 Tony Finch
11 * Copyright (C) 1991 Massachusetts Institute of Technology
12 * (See the file INSTALL for full details.)
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 3, or (at your option)
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.
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.
31 const char *config_text;
33 static int used, avail;
36 void quitnow(int rc) {
37 if (ads) adns_finish(ads);
43 void sysfail(const char *what, int errnoval) {
44 fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
48 void usageerr(const char *fmt, ...) {
50 fputs("adnshost usage error: ",stderr);
52 vfprintf(stderr,fmt,al);
59 sysfail("write to stdout",errno);
62 void *xmalloc(size_t sz) {
65 p= malloc(sz); if (!p) sysfail("malloc",sz);
69 char *xstrsave(const char *str) {
72 p= xmalloc(strlen(str)+1);
77 void of_config(const struct optioninfo *oi, const char *arg, const char *arg2) {
81 void of_type(const struct optioninfo *oi, const char *arg, const char *arg2) {
82 static const struct typename {
86 /* enhanced versions */
88 { adns_r_soa, "soa" },
89 { adns_r_ptr, "ptr" },
92 { adns_r_srv, "srv" },
93 { adns_r_addr, "addr" },
95 /* types with only one version */
96 { adns_r_cname, "cname" },
97 { adns_r_hinfo, "hinfo" },
98 { adns_r_txt, "txt" },
102 { adns_r_aaaa, "aaaa" },
103 { adns_r_ns_raw, "ns-" },
104 { adns_r_soa_raw, "soa-" },
105 { adns_r_ptr_raw, "ptr-" },
106 { adns_r_mx_raw, "mx-" },
107 { adns_r_rp_raw, "rp-" },
108 { adns_r_srv_raw, "srv-" },
113 const struct typename *tnp;
114 unsigned long unknowntype;
117 if (strlen(arg) > 4 && !memcmp(arg,"type",4) &&
118 (unknowntype= strtoul(arg+4, &ep, 10), !*ep) && unknowntype < 65536) {
119 ov_type= unknowntype | adns_r_unknown;
124 tnp->type && strcmp(arg,tnp->desc);
126 if (!tnp->type) usageerr("unknown RR type %s",arg);
130 static void process_optarg(const char *arg,
131 const char *const **argv_p,
133 const struct optioninfo *oip;
137 if (arg[0] == '-' || arg[0] == '+') {
138 if (arg[0] == '-' && arg[1] == '-') {
139 if (!strncmp(arg,"--no-",5)) {
141 oip= opt_findl(arg+5);
144 oip= opt_findl(arg+2);
146 if (oip->type == ot_funcarg) {
147 arg= argv_p ? *++(*argv_p) : value;
148 if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
150 } else if (oip->type == ot_funcarg2) {
153 arg2= arg ? *++(*argv_p) : 0;
156 char *space= strchr(value,' ');
157 if (space) *space++= 0;
163 usageerr("option --%s requires two more arguments", oip->lopt);
165 if (value) usageerr("option --%s does not take a value",oip->lopt);
169 opt_do(oip,invert,arg,arg2);
170 } else if (arg[0] == '-' && arg[1] == 0) {
171 arg= argv_p ? *++(*argv_p) : value;
172 if (!arg) usageerr("option `-' must be followed by a domain");
174 } else { /* arg[1] != '-', != '\0' */
175 invert= (arg[0] == '+');
178 oip= opt_finds(&arg);
179 if (oip->type == ot_funcarg) {
181 arg= argv_p ? *++(*argv_p) : value;
182 if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
184 if (value) usageerr("two values for option -%s given !",oip->sopt);
186 opt_do(oip,invert,arg,0);
189 if (value) usageerr("option -%s does not take a value",oip->sopt);
190 opt_do(oip,invert,0,0);
194 } else { /* arg[0] != '-' */
199 static void read_stdin(void) {
201 char *newline, *space;
204 while (!anydone || used) {
205 while (!(newline= memchr(buf,'\n',used))) {
207 avail += 20; avail <<= 1;
208 buf= realloc(buf,avail);
209 if (!buf) sysfail("realloc stdin buffer",errno);
212 r= read(0,buf+used,avail-used);
213 } while (r < 0 && errno == EINTR);
216 /* fake up final newline */
224 if (r < 0) sysfail("read stdin",errno);
228 space= strchr(buf,' ');
229 if (space) *space++= 0;
230 process_optarg(buf,0,space);
231 used -= (newline-buf);
232 memmove(buf,newline,used);
237 int main(int argc, const char *const *argv) {
238 struct timeval *tv, tvbuf;
243 fd_set readfds, writefds, exceptfds;
246 while ((arg= *++argv)) process_optarg(arg,&argv,0);
248 if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
254 qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
255 r= adns_check(ads,&qu,&answer,&qun_v);
256 if (r == EAGAIN) break;
257 if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
259 query_done(qun_v,answer);
270 adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
271 r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
273 if (errno == EINTR) continue;
274 sysfail("select",errno);
276 adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
277 if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
280 if (fclose(stdout)) outerr();