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