Sort out testing of forallqueries.
[adns] / client / adnstest.c
CommitLineData
e576be50 1/*
2 * dtest.c
3 * - simple test program, not part of the library
4 */
5/*
a719a4be 6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
e576be50 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 */
964344fc 22
23#include <stdio.h>
73dba56e 24#include <sys/time.h>
350d37d9 25#include <unistd.h>
86e7b8d9 26#include <assert.h>
27#include <stdlib.h>
9da4a044 28#include <string.h>
fc6a52ae 29#include <errno.h>
30#include <sys/poll.h>
964344fc 31
7d251914 32#ifndef OUTPUTSTREAM
33# define OUTPUTSTREAM stdout
34#endif
35
964344fc 36#include "adns.h"
37
9da4a044 38static void failure_status(const char *what, adns_status st) {
86e7b8d9 39 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
40 exit(2);
41}
42
9da4a044 43static void failure_errno(const char *what, int errnoval) {
44 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
45 exit(2);
46}
47
86e7b8d9 48static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
49
ffbda80c 50static const adns_rrtype defaulttypes[]= {
51 adns_r_a,
52 adns_r_ns_raw,
53 adns_r_cname,
9ec44266 54 adns_r_soa_raw,
ffbda80c 55 adns_r_ptr_raw,
9ec44266 56 adns_r_hinfo,
e062dcae 57 adns_r_mx_raw,
1b644113 58 adns_r_txt,
1dfe95d8 59 adns_r_rp_raw,
9ec44266 60
828d89bd 61 adns_r_addr,
1dfe95d8 62 adns_r_ns,
a6536d8b 63 adns_r_ptr,
9ec44266 64 adns_r_mx,
65
66 adns_r_soa,
67 adns_r_rp,
68
ffbda80c 69 adns_r_none
70};
71
e062dcae 72static 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
9da4a044 78static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
79 char *ownflags, int ownflags_l) {
5476fe64 80 int qf;
81 char *ep;
82
83 qf= strtoul(fdom,&ep,0);
9da4a044 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 }
5476fe64 91 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
92 else { *dom_r= ep+1; *qf_r= qf; }
9da4a044 93 *ownflags= 0;
5476fe64 94}
95
fc6a52ae 96static int consistsof(const char *string, const char *accept) {
97 return strspn(string,accept) == strlen(string);
98}
99
e062dcae 100int main(int argc, char *const *argv) {
669f9df5 101 struct myctx {
102 adns_query qu;
e42ce2b2 103 int doneyet, found;
104 const char *fdom;
669f9df5 105 };
106
964344fc 107 adns_state ads;
669f9df5 108 adns_query qu;
e42ce2b2 109 struct myctx *mcs, *mc, *mcw;
669f9df5 110 void *mcr;
4353a5c4 111 adns_answer *ans;
12c5b204 112 const char *initstring, *rrtn, *fmtn;
5476fe64 113 const char *const *fdomlist, *domain;
e062dcae 114 char *show, *cp;
fc6a52ae 115 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum, npollfds, npollfdsavail, timeout;
116 struct pollfd *pollfds;
86e7b8d9 117 adns_status r, ri;
ffbda80c 118 const adns_rrtype *types;
73dba56e 119 struct timeval now;
e062dcae 120 adns_rrtype *types_a;
9da4a044 121 char ownflags[10];
fc6a52ae 122 char *ep;
123 const char *initflags, *owninitflags;
86e7b8d9 124
fc6a52ae 125 if (argv[0] && argv[1] && argv[1][0] == '-') {
126 initflags= argv[1]+1;
127 argv++;
128 } else {
129 initflags= "";
130 }
12c5b204 131 if (argv[0] && argv[1] && argv[1][0] == '/') {
132 initstring= argv[1]+1;
133 argv++;
134 } else {
135 initstring= 0;
136 }
fc6a52ae 137
138 initflagsnum= strtoul(initflags,&ep,0);
139 if (*ep == ',') {
140 owninitflags= ep+1;
141 if (!consistsof(owninitflags,"p")) {
142 fputs("unknown owninitflag\n",stderr);
143 exit(4);
144 }
145 } else if (!*ep) {
146 owninitflags= "";
147 } else {
148 fputs("bad <initflagsnum>[,<owninitflags>]\n",stderr);
149 exit(4);
150 }
12c5b204 151
e062dcae 152 if (argv[0] && argv[1] && argv[1][0] == ':') {
153 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
154 if (ch==',') tc++;
e3d42241 155 types_a= malloc(sizeof(*types_a)*(tc+1));
e062dcae 156 if (!types_a) { perror("malloc types"); exit(3); }
157 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
158 types_a[ti]= strtoul(cp,&cp,10);
159 if ((ch= *cp)) {
a6536d8b 160 if (ch != ',') {
fc6a52ae 161 fputs("usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
162 " [ :<typenum>,... ]\n"
163 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
164 "initflags: p use poll(2) instead of select(2)\n"
165 "queryflags: a print status abbrevs instead of strings\n",
166 stderr);
e062dcae 167 exit(4);
168 }
169 cp++;
170 }
171 }
e3d42241 172 *cp++= adns_r_none;
e062dcae 173 types= types_a;
174 argv++;
175 } else {
176 types= defaulttypes;
177 }
178
5476fe64 179 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
180 else fdomlist= defaultargv;
ffbda80c 181
5476fe64 182 for (qc=0; fdomlist[qc]; qc++);
ffbda80c 183 for (tc=0; types[tc] != adns_r_none; tc++);
669f9df5 184 mcs= malloc(sizeof(*mcs)*qc*tc);
185 if (!mcs) { perror("malloc mcs"); exit(3); }
964344fc 186
12c5b204 187 if (initstring) {
fc6a52ae 188 r= adns_init_strcfg(&ads,
189 (adns_if_debug|adns_if_noautosys)^initflagsnum,
190 stdout,initstring);
12c5b204 191 } else {
fc6a52ae 192 r= adns_init(&ads,
193 (adns_if_debug|adns_if_noautosys)^initflagsnum,
194 0);
12c5b204 195 }
9da4a044 196 if (r) failure_errno("init",r);
86e7b8d9 197
fc6a52ae 198 npollfdsavail= 0;
199 pollfds= 0;
200
86e7b8d9 201 for (qi=0; qi<qc; qi++) {
9da4a044 202 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
fc6a52ae 203 if (!consistsof(ownflags,"a")) {
204 fputs("unknown ownqueryflag\n",stderr);
205 exit(4);
206 }
ffbda80c 207 for (ti=0; ti<tc; ti++) {
669f9df5 208 mc= &mcs[qi*tc+ti];
209 mc->doneyet= 0;
e42ce2b2 210 mc->fdom= fdomlist[qi];
669f9df5 211
1aa0213d 212 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
669f9df5 213 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
ea1e31e3 214 if (r == adns_s_unknownrrtype) {
916c16ca 215 fprintf(stdout," not implemented\n");
669f9df5 216 mc->qu= 0;
217 mc->doneyet= 1;
916c16ca 218 } else if (r) {
9da4a044 219 failure_errno("submit",r);
916c16ca 220 } else {
e062dcae 221 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 222 putc(' ',stdout);
e062dcae 223 dumptype(ri,rrtn,fmtn);
916c16ca 224 fprintf(stdout," submitted\n");
225 }
ffbda80c 226 }
86e7b8d9 227 }
350d37d9 228
e42ce2b2 229 for (;;) {
230 for (qi=0; qi<qc; qi++) {
231 for (ti=0; ti<tc; ti++) {
232 mc= &mcs[qi*tc+ti];
233 mc->found= 0;
234 }
235 }
236 for (adns_forallqueries_begin(ads);
237 (qu= adns_forallqueries_next(ads,&mcr));
238 ) {
239 mc= mcr;
240 assert(qu == mc->qu);
241 assert(!mc->doneyet);
242 mc->found= 1;
243 }
244 mcw= 0;
245 for (qi=0; qi<qc; qi++) {
246 for (ti=0; ti<tc; ti++) {
247 mc= &mcs[qi*tc+ti];
248 if (mc->doneyet) continue;
249 assert(mc->found);
250 if (!mcw) mcw= mc;
251 }
252 }
253 if (!mcw) break;
669f9df5 254
e42ce2b2 255 mc= mcw;
256 qu= mcw->qu;
fc6a52ae 257
258 if (strchr(owninitflags,'p')) {
259 for (;;) {
669f9df5 260 r= adns_check(ads,&qu,&ans,&mcr);
fc6a52ae 261 if (r != EWOULDBLOCK) break;
262 for (;;) {
263 npollfds= npollfdsavail;
264 timeout= -1;
265 r= adns_beforepoll(ads, pollfds, &npollfds, &timeout, 0);
266 if (r != ERANGE) break;
267 pollfds= realloc(pollfds,sizeof(*pollfds)*npollfds);
268 if (!pollfds) failure_errno("realloc pollfds",errno);
269 npollfdsavail= npollfds;
270 }
271 if (r) failure_errno("beforepoll",r);
272 r= poll(pollfds,npollfds,timeout);
273 if (r == -1) failure_errno("poll",errno);
274 adns_afterpoll(ads,pollfds, r?npollfds:0, 0);
275 }
276 } else {
669f9df5 277 r= adns_wait(ads,&qu,&ans,&mcr);
fc6a52ae 278 }
279 if (r) failure_errno("wait/check",r);
4353a5c4 280
669f9df5 281 assert(mcr==mc);
e42ce2b2 282 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
669f9df5 283
73dba56e 284 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
285
ffbda80c 286 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
1aa0213d 287 fprintf(stdout, "%s flags %d type ",domain,qflags);
e062dcae 288 dumptype(ri,rrtn,fmtn);
9da4a044 289 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
290 ownflags[0] ? " ownflags=" : "", ownflags,
fc6a52ae 291 strchr(ownflags,'a')
292 ? adns_errabbrev(ans->status)
9da4a044 293 : adns_strerror(ans->status),
22181a31 294 ans->nrrs,
295 ans->cname ? ans->cname : "$",
296 ans->owner ? ans->owner : "$",
73dba56e 297 (long)ans->expires - (long)now.tv_sec);
ffbda80c 298 if (ans->nrrs) {
299 assert(!ri);
300 for (i=0; i<ans->nrrs; i++) {
e3d42241 301 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
9da4a044 302 if (r) failure_status("info",r);
7d251914 303 fprintf(stdout," %s\n",show);
ffbda80c 304 free(show);
305 }
86e7b8d9 306 }
ffbda80c 307 free(ans);
e42ce2b2 308
309 mc->doneyet= 1;
86e7b8d9 310 }
4353a5c4 311
669f9df5 312 free(mcs);
9ec44266 313 adns_finish(ads);
314
964344fc 315 exit(0);
316}