98db6da3 |
1 | /* |
2 | * dtest.c |
3 | * - simple test program, not part of the library |
4 | */ |
5 | /* |
3ff64957 |
6 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
98db6da3 |
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 | */ |
e7afa27f |
22 | |
23 | #include <stdio.h> |
2c7b101b |
24 | #include <sys/time.h> |
31d3a9d0 |
25 | #include <unistd.h> |
1e9efa71 |
26 | #include <assert.h> |
27 | #include <stdlib.h> |
e7afa27f |
28 | |
4fb3d854 |
29 | #ifndef OUTPUTSTREAM |
30 | # define OUTPUTSTREAM stdout |
31 | #endif |
32 | |
e7afa27f |
33 | #include "adns.h" |
34 | |
1e9efa71 |
35 | static void failure(const char *what, adns_status st) { |
36 | fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st)); |
37 | exit(2); |
38 | } |
39 | |
40 | static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 }; |
41 | |
cfdca685 |
42 | static const adns_rrtype defaulttypes[]= { |
43 | adns_r_a, |
44 | adns_r_ns_raw, |
45 | adns_r_cname, |
61093792 |
46 | adns_r_soa_raw, |
cfdca685 |
47 | adns_r_ptr_raw, |
61093792 |
48 | adns_r_hinfo, |
f2ad23ee |
49 | adns_r_mx_raw, |
d4c7b0b1 |
50 | adns_r_txt, |
26eb6bdc |
51 | adns_r_rp_raw, |
61093792 |
52 | |
cd363ffd |
53 | adns_r_addr, |
26eb6bdc |
54 | adns_r_ns, |
cd1bde2f |
55 | adns_r_ptr, |
61093792 |
56 | adns_r_mx, |
57 | |
58 | adns_r_soa, |
59 | adns_r_rp, |
60 | |
cfdca685 |
61 | adns_r_none |
62 | }; |
63 | |
f2ad23ee |
64 | static 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 | |
82a3a6be |
70 | static void fdom_split(const char *fdom, const char **dom_r, int *qf_r) { |
71 | int qf; |
72 | char *ep; |
73 | |
74 | qf= strtoul(fdom,&ep,0); |
75 | if (*ep != '/') { *dom_r= fdom; *qf_r= 0; } |
76 | else { *dom_r= ep+1; *qf_r= qf; } |
77 | } |
78 | |
f2ad23ee |
79 | int main(int argc, char *const *argv) { |
e7afa27f |
80 | adns_state ads; |
1e9efa71 |
81 | adns_query *qus, qu; |
d05cc330 |
82 | adns_answer *ans; |
e3324da1 |
83 | const char *initstring, *rrtn, *fmtn; |
82a3a6be |
84 | const char *const *fdomlist, *domain; |
f2ad23ee |
85 | char *show, *cp; |
82a3a6be |
86 | int len, i, qc, qi, tc, ti, ch, qflags; |
1e9efa71 |
87 | adns_status r, ri; |
cfdca685 |
88 | const adns_rrtype *types; |
2c7b101b |
89 | struct timeval now; |
f2ad23ee |
90 | adns_rrtype *types_a; |
1e9efa71 |
91 | |
e3324da1 |
92 | if (argv[0] && argv[1] && argv[1][0] == '/') { |
93 | initstring= argv[1]+1; |
94 | argv++; |
95 | } else { |
96 | initstring= 0; |
97 | } |
98 | |
f2ad23ee |
99 | if (argv[0] && argv[1] && argv[1][0] == ':') { |
100 | for (cp= argv[1]+1, tc=1; (ch= *cp); cp++) |
101 | if (ch==',') tc++; |
102 | types_a= malloc(sizeof(*types_a)*tc); |
103 | if (!types_a) { perror("malloc types"); exit(3); } |
104 | for (cp= argv[1]+1, ti=0; ti<tc; ti++) { |
105 | types_a[ti]= strtoul(cp,&cp,10); |
106 | if ((ch= *cp)) { |
cd1bde2f |
107 | if (ch != ',') { |
e3324da1 |
108 | fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr); |
f2ad23ee |
109 | exit(4); |
110 | } |
111 | cp++; |
112 | } |
113 | } |
114 | types= types_a; |
115 | argv++; |
116 | } else { |
117 | types= defaulttypes; |
118 | } |
119 | |
82a3a6be |
120 | if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1; |
121 | else fdomlist= defaultargv; |
cfdca685 |
122 | |
82a3a6be |
123 | for (qc=0; fdomlist[qc]; qc++); |
cfdca685 |
124 | for (tc=0; types[tc] != adns_r_none; tc++); |
125 | qus= malloc(sizeof(qus)*qc*tc); |
1e9efa71 |
126 | if (!qus) { perror("malloc qus"); exit(3); } |
e7afa27f |
127 | |
e3324da1 |
128 | if (initstring) { |
129 | r= adns_init_strcfg(&ads,adns_if_debug|adns_if_noautosys,stdout,initstring); |
130 | } else { |
131 | r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0); |
132 | } |
1e9efa71 |
133 | if (r) failure("init",r); |
134 | |
135 | for (qi=0; qi<qc; qi++) { |
82a3a6be |
136 | fdom_split(fdomlist[qi],&domain,&qflags); |
cfdca685 |
137 | for (ti=0; ti<tc; ti++) { |
82a3a6be |
138 | fprintf(stdout,"%s type %d flags %d",domain,types[ti],qflags); |
139 | r= adns_submit(ads,domain,types[ti],qflags,0,&qus[qi*tc+ti]); |
9b86645c |
140 | if (r == adns_s_unknownrrtype) { |
b8358304 |
141 | fprintf(stdout," not implemented\n"); |
142 | qus[qi*tc+ti]= 0; |
143 | } else if (r) { |
144 | failure("submit",r); |
145 | } else { |
f2ad23ee |
146 | ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0); |
4fb3d854 |
147 | putc(' ',stdout); |
f2ad23ee |
148 | dumptype(ri,rrtn,fmtn); |
b8358304 |
149 | fprintf(stdout," submitted\n"); |
150 | } |
cfdca685 |
151 | } |
1e9efa71 |
152 | } |
31d3a9d0 |
153 | |
1e9efa71 |
154 | for (qi=0; qi<qc; qi++) { |
82a3a6be |
155 | fdom_split(fdomlist[qi],&domain,&qflags); |
156 | |
cfdca685 |
157 | for (ti=0; ti<tc; ti++) { |
158 | qu= qus[qi*tc+ti]; |
b8358304 |
159 | if (!qu) continue; |
82a3a6be |
160 | |
cfdca685 |
161 | r= adns_wait(ads,&qu,&ans,0); |
162 | if (r) failure("wait",r); |
d05cc330 |
163 | |
2c7b101b |
164 | if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); } |
165 | |
cfdca685 |
166 | ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0); |
82a3a6be |
167 | fprintf(stdout, "%s type ",domain); |
f2ad23ee |
168 | dumptype(ri,rrtn,fmtn); |
b2988d3e |
169 | fprintf(stdout, " flags %d: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n", |
82a3a6be |
170 | qflags, adns_strerror(ans->status), |
b2988d3e |
171 | ans->nrrs, |
172 | ans->cname ? ans->cname : "$", |
173 | ans->owner ? ans->owner : "$", |
2c7b101b |
174 | (long)ans->expires - (long)now.tv_sec); |
cfdca685 |
175 | if (ans->nrrs) { |
176 | assert(!ri); |
177 | for (i=0; i<ans->nrrs; i++) { |
178 | r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show); |
179 | if (r) failure("info",r); |
4fb3d854 |
180 | fprintf(stdout," %s\n",show); |
cfdca685 |
181 | free(show); |
182 | } |
1e9efa71 |
183 | } |
cfdca685 |
184 | free(ans); |
1e9efa71 |
185 | } |
1e9efa71 |
186 | } |
d05cc330 |
187 | |
1e9efa71 |
188 | free(qus); |
61093792 |
189 | adns_finish(ads); |
190 | |
e7afa27f |
191 | exit(0); |
192 | } |