98db6da3 |
1 | /* |
39d64e6e |
2 | * adnstest.c |
98db6da3 |
3 | * - simple test program, not part of the library |
4 | */ |
5 | /* |
a79ac5ba |
6 | * This file is |
7 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
8 | * |
9 | * It is part of adns, which is |
10 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
11 | * Copyright (C) 1999 Tony Finch <dot@dotat.at> |
98db6da3 |
12 | * |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the GNU General Public License as published by |
15 | * the Free Software Foundation; either version 2, or (at your option) |
16 | * any later version. |
17 | * |
18 | * This program is distributed in the hope that it will be useful, |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | * GNU General Public License for more details. |
22 | * |
23 | * You should have received a copy of the GNU General Public License |
24 | * along with this program; if not, write to the Free Software Foundation, |
25 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
26 | */ |
e7afa27f |
27 | |
28 | #include <stdio.h> |
2c7b101b |
29 | #include <sys/time.h> |
31d3a9d0 |
30 | #include <unistd.h> |
1e9efa71 |
31 | #include <assert.h> |
32 | #include <stdlib.h> |
c2875680 |
33 | #include <string.h> |
4f973eb5 |
34 | #include <errno.h> |
4bc87817 |
35 | |
36 | #include "adns.h" |
37 | |
38 | #include "config.h" |
e7afa27f |
39 | |
4fb3d854 |
40 | #ifndef OUTPUTSTREAM |
41 | # define OUTPUTSTREAM stdout |
42 | #endif |
43 | |
4bc87817 |
44 | #ifndef HAVE_POLL |
45 | #undef poll |
46 | int poll(struct pollfd *ufds, int nfds, int timeout) { |
47 | fputs("poll(2) not supported on this system\n",stderr); |
e516ee75 |
48 | exit(5); |
4bc87817 |
49 | } |
50 | #define adns_beforepoll(a,b,c,d,e) 0 |
51 | #define adns_afterpoll(a,b,c,d) 0 |
52 | #endif |
e7afa27f |
53 | |
e516ee75 |
54 | static void failure_status(const char *what, adns_status st) NONRETURNING; |
c2875680 |
55 | static void failure_status(const char *what, adns_status st) { |
1e9efa71 |
56 | fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st)); |
57 | exit(2); |
58 | } |
59 | |
e516ee75 |
60 | static void failure_errno(const char *what, int errnoval) NONRETURNING; |
c2875680 |
61 | static void failure_errno(const char *what, int errnoval) { |
62 | fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval); |
63 | exit(2); |
64 | } |
65 | |
e516ee75 |
66 | static void usageerr(const char *why) NONRETURNING; |
67 | static void usageerr(const char *why) { |
68 | fprintf(stderr, |
69 | "bad usage: %s\n" |
70 | "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n" |
71 | " [ :<typenum>,... ]\n" |
72 | " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n" |
73 | "initflags: p use poll(2) instead of select(2)\n" |
74 | " s use adns_wait with specified query, instead of 0\n" |
75 | "queryflags: a print status abbrevs instead of strings\n" |
76 | "exit status: 0 ok (though some queries may have failed)\n" |
77 | " 1 used by test harness to indicate test failed\n" |
78 | " 2 unable to submit or init or some such\n" |
79 | " 3 unexpected failure\n" |
80 | " 4 usage error\n" |
81 | " 5 operation not supported on this system\n", |
82 | why); |
83 | exit(4); |
84 | } |
1e9efa71 |
85 | |
cfdca685 |
86 | static const adns_rrtype defaulttypes[]= { |
87 | adns_r_a, |
88 | adns_r_ns_raw, |
89 | adns_r_cname, |
61093792 |
90 | adns_r_soa_raw, |
cfdca685 |
91 | adns_r_ptr_raw, |
61093792 |
92 | adns_r_hinfo, |
f2ad23ee |
93 | adns_r_mx_raw, |
d4c7b0b1 |
94 | adns_r_txt, |
26eb6bdc |
95 | adns_r_rp_raw, |
61093792 |
96 | |
cd363ffd |
97 | adns_r_addr, |
26eb6bdc |
98 | adns_r_ns, |
cd1bde2f |
99 | adns_r_ptr, |
61093792 |
100 | adns_r_mx, |
101 | |
102 | adns_r_soa, |
103 | adns_r_rp, |
104 | |
cfdca685 |
105 | adns_r_none |
106 | }; |
107 | |
f2ad23ee |
108 | static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) { |
109 | fprintf(stdout, "%s(%s)%s%s", |
110 | ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-", |
111 | ri ? " " : "", ri ? adns_strerror(ri) : ""); |
112 | } |
113 | |
c2875680 |
114 | static void fdom_split(const char *fdom, const char **dom_r, int *qf_r, |
115 | char *ownflags, int ownflags_l) { |
82a3a6be |
116 | int qf; |
117 | char *ep; |
118 | |
119 | qf= strtoul(fdom,&ep,0); |
c2875680 |
120 | if (*ep == ',' && strchr(ep,'/')) { |
121 | ep++; |
122 | while (*ep != '/') { |
123 | if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); exit(3); } |
124 | *ownflags++= *ep++; |
125 | } |
126 | } |
82a3a6be |
127 | if (*ep != '/') { *dom_r= fdom; *qf_r= 0; } |
128 | else { *dom_r= ep+1; *qf_r= qf; } |
c2875680 |
129 | *ownflags= 0; |
82a3a6be |
130 | } |
131 | |
4f973eb5 |
132 | static int consistsof(const char *string, const char *accept) { |
133 | return strspn(string,accept) == strlen(string); |
134 | } |
135 | |
f2ad23ee |
136 | int main(int argc, char *const *argv) { |
d6b05172 |
137 | struct myctx { |
138 | adns_query qu; |
0e02fa7a |
139 | int doneyet, found; |
140 | const char *fdom; |
d6b05172 |
141 | }; |
142 | |
e7afa27f |
143 | adns_state ads; |
d6b05172 |
144 | adns_query qu; |
0e02fa7a |
145 | struct myctx *mcs, *mc, *mcw; |
d6b05172 |
146 | void *mcr; |
d05cc330 |
147 | adns_answer *ans; |
e3324da1 |
148 | const char *initstring, *rrtn, *fmtn; |
82a3a6be |
149 | const char *const *fdomlist, *domain; |
f2ad23ee |
150 | char *show, *cp; |
85b8a2e6 |
151 | int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum; |
125de2a9 |
152 | adns_status ri; |
153 | int r; |
cfdca685 |
154 | const adns_rrtype *types; |
2c7b101b |
155 | struct timeval now; |
f2ad23ee |
156 | adns_rrtype *types_a; |
c2875680 |
157 | char ownflags[10]; |
4f973eb5 |
158 | char *ep; |
159 | const char *initflags, *owninitflags; |
1e9efa71 |
160 | |
4f973eb5 |
161 | if (argv[0] && argv[1] && argv[1][0] == '-') { |
162 | initflags= argv[1]+1; |
163 | argv++; |
164 | } else { |
165 | initflags= ""; |
166 | } |
e3324da1 |
167 | if (argv[0] && argv[1] && argv[1][0] == '/') { |
168 | initstring= argv[1]+1; |
169 | argv++; |
170 | } else { |
171 | initstring= 0; |
172 | } |
4f973eb5 |
173 | |
174 | initflagsnum= strtoul(initflags,&ep,0); |
175 | if (*ep == ',') { |
176 | owninitflags= ep+1; |
e516ee75 |
177 | if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag"); |
4f973eb5 |
178 | } else if (!*ep) { |
179 | owninitflags= ""; |
180 | } else { |
e516ee75 |
181 | usageerr("bad <initflagsnum>[,<owninitflags>]"); |
4f973eb5 |
182 | } |
e3324da1 |
183 | |
f2ad23ee |
184 | if (argv[0] && argv[1] && argv[1][0] == ':') { |
185 | for (cp= argv[1]+1, tc=1; (ch= *cp); cp++) |
186 | if (ch==',') tc++; |
04be144e |
187 | types_a= malloc(sizeof(*types_a)*(tc+1)); |
f2ad23ee |
188 | if (!types_a) { perror("malloc types"); exit(3); } |
189 | for (cp= argv[1]+1, ti=0; ti<tc; ti++) { |
190 | types_a[ti]= strtoul(cp,&cp,10); |
191 | if ((ch= *cp)) { |
e516ee75 |
192 | if (ch != ',') usageerr("unexpected char (not comma) in or between types"); |
f2ad23ee |
193 | cp++; |
194 | } |
195 | } |
04be144e |
196 | *cp++= adns_r_none; |
f2ad23ee |
197 | types= types_a; |
198 | argv++; |
199 | } else { |
200 | types= defaulttypes; |
201 | } |
202 | |
e516ee75 |
203 | if (!(argv[0] && argv[1])) usageerr("no query domains supplied"); |
204 | fdomlist= (const char *const*)argv+1; |
cfdca685 |
205 | |
82a3a6be |
206 | for (qc=0; fdomlist[qc]; qc++); |
cfdca685 |
207 | for (tc=0; types[tc] != adns_r_none; tc++); |
d6b05172 |
208 | mcs= malloc(sizeof(*mcs)*qc*tc); |
209 | if (!mcs) { perror("malloc mcs"); exit(3); } |
e7afa27f |
210 | |
e3324da1 |
211 | if (initstring) { |
4f973eb5 |
212 | r= adns_init_strcfg(&ads, |
1389dc72 |
213 | (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq) |
214 | ^initflagsnum, |
4f973eb5 |
215 | stdout,initstring); |
e3324da1 |
216 | } else { |
4f973eb5 |
217 | r= adns_init(&ads, |
218 | (adns_if_debug|adns_if_noautosys)^initflagsnum, |
219 | 0); |
e3324da1 |
220 | } |
c2875680 |
221 | if (r) failure_errno("init",r); |
1e9efa71 |
222 | |
223 | for (qi=0; qi<qc; qi++) { |
c2875680 |
224 | fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags)); |
e516ee75 |
225 | if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag"); |
cfdca685 |
226 | for (ti=0; ti<tc; ti++) { |
d6b05172 |
227 | mc= &mcs[qi*tc+ti]; |
228 | mc->doneyet= 0; |
0e02fa7a |
229 | mc->fdom= fdomlist[qi]; |
d6b05172 |
230 | |
18dc94a5 |
231 | fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]); |
d6b05172 |
232 | r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu); |
125de2a9 |
233 | if (r == ENOSYS) { |
b8358304 |
234 | fprintf(stdout," not implemented\n"); |
d6b05172 |
235 | mc->qu= 0; |
236 | mc->doneyet= 1; |
b8358304 |
237 | } else if (r) { |
c2875680 |
238 | failure_errno("submit",r); |
b8358304 |
239 | } else { |
f2ad23ee |
240 | ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0); |
4fb3d854 |
241 | putc(' ',stdout); |
f2ad23ee |
242 | dumptype(ri,rrtn,fmtn); |
b8358304 |
243 | fprintf(stdout," submitted\n"); |
244 | } |
cfdca685 |
245 | } |
1e9efa71 |
246 | } |
31d3a9d0 |
247 | |
0e02fa7a |
248 | for (;;) { |
249 | for (qi=0; qi<qc; qi++) { |
250 | for (ti=0; ti<tc; ti++) { |
251 | mc= &mcs[qi*tc+ti]; |
252 | mc->found= 0; |
253 | } |
254 | } |
255 | for (adns_forallqueries_begin(ads); |
256 | (qu= adns_forallqueries_next(ads,&mcr)); |
257 | ) { |
258 | mc= mcr; |
259 | assert(qu == mc->qu); |
260 | assert(!mc->doneyet); |
261 | mc->found= 1; |
262 | } |
263 | mcw= 0; |
264 | for (qi=0; qi<qc; qi++) { |
265 | for (ti=0; ti<tc; ti++) { |
266 | mc= &mcs[qi*tc+ti]; |
267 | if (mc->doneyet) continue; |
268 | assert(mc->found); |
269 | if (!mcw) mcw= mc; |
270 | } |
271 | } |
272 | if (!mcw) break; |
d6b05172 |
273 | |
8f2aa812 |
274 | if (strchr(owninitflags,'s')) { |
275 | qu= mcw->qu; |
276 | mc= mcw; |
277 | } else { |
278 | qu= 0; |
279 | mc= 0; |
280 | } |
4f973eb5 |
281 | |
3ea0e5b9 |
282 | if (strchr(owninitflags,'p')) { |
ef20fccf |
283 | r= adns_wait_poll(ads,&qu,&ans,&mcr); |
3ea0e5b9 |
284 | } else { |
285 | r= adns_wait(ads,&qu,&ans,&mcr); |
286 | } |
287 | if (r) failure_errno("wait/check",r); |
288 | |
8f2aa812 |
289 | if (mc) assert(mcr==mc); |
290 | else mc= mcr; |
291 | assert(qu==mc->qu); |
292 | assert(!mc->doneyet); |
293 | |
3ea0e5b9 |
294 | fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags)); |
d6b05172 |
295 | |
3ea0e5b9 |
296 | if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); } |
2c7b101b |
297 | |
3ea0e5b9 |
298 | ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0); |
299 | fprintf(stdout, "%s flags %d type ",domain,qflags); |
300 | dumptype(ri,rrtn,fmtn); |
301 | fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n", |
302 | ownflags[0] ? " ownflags=" : "", ownflags, |
303 | strchr(ownflags,'a') |
304 | ? adns_errabbrev(ans->status) |
305 | : adns_strerror(ans->status), |
306 | ans->nrrs, |
307 | ans->cname ? ans->cname : "$", |
308 | ans->owner ? ans->owner : "$", |
309 | (long)ans->expires - (long)now.tv_sec); |
310 | if (ans->nrrs) { |
311 | assert(!ri); |
312 | for (i=0; i<ans->nrrs; i++) { |
125de2a9 |
313 | ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show); |
314 | if (ri) failure_status("info",ri); |
3ea0e5b9 |
315 | fprintf(stdout," %s\n",show); |
316 | free(show); |
1e9efa71 |
317 | } |
3ea0e5b9 |
318 | } |
319 | free(ans); |
0e02fa7a |
320 | |
3ea0e5b9 |
321 | mc->doneyet= 1; |
1e9efa71 |
322 | } |
d05cc330 |
323 | |
d6b05172 |
324 | free(mcs); |
61093792 |
325 | adns_finish(ads); |
326 | |
e7afa27f |
327 | exit(0); |
328 | } |