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