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