3 * - useful general-purpose resolver client program
4 * make queries and print answers
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 struct outstanding_list outstanding;
33 static unsigned long idcounter;
35 void ensure_adns_init(void) {
36 adns_initflags initflags;
41 if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
43 initflags= adns_if_noautosys|adns_if_nosigpipe|ov_verbose;
44 if (!ov_env) initflags |= adns_if_noenv;
47 r= adns_init_strcfg(&ads, initflags, stderr, config_text);
49 r= adns_init(&ads, initflags, 0);
51 if (r) sysfail("adns_init",r);
53 if (ov_format == fmt_default)
54 ov_format= ov_asynch ? fmt_asynch : fmt_simple;
57 void type_info(adns_rrtype type, const char **typename_r,
58 const void *datap, char **data_r) {
62 st= adns_rr_info(type, typename_r, 0,0, datap,data_r);
63 if (st == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
65 if (typename_r && !*typename_r) {
66 sprintf(buf,"TYPE%d", (int)(type & adns_rrt_typemask));
71 static void prep_query(struct query_node **qun_r, int *quflags_r) {
72 struct query_node *qun;
75 if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
78 qun= malloc(sizeof(*qun));
81 qun->id= xstrsave(ov_id);
83 sprintf(idbuf,"%lu",idcounter++);
84 idcounter &= 0x0fffffffflu;
85 qun->id= xstrsave(idbuf);
89 (ov_search ? adns_qf_search : 0) |
90 (ov_tcp ? adns_qf_usevc : 0) |
91 ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
92 (ov_qc_query ? adns_qf_quoteok_query : 0) |
93 (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) |
94 (ov_qc_cname ? 0 : adns_qf_quoteok_cname) |
95 ov_cname | ov_afflags,
100 void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2) {
101 struct query_node *qun;
103 struct addrinfo *ai, ai_hint = { 0 };
106 ai_hint.ai_family = AF_UNSPEC;
107 ai_hint.ai_socktype = SOCK_DGRAM;
108 ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
110 err = getaddrinfo(arg, 0, &ai_hint, &ai);
111 if (err) usageerr("invalid IP address %s",arg);
113 prep_query(&qun,&quflags);
114 qun->owner= xstrsave(arg);
115 r= adns_submit_reverse(ads,
117 ov_type == adns_r_none ? adns_r_ptr : ov_type,
121 if (r) sysfail("adns_submit_reverse",r);
124 LIST_LINK_TAIL(outstanding,qun);
127 void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2) {
128 struct query_node *qun;
130 struct addrinfo *ai, ai_hint = { 0 };
133 ai_hint.ai_family = AF_UNSPEC;
134 ai_hint.ai_socktype = SOCK_DGRAM;
135 ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
137 err = getaddrinfo(arg, 0, &ai_hint, &ai);
138 if (err) usageerr("invalid IP address %s",arg);
140 prep_query(&qun,&quflags);
141 qun->owner= xmalloc(strlen(arg) + strlen(arg2) + 2);
142 sprintf(qun->owner, "%s %s", arg,arg2);
143 r= adns_submit_reverse_any(ads,
145 ov_type == adns_r_none ? adns_r_txt : ov_type,
149 if (r) sysfail("adns_submit_reverse",r);
152 LIST_LINK_TAIL(outstanding,qun);
155 void query_do(const char *domain) {
156 struct query_node *qun;
159 prep_query(&qun,&quflags);
160 qun->owner= xstrsave(domain);
161 r= adns_submit(ads, domain,
162 ov_type == adns_r_none ? adns_r_addr : ov_type,
166 if (r) sysfail("adns_submit",r);
168 LIST_LINK_TAIL(outstanding,qun);
171 static void dequeue_query(struct query_node *qun) {
172 LIST_UNLINK(outstanding,qun);
178 static void print_withspace(const char *str) {
179 if (printf("%s ", str) == EOF) outerr();
182 static void print_ttl(struct query_node *qun, adns_answer *answer) {
186 switch (qun->pqfr.ttl) {
190 if (time(&now) == (time_t)-1) sysfail("get current time",errno);
191 ttl= answer->expires < now ? 0 : answer->expires - now;
194 ttl= answer->expires;
199 if (printf("%lu ",ttl) == EOF) outerr();
202 static const char *owner_show(struct query_node *qun, adns_answer *answer) {
203 return answer->owner ? answer->owner : qun->owner;
206 static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
207 if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
208 print_ttl(qun,answer);
211 static void check_status(adns_status st) {
212 static const adns_status statuspoints[]= {
214 adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
215 adns_s_max_misconfig, adns_s_max_misquery
218 const adns_status *spp;
221 for (minrcode=0, spp=statuspoints;
222 spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
224 if (st > *spp) minrcode++;
225 if (rcode < minrcode) rcode= minrcode;
228 static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
229 const char *statustypeabbrev, *statusabbrev, *statusstring;
231 statustypeabbrev= adns_errtypeabbrev(st);
232 statusabbrev= adns_errabbrev(st);
233 statusstring= adns_strerror(st);
234 assert(!strchr(statusstring,'"'));
236 if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
238 print_owner_ttl(qun,answer);
239 if (qun->pqfr.show_cname)
240 print_withspace(answer->cname ? answer->cname : "$");
241 if (printf("\"%s\"\n", statusstring) == EOF) outerr();
244 static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
246 const char *typename, *statusstring;
248 if (ov_format == fmt_inline) {
249 if (fputs("; failed ",stdout) == EOF) outerr();
250 print_status(st,qun,answer);
253 assert(ov_format == fmt_simple);
254 if (st == adns_s_nxdomain) {
255 r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
257 type_info(answer->type, &typename, 0,0);
258 if (st == adns_s_nodata) {
259 r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
261 statusstring= adns_strerror(st);
262 r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
263 typename, owner_show(qun,answer), statusstring);
266 if (r == EOF) sysfail("write error message to stderr",errno);
269 void query_done(struct query_node *qun, adns_answer *answer) {
272 const char *rrp, *realowner, *typename;
277 if (ov_format == fmt_asynch) {
279 if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
280 print_status(st,qun,answer);
282 if (qun->pqfr.show_cname && answer->cname) {
283 print_owner_ttl(qun,answer);
284 if (qun->pqfr.show_type) print_withspace("CNAME");
285 if (printf("%s\n", answer->cname) == EOF) outerr();
289 print_dnsfail(st,qun,answer);
292 if (qun->pqfr.show_owner) {
293 realowner= answer->cname ? answer->cname : owner_show(qun,answer);
299 for (rrn=0, rrp = answer->rrs.untyped;
301 rrn++, rrp += answer->rrsz) {
302 if (realowner) print_withspace(realowner);
303 print_ttl(qun,answer);
304 type_info(answer->type,&typename, rrp,&datastr);
305 if (qun->pqfr.show_type) print_withspace(typename);
306 if (printf("%s\n",datastr) == EOF) outerr();
310 if (fflush(stdout)) outerr();
315 void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
317 ov_id= xstrsave(arg);
320 void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
321 struct query_node *qun;
323 for (qun= outstanding.head;
324 qun && strcmp(qun->id,arg);
327 adns_cancel(qun->qu);