e576be50 |
1 | /* |
b0f83da6 |
2 | * adnstest.c |
e576be50 |
3 | * - simple test program, not part of the library |
4 | */ |
5 | /* |
d942707d |
6 | * This file is |
5a0be244 |
7 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
d942707d |
8 | * |
9 | * It is part of adns, which is |
3d5cde09 |
10 | * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
bef232ae |
11 | * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
e576be50 |
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 | */ |
964344fc |
27 | |
28 | #include <stdio.h> |
73dba56e |
29 | #include <sys/time.h> |
350d37d9 |
30 | #include <unistd.h> |
86e7b8d9 |
31 | #include <assert.h> |
32 | #include <stdlib.h> |
9da4a044 |
33 | #include <string.h> |
fc6a52ae |
34 | #include <errno.h> |
16cf5367 |
35 | |
16cf5367 |
36 | #include "config.h" |
5a0be244 |
37 | #include "adns.h" |
964344fc |
38 | |
0ebff22d |
39 | #ifdef ADNS_REGRESS_TEST |
40 | # include "hredirect.h" |
7d251914 |
41 | #endif |
42 | |
914a5ff5 |
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 | |
16cf5367 |
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); |
914a5ff5 |
66 | quitnow(5); |
16cf5367 |
67 | } |
68 | #define adns_beforepoll(a,b,c,d,e) 0 |
69 | #define adns_afterpoll(a,b,c,d) 0 |
70 | #endif |
964344fc |
71 | |
2345cc9b |
72 | static void failure_status(const char *what, adns_status st) NONRETURNING; |
9da4a044 |
73 | static void failure_status(const char *what, adns_status st) { |
86e7b8d9 |
74 | fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st)); |
914a5ff5 |
75 | quitnow(2); |
86e7b8d9 |
76 | } |
77 | |
2345cc9b |
78 | static void failure_errno(const char *what, int errnoval) NONRETURNING; |
9da4a044 |
79 | static void failure_errno(const char *what, int errnoval) { |
fa1b90e5 |
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 | } |
914a5ff5 |
92 | quitnow(2); |
9da4a044 |
93 | } |
94 | |
2345cc9b |
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); |
914a5ff5 |
112 | quitnow(4); |
2345cc9b |
113 | } |
86e7b8d9 |
114 | |
ffbda80c |
115 | static const adns_rrtype defaulttypes[]= { |
116 | adns_r_a, |
117 | adns_r_ns_raw, |
118 | adns_r_cname, |
9ec44266 |
119 | adns_r_soa_raw, |
ffbda80c |
120 | adns_r_ptr_raw, |
9ec44266 |
121 | adns_r_hinfo, |
e062dcae |
122 | adns_r_mx_raw, |
1b644113 |
123 | adns_r_txt, |
1dfe95d8 |
124 | adns_r_rp_raw, |
401c256a |
125 | adns_r_srv_raw, |
9ec44266 |
126 | |
828d89bd |
127 | adns_r_addr, |
1dfe95d8 |
128 | adns_r_ns, |
a6536d8b |
129 | adns_r_ptr, |
9ec44266 |
130 | adns_r_mx, |
401c256a |
131 | adns_r_srv, |
9ec44266 |
132 | |
133 | adns_r_soa, |
134 | adns_r_rp, |
135 | |
ffbda80c |
136 | adns_r_none |
137 | }; |
138 | |
e062dcae |
139 | static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) { |
140 | fprintf(stdout, "%s(%s)%s%s", |
141 | ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-", |
142 | ri ? " " : "", ri ? adns_strerror(ri) : ""); |
143 | } |
144 | |
9da4a044 |
145 | static void fdom_split(const char *fdom, const char **dom_r, int *qf_r, |
146 | char *ownflags, int ownflags_l) { |
5476fe64 |
147 | int qf; |
148 | char *ep; |
149 | |
150 | qf= strtoul(fdom,&ep,0); |
9da4a044 |
151 | if (*ep == ',' && strchr(ep,'/')) { |
152 | ep++; |
153 | while (*ep != '/') { |
914a5ff5 |
154 | if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); quitnow(3); } |
9da4a044 |
155 | *ownflags++= *ep++; |
156 | } |
157 | } |
5476fe64 |
158 | if (*ep != '/') { *dom_r= fdom; *qf_r= 0; } |
159 | else { *dom_r= ep+1; *qf_r= qf; } |
9da4a044 |
160 | *ownflags= 0; |
5476fe64 |
161 | } |
162 | |
fc6a52ae |
163 | static int consistsof(const char *string, const char *accept) { |
164 | return strspn(string,accept) == strlen(string); |
165 | } |
166 | |
e062dcae |
167 | int main(int argc, char *const *argv) { |
669f9df5 |
168 | adns_query qu; |
914a5ff5 |
169 | struct myctx *mc, *mcw; |
669f9df5 |
170 | void *mcr; |
4353a5c4 |
171 | adns_answer *ans; |
12c5b204 |
172 | const char *initstring, *rrtn, *fmtn; |
5476fe64 |
173 | const char *const *fdomlist, *domain; |
e062dcae |
174 | char *show, *cp; |
6d682204 |
175 | int len, i, qc, qi, tc, ti, ch, qflags, initflagsnum; |
636b69b1 |
176 | adns_status ri; |
177 | int r; |
ffbda80c |
178 | const adns_rrtype *types; |
73dba56e |
179 | struct timeval now; |
9da4a044 |
180 | char ownflags[10]; |
fc6a52ae |
181 | char *ep; |
182 | const char *initflags, *owninitflags; |
86e7b8d9 |
183 | |
fc6a52ae |
184 | if (argv[0] && argv[1] && argv[1][0] == '-') { |
185 | initflags= argv[1]+1; |
186 | argv++; |
187 | } else { |
188 | initflags= ""; |
189 | } |
12c5b204 |
190 | if (argv[0] && argv[1] && argv[1][0] == '/') { |
191 | initstring= argv[1]+1; |
192 | argv++; |
193 | } else { |
194 | initstring= 0; |
195 | } |
fc6a52ae |
196 | |
197 | initflagsnum= strtoul(initflags,&ep,0); |
198 | if (*ep == ',') { |
199 | owninitflags= ep+1; |
2345cc9b |
200 | if (!consistsof(owninitflags,"ps")) usageerr("unknown owninitflag"); |
fc6a52ae |
201 | } else if (!*ep) { |
202 | owninitflags= ""; |
203 | } else { |
2345cc9b |
204 | usageerr("bad <initflagsnum>[,<owninitflags>]"); |
fc6a52ae |
205 | } |
12c5b204 |
206 | |
e062dcae |
207 | if (argv[0] && argv[1] && argv[1][0] == ':') { |
208 | for (cp= argv[1]+1, tc=1; (ch= *cp); cp++) |
209 | if (ch==',') tc++; |
e3d42241 |
210 | types_a= malloc(sizeof(*types_a)*(tc+1)); |
914a5ff5 |
211 | if (!types_a) { perror("malloc types"); quitnow(3); } |
e062dcae |
212 | for (cp= argv[1]+1, ti=0; ti<tc; ti++) { |
213 | types_a[ti]= strtoul(cp,&cp,10); |
214 | if ((ch= *cp)) { |
2345cc9b |
215 | if (ch != ',') usageerr("unexpected char (not comma) in or between types"); |
e062dcae |
216 | cp++; |
217 | } |
218 | } |
914a5ff5 |
219 | types_a[ti]= adns_r_none; |
e062dcae |
220 | types= types_a; |
221 | argv++; |
222 | } else { |
914a5ff5 |
223 | types_a= 0; |
e062dcae |
224 | types= defaulttypes; |
225 | } |
226 | |
2345cc9b |
227 | if (!(argv[0] && argv[1])) usageerr("no query domains supplied"); |
228 | fdomlist= (const char *const*)argv+1; |
ffbda80c |
229 | |
5476fe64 |
230 | for (qc=0; fdomlist[qc]; qc++); |
ffbda80c |
231 | for (tc=0; types[tc] != adns_r_none; tc++); |
914a5ff5 |
232 | mcs= malloc(tc ? sizeof(*mcs)*qc*tc : 1); |
233 | if (!mcs) { perror("malloc mcs"); quitnow(3); } |
964344fc |
234 | |
5a0be244 |
235 | setvbuf(stdout,0,_IOLBF,0); |
236 | |
12c5b204 |
237 | if (initstring) { |
fc6a52ae |
238 | r= adns_init_strcfg(&ads, |
3e2e5fab |
239 | (adns_if_debug|adns_if_noautosys|adns_if_checkc_freq) |
240 | ^initflagsnum, |
fc6a52ae |
241 | stdout,initstring); |
12c5b204 |
242 | } else { |
fc6a52ae |
243 | r= adns_init(&ads, |
244 | (adns_if_debug|adns_if_noautosys)^initflagsnum, |
245 | 0); |
12c5b204 |
246 | } |
9da4a044 |
247 | if (r) failure_errno("init",r); |
86e7b8d9 |
248 | |
249 | for (qi=0; qi<qc; qi++) { |
9da4a044 |
250 | fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags)); |
2345cc9b |
251 | if (!consistsof(ownflags,"a")) usageerr("unknown ownqueryflag"); |
ffbda80c |
252 | for (ti=0; ti<tc; ti++) { |
669f9df5 |
253 | mc= &mcs[qi*tc+ti]; |
254 | mc->doneyet= 0; |
e42ce2b2 |
255 | mc->fdom= fdomlist[qi]; |
669f9df5 |
256 | |
1aa0213d |
257 | fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]); |
669f9df5 |
258 | r= adns_submit(ads,domain,types[ti],qflags,mc,&mc->qu); |
636b69b1 |
259 | if (r == ENOSYS) { |
916c16ca |
260 | fprintf(stdout," not implemented\n"); |
669f9df5 |
261 | mc->qu= 0; |
262 | mc->doneyet= 1; |
916c16ca |
263 | } else if (r) { |
9da4a044 |
264 | failure_errno("submit",r); |
916c16ca |
265 | } else { |
e062dcae |
266 | ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0); |
7d251914 |
267 | putc(' ',stdout); |
e062dcae |
268 | dumptype(ri,rrtn,fmtn); |
916c16ca |
269 | fprintf(stdout," submitted\n"); |
270 | } |
ffbda80c |
271 | } |
86e7b8d9 |
272 | } |
350d37d9 |
273 | |
e42ce2b2 |
274 | for (;;) { |
275 | for (qi=0; qi<qc; qi++) { |
276 | for (ti=0; ti<tc; ti++) { |
277 | mc= &mcs[qi*tc+ti]; |
278 | mc->found= 0; |
279 | } |
280 | } |
281 | for (adns_forallqueries_begin(ads); |
282 | (qu= adns_forallqueries_next(ads,&mcr)); |
283 | ) { |
284 | mc= mcr; |
285 | assert(qu == mc->qu); |
286 | assert(!mc->doneyet); |
287 | mc->found= 1; |
288 | } |
289 | mcw= 0; |
290 | for (qi=0; qi<qc; qi++) { |
291 | for (ti=0; ti<tc; ti++) { |
292 | mc= &mcs[qi*tc+ti]; |
293 | if (mc->doneyet) continue; |
294 | assert(mc->found); |
295 | if (!mcw) mcw= mc; |
296 | } |
297 | } |
298 | if (!mcw) break; |
669f9df5 |
299 | |
8ce38e76 |
300 | if (strchr(owninitflags,'s')) { |
301 | qu= mcw->qu; |
302 | mc= mcw; |
303 | } else { |
304 | qu= 0; |
305 | mc= 0; |
306 | } |
fc6a52ae |
307 | |
207130d4 |
308 | if (strchr(owninitflags,'p')) { |
940356bd |
309 | r= adns_wait_poll(ads,&qu,&ans,&mcr); |
207130d4 |
310 | } else { |
311 | r= adns_wait(ads,&qu,&ans,&mcr); |
312 | } |
313 | if (r) failure_errno("wait/check",r); |
314 | |
8ce38e76 |
315 | if (mc) assert(mcr==mc); |
316 | else mc= mcr; |
317 | assert(qu==mc->qu); |
318 | assert(!mc->doneyet); |
319 | |
207130d4 |
320 | fdom_split(mc->fdom,&domain,&qflags,ownflags,sizeof(ownflags)); |
669f9df5 |
321 | |
914a5ff5 |
322 | if (gettimeofday(&now,0)) { perror("gettimeofday"); quitnow(3); } |
73dba56e |
323 | |
207130d4 |
324 | ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0); |
325 | fprintf(stdout, "%s flags %d type ",domain,qflags); |
326 | dumptype(ri,rrtn,fmtn); |
327 | fprintf(stdout, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n", |
328 | ownflags[0] ? " ownflags=" : "", ownflags, |
329 | strchr(ownflags,'a') |
330 | ? adns_errabbrev(ans->status) |
331 | : adns_strerror(ans->status), |
332 | ans->nrrs, |
333 | ans->cname ? ans->cname : "$", |
334 | ans->owner ? ans->owner : "$", |
335 | (long)ans->expires - (long)now.tv_sec); |
336 | if (ans->nrrs) { |
337 | assert(!ri); |
338 | for (i=0; i<ans->nrrs; i++) { |
636b69b1 |
339 | ri= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes + i*len, &show); |
340 | if (ri) failure_status("info",ri); |
207130d4 |
341 | fprintf(stdout," %s\n",show); |
342 | free(show); |
86e7b8d9 |
343 | } |
207130d4 |
344 | } |
345 | free(ans); |
e42ce2b2 |
346 | |
207130d4 |
347 | mc->doneyet= 1; |
86e7b8d9 |
348 | } |
4353a5c4 |
349 | |
914a5ff5 |
350 | quitnow(0); |
964344fc |
351 | } |