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