Work on harness is progressing.
[adns] / client / adnstest.c
CommitLineData
e576be50 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, 1998 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 */
964344fc 22
23#include <stdio.h>
350d37d9 24#include <unistd.h>
86e7b8d9 25#include <assert.h>
26#include <stdlib.h>
964344fc 27
28#include "adns.h"
29
86e7b8d9 30static void failure(const char *what, adns_status st) {
31 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
32 exit(2);
33}
34
35static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
36
ffbda80c 37static const adns_rrtype defaulttypes[]= {
38 adns_r_a,
39 adns_r_ns_raw,
40 adns_r_cname,
9ec44266 41 adns_r_soa_raw,
ffbda80c 42 adns_r_ptr_raw,
9ec44266 43 adns_r_hinfo,
e062dcae 44 adns_r_mx_raw,
1b644113 45 adns_r_txt,
1dfe95d8 46 adns_r_rp_raw,
9ec44266 47
828d89bd 48 adns_r_addr,
1dfe95d8 49 adns_r_ns,
a6536d8b 50 adns_r_ptr,
9ec44266 51 adns_r_mx,
52
53 adns_r_soa,
54 adns_r_rp,
55
ffbda80c 56 adns_r_none
57};
58
e062dcae 59static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
60 fprintf(stdout, "%s(%s)%s%s",
61 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
62 ri ? " " : "", ri ? adns_strerror(ri) : "");
63}
64
65int main(int argc, char *const *argv) {
964344fc 66 adns_state ads;
86e7b8d9 67 adns_query *qus, qu;
4353a5c4 68 adns_answer *ans;
e062dcae 69 const char *rrtn, *fmtn, *const *domlist;
70 char *show, *cp;
71 int len, i, qc, qi, tc, ti, ch;
86e7b8d9 72 adns_status r, ri;
ffbda80c 73 const adns_rrtype *types;
e062dcae 74 adns_rrtype *types_a;
86e7b8d9 75
e062dcae 76 if (argv[0] && argv[1] && argv[1][0] == ':') {
77 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
78 if (ch==',') tc++;
79 types_a= malloc(sizeof(*types_a)*tc);
80 if (!types_a) { perror("malloc types"); exit(3); }
81 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
82 types_a[ti]= strtoul(cp,&cp,10);
83 if ((ch= *cp)) {
a6536d8b 84 if (ch != ',') {
85 fputs("usage: dtest [:<typenum>,...] [<domain> ...]\n",stderr);
e062dcae 86 exit(4);
87 }
88 cp++;
89 }
90 }
91 types= types_a;
92 argv++;
93 } else {
94 types= defaulttypes;
95 }
96
97 if (argv[0] && argv[1]) domlist= (const char *const*)argv+1;
98 else domlist= defaultargv;
ffbda80c 99
e062dcae 100 for (qc=0; qc[domlist]; qc++);
ffbda80c 101 for (tc=0; types[tc] != adns_r_none; tc++);
102 qus= malloc(sizeof(qus)*qc*tc);
86e7b8d9 103 if (!qus) { perror("malloc qus"); exit(3); }
964344fc 104
4353a5c4 105 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
86e7b8d9 106 if (r) failure("init",r);
107
108 for (qi=0; qi<qc; qi++) {
ffbda80c 109 for (ti=0; ti<tc; ti++) {
e062dcae 110 fprintf(stdout,"%s type %d",domlist[qi],types[ti]);
111 r= adns_submit(ads,domlist[qi],types[ti],0,0,&qus[qi*tc+ti]);
ea1e31e3 112 if (r == adns_s_unknownrrtype) {
916c16ca 113 fprintf(stdout," not implemented\n");
114 qus[qi*tc+ti]= 0;
115 } else if (r) {
116 failure("submit",r);
117 } else {
e062dcae 118 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
119 putchar(' ');
120 dumptype(ri,rrtn,fmtn);
916c16ca 121 fprintf(stdout," submitted\n");
122 }
ffbda80c 123 }
86e7b8d9 124 }
350d37d9 125
86e7b8d9 126 for (qi=0; qi<qc; qi++) {
ffbda80c 127 for (ti=0; ti<tc; ti++) {
128 qu= qus[qi*tc+ti];
916c16ca 129 if (!qu) continue;
ffbda80c 130 r= adns_wait(ads,&qu,&ans,0);
131 if (r) failure("wait",r);
4353a5c4 132
ffbda80c 133 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
e062dcae 134 fprintf(stdout, "%s type ", domlist[qi]);
135 dumptype(ri,rrtn,fmtn);
136 fprintf(stdout, ": %s; nrrs=%d; cname=%s\n",
1b644113 137 adns_strerror(ans->status),
138 ans->nrrs, ans->cname ? ans->cname : "$");
ffbda80c 139 if (ans->nrrs) {
140 assert(!ri);
141 for (i=0; i<ans->nrrs; i++) {
142 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
143 if (r) failure("info",r);
144 printf(" %s\n",show);
145 free(show);
146 }
86e7b8d9 147 }
ffbda80c 148 free(ans);
86e7b8d9 149 }
86e7b8d9 150 }
4353a5c4 151
86e7b8d9 152 free(qus);
9ec44266 153 adns_finish(ads);
154
964344fc 155 exit(0);
156}