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