3 * - simple test program, not part of the library
6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
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)
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.
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.
36 # define OUTPUTSTREAM stdout
41 int poll(struct pollfd
*ufds
, int nfds
, int timeout
) {
42 fputs("poll(2) not supported on this system\n",stderr
);
45 #define adns_beforepoll(a,b,c,d,e) 0
46 #define adns_afterpoll(a,b,c,d) 0
49 static void failure_status(const char *what
, adns_status st
) NONRETURNING
;
50 static void failure_status(const char *what
, adns_status st
) {
51 fprintf(stderr
,"adns failure: %s: %s\n",what
,adns_strerror(st
));
55 static void failure_errno(const char *what
, int errnoval
) NONRETURNING
;
56 static void failure_errno(const char *what
, int errnoval
) {
57 fprintf(stderr
,"adns failure: %s: errno=%d\n",what
,errnoval
);
61 static void usageerr(const char *why
) NONRETURNING
;
62 static void usageerr(const char *why
) {
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"
76 " 5 operation not supported on this system\n",
81 static const adns_rrtype defaulttypes
[]= {
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
) : "");
109 static void fdom_split(const char *fdom
, const char **dom_r
, int *qf_r
,
110 char *ownflags
, int ownflags_l
) {
114 qf
= strtoul(fdom
,&ep
,0);
115 if (*ep
== ',' && strchr(ep
,'/')) {
118 if (--ownflags_l
<= 0) { fputs("too many flags\n",stderr
); exit(3); }
122 if (*ep
!= '/') { *dom_r
= fdom
; *qf_r
= 0; }
123 else { *dom_r
= ep
+1; *qf_r
= qf
; }
127 static int consistsof(const char *string
, const char *accept
) {
128 return strspn(string
,accept
) == strlen(string
);
131 int main(int argc
, char *const *argv
) {
140 struct myctx
*mcs
, *mc
, *mcw
;
143 const char *initstring
, *rrtn
, *fmtn
;
144 const char *const *fdomlist
, *domain
;
146 int len
, i
, qc
, qi
, tc
, ti
, ch
, qflags
, initflagsnum
;
149 const adns_rrtype
*types
;
151 adns_rrtype
*types_a
;
154 const char *initflags
, *owninitflags
;
156 if (argv
[0] && argv
[1] && argv
[1][0] == '-') {
157 initflags
= argv
[1]+1;
162 if (argv
[0] && argv
[1] && argv
[1][0] == '/') {
163 initstring
= argv
[1]+1;
169 initflagsnum
= strtoul(initflags
,&ep
,0);
172 if (!consistsof(owninitflags
,"ps")) usageerr("unknown owninitflag");
176 usageerr("bad <initflagsnum>[,<owninitflags>]");
179 if (argv
[0] && argv
[1] && argv
[1][0] == ':') {
180 for (cp
= argv
[1]+1, tc
=1; (ch
= *cp
); cp
++)
182 types_a
= malloc(sizeof(*types_a
)*(tc
+1));
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);
187 if (ch
!= ',') usageerr("unexpected char (not comma) in or between types");
198 if (!(argv
[0] && argv
[1])) usageerr("no query domains supplied");
199 fdomlist
= (const char *const*)argv
+1;
201 for (qc
=0; fdomlist
[qc
]; qc
++);
202 for (tc
=0; types
[tc
] != adns_r_none
; tc
++);
203 mcs
= malloc(sizeof(*mcs
)*qc
*tc
);
204 if (!mcs
) { perror("malloc mcs"); exit(3); }
207 r
= adns_init_strcfg(&ads
,
208 (adns_if_debug
|adns_if_noautosys
|adns_if_checkc_freq
)
213 (adns_if_debug
|adns_if_noautosys
)^initflagsnum
,
216 if (r
) failure_errno("init",r
);
218 for (qi
=0; qi
<qc
; qi
++) {
219 fdom_split(fdomlist
[qi
],&domain
,&qflags
,ownflags
,sizeof(ownflags
));
220 if (!consistsof(ownflags
,"a")) usageerr("unknown ownqueryflag");
221 for (ti
=0; ti
<tc
; ti
++) {
224 mc
->fdom
= fdomlist
[qi
];
226 fprintf(stdout
,"%s flags %d type %d",domain
,qflags
,types
[ti
]);
227 r
= adns_submit(ads
,domain
,types
[ti
],qflags
,mc
,&mc
->qu
);
229 fprintf(stdout
," not implemented\n");
233 failure_errno("submit",r
);
235 ri
= adns_rr_info(types
[ti
], &rrtn
,&fmtn
,0, 0,0);
237 dumptype(ri
,rrtn
,fmtn
);
238 fprintf(stdout
," submitted\n");
244 for (qi
=0; qi
<qc
; qi
++) {
245 for (ti
=0; ti
<tc
; ti
++) {
250 for (adns_forallqueries_begin(ads
);
251 (qu
= adns_forallqueries_next(ads
,&mcr
));
254 assert(qu
== mc
->qu
);
255 assert(!mc
->doneyet
);
259 for (qi
=0; qi
<qc
; qi
++) {
260 for (ti
=0; ti
<tc
; ti
++) {
262 if (mc
->doneyet
) continue;
269 if (strchr(owninitflags
,'s')) {
277 if (strchr(owninitflags
,'p')) {
278 r
= adns_wait_poll(ads
,&qu
,&ans
,&mcr
);
280 r
= adns_wait(ads
,&qu
,&ans
,&mcr
);
282 if (r
) failure_errno("wait/check",r
);
284 if (mc
) assert(mcr
==mc
);
287 assert(!mc
->doneyet
);
289 fdom_split(mc
->fdom
,&domain
,&qflags
,ownflags
,sizeof(ownflags
));
291 if (gettimeofday(&now
,0)) { perror("gettimeofday"); exit(3); }
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
,
299 ?
adns_errabbrev(ans
->status
)
300 : adns_strerror(ans
->status
),
302 ans
->cname ? ans
->cname
: "$",
303 ans
->owner ? ans
->owner
: "$",
304 (long)ans
->expires
- (long)now
.tv_sec
);
307 for (i
=0; i
<ans
->nrrs
; i
++) {
308 ri
= adns_rr_info(ans
->type
, 0,0,0, ans
->rrs
.bytes
+ i
*len
, &show
);
309 if (ri
) failure_status("info",ri
);
310 fprintf(stdout
," %s\n",show
);