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