3 * - simple test program, not part of the library
7 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
9 * It is part of adns, which is
10 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
11 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
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)
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,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 # define OUTPUTSTREAM stdout
50 static struct myctx *mcs;
51 static adns_state ads;
52 static adns_rrtype *types_a;
54 static void quitnow(int rc) NONRETURNING;
55 static void quitnow(int rc) {
58 if (ads) adns_finish(ads);
65 int poll(struct pollfd *ufds, int nfds, int timeout) {
66 fputs("poll(2) not supported on this system\n",stderr);
69 #define adns_beforepoll(a,b,c,d,e) 0
70 #define adns_afterpoll(a,b,c,d) 0
73 static void failure_status(const char *what, adns_status st) NONRETURNING;
74 static void failure_status(const char *what, adns_status st) {
75 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
79 static void failure_errno(const char *what, int errnoval) NONRETURNING;
80 static void failure_errno(const char *what, int errnoval) {
81 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
85 static void usageerr(const char *why) NONRETURNING;
86 static void usageerr(const char *why) {
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"
100 " 5 operation not supported on this system\n",
105 static const adns_rrtype defaulttypes[]= {
127 static 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) : "");
133 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
134 char *ownflags, int ownflags_l) {
138 qf= strtoul(fdom,&ep,0);
139 if (*ep == ',' && strchr(ep,'/')) {
142 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
146 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
147 else { *dom_r= ep+1; *qf_r= qf; }
151 static int consistsof(const char *string, const char *accept) {
152 return strspn(string,accept) == strlen(string);
155 int main(int argc, char *const *argv) {
157 struct myctx *mc, *mcw;
160 const char *initstring, *rrtn, *fmtn;
161 const char *const *fdomlist, *domain;
163 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
166 const adns_rrtype *types;
170 const char *initflags, *owninitflags;
172 if (argv[0] && argv[1] && argv[1][0] == '-') {
173 initflags= argv[1]+1;
178 if (argv[0] && argv[1] && argv[1][0] == '/') {
179 initstring= argv[1]+1;
185 initflagsnum= strtoul(initflags,&ep,0);
188 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
192 usageerr("bad <initflagsnum>[,<owninitflags>]");
195 if (argv[0] && argv[1] && argv[1][0] == ':') {
196 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
198 types_a= malloc(sizeof(*types_a)*(tc+1));
199 if (!types_a) { perror("malloc types"); quitnow(3); }
200 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
201 types_a[ti]= strtoul(cp,&cp,10);
203 if (ch != ',') usageerr("unexpected char (not comma) in or between types");
207 types_a[ti]= adns_r_none;
215 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
216 fdomlist= (const char *const*)argv+1;
218 for (qc=0; fdomlist[qc]; qc++);
219 for (tc=0; types[tc] != adns_r_none; tc++);
220 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
221 if (!mcs) { perror("malloc mcs"); quitnow(3); }
224 r= adns_init_strcfg(&ads,
225 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
230 (adns_if_debug|adns_if_noautosys)^initflagsnum,
233 if (r) failure_errno("init",r);
235 for (qi=0; qi<qc; qi++) {
236 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
237 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
238 for (ti=0; ti<tc; ti++) {
241 mc->fdom= fdomlist[qi];
243 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
244 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
246 fprintf(stdout," not implemented\n");
250 failure_errno("submit",r);
252 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
254 dumptype(ri,rrtn,fmtn);
255 fprintf(stdout," submitted\n");
261 for (qi=0; qi<qc; qi++) {
262 for (ti=0; ti<tc; ti++) {
267 for (adns_forallqueries_begin(ads);
268 (qu= adns_forallqueries_next(ads,&mcr));
271 assert(qu == mc->qu);
272 assert(!mc->doneyet);
276 for (qi=0; qi<qc; qi++) {
277 for (ti=0; ti<tc; ti++) {
279 if (mc->doneyet) continue;
286 if (strchr(owninitflags,'s')) {
294 if (strchr(owninitflags,'p')) {
295 r= adns_wait_poll(ads,&qu,&ans,&mcr);
297 r= adns_wait(ads,&qu,&ans,&mcr);
299 if (r) failure_errno("wait/check",r);
301 if (mc) assert(mcr==mc);
304 assert(!mc->doneyet);
306 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
308 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
310 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
311 fprintf(stdout, "%s flags %d type ",domain,qflags);
312 dumptype(ri,rrtn,fmtn);
313 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
314 ownflags[0] ? " ownflags=" : "", ownflags,
316 ? adns_errabbrev(ans->status)
317 : adns_strerror(ans->status),
319 ans->cname ? ans->cname : "$",
320 ans->owner ? ans->owner : "$",
321 (long)ans->expires - (long)now.tv_sec);
324 for (i=0; i<ans->nrrs; i++) {
325 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
326 if (ri) failure_status("info",ri);
327 fprintf(stdout," %s\n",show);