adnstest: Support typenum being 0xXX|DDD
[adns] / client / adnstest.c
CommitLineData
e576be50 1/*
b0f83da6 2 * adnstest.c
e576be50 3 * - simple test program, not part of the library
4 */
5/*
ae8cc977 6 * This file is part of adns, which is
26e1c3d6 7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
17cd4f48 8 * Copyright (C) 2014 Mark Wooding
ae8cc977 9 * Copyright (C) 1999-2000,2003,2006 Tony Finch
10 * Copyright (C) 1991 Massachusetts Institute of Technology
11 * (See the file INSTALL for full details.)
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
7f8bbe29 15 * the Free Software Foundation; either version 3, or (at your option)
e576be50 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
8c09a4c6 24 * along with this program; if not, write to the Free Software Foundation.
e576be50 25 */
964344fc 26
27#include <stdio.h>
73dba56e 28#include <sys/time.h>
350d37d9 29#include <unistd.h>
86e7b8d9 30#include <assert.h>
31#include <stdlib.h>
9da4a044 32#include <string.h>
fc6a52ae 33#include <errno.h>
16cf5367 34
138722f0 35#define ADNS_FEATURE_MANYAF
16cf5367 36#include "config.h"
5a0be244 37#include "adns.h"
964344fc 38
0ebff22d 39#ifdef ADNS_REGRESS_TEST
40# include "hredirect.h"
7d251914 41#endif
42
914a5ff5 43struct myctx {
44 adns_query qu;
45 int doneyet, found;
46 const char *fdom;
47};
48
49static struct myctx *mcs;
50static adns_state ads;
51static adns_rrtype *types_a;
52
53static void quitnow(int rc) NONRETURNING;
54static void quitnow(int rc) {
55 free(mcs);
56 free(types_a);
57 if (ads) adns_finish(ads);
58
59 exit(rc);
60}
61
16cf5367 62#ifndef HAVE_POLL
63#undef poll
64int poll(struct pollfd *ufds, int nfds, int timeout) {
65 fputs("poll(2) not supported on this system\n",stderr);
914a5ff5 66 quitnow(5);
16cf5367 67}
68#define adns_beforepoll(a,b,c,d,e) 0
69#define adns_afterpoll(a,b,c,d) 0
70#endif
964344fc 71
2345cc9b 72static void failure_status(const char *what, adns_status st) NONRETURNING;
9da4a044 73static void failure_status(const char *what, adns_status st) {
86e7b8d9 74 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
914a5ff5 75 quitnow(2);
86e7b8d9 76}
77
2345cc9b 78static void failure_errno(const char *what, int errnoval) NONRETURNING;
9da4a044 79static void failure_errno(const char *what, int errnoval) {
fa1b90e5 80 switch (errnoval) {
81#define CE(e) \
82 case e: fprintf(stderr,"adns failure: %s: errno=" #e "\n",what); break
83 CE(EINVAL);
84 CE(EINTR);
85 CE(ESRCH);
86 CE(EAGAIN);
87 CE(ENOSYS);
88 CE(ERANGE);
89#undef CE
90 default: fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); break;
91 }
914a5ff5 92 quitnow(2);
9da4a044 93}
94
2345cc9b 95static void usageerr(const char *why) NONRETURNING;
96static void usageerr(const char *why) {
97 fprintf(stderr,
98 "bad usage: %s\n"
99 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
100 " [ :<typenum>,... ]\n"
101 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
102 "initflags: p use poll(2) instead of select(2)\n"
103 " s use adns_wait with specified query, instead of 0\n"
104 "queryflags: a print status abbrevs instead of strings\n"
717d4481 105 "typenum: may be 0x<hex>|<dec>, or 0x<hex> or <dec>\n"
2345cc9b 106 "exit status: 0 ok (though some queries may have failed)\n"
107 " 1 used by test harness to indicate test failed\n"
108 " 2 unable to submit or init or some such\n"
109 " 3 unexpected failure\n"
110 " 4 usage error\n"
111 " 5 operation not supported on this system\n",
112 why);
914a5ff5 113 quitnow(4);
2345cc9b 114}
86e7b8d9 115
ffbda80c 116static const adns_rrtype defaulttypes[]= {
117 adns_r_a,
118 adns_r_ns_raw,
119 adns_r_cname,
9ec44266 120 adns_r_soa_raw,
ffbda80c 121 adns_r_ptr_raw,
9ec44266 122 adns_r_hinfo,
e062dcae 123 adns_r_mx_raw,
1b644113 124 adns_r_txt,
1dfe95d8 125 adns_r_rp_raw,
9ec44266 126
828d89bd 127 adns_r_addr,
1dfe95d8 128 adns_r_ns,
a6536d8b 129 adns_r_ptr,
9ec44266 130 adns_r_mx,
131
132 adns_r_soa,
133 adns_r_rp,
134
ffbda80c 135 adns_r_none
136};
137
e062dcae 138static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
139 fprintf(stdout, "%s(%s)%s%s",
db540ace 140 (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
e062dcae 141 ri ? " " : "", ri ? adns_strerror(ri) : "");
142}
143
9da4a044 144static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
145 char *ownflags, int ownflags_l) {
5476fe64 146 int qf;
147 char *ep;
148
149 qf= strtoul(fdom,&ep,0);
9da4a044 150 if (*ep == ',' && strchr(ep,'/')) {
151 ep++;
152 while (*ep != '/') {
914a5ff5 153 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
9da4a044 154 *ownflags++= *ep++;
155 }
156 }
5476fe64 157 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
158 else { *dom_r= ep+1; *qf_r= qf; }
9da4a044 159 *ownflags= 0;
5476fe64 160}
161
fc6a52ae 162static int consistsof(const char *string, const char *accept) {
163 return strspn(string,accept) == strlen(string);
164}
165
e062dcae 166int main(int argc, char *const *argv) {
669f9df5 167 adns_query qu;
914a5ff5 168 struct myctx *mc, *mcw;
669f9df5 169 void *mcr;
4353a5c4 170 adns_answer *ans;
12c5b204 171 const char *initstring, *rrtn, *fmtn;
5476fe64 172 const char *const *fdomlist, *domain;
e062dcae 173 char *show, *cp;
6d682204 174 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
636b69b1 175 adns_status ri;
176 int r;
ffbda80c 177 const adns_rrtype *types;
73dba56e 178 struct timeval now;
9da4a044 179 char ownflags[10];
fc6a52ae 180 char *ep;
181 const char *initflags, *owninitflags;
86e7b8d9 182
fc6a52ae 183 if (argv[0] && argv[1] && argv[1][0] == '-') {
184 initflags= argv[1]+1;
185 argv++;
186 } else {
187 initflags= "";
188 }
12c5b204 189 if (argv[0] && argv[1] && argv[1][0] == '/') {
190 initstring= argv[1]+1;
191 argv++;
192 } else {
193 initstring= 0;
194 }
fc6a52ae 195
196 initflagsnum= strtoul(initflags,&ep,0);
197 if (*ep == ',') {
198 owninitflags= ep+1;
2345cc9b 199 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
fc6a52ae 200 } else if (!*ep) {
201 owninitflags= "";
202 } else {
2345cc9b 203 usageerr("bad <initflagsnum>[,<owninitflags>]");
fc6a52ae 204 }
12c5b204 205
e062dcae 206 if (argv[0] && argv[1] && argv[1][0] == ':') {
207 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
208 if (ch==',') tc++;
e3d42241 209 types_a= malloc(sizeof(*types_a)*(tc+1));
914a5ff5 210 if (!types_a) { perror("malloc types"); quitnow(3); }
717d4481
IJ
211 for (cp= argv[1]+1, ti=0; ti<tc; ) {
212 types_a[ti]= 0;
213 for (;;) {
214 types_a[ti] |= strtoul(cp,&cp,0);
215 ch= *cp;
216 if (!ch) break;
e062dcae 217 cp++;
717d4481
IJ
218 if (ch=='|') continue;
219 if (ch==',') break;
220 usageerr("unexpected char (not comma) in or between types");
e062dcae 221 }
717d4481
IJ
222 ti++;
223 if (!ch) break;
e062dcae 224 }
914a5ff5 225 types_a[ti]= adns_r_none;
e062dcae 226 types= types_a;
227 argv++;
228 } else {
914a5ff5 229 types_a= 0;
e062dcae 230 types= defaulttypes;
231 }
232
2345cc9b 233 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
234 fdomlist= (const char *const*)argv+1;
ffbda80c 235
5476fe64 236 for (qc=0; fdomlist[qc]; qc++);
ffbda80c 237 for (tc=0; types[tc] != adns_r_none; tc++);
914a5ff5 238 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
239 if (!mcs) { perror("malloc mcs"); quitnow(3); }
964344fc 240
5a0be244 241 setvbuf(stdout,0,_IOLBF,0);
242
12c5b204 243 if (initstring) {
fc6a52ae 244 r= adns_init_strcfg(&ads,
3e2e5fab 245 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
246 ^initflagsnum,
fc6a52ae 247 stdout,initstring);
12c5b204 248 } else {
fc6a52ae 249 r= adns_init(&ads,
250 (adns_if_debug|adns_if_noautosys)^initflagsnum,
251 0);
12c5b204 252 }
9da4a044 253 if (r) failure_errno("init",r);
86e7b8d9 254
255 for (qi=0; qi<qc; qi++) {
9da4a044 256 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
2345cc9b 257 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
ffbda80c 258 for (ti=0; ti<tc; ti++) {
669f9df5 259 mc= &mcs[qi*tc+ti];
260 mc->doneyet= 0;
e42ce2b2 261 mc->fdom= fdomlist[qi];
669f9df5 262
d24b603f
MW
263 fprintf(stdout,"%s flags %d type %d",
264 domain,qflags,types[ti]&adns_rrt_reprmask);
669f9df5 265 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
636b69b1 266 if (r == ENOSYS) {
916c16ca 267 fprintf(stdout," not implemented\n");
669f9df5 268 mc->qu= 0;
269 mc->doneyet= 1;
916c16ca 270 } else if (r) {
9da4a044 271 failure_errno("submit",r);
916c16ca 272 } else {
e062dcae 273 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 274 putc(' ',stdout);
e062dcae 275 dumptype(ri,rrtn,fmtn);
916c16ca 276 fprintf(stdout," submitted\n");
277 }
ffbda80c 278 }
86e7b8d9 279 }
350d37d9 280
e42ce2b2 281 for (;;) {
282 for (qi=0; qi<qc; qi++) {
283 for (ti=0; ti<tc; ti++) {
284 mc= &mcs[qi*tc+ti];
285 mc->found= 0;
286 }
287 }
288 for (adns_forallqueries_begin(ads);
289 (qu= adns_forallqueries_next(ads,&mcr));
290 ) {
291 mc= mcr;
292 assert(qu == mc->qu);
293 assert(!mc->doneyet);
294 mc->found= 1;
295 }
296 mcw= 0;
297 for (qi=0; qi<qc; qi++) {
298 for (ti=0; ti<tc; ti++) {
299 mc= &mcs[qi*tc+ti];
300 if (mc->doneyet) continue;
301 assert(mc->found);
302 if (!mcw) mcw= mc;
303 }
304 }
305 if (!mcw) break;
669f9df5 306
8ce38e76 307 if (strchr(owninitflags,'s')) {
308 qu= mcw->qu;
309 mc= mcw;
310 } else {
311 qu= 0;
312 mc= 0;
313 }
fc6a52ae 314
207130d4 315 if (strchr(owninitflags,'p')) {
940356bd 316 r= adns_wait_poll(ads,&qu,&ans,&mcr);
207130d4 317 } else {
318 r= adns_wait(ads,&qu,&ans,&mcr);
319 }
320 if (r) failure_errno("wait/check",r);
321
8ce38e76 322 if (mc) assert(mcr==mc);
323 else mc= mcr;
324 assert(qu==mc->qu);
325 assert(!mc->doneyet);
326
207130d4 327 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
669f9df5 328
914a5ff5 329 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
73dba56e 330
207130d4 331 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
332 fprintf(stdout, "%s flags %d type ",domain,qflags);
333 dumptype(ri,rrtn,fmtn);
334 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
335 ownflags[0] ? " ownflags=" : "", ownflags,
336 strchr(ownflags,'a')
337 ? adns_errabbrev(ans->status)
338 : adns_strerror(ans->status),
339 ans->nrrs,
340 ans->cname ? ans->cname : "$",
341 ans->owner ? ans->owner : "$",
342 (long)ans->expires - (long)now.tv_sec);
343 if (ans->nrrs) {
344 assert(!ri);
345 for (i=0; i<ans->nrrs; i++) {
636b69b1 346 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
347 if (ri) failure_status("info",ri);
207130d4 348 fprintf(stdout," %s\n",show);
349 free(show);
86e7b8d9 350 }
207130d4 351 }
352 free(ans);
e42ce2b2 353
207130d4 354 mc->doneyet= 1;
86e7b8d9 355 }
4353a5c4 356
914a5ff5 357 quitnow(0);
964344fc 358}