3 * - useful general-purpose resolver client program
4 * make queries and print answers
7 * This file is part of adns, which is Copyright Ian Jackson
8 * and contributors (see the file INSTALL for full details).
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation.
27 struct outstanding_list outstanding;
29 static unsigned long idcounter;
31 void ensure_adns_init(void) {
32 adns_initflags initflags;
37 if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
39 initflags= adns_if_noautosys|adns_if_nosigpipe|ov_verbose|ov_checkc;
40 if (!ov_env) initflags |= adns_if_noenv;
43 r= adns_init_strcfg(&ads, initflags, stderr, config_text);
45 r= adns_init(&ads, initflags, 0);
47 if (r) sysfail("adns_init",r);
49 if (ov_format == fmt_default)
50 ov_format= ov_asynch ? fmt_asynch : fmt_simple;
53 void type_info(adns_rrtype type, const char **typename_r,
54 const void *datap, char **data_r) {
58 st= adns_rr_info(type, typename_r, 0,0, datap,data_r);
59 if (st == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
61 if (typename_r && !*typename_r) {
62 sprintf(buf,"TYPE%d", (int)(type & adns_rrt_typemask));
67 static void prep_query(struct query_node **qun_r, int *quflags_r) {
68 struct query_node *qun;
71 if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
74 qun= malloc(sizeof(*qun));
77 qun->id= xstrsave(ov_id);
79 sprintf(idbuf,"%lu",idcounter++);
80 idcounter &= 0x0fffffffflu;
81 qun->id= xstrsave(idbuf);
85 (ov_search ? adns_qf_search : 0) |
86 (ov_tcp ? adns_qf_usevc : 0) |
87 ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
88 (ov_qc_query ? adns_qf_quoteok_query : 0) |
89 (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) |
90 (ov_qc_cname ? 0 : adns_qf_quoteok_cname) |
91 (ov_v6map ? adns_qf_ipv6_mapv4 : 0) |
92 ov_cname | ov_afflags;
97 static void sockaddr_aton(const char *text, adns_rr_addr *a) {
100 a->len= sizeof(a->addr);
101 err= adns_text2addr(text,0,adns_qf_addrlit_scope_forbid,
102 &a->addr.sa,&a->len);
103 if (err == EINVAL) usageerr("invalid IP address %s",text);
104 else if (err) sysfail("adns_text2addr",err);
107 void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2) {
108 struct query_node *qun;
112 sockaddr_aton(arg,&a);
113 prep_query(&qun,&quflags);
114 qun->owner= xstrsave(arg);
115 r= adns_submit_reverse(ads, &a.addr.sa,
116 ov_type == adns_r_none ? adns_r_ptr : ov_type,
120 if (r) sysfail("adns_submit_reverse",r);
122 LIST_LINK_TAIL(outstanding,qun);
125 void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2) {
126 struct query_node *qun;
130 sockaddr_aton(arg,&a);
131 prep_query(&qun,&quflags);
132 qun->owner= xmalloc(strlen(arg) + strlen(arg2) + 2);
133 sprintf(qun->owner, "%s %s", arg,arg2);
134 r= adns_submit_reverse_any(ads, &a.addr.sa,arg2,
135 ov_type == adns_r_none ? adns_r_txt : ov_type,
139 if (r) sysfail("adns_submit_reverse",r);
141 LIST_LINK_TAIL(outstanding,qun);
144 void query_do(const char *domain) {
145 struct query_node *qun;
148 prep_query(&qun,&quflags);
149 qun->owner= xstrsave(domain);
150 r= adns_submit(ads, domain,
151 ov_type == adns_r_none ? adns_r_addr : ov_type,
155 if (r) sysfail("adns_submit",r);
157 LIST_LINK_TAIL(outstanding,qun);
160 static void dequeue_query(struct query_node *qun) {
161 LIST_UNLINK(outstanding,qun);
167 static void print_withspace(const char *str) {
168 if (printf("%s ", str) == EOF) outerr();
171 static void print_ttl(struct query_node *qun, adns_answer *answer) {
175 switch (qun->pqfr.ttl) {
179 if (time(&now) == (time_t)-1) sysfail("get current time",errno);
180 ttl= answer->expires < now ? 0 : answer->expires - now;
183 ttl= answer->expires;
188 if (printf("%lu ",ttl) == EOF) outerr();
191 static const char *owner_show(struct query_node *qun, adns_answer *answer) {
192 return answer->owner ? answer->owner : qun->owner;
195 static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
196 if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
197 print_ttl(qun,answer);
200 static void check_status(adns_status st) {
201 static const adns_status statuspoints[]= {
203 adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
204 adns_s_max_misconfig, adns_s_max_misquery
207 const adns_status *spp;
210 for (minrcode=0, spp=statuspoints;
211 spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
213 if (st > *spp) minrcode++;
214 if (rcode < minrcode) rcode= minrcode;
217 static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
218 const char *statustypeabbrev, *statusabbrev, *statusstring;
220 statustypeabbrev= adns_errtypeabbrev(st);
221 statusabbrev= adns_errabbrev(st);
222 statusstring= adns_strerror(st);
223 assert(!strchr(statusstring,'"'));
225 if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
227 print_owner_ttl(qun,answer);
228 if (qun->pqfr.show_cname)
229 print_withspace(answer->cname ? answer->cname : "$");
230 if (printf("\"%s\"\n", statusstring) == EOF) outerr();
233 static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
235 const char *typename, *statusstring;
237 if (ov_format == fmt_inline) {
238 if (fputs("; failed ",stdout) == EOF) outerr();
239 print_status(st,qun,answer);
242 assert(ov_format == fmt_simple);
243 if (st == adns_s_nxdomain) {
244 r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
246 type_info(answer->type, &typename, 0,0);
247 if (st == adns_s_nodata) {
248 r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
250 statusstring= adns_strerror(st);
251 r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
252 typename, owner_show(qun,answer), statusstring);
255 if (r == EOF) sysfail("write error message to stderr",errno);
258 void query_done(struct query_node *qun, adns_answer *answer) {
261 const char *rrp, *realowner, *typename;
266 if (ov_format == fmt_asynch) {
268 if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
269 print_status(st,qun,answer);
271 if (qun->pqfr.show_cname && answer->cname) {
272 print_owner_ttl(qun,answer);
273 if (qun->pqfr.show_type) print_withspace("CNAME");
274 if (printf("%s\n", answer->cname) == EOF) outerr();
278 print_dnsfail(st,qun,answer);
281 if (qun->pqfr.show_owner) {
282 realowner= answer->cname ? answer->cname : owner_show(qun,answer);
288 for (rrn=0, rrp = answer->rrs.untyped;
290 rrn++, rrp += answer->rrsz) {
291 if (realowner) print_withspace(realowner);
292 print_ttl(qun,answer);
293 type_info(answer->type,&typename, rrp,&datastr);
294 if (qun->pqfr.show_type) print_withspace(typename);
295 if (printf("%s\n",datastr) == EOF) outerr();
299 if (fflush(stdout)) outerr();
304 void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
306 ov_id= xstrsave(arg);
309 void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
310 struct query_node *qun;
312 for (qun= outstanding.head;
313 qun && strcmp(qun->id,arg);
316 adns_cancel(qun->qu);