3 * - simple test program, not part of the library
6 * This file is part of adns, which is Copyright Ian Jackson
7 * and contributors (see the file INSTALL for full details).
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation.
31 #define ADNS_FEATURE_MANYAF
35 #ifdef ADNS_REGRESS_TEST
36 # include "hredirect.h"
48 static struct myctx *mcs;
49 static adns_state ads;
50 static adns_rrtype *types_a;
52 static void quitnow(int rc) NONRETURNING;
53 static void quitnow(int rc) {
56 if (ads) adns_finish(ads);
63 int poll(struct pollfd *ufds, int nfds, int timeout) {
64 fputs("poll(2) not supported on this system\n",stderr);
67 #define adns_beforepoll(a,b,c,d,e) 0
68 #define adns_afterpoll(a,b,c,d) 0
71 static void failure_status(const char *what, adns_status st) NONRETURNING;
72 static void failure_status(const char *what, adns_status st) {
73 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
77 static void failure_errno(const char *what, int errnoval) NONRETURNING;
78 static void failure_errno(const char *what, int errnoval) {
81 case e: fprintf(stderr,"adns failure: %s: errno=" #e "\n",what); break
89 default: fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); break;
94 static void usageerr(const char *why) NONRETURNING;
95 static void usageerr(const char *why) {
98 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
99 " [ :<typenum>,... ]\n"
100 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
101 "initflags: p use poll(2) instead of select(2)\n"
102 " s use adns_wait with specified query, instead of 0\n"
103 "queryflags: a print status abbrevs instead of strings\n"
104 "typenum: may be 0x<hex>|<dec>, or 0x<hex> or <dec>\n"
105 "exit status: 0 ok (though some queries may have failed)\n"
106 " 1 used by test harness to indicate test failed\n"
107 " 2 unable to submit or init or some such\n"
108 " 3 unexpected failure\n"
110 " 5 operation not supported on this system\n",
115 static const adns_rrtype defaulttypes[]= {
137 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
138 fprintf(stdout, "%s(%s)%s%s",
139 (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
140 ri ? " " : "", ri ? adns_strerror(ri) : "");
143 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
144 char *ownflags, int ownflags_l) {
148 qf= strtoul(fdom,&ep,0);
149 if (*ep == ',' && strchr(ep,'/')) {
152 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
156 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
157 else { *dom_r= ep+1; *qf_r= qf; }
161 static int consistsof(const char *string, const char *accept) {
162 return strspn(string,accept) == strlen(string);
165 int main(int argc, char *const *argv) {
167 struct myctx *mc, *mcw;
170 const char *initstring, *rrtn, *fmtn;
171 const char *const *fdomlist, *domain;
173 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
176 const adns_rrtype *types;
180 const char *initflags, *owninitflags;
182 if (argv[0] && argv[1] && argv[1][0] == '-') {
183 initflags= argv[1]+1;
188 if (argv[0] && argv[1] && argv[1][0] == '/') {
189 initstring= argv[1]+1;
195 initflagsnum= strtoul(initflags,&ep,0);
198 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
202 usageerr("bad <initflagsnum>[,<owninitflags>]");
205 if (argv[0] && argv[1] && argv[1][0] == ':') {
206 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
208 types_a= malloc(sizeof(*types_a)*(tc+1));
209 if (!types_a) { perror("malloc types"); quitnow(3); }
210 for (cp= argv[1]+1, ti=0; ti<tc; ) {
213 types_a[ti] |= strtoul(cp,&cp,0);
217 if (ch=='|') continue;
219 usageerr("unexpected char (not comma) in or between types");
224 types_a[ti]= adns_r_none;
232 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
233 fdomlist= (const char *const*)argv+1;
235 for (qc=0; fdomlist[qc]; qc++);
236 for (tc=0; types[tc] != adns_r_none; tc++);
237 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
238 if (!mcs) { perror("malloc mcs"); quitnow(3); }
240 setvbuf(stdout,0,_IOLBF,0);
243 r= adns_init_strcfg(&ads,
244 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
249 (adns_if_debug|adns_if_noautosys)^initflagsnum,
252 if (r) failure_errno("init",r);
254 for (qi=0; qi<qc; qi++) {
255 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
256 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
257 for (ti=0; ti<tc; ti++) {
260 mc->fdom= fdomlist[qi];
262 fprintf(stdout,"%s flags %d type %d",
263 domain,qflags,types[ti]&adns_rrt_reprmask);
264 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
266 fprintf(stdout," not implemented\n");
270 failure_errno("submit",r);
272 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
274 dumptype(ri,rrtn,fmtn);
275 fprintf(stdout," submitted\n");
281 for (qi=0; qi<qc; qi++) {
282 for (ti=0; ti<tc; ti++) {
287 for (adns_forallqueries_begin(ads);
288 (qu= adns_forallqueries_next(ads,&mcr));
291 assert(qu == mc->qu);
292 assert(!mc->doneyet);
296 for (qi=0; qi<qc; qi++) {
297 for (ti=0; ti<tc; ti++) {
299 if (mc->doneyet) continue;
306 if (strchr(owninitflags,'s')) {
314 if (strchr(owninitflags,'p')) {
315 r= adns_wait_poll(ads,&qu,&ans,&mcr);
317 r= adns_wait(ads,&qu,&ans,&mcr);
319 if (r) failure_errno("wait/check",r);
321 if (mc) assert(mcr==mc);
324 assert(!mc->doneyet);
326 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
328 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
330 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
331 fprintf(stdout, "%s flags %d type ",domain,qflags);
332 dumptype(ri,rrtn,fmtn);
333 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
334 ownflags[0] ? " ownflags=" : "", ownflags,
336 ? adns_errabbrev(ans->status)
337 : adns_strerror(ans->status),
339 ans->cname ? ans->cname : "$",
340 ans->owner ? ans->owner : "$",
341 (long)ans->expires - (long)now.tv_sec);
344 for (i=0; i<ans->nrrs; i++) {
345 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
346 if (ri) failure_status("info",ri);
347 fprintf(stdout," %s\n",show);