debug message about unexpected server removed.
[adns] / client / adnstest.c
CommitLineData
e576be50 1/*
2 * dtest.c
3 * - simple test program, not part of the library
4 */
5/*
a719a4be 6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
e576be50 7 *
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)
11 * any later version.
12 *
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.
17 *
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.
21 */
964344fc 22
23#include <stdio.h>
73dba56e 24#include <sys/time.h>
350d37d9 25#include <unistd.h>
86e7b8d9 26#include <assert.h>
27#include <stdlib.h>
9da4a044 28#include <string.h>
fc6a52ae 29#include <errno.h>
16cf5367 30
31#include "adns.h"
32
33#include "config.h"
964344fc 34
7d251914 35#ifndef OUTPUTSTREAM
36# define OUTPUTSTREAM stdout
37#endif
38
16cf5367 39#ifndef HAVE_POLL
40#undef poll
41int poll(struct pollfd *ufds, int nfds, int timeout) {
42 fputs("poll(2) not supported on this system\n",stderr);
43 exit(3);
44}
45#define adns_beforepoll(a,b,c,d,e) 0
46#define adns_afterpoll(a,b,c,d) 0
47#endif
964344fc 48
9da4a044 49static void failure_status(const char *what, adns_status st) {
86e7b8d9 50 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
51 exit(2);
52}
53
9da4a044 54static void failure_errno(const char *what, int errnoval) {
55 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
56 exit(2);
57}
58
86e7b8d9 59static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
60
ffbda80c 61static const adns_rrtype defaulttypes[]= {
62 adns_r_a,
63 adns_r_ns_raw,
64 adns_r_cname,
9ec44266 65 adns_r_soa_raw,
ffbda80c 66 adns_r_ptr_raw,
9ec44266 67 adns_r_hinfo,
e062dcae 68 adns_r_mx_raw,
1b644113 69 adns_r_txt,
1dfe95d8 70 adns_r_rp_raw,
9ec44266 71
828d89bd 72 adns_r_addr,
1dfe95d8 73 adns_r_ns,
a6536d8b 74 adns_r_ptr,
9ec44266 75 adns_r_mx,
76
77 adns_r_soa,
78 adns_r_rp,
79
ffbda80c 80 adns_r_none
81};
82
e062dcae 83static 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) : "");
87}
88
9da4a044 89static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
90 char *ownflags, int ownflags_l) {
5476fe64 91 int qf;
92 char *ep;
93
94 qf= strtoul(fdom,&ep,0);
9da4a044 95 if (*ep == ',' && strchr(ep,'/')) {
96 ep++;
97 while (*ep != '/') {
98 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); exit(3); }
99 *ownflags++= *ep++;
100 }
101 }
5476fe64 102 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
103 else { *dom_r= ep+1; *qf_r= qf; }
9da4a044 104 *ownflags= 0;
5476fe64 105}
106
fc6a52ae 107static int consistsof(const char *string, const char *accept) {
108 return strspn(string,accept) == strlen(string);
109}
110
e062dcae 111int main(int argc, char *const *argv) {
669f9df5 112 struct myctx {
113 adns_query qu;
e42ce2b2 114 int doneyet, found;
115 const char *fdom;
669f9df5 116 };
117
964344fc 118 adns_state ads;
669f9df5 119 adns_query qu;
e42ce2b2 120 struct myctx *mcs, *mc, *mcw;
669f9df5 121 void *mcr;
4353a5c4 122 adns_answer *ans;
12c5b204 123 const char *initstring, *rrtn, *fmtn;
5476fe64 124 const char *const *fdomlist, *domain;
e062dcae 125 char *show, *cp;
fc6a52ae 126 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum, npollfds, npollfdsavail, timeout;
127 struct pollfd *pollfds;
86e7b8d9 128 adns_status r, ri;
ffbda80c 129 const adns_rrtype *types;
73dba56e 130 struct timeval now;
e062dcae 131 adns_rrtype *types_a;
9da4a044 132 char ownflags[10];
fc6a52ae 133 char *ep;
134 const char *initflags, *owninitflags;
86e7b8d9 135
fc6a52ae 136 if (argv[0] && argv[1] && argv[1][0] == '-') {
137 initflags= argv[1]+1;
138 argv++;
139 } else {
140 initflags= "";
141 }
12c5b204 142 if (argv[0] && argv[1] && argv[1][0] == '/') {
143 initstring= argv[1]+1;
144 argv++;
145 } else {
146 initstring= 0;
147 }
fc6a52ae 148
149 initflagsnum= strtoul(initflags,&ep,0);
150 if (*ep == ',') {
151 owninitflags= ep+1;
8ce38e76 152 if (!consistsof(owninitflags,"ps")) {
fc6a52ae 153 fputs("unknown owninitflag\n",stderr);
154 exit(4);
155 }
156 } else if (!*ep) {
157 owninitflags= "";
158 } else {
159 fputs("bad <initflagsnum>[,<owninitflags>]\n",stderr);
160 exit(4);
161 }
12c5b204 162
e062dcae 163 if (argv[0] && argv[1] && argv[1][0] == ':') {
164 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
165 if (ch==',') tc++;
e3d42241 166 types_a= malloc(sizeof(*types_a)*(tc+1));
e062dcae 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);
170 if ((ch= *cp)) {
a6536d8b 171 if (ch != ',') {
fc6a52ae 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"
8ce38e76 176 " s use adns_wait with specified query, instead of 0\n"
fc6a52ae 177 "queryflags: a print status abbrevs instead of strings\n",
178 stderr);
e062dcae 179 exit(4);
180 }
181 cp++;
182 }
183 }
e3d42241 184 *cp++= adns_r_none;
e062dcae 185 types= types_a;
186 argv++;
187 } else {
188 types= defaulttypes;
189 }
190
5476fe64 191 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
192 else fdomlist= defaultargv;
ffbda80c 193
5476fe64 194 for (qc=0; fdomlist[qc]; qc++);
ffbda80c 195 for (tc=0; types[tc] != adns_r_none; tc++);
669f9df5 196 mcs= malloc(sizeof(*mcs)*qc*tc);
197 if (!mcs) { perror("malloc mcs"); exit(3); }
964344fc 198
12c5b204 199 if (initstring) {
fc6a52ae 200 r= adns_init_strcfg(&ads,
201 (adns_if_debug|adns_if_noautosys)^initflagsnum,
202 stdout,initstring);
12c5b204 203 } else {
fc6a52ae 204 r= adns_init(&ads,
205 (adns_if_debug|adns_if_noautosys)^initflagsnum,
206 0);
12c5b204 207 }
9da4a044 208 if (r) failure_errno("init",r);
86e7b8d9 209
fc6a52ae 210 npollfdsavail= 0;
211 pollfds= 0;
212
86e7b8d9 213 for (qi=0; qi<qc; qi++) {
9da4a044 214 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
fc6a52ae 215 if (!consistsof(ownflags,"a")) {
216 fputs("unknown ownqueryflag\n",stderr);
217 exit(4);
218 }
ffbda80c 219 for (ti=0; ti<tc; ti++) {
669f9df5 220 mc= &mcs[qi*tc+ti];
221 mc->doneyet= 0;
e42ce2b2 222 mc->fdom= fdomlist[qi];
669f9df5 223
1aa0213d 224 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
669f9df5 225 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
ea1e31e3 226 if (r == adns_s_unknownrrtype) {
916c16ca 227 fprintf(stdout," not implemented\n");
669f9df5 228 mc->qu= 0;
229 mc->doneyet= 1;
916c16ca 230 } else if (r) {
9da4a044 231 failure_errno("submit",r);
916c16ca 232 } else {
e062dcae 233 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 234 putc(' ',stdout);
e062dcae 235 dumptype(ri,rrtn,fmtn);
916c16ca 236 fprintf(stdout," submitted\n");
237 }
ffbda80c 238 }
86e7b8d9 239 }
350d37d9 240
e42ce2b2 241 for (;;) {
242 for (qi=0; qi<qc; qi++) {
243 for (ti=0; ti<tc; ti++) {
244 mc= &mcs[qi*tc+ti];
245 mc->found= 0;
246 }
247 }
248 for (adns_forallqueries_begin(ads);
249 (qu= adns_forallqueries_next(ads,&mcr));
250 ) {
251 mc= mcr;
252 assert(qu == mc->qu);
253 assert(!mc->doneyet);
254 mc->found= 1;
255 }
256 mcw= 0;
257 for (qi=0; qi<qc; qi++) {
258 for (ti=0; ti<tc; ti++) {
259 mc= &mcs[qi*tc+ti];
260 if (mc->doneyet) continue;
261 assert(mc->found);
262 if (!mcw) mcw= mc;
263 }
264 }
265 if (!mcw) break;
669f9df5 266
8ce38e76 267 if (strchr(owninitflags,'s')) {
268 qu= mcw->qu;
269 mc= mcw;
270 } else {
271 qu= 0;
272 mc= 0;
273 }
fc6a52ae 274
207130d4 275 if (strchr(owninitflags,'p')) {
276 for (;;) {
277 r= adns_check(ads,&qu,&ans,&mcr);
278 if (r != EWOULDBLOCK) break;
fc6a52ae 279 for (;;) {
207130d4 280 npollfds= npollfdsavail;
281 timeout= -1;
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;
fc6a52ae 287 }
207130d4 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);
fc6a52ae 292 }
207130d4 293 } else {
294 r= adns_wait(ads,&qu,&ans,&mcr);
295 }
296 if (r) failure_errno("wait/check",r);
297
8ce38e76 298 if (mc) assert(mcr==mc);
299 else mc= mcr;
300 assert(qu==mc->qu);
301 assert(!mc->doneyet);
302
207130d4 303 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
669f9df5 304
207130d4 305 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
73dba56e 306
207130d4 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,
312 strchr(ownflags,'a')
313 ? adns_errabbrev(ans->status)
314 : adns_strerror(ans->status),
315 ans->nrrs,
316 ans->cname ? ans->cname : "$",
317 ans->owner ? ans->owner : "$",
318 (long)ans->expires - (long)now.tv_sec);
319 if (ans->nrrs) {
320 assert(!ri);
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);
325 free(show);
86e7b8d9 326 }
207130d4 327 }
328 free(ans);
e42ce2b2 329
207130d4 330 mc->doneyet= 1;
86e7b8d9 331 }
4353a5c4 332
669f9df5 333 free(mcs);
9ec44266 334 adns_finish(ads);
335
964344fc 336 exit(0);
337}