Halfway through IPv6 stuff. Most of the _rr_addr handling is left
[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>
350d37d9 24#include <unistd.h>
86e7b8d9 25#include <assert.h>
26#include <stdlib.h>
964344fc 27
7d251914 28#ifndef OUTPUTSTREAM
29# define OUTPUTSTREAM stdout
30#endif
31
964344fc 32#include "adns.h"
33
86e7b8d9 34static void failure(const char *what, adns_status st) {
35 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
36 exit(2);
37}
38
39static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
40
ffbda80c 41static const adns_rrtype defaulttypes[]= {
42 adns_r_a,
43 adns_r_ns_raw,
44 adns_r_cname,
9ec44266 45 adns_r_soa_raw,
ffbda80c 46 adns_r_ptr_raw,
9ec44266 47 adns_r_hinfo,
e062dcae 48 adns_r_mx_raw,
1b644113 49 adns_r_txt,
1dfe95d8 50 adns_r_rp_raw,
5ed1b7e7 51 adns_r_aaaa,
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
70int main(int argc, char *const *argv) {
964344fc 71 adns_state ads;
86e7b8d9 72 adns_query *qus, qu;
4353a5c4 73 adns_answer *ans;
12c5b204 74 const char *initstring, *rrtn, *fmtn;
75 const char *const *domlist;
e062dcae 76 char *show, *cp;
77 int len, i, qc, qi, tc, ti, ch;
86e7b8d9 78 adns_status r, ri;
ffbda80c 79 const adns_rrtype *types;
e062dcae 80 adns_rrtype *types_a;
86e7b8d9 81
12c5b204 82 if (argv[0] && argv[1] && argv[1][0] == '/') {
83 initstring= argv[1]+1;
84 argv++;
85 } else {
86 initstring= 0;
87 }
88
e062dcae 89 if (argv[0] && argv[1] && argv[1][0] == ':') {
90 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
91 if (ch==',') tc++;
92 types_a= malloc(sizeof(*types_a)*tc);
93 if (!types_a) { perror("malloc types"); exit(3); }
94 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
95 types_a[ti]= strtoul(cp,&cp,10);
96 if ((ch= *cp)) {
a6536d8b 97 if (ch != ',') {
12c5b204 98 fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr);
e062dcae 99 exit(4);
100 }
101 cp++;
102 }
103 }
104 types= types_a;
105 argv++;
106 } else {
107 types= defaulttypes;
108 }
109
110 if (argv[0] && argv[1]) domlist= (const char *const*)argv+1;
111 else domlist= defaultargv;
ffbda80c 112
e062dcae 113 for (qc=0; qc[domlist]; qc++);
ffbda80c 114 for (tc=0; types[tc] != adns_r_none; tc++);
115 qus= malloc(sizeof(qus)*qc*tc);
86e7b8d9 116 if (!qus) { perror("malloc qus"); exit(3); }
964344fc 117
12c5b204 118 if (initstring) {
119 r= adns_init_strcfg(&ads,adns_if_debug|adns_if_noautosys,stdout,initstring);
120 } else {
121 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
122 }
86e7b8d9 123 if (r) failure("init",r);
124
125 for (qi=0; qi<qc; qi++) {
ffbda80c 126 for (ti=0; ti<tc; ti++) {
e062dcae 127 fprintf(stdout,"%s type %d",domlist[qi],types[ti]);
128 r= adns_submit(ads,domlist[qi],types[ti],0,0,&qus[qi*tc+ti]);
ea1e31e3 129 if (r == adns_s_unknownrrtype) {
916c16ca 130 fprintf(stdout," not implemented\n");
131 qus[qi*tc+ti]= 0;
132 } else if (r) {
133 failure("submit",r);
134 } else {
e062dcae 135 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
7d251914 136 putc(' ',stdout);
e062dcae 137 dumptype(ri,rrtn,fmtn);
916c16ca 138 fprintf(stdout," submitted\n");
139 }
ffbda80c 140 }
86e7b8d9 141 }
350d37d9 142
86e7b8d9 143 for (qi=0; qi<qc; qi++) {
ffbda80c 144 for (ti=0; ti<tc; ti++) {
145 qu= qus[qi*tc+ti];
916c16ca 146 if (!qu) continue;
ffbda80c 147 r= adns_wait(ads,&qu,&ans,0);
148 if (r) failure("wait",r);
4353a5c4 149
ffbda80c 150 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
e062dcae 151 fprintf(stdout, "%s type ", domlist[qi]);
152 dumptype(ri,rrtn,fmtn);
153 fprintf(stdout, ": %s; nrrs=%d; cname=%s\n",
1b644113 154 adns_strerror(ans->status),
155 ans->nrrs, ans->cname ? ans->cname : "$");
ffbda80c 156 if (ans->nrrs) {
157 assert(!ri);
158 for (i=0; i<ans->nrrs; i++) {
159 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
160 if (r) failure("info",r);
7d251914 161 fprintf(stdout," %s\n",show);
ffbda80c 162 free(show);
163 }
86e7b8d9 164 }
ffbda80c 165 free(ans);
86e7b8d9 166 }
86e7b8d9 167 }
4353a5c4 168
86e7b8d9 169 free(qus);
9ec44266 170 adns_finish(ads);
171
964344fc 172 exit(0);
173}