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