3 * - useful general-purpose resolver client program
4 * make queries and print answers
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.
32 struct outstanding_list outstanding;
34 static unsigned long idcounter;
36 void ensure_adns_init(void) {
41 if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
43 adns_if_noautosys|adns_if_nosigpipe |
44 (ov_env ? 0 : adns_if_noenv) |
47 if (r) sysfail("adns_init",r);
49 if (ov_format == fmt_default)
50 ov_format= ov_asynch ? fmt_asynch : fmt_simple;
53 static void prep_query(struct query_node **qun_r, int *quflags_r) {
54 struct query_node *qun;
57 if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
60 qun= malloc(sizeof(*qun));
63 qun->id= xstrsave(ov_id);
65 sprintf(idbuf,"%lu",idcounter++);
66 idcounter &= 0x0fffffffflu;
67 qun->id= xstrsave(idbuf);
71 (ov_search ? adns_qf_search : 0) |
72 (ov_tcp ? adns_qf_usevc : 0) |
73 ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
74 (ov_qc_query ? adns_qf_quoteok_query : 0) |
75 (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) |
76 (ov_qc_cname ? 0 : adns_qf_quoteok_cname) |
82 void of_ptr(const struct optioninfo *oi, const char *arg) {
83 struct query_node *qun;
85 struct sockaddr_in sa;
87 memset(&sa,0,sizeof(sa));
88 sa.sin_family= AF_INET;
89 if (!inet_aton(arg,&sa.sin_addr)) usageerr("invalid IP address %s",arg);
91 prep_query(&qun,&quflags);
92 qun->owner= xstrsave(arg);
93 r= adns_submit_reverse(ads,
94 (struct sockaddr*)&sa,
95 ov_type == adns_r_none ? adns_r_ptr : ov_type,
99 if (r) sysfail("adns_submit_reverse",r);
101 LIST_LINK_TAIL(outstanding,qun);
104 void query_do(const char *domain) {
105 struct query_node *qun;
108 prep_query(&qun,&quflags);
109 qun->owner= xstrsave(domain);
110 r= adns_submit(ads, domain,
111 ov_type == adns_r_none ? adns_r_addr : ov_type,
115 if (r) sysfail("adns_submit",r);
117 LIST_LINK_TAIL(outstanding,qun);
120 static void dequeue_query(struct query_node *qun) {
121 LIST_UNLINK(outstanding,qun);
126 static void print_withspace(const char *str) {
127 if (printf("%s ", str) == EOF) outerr();
130 static void print_ttl(struct query_node *qun, adns_answer *answer) {
134 switch (qun->pqfr.ttl) {
138 if (time(&now) == (time_t)-1) sysfail("get current time",errno);
139 ttl= answer->expires < now ? 0 : answer->expires - now;
142 ttl= answer->expires;
147 if (printf("%lu ",ttl) == EOF) outerr();
150 static const char *owner_show(struct query_node *qun, adns_answer *answer) {
151 return answer->owner ? answer->owner : qun->owner;
154 static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
155 if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
156 print_ttl(qun,answer);
159 static void check_status(adns_status st) {
160 static const adns_status statuspoints[]= {
162 adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
163 adns_s_max_misconfig, adns_s_max_misquery
166 const adns_status *spp;
169 for (minrcode=0, spp=statuspoints;
170 spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
172 if (st > *spp) minrcode++;
173 if (rcode < minrcode) rcode= minrcode;
176 static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
177 const char *statustypeabbrev, *statusabbrev, *statusstring;
179 statustypeabbrev= adns_errtypeabbrev(st);
180 statusabbrev= adns_errabbrev(st);
181 statusstring= adns_strerror(st);
182 assert(!strchr(statusstring,'"'));
184 if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
186 print_owner_ttl(qun,answer);
187 if (qun->pqfr.show_cname)
188 print_withspace(answer->cname ? answer->cname : "$");
189 if (printf("\"%s\"\n", statusstring) == EOF) outerr();
192 static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
194 const char *typename, *statusstring;
197 if (ov_format == fmt_inline) {
198 if (fputs("; failed ",stdout) == EOF) outerr();
199 print_status(st,qun,answer);
202 assert(ov_format == fmt_simple);
203 if (st == adns_s_nxdomain) {
204 r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
206 ist= adns_rr_info(answer->type, &typename, 0,0,0,0);
207 if (st == adns_s_nodata) {
208 r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
210 statusstring= adns_strerror(st);
211 r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
212 typename, owner_show(qun,answer), statusstring);
215 if (r == EOF) sysfail("write error message to stderr",errno);
218 void query_done(struct query_node *qun, adns_answer *answer) {
221 const char *rrp, *realowner, *typename;
226 if (ov_format == fmt_asynch) {
228 if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
229 print_status(st,qun,answer);
231 if (qun->pqfr.show_cname && answer->cname) {
232 print_owner_ttl(qun,answer);
233 if (qun->pqfr.show_type) print_withspace("CNAME");
234 if (printf("%s\n", answer->cname) == EOF) outerr();
238 print_dnsfail(st,qun,answer);
241 if (qun->pqfr.show_owner) {
242 realowner= answer->cname ? answer->cname : owner_show(qun,answer);
248 for (rrn=0, rrp = answer->rrs.untyped;
250 rrn++, rrp += answer->rrsz) {
251 if (realowner) print_withspace(realowner);
252 print_ttl(qun,answer);
253 ist= adns_rr_info(answer->type, &typename, 0, 0, rrp, &datastr);
254 if (ist == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
256 if (qun->pqfr.show_type) print_withspace(typename);
257 if (printf("%s\n",datastr) == EOF) outerr();
261 if (fflush(stdout)) outerr();
266 void of_asynch_id(const struct optioninfo *oi, const char *arg) {
268 ov_id= xstrsave(arg);
271 void of_cancel_id(const struct optioninfo *oi, const char *arg) {
272 struct query_node *qun;
274 for (qun= outstanding.head;
275 qun && strcmp(qun->id,arg);
278 adns_cancel(qun->qu);