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