Clean up parents from adns->childw (otherwise would abort/segfault).
[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>
964344fc 28
7d251914 29#ifndef OUTPUTSTREAM
30# define OUTPUTSTREAM stdout
31#endif
32
964344fc 33#include "adns.h"
34
86e7b8d9 35static void failure(const char *what, adns_status st) {
36 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
37 exit(2);
38}
39
40static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
41
ffbda80c 42static const adns_rrtype defaulttypes[]= {
43 adns_r_a,
44 adns_r_ns_raw,
45 adns_r_cname,
9ec44266 46 adns_r_soa_raw,
ffbda80c 47 adns_r_ptr_raw,
9ec44266 48 adns_r_hinfo,
e062dcae 49 adns_r_mx_raw,
1b644113 50 adns_r_txt,
1dfe95d8 51 adns_r_rp_raw,
9ec44266 52
828d89bd 53 adns_r_addr,
1dfe95d8 54 adns_r_ns,
a6536d8b 55 adns_r_ptr,
9ec44266 56 adns_r_mx,
57
58 adns_r_soa,
59 adns_r_rp,
60
ffbda80c 61 adns_r_none
62};
63
e062dcae 64static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
65 fprintf(stdout, "%s(%s)%s%s",
66 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
67 ri ? " " : "", ri ? adns_strerror(ri) : "");
68}
69
5476fe64 70static void fdom_split(const char *fdom, const char **dom_r, int *qf_r) {
71 int qf;
72 char *ep;
73
74 qf= strtoul(fdom,&ep,0);
75 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
76 else { *dom_r= ep+1; *qf_r= qf; }
77}
78
e062dcae 79int main(int argc, char *const *argv) {
964344fc 80 adns_state ads;
86e7b8d9 81 adns_query *qus, qu;
4353a5c4 82 adns_answer *ans;
12c5b204 83 const char *initstring, *rrtn, *fmtn;
5476fe64 84 const char *const *fdomlist, *domain;
e062dcae 85 char *show, *cp;
5476fe64 86 int len, i, qc, qi, tc, ti, ch, qflags;
86e7b8d9 87 adns_status r, ri;
ffbda80c 88 const adns_rrtype *types;
73dba56e 89 struct timeval now;
e062dcae 90 adns_rrtype *types_a;
86e7b8d9 91
12c5b204 92 if (argv[0] && argv[1] && argv[1][0] == '/') {
93 initstring= argv[1]+1;
94 argv++;
95 } else {
96 initstring= 0;
97 }
98
e062dcae 99 if (argv[0] && argv[1] && argv[1][0] == ':') {
100 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
101 if (ch==',') tc++;
e3d42241 102 types_a= malloc(sizeof(*types_a)*(tc+1));
e062dcae 103 if (!types_a) { perror("malloc types"); exit(3); }
104 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
105 types_a[ti]= strtoul(cp,&cp,10);
106 if ((ch= *cp)) {
a6536d8b 107 if (ch != ',') {
12c5b204 108 fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr);
e062dcae 109 exit(4);
110 }
111 cp++;
112 }
113 }
e3d42241 114 *cp++= adns_r_none;
e062dcae 115 types= types_a;
116 argv++;
117 } else {
118 types= defaulttypes;
119 }
120
5476fe64 121 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
122 else fdomlist= defaultargv;
ffbda80c 123
5476fe64 124 for (qc=0; fdomlist[qc]; qc++);
ffbda80c 125 for (tc=0; types[tc] != adns_r_none; tc++);
126 qus= malloc(sizeof(qus)*qc*tc);
86e7b8d9 127 if (!qus) { perror("malloc qus"); exit(3); }
964344fc 128
12c5b204 129 if (initstring) {
130 r= adns_init_strcfg(&ads,adns_if_debug|adns_if_noautosys,stdout,initstring);
131 } else {
132 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
133 }
86e7b8d9 134 if (r) failure("init",r);
135
136 for (qi=0; qi<qc; qi++) {
5476fe64 137 fdom_split(fdomlist[qi],&domain,&qflags);
ffbda80c 138 for (ti=0; ti<tc; ti++) {
1aa0213d 139 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
5476fe64 140 r= adns_submit(ads,domain,types[ti],qflags,0,&qus[qi*tc+ti]);
ea1e31e3 141 if (r == adns_s_unknownrrtype) {
916c16ca 142 fprintf(stdout," not implemented\n");
143 qus[qi*tc+ti]= 0;
144 } else if (r) {
145 failure("submit",r);
146 } else {
e062dcae 147 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 148 putc(' ',stdout);
e062dcae 149 dumptype(ri,rrtn,fmtn);
916c16ca 150 fprintf(stdout," submitted\n");
151 }
ffbda80c 152 }
86e7b8d9 153 }
350d37d9 154
86e7b8d9 155 for (qi=0; qi<qc; qi++) {
5476fe64 156 fdom_split(fdomlist[qi],&domain,&qflags);
157
ffbda80c 158 for (ti=0; ti<tc; ti++) {
159 qu= qus[qi*tc+ti];
916c16ca 160 if (!qu) continue;
5476fe64 161
ffbda80c 162 r= adns_wait(ads,&qu,&ans,0);
163 if (r) failure("wait",r);
4353a5c4 164
73dba56e 165 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
166
ffbda80c 167 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
1aa0213d 168 fprintf(stdout, "%s flags %d type ",domain,qflags);
e062dcae 169 dumptype(ri,rrtn,fmtn);
1aa0213d 170 fprintf(stdout, ": %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
171 adns_strerror(ans->status),
22181a31 172 ans->nrrs,
173 ans->cname ? ans->cname : "$",
174 ans->owner ? ans->owner : "$",
73dba56e 175 (long)ans->expires - (long)now.tv_sec);
ffbda80c 176 if (ans->nrrs) {
177 assert(!ri);
178 for (i=0; i<ans->nrrs; i++) {
e3d42241 179 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show);
ffbda80c 180 if (r) failure("info",r);
7d251914 181 fprintf(stdout," %s\n",show);
ffbda80c 182 free(show);
183 }
86e7b8d9 184 }
ffbda80c 185 free(ans);
86e7b8d9 186 }
86e7b8d9 187 }
4353a5c4 188
86e7b8d9 189 free(qus);
9ec44266 190 adns_finish(ads);
191
964344fc 192 exit(0);
193}