Licensing: Update copyright dates for Ian Jackson
[adns] / client / adnstest.c
CommitLineData
e576be50 1/*
b0f83da6 2 * adnstest.c
e576be50 3 * - simple test program, not part of the library
4 */
5/*
ae8cc977 6 * This file is part of adns, which is
26e1c3d6 7 * Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson
ae8cc977 8 * Copyright (C) 1999-2000,2003,2006 Tony Finch
9 * Copyright (C) 1991 Massachusetts Institute of Technology
10 * (See the file INSTALL for full details.)
e576be50 11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
7f8bbe29 14 * the Free Software Foundation; either version 3, or (at your option)
e576be50 15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
8c09a4c6 23 * along with this program; if not, write to the Free Software Foundation.
e576be50 24 */
964344fc 25
26#include <stdio.h>
73dba56e 27#include <sys/time.h>
350d37d9 28#include <unistd.h>
86e7b8d9 29#include <assert.h>
30#include <stdlib.h>
9da4a044 31#include <string.h>
fc6a52ae 32#include <errno.h>
16cf5367 33
138722f0 34#define ADNS_FEATURE_MANYAF
16cf5367 35#include "config.h"
5a0be244 36#include "adns.h"
964344fc 37
0ebff22d 38#ifdef ADNS_REGRESS_TEST
39# include "hredirect.h"
7d251914 40#endif
41
914a5ff5 42struct myctx {
43 adns_query qu;
44 int doneyet, found;
45 const char *fdom;
46};
47
48static struct myctx *mcs;
49static adns_state ads;
50static adns_rrtype *types_a;
51
52static void quitnow(int rc) NONRETURNING;
53static void quitnow(int rc) {
54 free(mcs);
55 free(types_a);
56 if (ads) adns_finish(ads);
57
58 exit(rc);
59}
60
16cf5367 61#ifndef HAVE_POLL
62#undef poll
63int poll(struct pollfd *ufds, int nfds, int timeout) {
64 fputs("poll(2) not supported on this system\n",stderr);
914a5ff5 65 quitnow(5);
16cf5367 66}
67#define adns_beforepoll(a,b,c,d,e) 0
68#define adns_afterpoll(a,b,c,d) 0
69#endif
964344fc 70
2345cc9b 71static void failure_status(const char *what, adns_status st) NONRETURNING;
9da4a044 72static void failure_status(const char *what, adns_status st) {
86e7b8d9 73 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
914a5ff5 74 quitnow(2);
86e7b8d9 75}
76
2345cc9b 77static void failure_errno(const char *what, int errnoval) NONRETURNING;
9da4a044 78static void failure_errno(const char *what, int errnoval) {
fa1b90e5 79 switch (errnoval) {
80#define CE(e) \
81 case e: fprintf(stderr,"adns failure: %s: errno=" #e "\n",what); break
82 CE(EINVAL);
83 CE(EINTR);
84 CE(ESRCH);
85 CE(EAGAIN);
86 CE(ENOSYS);
87 CE(ERANGE);
88#undef CE
89 default: fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); break;
90 }
914a5ff5 91 quitnow(2);
9da4a044 92}
93
2345cc9b 94static void usageerr(const char *why) NONRETURNING;
95static void usageerr(const char *why) {
96 fprintf(stderr,
97 "bad usage: %s\n"
98 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
99 " [ :<typenum>,... ]\n"
100 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
101 "initflags: p use poll(2) instead of select(2)\n"
102 " s use adns_wait with specified query, instead of 0\n"
103 "queryflags: a print status abbrevs instead of strings\n"
104 "exit status: 0 ok (though some queries may have failed)\n"
105 " 1 used by test harness to indicate test failed\n"
106 " 2 unable to submit or init or some such\n"
107 " 3 unexpected failure\n"
108 " 4 usage error\n"
109 " 5 operation not supported on this system\n",
110 why);
914a5ff5 111 quitnow(4);
2345cc9b 112}
86e7b8d9 113
ffbda80c 114static const adns_rrtype defaulttypes[]= {
115 adns_r_a,
116 adns_r_ns_raw,
117 adns_r_cname,
9ec44266 118 adns_r_soa_raw,
ffbda80c 119 adns_r_ptr_raw,
9ec44266 120 adns_r_hinfo,
e062dcae 121 adns_r_mx_raw,
1b644113 122 adns_r_txt,
1dfe95d8 123 adns_r_rp_raw,
9ec44266 124
828d89bd 125 adns_r_addr,
1dfe95d8 126 adns_r_ns,
a6536d8b 127 adns_r_ptr,
9ec44266 128 adns_r_mx,
129
130 adns_r_soa,
131 adns_r_rp,
132
ffbda80c 133 adns_r_none
134};
135
e062dcae 136static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
137 fprintf(stdout, "%s(%s)%s%s",
db540ace 138 (!ri && rrtn) ? rrtn : "?", ri ? "?" : fmtn ? fmtn : "-",
e062dcae 139 ri ? " " : "", ri ? adns_strerror(ri) : "");
140}
141
9da4a044 142static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
143 char *ownflags, int ownflags_l) {
5476fe64 144 int qf;
145 char *ep;
146
147 qf= strtoul(fdom,&ep,0);
9da4a044 148 if (*ep == ',' && strchr(ep,'/')) {
149 ep++;
150 while (*ep != '/') {
914a5ff5 151 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); }
9da4a044 152 *ownflags++= *ep++;
153 }
154 }
5476fe64 155 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
156 else { *dom_r= ep+1; *qf_r= qf; }
9da4a044 157 *ownflags= 0;
5476fe64 158}
159
fc6a52ae 160static int consistsof(const char *string, const char *accept) {
161 return strspn(string,accept) == strlen(string);
162}
163
e062dcae 164int main(int argc, char *const *argv) {
669f9df5 165 adns_query qu;
914a5ff5 166 struct myctx *mc, *mcw;
669f9df5 167 void *mcr;
4353a5c4 168 adns_answer *ans;
12c5b204 169 const char *initstring, *rrtn, *fmtn;
5476fe64 170 const char *const *fdomlist, *domain;
e062dcae 171 char *show, *cp;
6d682204 172 int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum;
636b69b1 173 adns_status ri;
174 int r;
ffbda80c 175 const adns_rrtype *types;
73dba56e 176 struct timeval now;
9da4a044 177 char ownflags[10];
fc6a52ae 178 char *ep;
179 const char *initflags, *owninitflags;
86e7b8d9 180
fc6a52ae 181 if (argv[0] && argv[1] && argv[1][0] == '-') {
182 initflags= argv[1]+1;
183 argv++;
184 } else {
185 initflags= "";
186 }
12c5b204 187 if (argv[0] && argv[1] && argv[1][0] == '/') {
188 initstring= argv[1]+1;
189 argv++;
190 } else {
191 initstring= 0;
192 }
fc6a52ae 193
194 initflagsnum= strtoul(initflags,&ep,0);
195 if (*ep == ',') {
196 owninitflags= ep+1;
2345cc9b 197 if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag");
fc6a52ae 198 } else if (!*ep) {
199 owninitflags= "";
200 } else {
2345cc9b 201 usageerr("bad <initflagsnum>[,<owninitflags>]");
fc6a52ae 202 }
12c5b204 203
e062dcae 204 if (argv[0] && argv[1] && argv[1][0] == ':') {
205 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
206 if (ch==',') tc++;
e3d42241 207 types_a= malloc(sizeof(*types_a)*(tc+1));
914a5ff5 208 if (!types_a) { perror("malloc types"); quitnow(3); }
e062dcae 209 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
210 types_a[ti]= strtoul(cp,&cp,10);
211 if ((ch= *cp)) {
2345cc9b 212 if (ch != ',') usageerr("unexpected char (not comma) in or between types");
e062dcae 213 cp++;
214 }
215 }
914a5ff5 216 types_a[ti]= adns_r_none;
e062dcae 217 types= types_a;
218 argv++;
219 } else {
914a5ff5 220 types_a= 0;
e062dcae 221 types= defaulttypes;
222 }
223
2345cc9b 224 if (!(argv[0] && argv[1])) usageerr("no query domains supplied");
225 fdomlist= (const char *const*)argv+1;
ffbda80c 226
5476fe64 227 for (qc=0; fdomlist[qc]; qc++);
ffbda80c 228 for (tc=0; types[tc] != adns_r_none; tc++);
914a5ff5 229 mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1);
230 if (!mcs) { perror("malloc mcs"); quitnow(3); }
964344fc 231
5a0be244 232 setvbuf(stdout,0,_IOLBF,0);
233
12c5b204 234 if (initstring) {
fc6a52ae 235 r= adns_init_strcfg(&ads,
3e2e5fab 236 (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq)
237 ^initflagsnum,
fc6a52ae 238 stdout,initstring);
12c5b204 239 } else {
fc6a52ae 240 r= adns_init(&ads,
241 (adns_if_debug|adns_if_noautosys)^initflagsnum,
242 0);
12c5b204 243 }
9da4a044 244 if (r) failure_errno("init",r);
86e7b8d9 245
246 for (qi=0; qi<qc; qi++) {
9da4a044 247 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
2345cc9b 248 if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag");
ffbda80c 249 for (ti=0; ti<tc; ti++) {
669f9df5 250 mc= &mcs[qi*tc+ti];
251 mc->doneyet= 0;
e42ce2b2 252 mc->fdom= fdomlist[qi];
669f9df5 253
d24b603f
MW
254 fprintf(stdout,"%s flags %d type %d",
255 domain,qflags,types[ti]&adns_rrt_reprmask);
669f9df5 256 r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu);
636b69b1 257 if (r == ENOSYS) {
916c16ca 258 fprintf(stdout," not implemented\n");
669f9df5 259 mc->qu= 0;
260 mc->doneyet= 1;
916c16ca 261 } else if (r) {
9da4a044 262 failure_errno("submit",r);
916c16ca 263 } else {
e062dcae 264 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 265 putc(' ',stdout);
e062dcae 266 dumptype(ri,rrtn,fmtn);
916c16ca 267 fprintf(stdout," submitted\n");
268 }
ffbda80c 269 }
86e7b8d9 270 }
350d37d9 271
e42ce2b2 272 for (;;) {
273 for (qi=0; qi<qc; qi++) {
274 for (ti=0; ti<tc; ti++) {
275 mc= &mcs[qi*tc+ti];
276 mc->found= 0;
277 }
278 }
279 for (adns_forallqueries_begin(ads);
280 (qu= adns_forallqueries_next(ads,&mcr));
281 ) {
282 mc= mcr;
283 assert(qu == mc->qu);
284 assert(!mc->doneyet);
285 mc->found= 1;
286 }
287 mcw= 0;
288 for (qi=0; qi<qc; qi++) {
289 for (ti=0; ti<tc; ti++) {
290 mc= &mcs[qi*tc+ti];
291 if (mc->doneyet) continue;
292 assert(mc->found);
293 if (!mcw) mcw= mc;
294 }
295 }
296 if (!mcw) break;
669f9df5 297
8ce38e76 298 if (strchr(owninitflags,'s')) {
299 qu= mcw->qu;
300 mc= mcw;
301 } else {
302 qu= 0;
303 mc= 0;
304 }
fc6a52ae 305
207130d4 306 if (strchr(owninitflags,'p')) {
940356bd 307 r= adns_wait_poll(ads,&qu,&ans,&mcr);
207130d4 308 } else {
309 r= adns_wait(ads,&qu,&ans,&mcr);
310 }
311 if (r) failure_errno("wait/check",r);
312
8ce38e76 313 if (mc) assert(mcr==mc);
314 else mc= mcr;
315 assert(qu==mc->qu);
316 assert(!mc->doneyet);
317
207130d4 318 fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags));
669f9df5 319
914a5ff5 320 if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); }
73dba56e 321
207130d4 322 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
323 fprintf(stdout, "%s flags %d type ",domain,qflags);
324 dumptype(ri,rrtn,fmtn);
325 fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
326 ownflags[0] ? " ownflags=" : "", ownflags,
327 strchr(ownflags,'a')
328 ? adns_errabbrev(ans->status)
329 : adns_strerror(ans->status),
330 ans->nrrs,
331 ans->cname ? ans->cname : "$",
332 ans->owner ? ans->owner : "$",
333 (long)ans->expires - (long)now.tv_sec);
334 if (ans->nrrs) {
335 assert(!ri);
336 for (i=0; i<ans->nrrs; i++) {
636b69b1 337 ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
338 if (ri) failure_status("info",ri);
207130d4 339 fprintf(stdout," %s\n",show);
340 free(show);
86e7b8d9 341 }
207130d4 342 }
343 free(ans);
e42ce2b2 344
207130d4 345 mc->doneyet= 1;
86e7b8d9 346 }
4353a5c4 347
914a5ff5 348 quitnow(0);
964344fc 349}