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