Merge changes from chiark: cvs rdiff -u -r tochiark-1998-11-09-b -r
[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,
41 adns_r_ptr_raw,
e062dcae 42 adns_r_mx_raw,
43 adns_r_rp_raw,
1b644113 44 adns_r_txt,
ffbda80c 45 adns_r_none
46};
47
e062dcae 48static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
49 fprintf(stdout, "%s(%s)%s%s",
50 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
51 ri ? " " : "", ri ? adns_strerror(ri) : "");
52}
53
54int main(int argc, char *const *argv) {
964344fc 55 adns_state ads;
86e7b8d9 56 adns_query *qus, qu;
4353a5c4 57 adns_answer *ans;
e062dcae 58 const char *rrtn, *fmtn, *const *domlist;
59 char *show, *cp;
60 int len, i, qc, qi, tc, ti, ch;
86e7b8d9 61 adns_status r, ri;
ffbda80c 62 const adns_rrtype *types;
e062dcae 63 adns_rrtype *types_a;
86e7b8d9 64
e062dcae 65 if (argv[0] && argv[1] && argv[1][0] == ':') {
66 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
67 if (ch==',') tc++;
68 types_a= malloc(sizeof(*types_a)*tc);
69 if (!types_a) { perror("malloc types"); exit(3); }
70 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
71 types_a[ti]= strtoul(cp,&cp,10);
72 if ((ch= *cp)) {
73 if (ch != ':') {
74 fputs("usage: dtest [:<typenum>,...] [<domain> ...]",stderr);
75 exit(4);
76 }
77 cp++;
78 }
79 }
80 types= types_a;
81 argv++;
82 } else {
83 types= defaulttypes;
84 }
85
86 if (argv[0] && argv[1]) domlist= (const char *const*)argv+1;
87 else domlist= defaultargv;
ffbda80c 88
e062dcae 89 for (qc=0; qc[domlist]; qc++);
ffbda80c 90 for (tc=0; types[tc] != adns_r_none; tc++);
91 qus= malloc(sizeof(qus)*qc*tc);
86e7b8d9 92 if (!qus) { perror("malloc qus"); exit(3); }
964344fc 93
4353a5c4 94 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
86e7b8d9 95 if (r) failure("init",r);
96
97 for (qi=0; qi<qc; qi++) {
ffbda80c 98 for (ti=0; ti<tc; ti++) {
e062dcae 99 fprintf(stdout,"%s type %d",domlist[qi],types[ti]);
100 r= adns_submit(ads,domlist[qi],types[ti],0,0,&qus[qi*tc+ti]);
916c16ca 101 if (r == adns_s_notimplemented) {
102 fprintf(stdout," not implemented\n");
103 qus[qi*tc+ti]= 0;
104 } else if (r) {
105 failure("submit",r);
106 } else {
e062dcae 107 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
108 putchar(' ');
109 dumptype(ri,rrtn,fmtn);
916c16ca 110 fprintf(stdout," submitted\n");
111 }
ffbda80c 112 }
86e7b8d9 113 }
350d37d9 114
86e7b8d9 115 for (qi=0; qi<qc; qi++) {
ffbda80c 116 for (ti=0; ti<tc; ti++) {
117 qu= qus[qi*tc+ti];
916c16ca 118 if (!qu) continue;
ffbda80c 119 r= adns_wait(ads,&qu,&ans,0);
120 if (r) failure("wait",r);
4353a5c4 121
ffbda80c 122 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
e062dcae 123 fprintf(stdout, "%s type ", domlist[qi]);
124 dumptype(ri,rrtn,fmtn);
125 fprintf(stdout, ": %s; nrrs=%d; cname=%s\n",
1b644113 126 adns_strerror(ans->status),
127 ans->nrrs, ans->cname ? ans->cname : "$");
ffbda80c 128 if (ans->nrrs) {
129 assert(!ri);
130 for (i=0; i<ans->nrrs; i++) {
131 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
132 if (r) failure("info",r);
133 printf(" %s\n",show);
134 free(show);
135 }
86e7b8d9 136 }
ffbda80c 137 free(ans);
86e7b8d9 138 }
86e7b8d9 139 }
4353a5c4 140
86e7b8d9 141 free(qus);
964344fc 142 exit(0);
143}