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