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