chiark / gitweb /
a293d3b1eb6e6102c3af028c352f242f2f3fd550
[adns.git] / client / adnstest.c
1 /*
2  * dtest.c
3  * - simple test program, not part of the library
4  */
5 /*
6  *  This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software Foundation,
20  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
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 #include <sys/poll.h>
31
32 #ifndef OUTPUTSTREAM
33 # define OUTPUTSTREAM stdout
34 #endif
35
36 #include "adns.h"
37
38 static void failure_status(const char *what, adns_status st) {
39   fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
40   exit(2);
41 }
42
43 static void failure_errno(const char *what, int errnoval) {
44   fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
45   exit(2);
46 }
47
48 static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
49
50 static const adns_rrtype defaulttypes[]= {
51   adns_r_a,
52   adns_r_ns_raw,
53   adns_r_cname,
54   adns_r_soa_raw,
55   adns_r_ptr_raw,
56   adns_r_hinfo,
57   adns_r_mx_raw,
58   adns_r_txt,
59   adns_r_rp_raw,
60   
61   adns_r_addr,
62   adns_r_ns,
63   adns_r_ptr,
64   adns_r_mx,
65   
66   adns_r_soa,
67   adns_r_rp,
68
69   adns_r_none
70 };
71
72 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
73   fprintf(stdout, "%s(%s)%s%s",
74           ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
75           ri ? " " : "", ri ? adns_strerror(ri) : "");
76 }
77
78 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
79                        char *ownflags, int ownflags_l) {
80   int qf;
81   char *ep;
82
83   qf= strtoul(fdom,&ep,0);
84   if (*ep == ',' && strchr(ep,'/')) {
85     ep++;
86     while (*ep != '/') {
87       if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); exit(3); }
88       *ownflags++= *ep++;
89     }
90   }
91   if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
92   else { *dom_r= ep+1; *qf_r= qf; }
93   *ownflags= 0;
94 }
95
96 static int consistsof(const char *string, const char *accept) {
97   return strspn(string,accept) == strlen(string);
98 }
99
100 int main(int argc, char *const *argv) {
101   adns_state ads;
102   adns_query *qus, qu;
103   adns_answer *ans;
104   const char *initstring, *rrtn, *fmtn;
105   const char *const *fdomlist, *domain;
106   char *show, *cp;
107   int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum, npollfds, npollfdsavail, timeout;
108   struct pollfd *pollfds;
109   adns_status r, ri;
110   const adns_rrtype *types;
111   struct timeval now;
112   adns_rrtype *types_a;
113   char ownflags[10];
114   char *ep;
115   const char *initflags, *owninitflags;
116
117   if (argv[0] && argv[1] && argv[1][0] == '-') {
118     initflags= argv[1]+1;
119     argv++;
120   } else {
121     initflags= "";
122   }
123   if (argv[0] && argv[1] && argv[1][0] == '/') {
124     initstring= argv[1]+1;
125     argv++;
126   } else {
127     initstring= 0;
128   }
129
130   initflagsnum= strtoul(initflags,&ep,0);
131   if (*ep == ',') {
132     owninitflags= ep+1;
133     if (!consistsof(owninitflags,"p")) {
134       fputs("unknown owninitflag\n",stderr);
135       exit(4);
136     }
137   } else if (!*ep) {
138     owninitflags= "";
139   } else {
140     fputs("bad <initflagsnum>[,<owninitflags>]\n",stderr);
141     exit(4);
142   }
143   
144   if (argv[0] && argv[1] && argv[1][0] == ':') {
145     for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
146       if (ch==',') tc++;
147     types_a= malloc(sizeof(*types_a)*(tc+1));
148     if (!types_a) { perror("malloc types"); exit(3); }
149     for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
150       types_a[ti]= strtoul(cp,&cp,10);
151       if ((ch= *cp)) {
152         if (ch != ',') {
153           fputs("usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
154                 "              [ :<typenum>,... ]\n"
155                 "              [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
156                 "initflags:   p  use poll(2) instead of select(2)\n"
157                 "queryflags:  a  print status abbrevs instead of strings\n",
158                 stderr);
159           exit(4);
160         }
161         cp++;
162       }
163     }
164     *cp++= adns_r_none;
165     types= types_a;
166     argv++;
167   } else {
168     types= defaulttypes;
169   }
170   
171   if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
172   else fdomlist= defaultargv;
173
174   for (qc=0; fdomlist[qc]; qc++);
175   for (tc=0; types[tc] != adns_r_none; tc++);
176   qus= malloc(sizeof(qus)*qc*tc);
177   if (!qus) { perror("malloc qus"); exit(3); }
178
179   if (initstring) {
180     r= adns_init_strcfg(&ads,
181                         (adns_if_debug|adns_if_noautosys)^initflagsnum,
182                         stdout,initstring);
183   } else {
184     r= adns_init(&ads,
185                  (adns_if_debug|adns_if_noautosys)^initflagsnum,
186                  0);
187   }
188   if (r) failure_errno("init",r);
189
190   npollfdsavail= 0;
191   pollfds= 0;
192   
193   for (qi=0; qi<qc; qi++) {
194     fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
195     if (!consistsof(ownflags,"a")) {
196       fputs("unknown ownqueryflag\n",stderr);
197       exit(4);
198     }
199     for (ti=0; ti<tc; ti++) {
200       fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
201       r= adns_submit(ads,domain,types[ti],qflags,0,&qus[qi*tc+ti]);
202       if (r == adns_s_unknownrrtype) {
203         fprintf(stdout," not implemented\n");
204         qus[qi*tc+ti]= 0;
205       } else if (r) {
206         failure_errno("submit",r);
207       } else {
208         ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
209         putc(' ',stdout);
210         dumptype(ri,rrtn,fmtn);
211         fprintf(stdout," submitted\n");
212       }
213     }
214   }
215
216   for (qi=0; qi<qc; qi++) {
217     fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
218       
219     for (ti=0; ti<tc; ti++) {
220       qu= qus[qi*tc+ti];
221       if (!qu) continue;
222
223       if (strchr(owninitflags,'p')) {
224         for (;;) {
225           r= adns_check(ads,&qu,&ans,0);
226           if (r != EWOULDBLOCK) break;
227           for (;;) {
228             npollfds= npollfdsavail;
229             timeout= -1;
230             r= adns_beforepoll(ads, pollfds, &npollfds, &timeout, 0);
231             if (r != ERANGE) break;
232             pollfds= realloc(pollfds,sizeof(*pollfds)*npollfds);
233             if (!pollfds) failure_errno("realloc pollfds",errno);
234             npollfdsavail= npollfds;
235           }
236           if (r) failure_errno("beforepoll",r);
237           r= poll(pollfds,npollfds,timeout);
238           if (r == -1) failure_errno("poll",errno);
239           adns_afterpoll(ads,pollfds, r?npollfds:0, 0);
240         }
241       } else {
242         r= adns_wait(ads,&qu,&ans,0);
243       }
244       if (r) failure_errno("wait/check",r);
245
246       if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
247       
248       ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
249       fprintf(stdout, "%s flags %d type ",domain,qflags);
250       dumptype(ri,rrtn,fmtn);
251       fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
252               ownflags[0] ? " ownflags=" : "", ownflags,
253               strchr(ownflags,'a')
254               ? adns_errabbrev(ans->status)
255               : adns_strerror(ans->status),
256               ans->nrrs,
257               ans->cname ? ans->cname : "$",
258               ans->owner ? ans->owner : "$",
259               (long)ans->expires - (long)now.tv_sec);
260       if (ans->nrrs) {
261         assert(!ri);
262         for (i=0; i<ans->nrrs; i++) {
263           r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
264           if (r) failure_status("info",r);
265           fprintf(stdout," %s\n",show);
266           free(show);
267         }
268       }
269       free(ans);
270     }
271   }
272
273   free(qus);
274   adns_finish(ads);
275   
276   exit(0);
277 }