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.
33 # define OUTPUTSTREAM stdout
38 static void failure_status(const char *what, adns_status st) {
39 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
43 static void failure_errno(const char *what, int errnoval) {
44 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
48 static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
50 static const adns_rrtype defaulttypes[]= {
72 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
73 fprintf(stdout, "%s(%s)%s%s",
74 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
75 ri ? " " : "", ri ? adns_strerror(ri) : "");
78 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
79 char *ownflags, int ownflags_l) {
83 qf= strtoul(fdom,&ep,0);
84 if (*ep == ',' && strchr(ep,'/')) {
87 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); exit(3); }
91 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
92 else { *dom_r= ep+1; *qf_r= qf; }
96 static int consistsof(const char *string, const char *accept) {
97 return strspn(string,accept) == strlen(string);
100 int main(int argc, char *const *argv) {
108 struct myctx *mcs, *mc;
111 const char *initstring, *rrtn, *fmtn;
112 const char *const *fdomlist, *domain;
114 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum, npollfds, npollfdsavail, timeout;
115 struct pollfd *pollfds;
117 const adns_rrtype *types;
119 adns_rrtype *types_a;
122 const char *initflags, *owninitflags;
124 if (argv[0] && argv[1] && argv[1][0] == '-') {
125 initflags= argv[1]+1;
130 if (argv[0] && argv[1] && argv[1][0] == '/') {
131 initstring= argv[1]+1;
137 initflagsnum= strtoul(initflags,&ep,0);
140 if (!consistsof(owninitflags,"p")) {
141 fputs("unknown owninitflag\n",stderr);
147 fputs("bad <initflagsnum>[,<owninitflags>]\n",stderr);
151 if (argv[0] && argv[1] && argv[1][0] == ':') {
152 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
154 types_a= malloc(sizeof(*types_a)*(tc+1));
155 if (!types_a) { perror("malloc types"); exit(3); }
156 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
157 types_a[ti]= strtoul(cp,&cp,10);
160 fputs("usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
161 " [ :<typenum>,... ]\n"
162 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
163 "initflags: p use poll(2) instead of select(2)\n"
164 "queryflags: a print status abbrevs instead of strings\n",
178 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
179 else fdomlist= defaultargv;
181 for (qc=0; fdomlist[qc]; qc++);
182 for (tc=0; types[tc] != adns_r_none; tc++);
183 mcs= malloc(sizeof(*mcs)*qc*tc);
184 if (!mcs) { perror("malloc mcs"); exit(3); }
187 r= adns_init_strcfg(&ads,
188 (adns_if_debug|adns_if_noautosys)^initflagsnum,
192 (adns_if_debug|adns_if_noautosys)^initflagsnum,
195 if (r) failure_errno("init",r);
200 for (qi=0; qi<qc; qi++) {
201 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
202 if (!consistsof(ownflags,"a")) {
203 fputs("unknown ownqueryflag\n",stderr);
206 for (ti=0; ti<tc; ti++) {
210 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
211 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
212 if (r == adns_s_unknownrrtype) {
213 fprintf(stdout," not implemented\n");
217 failure_errno("submit",r);
219 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
221 dumptype(ri,rrtn,fmtn);
222 fprintf(stdout," submitted\n");
227 for (qi=0; qi<qc; qi++) {
228 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
230 for (ti=0; ti<tc; ti++) {
232 if (mc->doneyet) continue;
235 if (strchr(owninitflags,'p')) {
237 r= adns_check(ads,&qu,&ans,&mcr);
238 if (r != EWOULDBLOCK) break;
240 npollfds= npollfdsavail;
242 r= adns_beforepoll(ads, pollfds, &npollfds, &timeout, 0);
243 if (r != ERANGE) break;
244 pollfds= realloc(pollfds,sizeof(*pollfds)*npollfds);
245 if (!pollfds) failure_errno("realloc pollfds",errno);
246 npollfdsavail= npollfds;
248 if (r) failure_errno("beforepoll",r);
249 r= poll(pollfds,npollfds,timeout);
250 if (r == -1) failure_errno("poll",errno);
251 adns_afterpoll(ads,pollfds, r?npollfds:0, 0);
254 r= adns_wait(ads,&qu,&ans,&mcr);
256 if (r) failure_errno("wait/check",r);
260 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
262 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
263 fprintf(stdout, "%s flags %d type ",domain,qflags);
264 dumptype(ri,rrtn,fmtn);
265 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
266 ownflags[0] ? " ownflags=" : "", ownflags,
268 ? adns_errabbrev(ans->status)
269 : adns_strerror(ans->status),
271 ans->cname ? ans->cname : "$",
272 ans->owner ? ans->owner : "$",
273 (long)ans->expires - (long)now.tv_sec);
276 for (i=0; i<ans->nrrs; i++) {
277 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
278 if (r) failure_status("info",r);
279 fprintf(stdout," %s\n",show);