Update copyright dates
[adns] / client / adnstest.c
1 /*
2 * adnstest.c
3 * - simple test program, not part of the library
4 */
5 /*
6 * This file is part of adns, which is
7 * Copyright (C) 1997-2000,2003,2006,2014-2016 Ian Jackson
8 * Copyright (C) 2014 Mark Wooding
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.)
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 3, 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 */
26
27 #include <stdio.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #define ADNS_FEATURE_MANYAF
36 #include "config.h"
37 #include "adns.h"
38
39 #ifdef ADNS_REGRESS_TEST
40 # include "hredirect.h"
41 # include "harness.h"
42 # undef exit
43 # define exit Texit
44 #endif
45
46 struct myctx {
47 adns_query qu;
48 int doneyet, found;
49 const char *fdom;
50 };
51
52 static struct myctx *mcs;
53 static adns_state ads;
54 static adns_rrtype *types_a;
55
56 static void quitnow(int rc) NONRETURNING;
57 static void quitnow(int rc) {
58 free(mcs);
59 free(types_a);
60 if (ads) adns_finish(ads);
61
62 exit(rc);
63 }
64
65 #ifndef HAVE_POLL
66 #undef poll
67 int poll(struct pollfd *ufds, int nfds, int timeout) {
68 fputs("poll(2) not supported on this system\n",stderr);
69 quitnow(5);
70 }
71 #define adns_beforepoll(a,b,c,d,e) 0
72 #define adns_afterpoll(a,b,c,d) 0
73 #endif
74
75 static void failure_status(const char *what, adns_status st) NONRETURNING;
76 static void failure_status(const char *what, adns_status st) {
77 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
78 quitnow(2);
79 }
80
81 static void failure_errno(const char *what, int errnoval) NONRETURNING;
82 static void failure_errno(const char *what, int errnoval) {
83 switch (errnoval) {
84 #define CE(e) \
85 case e: fprintf(stderr,"adns failure: %s: errno=" #e "\n",what); break
86 CE(EINVAL);
87 CE(EINTR);
88 CE(ESRCH);
89 CE(EAGAIN);
90 CE(ENOSYS);
91 CE(ERANGE);
92 #undef CE
93 default: fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); break;
94 }
95 quitnow(2);
96 }
97
98 static void usageerr(const char *why) NONRETURNING;
99 static void usageerr(const char *why) {
100 fprintf(stderr,
101 "bad usage: %s\n"
102 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
103 " [ :<typenum>,... ]\n"
104 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
105 "initflags: p use poll(2) instead of select(2)\n"
106 " s use adns_wait with specified query, instead of 0\n"
107 "queryflags: a print status abbrevs instead of strings\n"
108 "typenum: may be 0x<hex>|<dec>, or 0x<hex> or <dec>\n"
109 "exit status: 0 ok (though some queries may have failed)\n"
110 " 1 used by test harness to indicate test failed\n"
111 " 2 unable to submit or init or some such\n"
112 " 3 unexpected failure\n"
113 " 4 usage error\n"
114 " 5 operation not supported on this system\n",
115 why);
116 quitnow(4);
117 }
118
119 static const adns_rrtype defaulttypes[]= {
120 adns_r_a,
121 adns_r_ns_raw,
122 adns_r_cname,
123 adns_r_soa_raw,
124 adns_r_ptr_raw,
125 adns_r_hinfo,
126 adns_r_mx_raw,
127 adns_r_txt,
128 adns_r_rp_raw,
129
130 adns_r_addr,
131 adns_r_ns,
132 adns_r_ptr,
133 adns_r_mx,
134
135 adns_r_soa,
136 adns_r_rp,
137
138 adns_r_none
139 };
140
141 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
142 fprintf(stdout, "%s(%s)%s%s",
143 (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
144 ri ? " " : "", ri ? adns_strerror(ri) : "");
145 }
146
147 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
148 char *ownflags, int ownflags_l) {
149 int qf;
150 char *ep;
151
152 qf= strtoul(fdom,&ep,0);
153 if (*ep == ',' && strchr(ep,'/')) {
154 ep++;
155 while (*ep != '/') {
156 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
157 *ownflags++= *ep++;
158 }
159 }
160 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
161 else { *dom_r= ep+1; *qf_r= qf; }
162 *ownflags= 0;
163 }
164
165 static int consistsof(const char *string, const char *accept) {
166 return strspn(string,accept) == strlen(string);
167 }
168
169 int main(int argc, char *const *argv) {
170 adns_query qu;
171 struct myctx *mc, *mcw;
172 void *mcr;
173 adns_answer *ans;
174 const char *initstring, *rrtn, *fmtn;
175 const char *const *fdomlist, *domain;
176 char *show, *cp;
177 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
178 adns_status ri;
179 int r;
180 const adns_rrtype *types;
181 struct timeval now;
182 char ownflags[10];
183 char *ep;
184 const char *initflags, *owninitflags;
185
186 if (argv[0] && argv[1] && argv[1][0] == '-') {
187 initflags= argv[1]+1;
188 argv++;
189 } else {
190 initflags= "";
191 }
192 if (argv[0] && argv[1] && argv[1][0] == '/') {
193 initstring= argv[1]+1;
194 argv++;
195 } else {
196 initstring= 0;
197 }
198
199 initflagsnum= strtoul(initflags,&ep,0);
200 if (*ep == ',') {
201 owninitflags= ep+1;
202 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
203 } else if (!*ep) {
204 owninitflags= "";
205 } else {
206 usageerr("bad <initflagsnum>[,<owninitflags>]");
207 }
208
209 if (argv[0] && argv[1] && argv[1][0] == ':') {
210 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
211 if (ch==',') tc++;
212 types_a= malloc(sizeof(*types_a)*(tc+1));
213 if (!types_a) { perror("malloc types"); quitnow(3); }
214 for (cp= argv[1]+1, ti=0; ti<tc; ) {
215 types_a[ti]= 0;
216 for (;;) {
217 types_a[ti] |= strtoul(cp,&cp,0);
218 ch= *cp;
219 if (!ch) break;
220 cp++;
221 if (ch=='|') continue;
222 if (ch==',') break;
223 usageerr("unexpected char (not comma) in or between types");
224 }
225 ti++;
226 if (!ch) break;
227 }
228 types_a[ti]= adns_r_none;
229 types= types_a;
230 argv++;
231 } else {
232 types_a= 0;
233 types= defaulttypes;
234 }
235
236 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
237 fdomlist= (const char *const*)argv+1;
238
239 for (qc=0; fdomlist[qc]; qc++);
240 for (tc=0; types[tc] != adns_r_none; tc++);
241 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
242 if (!mcs) { perror("malloc mcs"); quitnow(3); }
243
244 setvbuf(stdout,0,_IOLBF,0);
245
246 if (initstring) {
247 r= adns_init_strcfg(&ads,
248 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
249 ^initflagsnum,
250 stdout,initstring);
251 } else {
252 r= adns_init(&ads,
253 (adns_if_debug|adns_if_noautosys)^initflagsnum,
254 0);
255 }
256 if (r) failure_errno("init",r);
257
258 for (qi=0; qi<qc; qi++) {
259 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
260 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
261 for (ti=0; ti<tc; ti++) {
262 mc= &mcs[qi*tc+ti];
263 mc->doneyet= 0;
264 mc->fdom= fdomlist[qi];
265
266 fprintf(stdout,"%s flags %d type %d",
267 domain,qflags,types[ti]&adns_rrt_reprmask);
268 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
269 if (r == ENOSYS) {
270 fprintf(stdout," not implemented\n");
271 mc->qu= 0;
272 mc->doneyet= 1;
273 } else if (r) {
274 failure_errno("submit",r);
275 } else {
276 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
277 putc(' ',stdout);
278 dumptype(ri,rrtn,fmtn);
279 fprintf(stdout," submitted\n");
280 }
281 }
282 }
283
284 for (;;) {
285 for (qi=0; qi<qc; qi++) {
286 for (ti=0; ti<tc; ti++) {
287 mc= &mcs[qi*tc+ti];
288 mc->found= 0;
289 }
290 }
291 for (adns_forallqueries_begin(ads);
292 (qu= adns_forallqueries_next(ads,&mcr));
293 ) {
294 mc= mcr;
295 assert(qu == mc->qu);
296 assert(!mc->doneyet);
297 mc->found= 1;
298 }
299 mcw= 0;
300 for (qi=0; qi<qc; qi++) {
301 for (ti=0; ti<tc; ti++) {
302 mc= &mcs[qi*tc+ti];
303 if (mc->doneyet) continue;
304 assert(mc->found);
305 if (!mcw) mcw= mc;
306 }
307 }
308 if (!mcw) break;
309
310 if (strchr(owninitflags,'s')) {
311 qu= mcw->qu;
312 mc= mcw;
313 } else {
314 qu= 0;
315 mc= 0;
316 }
317
318 if (strchr(owninitflags,'p')) {
319 r= adns_wait_poll(ads,&qu,&ans,&mcr);
320 } else {
321 r= adns_wait(ads,&qu,&ans,&mcr);
322 }
323 if (r) failure_errno("wait/check",r);
324
325 if (mc) assert(mcr==mc);
326 else mc= mcr;
327 assert(qu==mc->qu);
328 assert(!mc->doneyet);
329
330 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
331
332 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
333
334 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
335 fprintf(stdout, "%s flags %d type ",domain,qflags);
336 dumptype(ri,rrtn,fmtn);
337 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
338 ownflags[0] ? " ownflags=" : "", ownflags,
339 strchr(ownflags,'a')
340 ? adns_errabbrev(ans->status)
341 : adns_strerror(ans->status),
342 ans->nrrs,
343 ans->cname ? ans->cname : "$",
344 ans->owner ? ans->owner : "$",
345 (long)ans->expires - (long)now.tv_sec);
346 if (ans->nrrs) {
347 assert(!ri);
348 for (i=0; i<ans->nrrs; i++) {
349 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
350 if (ri) failure_status("info",ri);
351 fprintf(stdout," %s\n",show);
352 free(show);
353 }
354 }
355 free(ans);
356
357 mc->doneyet= 1;
358 }
359
360 quitnow(0);
361 }