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