3 * - simple test program, not part of the library
6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 # define OUTPUTSTREAM stdout
41 int poll(struct pollfd *ufds, int nfds, int timeout) {
42 fputs("poll(2) not supported on this system\n",stderr);
45 #define adns_beforepoll(a,b,c,d,e) 0
46 #define adns_afterpoll(a,b,c,d) 0
49 static void failure_status(const char *what, adns_status st) {
50 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
54 static void failure_errno(const char *what, int errnoval) {
55 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
59 static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
61 static const adns_rrtype defaulttypes[]= {
83 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
84 fprintf(stdout, "%s(%s)%s%s",
85 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
86 ri ? " " : "", ri ? adns_strerror(ri) : "");
89 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
90 char *ownflags, int ownflags_l) {
94 qf= strtoul(fdom,&ep,0);
95 if (*ep == ',' && strchr(ep,'/')) {
98 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); exit(3); }
102 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
103 else { *dom_r= ep+1; *qf_r= qf; }
107 static int consistsof(const char *string, const char *accept) {
108 return strspn(string,accept) == strlen(string);
111 int main(int argc, char *const *argv) {
120 struct myctx *mcs, *mc, *mcw;
123 const char *initstring, *rrtn, *fmtn;
124 const char *const *fdomlist, *domain;
126 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum, npollfds, npollfdsavail, timeout;
127 struct pollfd *pollfds;
129 const adns_rrtype *types;
131 adns_rrtype *types_a;
134 const char *initflags, *owninitflags;
136 if (argv[0] && argv[1] && argv[1][0] == '-') {
137 initflags= argv[1]+1;
142 if (argv[0] && argv[1] && argv[1][0] == '/') {
143 initstring= argv[1]+1;
149 initflagsnum= strtoul(initflags,&ep,0);
152 if (!consistsof(owninitflags,"ps")) {
153 fputs("unknown owninitflag\n",stderr);
159 fputs("bad <initflagsnum>[,<owninitflags>]\n",stderr);
163 if (argv[0] && argv[1] && argv[1][0] == ':') {
164 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
166 types_a= malloc(sizeof(*types_a)*(tc+1));
167 if (!types_a) { perror("malloc types"); exit(3); }
168 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
169 types_a[ti]= strtoul(cp,&cp,10);
172 fputs("usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
173 " [ :<typenum>,... ]\n"
174 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
175 "initflags: p use poll(2) instead of select(2)\n"
176 " s use adns_wait with specified query, instead of 0\n"
177 "queryflags: a print status abbrevs instead of strings\n",
191 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
192 else fdomlist= defaultargv;
194 for (qc=0; fdomlist[qc]; qc++);
195 for (tc=0; types[tc] != adns_r_none; tc++);
196 mcs= malloc(sizeof(*mcs)*qc*tc);
197 if (!mcs) { perror("malloc mcs"); exit(3); }
200 r= adns_init_strcfg(&ads,
201 (adns_if_debug|adns_if_noautosys)^initflagsnum,
205 (adns_if_debug|adns_if_noautosys)^initflagsnum,
208 if (r) failure_errno("init",r);
213 for (qi=0; qi<qc; qi++) {
214 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
215 if (!consistsof(ownflags,"a")) {
216 fputs("unknown ownqueryflag\n",stderr);
219 for (ti=0; ti<tc; ti++) {
222 mc->fdom= fdomlist[qi];
224 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
225 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
226 if (r == adns_s_unknownrrtype) {
227 fprintf(stdout," not implemented\n");
231 failure_errno("submit",r);
233 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
235 dumptype(ri,rrtn,fmtn);
236 fprintf(stdout," submitted\n");
242 for (qi=0; qi<qc; qi++) {
243 for (ti=0; ti<tc; ti++) {
248 for (adns_forallqueries_begin(ads);
249 (qu= adns_forallqueries_next(ads,&mcr));
252 assert(qu == mc->qu);
253 assert(!mc->doneyet);
257 for (qi=0; qi<qc; qi++) {
258 for (ti=0; ti<tc; ti++) {
260 if (mc->doneyet) continue;
267 if (strchr(owninitflags,'s')) {
275 if (strchr(owninitflags,'p')) {
277 r= adns_check(ads,&qu,&ans,&mcr);
278 if (r != EWOULDBLOCK) break;
280 npollfds= npollfdsavail;
282 r= adns_beforepoll(ads, pollfds, &npollfds, &timeout, 0);
283 if (r != ERANGE) break;
284 pollfds= realloc(pollfds,sizeof(*pollfds)*npollfds);
285 if (!pollfds) failure_errno("realloc pollfds",errno);
286 npollfdsavail= npollfds;
288 if (r) failure_errno("beforepoll",r);
289 r= poll(pollfds,npollfds,timeout);
290 if (r == -1) failure_errno("poll",errno);
291 adns_afterpoll(ads,pollfds, r?npollfds:0, 0);
294 r= adns_wait(ads,&qu,&ans,&mcr);
296 if (r) failure_errno("wait/check",r);
298 if (mc) assert(mcr==mc);
301 assert(!mc->doneyet);
303 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
305 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
307 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
308 fprintf(stdout, "%s flags %d type ",domain,qflags);
309 dumptype(ri,rrtn,fmtn);
310 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
311 ownflags[0] ? " ownflags=" : "", ownflags,
313 ? adns_errabbrev(ans->status)
314 : adns_strerror(ans->status),
316 ans->cname ? ans->cname : "$",
317 ans->owner ? ans->owner : "$",
318 (long)ans->expires - (long)now.tv_sec);
321 for (i=0; i<ans->nrrs; i++) {
322 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
323 if (r) failure_status("info",r);
324 fprintf(stdout," %s\n",show);