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