3 * - simple test program, not part of the library
6 * This file is part of adns, which is
7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
8 * Copyright (C) 2014 Mark Wooding
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.)
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 3, or (at your option)
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.
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.
35 #define ADNS_FEATURE_MANYAF
39 #ifdef ADNS_REGRESS_TEST
40 # include "hredirect.h"
49 static struct myctx *mcs;
50 static adns_state ads;
51 static adns_rrtype *types_a;
53 static void quitnow(int rc) NONRETURNING;
54 static void quitnow(int rc) {
57 if (ads) adns_finish(ads);
64 int poll(struct pollfd *ufds, int nfds, int timeout) {
65 fputs("poll(2) not supported on this system\n",stderr);
68 #define adns_beforepoll(a,b,c,d,e) 0
69 #define adns_afterpoll(a,b,c,d) 0
72 static void failure_status(const char *what, adns_status st) NONRETURNING;
73 static void failure_status(const char *what, adns_status st) {
74 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
78 static void failure_errno(const char *what, int errnoval) NONRETURNING;
79 static void failure_errno(const char *what, int errnoval) {
82 case e: fprintf(stderr,"adns failure: %s: errno=" #e "\n",what); break
90 default: fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); break;
95 static void usageerr(const char *why) NONRETURNING;
96 static void usageerr(const char *why) {
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"
105 "typenum: may be 0x<hex>|<dec>, or 0x<hex> or <dec>\n"
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"
111 " 5 operation not supported on this system\n",
116 static const adns_rrtype defaulttypes[]= {
138 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
139 fprintf(stdout, "%s(%s)%s%s",
140 (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
141 ri ? " " : "", ri ? adns_strerror(ri) : "");
144 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
145 char *ownflags, int ownflags_l) {
149 qf= strtoul(fdom,&ep,0);
150 if (*ep == ',' && strchr(ep,'/')) {
153 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
157 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
158 else { *dom_r= ep+1; *qf_r= qf; }
162 static int consistsof(const char *string, const char *accept) {
163 return strspn(string,accept) == strlen(string);
166 int main(int argc, char *const *argv) {
168 struct myctx *mc, *mcw;
171 const char *initstring, *rrtn, *fmtn;
172 const char *const *fdomlist, *domain;
174 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
177 const adns_rrtype *types;
181 const char *initflags, *owninitflags;
183 if (argv[0] && argv[1] && argv[1][0] == '-') {
184 initflags= argv[1]+1;
189 if (argv[0] && argv[1] && argv[1][0] == '/') {
190 initstring= argv[1]+1;
196 initflagsnum= strtoul(initflags,&ep,0);
199 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
203 usageerr("bad <initflagsnum>[,<owninitflags>]");
206 if (argv[0] && argv[1] && argv[1][0] == ':') {
207 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
209 types_a= malloc(sizeof(*types_a)*(tc+1));
210 if (!types_a) { perror("malloc types"); quitnow(3); }
211 for (cp= argv[1]+1, ti=0; ti<tc; ) {
214 types_a[ti] |= strtoul(cp,&cp,0);
218 if (ch=='|') continue;
220 usageerr("unexpected char (not comma) in or between types");
225 types_a[ti]= adns_r_none;
233 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
234 fdomlist= (const char *const*)argv+1;
236 for (qc=0; fdomlist[qc]; qc++);
237 for (tc=0; types[tc] != adns_r_none; tc++);
238 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
239 if (!mcs) { perror("malloc mcs"); quitnow(3); }
241 setvbuf(stdout,0,_IOLBF,0);
244 r= adns_init_strcfg(&ads,
245 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
250 (adns_if_debug|adns_if_noautosys)^initflagsnum,
253 if (r) failure_errno("init",r);
255 for (qi=0; qi<qc; qi++) {
256 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
257 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
258 for (ti=0; ti<tc; ti++) {
261 mc->fdom= fdomlist[qi];
263 fprintf(stdout,"%s flags %d type %d",
264 domain,qflags,types[ti]&adns_rrt_reprmask);
265 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
267 fprintf(stdout," not implemented\n");
271 failure_errno("submit",r);
273 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
275 dumptype(ri,rrtn,fmtn);
276 fprintf(stdout," submitted\n");
282 for (qi=0; qi<qc; qi++) {
283 for (ti=0; ti<tc; ti++) {
288 for (adns_forallqueries_begin(ads);
289 (qu= adns_forallqueries_next(ads,&mcr));
292 assert(qu == mc->qu);
293 assert(!mc->doneyet);
297 for (qi=0; qi<qc; qi++) {
298 for (ti=0; ti<tc; ti++) {
300 if (mc->doneyet) continue;
307 if (strchr(owninitflags,'s')) {
315 if (strchr(owninitflags,'p')) {
316 r= adns_wait_poll(ads,&qu,&ans,&mcr);
318 r= adns_wait(ads,&qu,&ans,&mcr);
320 if (r) failure_errno("wait/check",r);
322 if (mc) assert(mcr==mc);
325 assert(!mc->doneyet);
327 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
329 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
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,
337 ? adns_errabbrev(ans->status)
338 : adns_strerror(ans->status),
340 ans->cname ? ans->cname : "$",
341 ans->owner ? ans->owner : "$",
342 (long)ans->expires - (long)now.tv_sec);
345 for (i=0; i<ans->nrrs; i++) {
346 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
347 if (ri) failure_status("info",ri);
348 fprintf(stdout," %s\n",show);