3 * - useful general-purpose resolver client program
4 * main program and useful subroutines
8 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
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>
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)
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,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 void sysfail(const char *what, int errnoval) {
32 fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval));
36 void usageerr(const char *fmt, ...) {
38 fputs("adnshost usage error: ",stderr);
40 vfprintf(stderr,fmt,al);
47 sysfail("write to stdout",errno);
50 void *xmalloc(size_t sz) {
53 p= malloc(sz); if (!p) sysfail("malloc",sz);
57 char *xstrsave(const char *str) {
60 p= xmalloc(strlen(str)+1);
65 void of_type(const struct optioninfo *oi, const char *arg) {
66 static const struct typename {
70 /* enhanced versions */
72 { adns_r_soa, "soa" },
73 { adns_r_ptr, "ptr" },
76 { adns_r_addr, "addr" },
78 /* types with only one version */
79 { adns_r_cname, "cname" },
80 { adns_r_hinfo, "hinfo" },
81 { adns_r_txt, "txt" },
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-" },
94 const struct typename *tnp;
97 tnp->type && strcmp(arg,tnp->desc);
99 if (!tnp->type) usageerr("unknown RR type %s",arg);
105 static void process_optarg(const char *arg,
106 const char *const **argv_p,
108 const struct optioninfo *oip;
111 if (arg[0] == '-' || arg[0] == '+') {
112 if (arg[0] == '-' && arg[1] == '-') {
113 if (!strncmp(arg,"--no-",5)) {
115 oip= opt_findl(arg+5);
118 oip= opt_findl(arg+2);
120 if (oip->type == ot_funcarg) {
121 arg= argv_p ? *++(*argv_p) : value;
122 if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
124 if (value) usageerr("option --%s does not take a value",oip->lopt);
127 opt_do(oip,arg,invert);
128 } else if (arg[0] == '-' && arg[1] == 0) {
129 arg= argv_p ? *++(*argv_p) : value;
130 if (!arg) usageerr("option `-' must be followed by a domain");
132 } else { /* arg[1] != '-', != '\0' */
133 invert= (arg[0] == '+');
136 oip= opt_finds(&arg);
137 if (oip->type == ot_funcarg) {
139 arg= argv_p ? *++(*argv_p) : value;
140 if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
142 if (value) usageerr("two values for option -%s given !",oip->sopt);
144 opt_do(oip,arg,invert);
147 if (value) usageerr("option -%s does not take a value",oip->sopt);
148 opt_do(oip,0,invert);
152 } else { /* arg[0] != '-' */
157 static void read_stdin(void) {
158 static int used, avail;
162 char *newline, *space;
165 while (!anydone || used) {
166 while (!(newline= memchr(buf,'\n',used))) {
168 avail += 20; avail <<= 1;
169 buf= realloc(buf,avail);
170 if (!buf) sysfail("realloc stdin buffer",errno);
173 r= read(0,buf+used,avail-used);
174 } while (r < 0 && errno == EINTR);
177 /* fake up final newline */
185 if (r < 0) sysfail("read stdin",errno);
189 space= strchr(buf,' ');
190 if (space) *space++= 0;
191 process_optarg(buf,0,space);
192 used -= (newline-buf);
193 memmove(buf,newline,used);
198 int main(int argc, const char *const *argv) {
199 struct timeval *tv, tvbuf;
204 fd_set readfds, writefds, exceptfds;
207 while ((arg= *++argv)) process_optarg(arg,&argv,0);
209 if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
215 qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
216 r= adns_check(ads,&qu,&answer,&qun_v);
217 if (r == EAGAIN) break;
218 if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
220 query_done(qun_v,answer);
231 adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
232 r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
234 if (errno == EINTR) continue;
235 sysfail("select",errno);
237 adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
238 if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
241 if (fclose(stdout)) outerr();