chiark / gitweb /
client/adh-query.c: Surprising comma rather than semicolon.
[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
7  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
8  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
9  *    Copyright (C) 1991 Massachusetts Institute of Technology
10  *  (See the file INSTALL for full details.)
11  *  
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *  
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *  
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software Foundation,
24  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
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 #include "config.h"
36 #include "adns.h"
37
38 #ifdef ADNS_REGRESS_TEST
39 # include "hredirect.h"
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           "exit status:  0 ok (though some queries may have failed)\n"
105           "              1 used by test harness to indicate test failed\n"
106           "              2 unable to submit or init or some such\n"
107           "              3 unexpected failure\n"
108           "              4 usage error\n"
109           "              5 operation not supported on this system\n",
110           why);
111   quitnow(4);
112 }
113
114 static const adns_rrtype defaulttypes[]= {
115   adns_r_a,
116   adns_r_ns_raw,
117   adns_r_cname,
118   adns_r_soa_raw,
119   adns_r_ptr_raw,
120   adns_r_hinfo,
121   adns_r_mx_raw,
122   adns_r_txt,
123   adns_r_rp_raw,
124   
125   adns_r_addr,
126   adns_r_ns,
127   adns_r_ptr,
128   adns_r_mx,
129   
130   adns_r_soa,
131   adns_r_rp,
132
133   adns_r_none
134 };
135
136 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
137   fprintf(stdout, "%s(%s)%s%s",
138           (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
139           ri ? " " : "", ri ? adns_strerror(ri) : "");
140 }
141
142 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
143                        char *ownflags, int ownflags_l) {
144   int qf;
145   char *ep;
146
147   qf= strtoul(fdom,&ep,0);
148   if (*ep == ',' && strchr(ep,'/')) {
149     ep++;
150     while (*ep != '/') {
151       if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
152       *ownflags++= *ep++;
153     }
154   }
155   if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
156   else { *dom_r= ep+1; *qf_r= qf; }
157   *ownflags= 0;
158 }
159
160 static int consistsof(const char *string, const char *accept) {
161   return strspn(string,accept) == strlen(string);
162 }
163
164 int main(int argc, char *const *argv) {
165   adns_query qu;
166   struct myctx *mc, *mcw;
167   void *mcr;
168   adns_answer *ans;
169   const char *initstring, *rrtn, *fmtn;
170   const char *const *fdomlist, *domain;
171   char *show, *cp;
172   int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
173   adns_status ri;
174   int r;
175   const adns_rrtype *types;
176   struct timeval now;
177   char ownflags[10];
178   char *ep;
179   const char *initflags, *owninitflags;
180
181   if (argv[0] && argv[1] && argv[1][0] == '-') {
182     initflags= argv[1]+1;
183     argv++;
184   } else {
185     initflags= "";
186   }
187   if (argv[0] && argv[1] && argv[1][0] == '/') {
188     initstring= argv[1]+1;
189     argv++;
190   } else {
191     initstring= 0;
192   }
193
194   initflagsnum= strtoul(initflags,&ep,0);
195   if (*ep == ',') {
196     owninitflags= ep+1;
197     if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
198   } else if (!*ep) {
199     owninitflags= "";
200   } else {
201     usageerr("bad <initflagsnum>[,<owninitflags>]");
202   }
203   
204   if (argv[0] && argv[1] && argv[1][0] == ':') {
205     for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
206       if (ch==',') tc++;
207     types_a= malloc(sizeof(*types_a)*(tc+1));
208     if (!types_a) { perror("malloc types"); quitnow(3); }
209     for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
210       types_a[ti]= strtoul(cp,&cp,10);
211       if ((ch= *cp)) {
212         if (ch != ',') usageerr("unexpected char (not comma) in or between types");
213         cp++;
214       }
215     }
216     types_a[ti]= adns_r_none;
217     types= types_a;
218     argv++;
219   } else {
220     types_a= 0;
221     types= defaulttypes;
222   }
223   
224   if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
225   fdomlist= (const char *const*)argv+1;
226
227   for (qc=0; fdomlist[qc]; qc++);
228   for (tc=0; types[tc] != adns_r_none; tc++);
229   mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
230   if (!mcs) { perror("malloc mcs"); quitnow(3); }
231
232   setvbuf(stdout,0,_IOLBF,0);
233   
234   if (initstring) {
235     r= adns_init_strcfg(&ads,
236                         (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
237                         ^initflagsnum,
238                         stdout,initstring);
239   } else {
240     r= adns_init(&ads,
241                  (adns_if_debug|adns_if_noautosys)^initflagsnum,
242                  0);
243   }
244   if (r) failure_errno("init",r);
245
246   for (qi=0; qi<qc; qi++) {
247     fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
248     if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
249     for (ti=0; ti<tc; ti++) {
250       mc= &mcs[qi*tc+ti];
251       mc->doneyet= 0;
252       mc->fdom= fdomlist[qi];
253
254       fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
255       r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
256       if (r == ENOSYS) {
257         fprintf(stdout," not implemented\n");
258         mc->qu= 0;
259         mc->doneyet= 1;
260       } else if (r) {
261         failure_errno("submit",r);
262       } else {
263         ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
264         putc(' ',stdout);
265         dumptype(ri,rrtn,fmtn);
266         fprintf(stdout," submitted\n");
267       }
268     }
269   }
270
271   for (;;) {
272     for (qi=0; qi<qc; qi++) {
273       for (ti=0; ti<tc; ti++) {
274         mc= &mcs[qi*tc+ti];
275         mc->found= 0;
276       }
277     }
278     for (adns_forallqueries_begin(ads);
279          (qu= adns_forallqueries_next(ads,&mcr));
280          ) {
281       mc= mcr;
282       assert(qu == mc->qu);
283       assert(!mc->doneyet);
284       mc->found= 1;
285     }
286     mcw= 0;
287     for (qi=0; qi<qc; qi++) {
288       for (ti=0; ti<tc; ti++) {
289         mc= &mcs[qi*tc+ti];
290         if (mc->doneyet) continue;
291         assert(mc->found);
292         if (!mcw) mcw= mc;
293       }
294     }
295     if (!mcw) break;
296
297     if (strchr(owninitflags,'s')) {
298       qu= mcw->qu;
299       mc= mcw;
300     } else {
301       qu= 0;
302       mc= 0;
303     }
304
305     if (strchr(owninitflags,'p')) {
306       r= adns_wait_poll(ads,&qu,&ans,&mcr);
307     } else {
308       r= adns_wait(ads,&qu,&ans,&mcr);
309     }
310     if (r) failure_errno("wait/check",r);
311     
312     if (mc) assert(mcr==mc);
313     else mc= mcr;
314     assert(qu==mc->qu);
315     assert(!mc->doneyet);
316     
317     fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
318
319     if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
320       
321     ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
322     fprintf(stdout, "%s flags %d type ",domain,qflags);
323     dumptype(ri,rrtn,fmtn);
324     fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
325             ownflags[0] ? " ownflags=" : "", ownflags,
326             strchr(ownflags,'a')
327             ? adns_errabbrev(ans->status)
328             : adns_strerror(ans->status),
329             ans->nrrs,
330             ans->cname ? ans->cname : "$",
331             ans->owner ? ans->owner : "$",
332             (long)ans->expires - (long)now.tv_sec);
333     if (ans->nrrs) {
334       assert(!ri);
335       for (i=0; i<ans->nrrs; i++) {
336         ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
337         if (ri) failure_status("info",ri);
338         fprintf(stdout," %s\n",show);
339         free(show);
340       }
341     }
342     free(ans);
343
344     mc->doneyet= 1;
345   }
346
347   quitnow(0);
348 }