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