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 Ian Jackson
9 * Copyright (C) 1999-2000,2003,2006 Tony Finch
10 * Copyright (C) 1991 Massachusetts Institute of Technology
11 * (See the file INSTALL for full details.)
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.
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;
155 usageerr("option --%s requires two more arguments", oip->lopt);
157 if (value) usageerr("option --%s does not take a value",oip->lopt);
161 opt_do(oip,invert,arg,arg2);
162 } else if (arg[0] == '-' && arg[1] == 0) {
163 arg= argv_p ? *++(*argv_p) : value;
164 if (!arg) usageerr("option `-' must be followed by a domain");
166 } else { /* arg[1] != '-', != '\0' */
167 invert= (arg[0] == '+');
170 oip= opt_finds(&arg);
171 if (oip->type == ot_funcarg) {
173 arg= argv_p ? *++(*argv_p) : value;
174 if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
176 if (value) usageerr("two values for option -%s given !",oip->sopt);
178 opt_do(oip,invert,arg,0);
181 if (value) usageerr("option -%s does not take a value",oip->sopt);
182 opt_do(oip,invert,0,0);
186 } else { /* arg[0] != '-' */
191 static void read_stdin(void) {
193 char *newline, *space;
196 while (!anydone || used) {
197 while (!(newline= memchr(buf,'\n',used))) {
199 avail += 20; avail <<= 1;
200 buf= realloc(buf,avail);
201 if (!buf) sysfail("realloc stdin buffer",errno);
204 r= read(0,buf+used,avail-used);
205 } while (r < 0 && errno == EINTR);
208 /* fake up final newline */
216 if (r < 0) sysfail("read stdin",errno);
220 space= strchr(buf,' ');
221 if (space) *space++= 0;
222 process_optarg(buf,0,space);
223 used -= (newline-buf);
224 memmove(buf,newline,used);
229 int main(int argc, const char *const *argv) {
230 struct timeval *tv, tvbuf;
235 fd_set readfds, writefds, exceptfds;
238 while ((arg= *++argv)) process_optarg(arg,&argv,0);
240 if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
246 qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
247 r= adns_check(ads,&qu,&answer,&qun_v);
248 if (r == EAGAIN) break;
249 if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
251 query_done(qun_v,answer);
262 adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
263 r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
265 if (errno == EINTR) continue;
266 sysfail("select",errno);
268 adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
269 if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
272 if (fclose(stdout)) outerr();