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