More progress on addr support.
[adns] / client / adh-query.c
CommitLineData
b3543b6b 1/*
2 * adh-query.c
3 * - useful general-purpose resolver client program
4 * make queries and print answers
5 */
6/*
39f45e7e 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.)
b3543b6b 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
15 * the Free Software Foundation; either version 2, or (at your option)
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
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.
26 */
27
aa1d7195 28#include "adnshost.h"
29
30adns_state ads;
2798305f 31struct outstanding_list outstanding;
aa1d7195 32
2798305f 33static unsigned long idcounter;
34
2798305f 35void ensure_adns_init(void) {
09aee00b 36 adns_initflags initflags;
2798305f 37 int r;
38
39 if (ads) return;
b3543b6b 40
2798305f 41 if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
09aee00b 42
43 initflags= adns_if_noautosys|adns_if_nosigpipe|ov_verbose;
44 if (!ov_env) initflags |= adns_if_noenv;
45
46 if (config_text) {
47 r= adns_init_strcfg(&ads, initflags, stderr, config_text);
48 } else {
49 r= adns_init(&ads, initflags, 0);
50 }
2798305f 51 if (r) sysfail("adns_init",r);
e02793ec 52
53 if (ov_format == fmt_default)
54 ov_format= ov_asynch ? fmt_asynch : fmt_simple;
2798305f 55}
b3543b6b 56
7d0aaee4 57void type_info(adns_rrtype type, const char **typename_r,
58 const void *datap, char **data_r) {
59 static char buf[12];
60 adns_status st;
61
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);
64 assert(!st);
65 if (typename_r && !*typename_r) {
66 sprintf(buf,"TYPE%d", (int)(type & adns_rrt_typemask));
67 *typename_r= buf;
68 }
69}
70
5e6342f0 71static void prep_query(struct query_node **qun_r, int *quflags_r) {
b3543b6b 72 struct query_node *qun;
73 char idbuf[20];
5e6342f0 74
75 if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
76 ensure_adns_init();
77
b3543b6b 78 qun= malloc(sizeof(*qun));
79 qun->pqfr= ov_pqfr;
80 if (ov_id) {
81 qun->id= xstrsave(ov_id);
82 } else {
83 sprintf(idbuf,"%lu",idcounter++);
84 idcounter &= 0x0fffffffflu;
85 qun->id= xstrsave(idbuf);
86 }
5e6342f0 87
88 *quflags_r=
89 (ov_search ? adns_qf_search : 0) |
90 (ov_tcp ? adns_qf_usevc : 0) |
e02793ec 91 ((ov_pqfr.show_owner || ov_format == fmt_simple) ? adns_qf_owner : 0) |
5e6342f0 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) |
bcfd6f13 95 ov_cname | ov_afflags,
5e6342f0 96
97 *qun_r= qun;
98}
b3543b6b 99
d2f6d877 100void of_ptr(const struct optioninfo *oi, const char *arg, const char *arg2) {
5e6342f0 101 struct query_node *qun;
102 int quflags, r;
8a53cf7f
MW
103 struct addrinfo *ai, ai_hint = { 0 };
104 int err;
5e6342f0 105
8a53cf7f
MW
106 ai_hint.ai_family = AF_UNSPEC;
107 ai_hint.ai_socktype = SOCK_DGRAM;
108 ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
109
110 err = getaddrinfo(arg, 0, &ai_hint, &ai);
111 if (err) usageerr("invalid IP address %s",arg);
5e6342f0 112
113 prep_query(&qun,&quflags);
f57665b9 114 qun->owner= xstrsave(arg);
5e6342f0 115 r= adns_submit_reverse(ads,
8a53cf7f 116 ai->ai_addr,
5e6342f0 117 ov_type == adns_r_none ? adns_r_ptr : ov_type,
118 quflags,
119 qun,
120 &qun->qu);
121 if (r) sysfail("adns_submit_reverse",r);
8a53cf7f 122 freeaddrinfo(ai);
5e6342f0 123
124 LIST_LINK_TAIL(outstanding,qun);
125}
126
d2f6d877 127void of_reverse(const struct optioninfo *oi, const char *arg, const char *arg2) {
128 struct query_node *qun;
129 int quflags, r;
8a53cf7f
MW
130 struct addrinfo *ai, ai_hint = { 0 };
131 int err;
132
133 ai_hint.ai_family = AF_UNSPEC;
134 ai_hint.ai_socktype = SOCK_DGRAM;
135 ai_hint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
d2f6d877 136
8a53cf7f
MW
137 err = getaddrinfo(arg, 0, &ai_hint, &ai);
138 if (err) usageerr("invalid IP address %s",arg);
d2f6d877 139
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,
8a53cf7f 144 ai->ai_addr, arg2,
d2f6d877 145 ov_type == adns_r_none ? adns_r_txt : ov_type,
146 quflags,
147 qun,
148 &qun->qu);
149 if (r) sysfail("adns_submit_reverse",r);
8a53cf7f 150 freeaddrinfo(ai);
d2f6d877 151
152 LIST_LINK_TAIL(outstanding,qun);
153}
154
5e6342f0 155void query_do(const char *domain) {
156 struct query_node *qun;
157 int quflags, r;
158
159 prep_query(&qun,&quflags);
f57665b9 160 qun->owner= xstrsave(domain);
aa1d7195 161 r= adns_submit(ads, domain,
162 ov_type == adns_r_none ? adns_r_addr : ov_type,
5e6342f0 163 quflags,
b3543b6b 164 qun,
165 &qun->qu);
166 if (r) sysfail("adns_submit",r);
167
aa1d7195 168 LIST_LINK_TAIL(outstanding,qun);
b3543b6b 169}
aa1d7195 170
5692efb1 171static void dequeue_query(struct query_node *qun) {
172 LIST_UNLINK(outstanding,qun);
173 free(qun->id);
09aee00b 174 free(qun->owner);
5692efb1 175 free(qun);
176}
177
2798305f 178static void print_withspace(const char *str) {
179 if (printf("%s ", str) == EOF) outerr();
180}
181
182static void print_ttl(struct query_node *qun, adns_answer *answer) {
183 unsigned long ttl;
184 time_t now;
185
186 switch (qun->pqfr.ttl) {
187 case tm_none:
188 return;
189 case tm_rel:
190 if (time(&now) == (time_t)-1) sysfail("get current time",errno);
191 ttl= answer->expires < now ? 0 : answer->expires - now;
192 break;
193 case tm_abs:
194 ttl= answer->expires;
195 break;
196 default:
197 abort();
198 }
199 if (printf("%lu ",ttl) == EOF) outerr();
200}
201
f57665b9 202static const char *owner_show(struct query_node *qun, adns_answer *answer) {
203 return answer->owner ? answer->owner : qun->owner;
204}
205
2798305f 206static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
f57665b9 207 if (qun->pqfr.show_owner) print_withspace(owner_show(qun,answer));
2798305f 208 print_ttl(qun,answer);
209}
210
e02793ec 211static void check_status(adns_status st) {
5692efb1 212 static const adns_status statuspoints[]= {
213 adns_s_ok,
214 adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
215 adns_s_max_misconfig, adns_s_max_misquery
216 };
217
218 const adns_status *spp;
5692efb1 219 int minrcode;
220
5692efb1 221 for (minrcode=0, spp=statuspoints;
222 spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
223 spp++)
224 if (st > *spp) minrcode++;
225 if (rcode < minrcode) rcode= minrcode;
e02793ec 226}
2798305f 227
e02793ec 228static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
229 const char *statustypeabbrev, *statusabbrev, *statusstring;
230
231 statustypeabbrev= adns_errtypeabbrev(st);
2798305f 232 statusabbrev= adns_errabbrev(st);
233 statusstring= adns_strerror(st);
234 assert(!strchr(statusstring,'"'));
235
5692efb1 236 if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
2798305f 237 == EOF) outerr();
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();
242}
243
e02793ec 244static void print_dnsfail(adns_status st, struct query_node *qun, adns_answer *answer) {
245 int r;
246 const char *typename, *statusstring;
e02793ec 247
248 if (ov_format == fmt_inline) {
249 if (fputs("; failed ",stdout) == EOF) outerr();
250 print_status(st,qun,answer);
251 return;
252 }
253 assert(ov_format == fmt_simple);
254 if (st == adns_s_nxdomain) {
f57665b9 255 r= fprintf(stderr,"%s does not exist\n", owner_show(qun,answer));
e02793ec 256 } else {
7d0aaee4 257 type_info(answer->type, &typename, 0,0);
e02793ec 258 if (st == adns_s_nodata) {
f57665b9 259 r= fprintf(stderr,"%s has no %s record\n", owner_show(qun,answer), typename);
e02793ec 260 } else {
261 statusstring= adns_strerror(st);
262 r= fprintf(stderr,"Error during DNS %s lookup for %s: %s\n",
f57665b9 263 typename, owner_show(qun,answer), statusstring);
e02793ec 264 }
265 }
266 if (r == EOF) sysfail("write error message to stderr",errno);
267}
268
2798305f 269void query_done(struct query_node *qun, adns_answer *answer) {
7d0aaee4 270 adns_status st;
2798305f 271 int rrn, nrrs;
272 const char *rrp, *realowner, *typename;
273 char *datastr;
274
2798305f 275 st= answer->status;
276 nrrs= answer->nrrs;
e02793ec 277 if (ov_format == fmt_asynch) {
278 check_status(st);
196e9104 279 if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
2798305f 280 print_status(st,qun,answer);
281 } else {
e02793ec 282 if (qun->pqfr.show_cname && answer->cname) {
2798305f 283 print_owner_ttl(qun,answer);
e02793ec 284 if (qun->pqfr.show_type) print_withspace("CNAME");
285 if (printf("%s\n", answer->cname) == EOF) outerr();
286 }
287 if (st) {
288 check_status(st);
289 print_dnsfail(st,qun,answer);
2798305f 290 }
291 }
292 if (qun->pqfr.show_owner) {
f57665b9 293 realowner= answer->cname ? answer->cname : owner_show(qun,answer);
2798305f 294 assert(realowner);
295 } else {
296 realowner= 0;
297 }
298 if (nrrs) {
299 for (rrn=0, rrp = answer->rrs.untyped;
300 rrn < nrrs;
301 rrn++, rrp += answer->rrsz) {
302 if (realowner) print_withspace(realowner);
303 print_ttl(qun,answer);
7d0aaee4 304 type_info(answer->type,&typename, rrp,&datastr);
2798305f 305 if (qun->pqfr.show_type) print_withspace(typename);
306 if (printf("%s\n",datastr) == EOF) outerr();
307 free(datastr);
308 }
309 }
310 if (fflush(stdout)) outerr();
2798305f 311 free(answer);
5692efb1 312 dequeue_query(qun);
2798305f 313}
314
d2f6d877 315void of_asynch_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
2798305f 316 free(ov_id);
317 ov_id= xstrsave(arg);
318}
319
d2f6d877 320void of_cancel_id(const struct optioninfo *oi, const char *arg, const char *arg2) {
5692efb1 321 struct query_node *qun;
2798305f 322
5692efb1 323 for (qun= outstanding.head;
caa16c6a 324 qun && strcmp(qun->id,arg);
5692efb1 325 qun= qun->next);
326 if (!qun) return;
327 adns_cancel(qun->qu);
328 dequeue_query(qun);
329}