3 * - simple test program, not part of the library
7 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
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>
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)
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.
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.
41 # define OUTPUTSTREAM stdout
46 int poll(struct pollfd
*ufds
, int nfds
, int timeout
) {
47 fputs("poll(2) not supported on this system\n",stderr
);
50 #define adns_beforepoll(a,b,c,d,e) 0
51 #define adns_afterpoll(a,b,c,d) 0
54 static void failure_status(const char *what
, adns_status st
) NONRETURNING
;
55 static void failure_status(const char *what
, adns_status st
) {
56 fprintf(stderr
,"adns failure: %s: %s\n",what
,adns_strerror(st
));
60 static void failure_errno(const char *what
, int errnoval
) NONRETURNING
;
61 static void failure_errno(const char *what
, int errnoval
) {
62 fprintf(stderr
,"adns failure: %s: errno=%d\n",what
,errnoval
);
66 static void usageerr(const char *why
) NONRETURNING
;
67 static void usageerr(const char *why
) {
70 "usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
71 " [ :<typenum>,... ]\n"
72 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
73 "initflags: p use poll(2) instead of select(2)\n"
74 " s use adns_wait with specified query, instead of 0\n"
75 "queryflags: a print status abbrevs instead of strings\n"
76 "exit status: 0 ok (though some queries may have failed)\n"
77 " 1 used by test harness to indicate test failed\n"
78 " 2 unable to submit or init or some such\n"
79 " 3 unexpected failure\n"
81 " 5 operation not supported on this system\n",
86 static const adns_rrtype defaulttypes
[]= {
108 static void dumptype(adns_status ri
, const char *rrtn
, const char *fmtn
) {
109 fprintf(stdout
, "%s(%s)%s%s",
110 ri ?
"?" : rrtn
, ri ?
"?" : fmtn ? fmtn
: "-",
111 ri ?
" " : "", ri ?
adns_strerror(ri
) : "");
114 static void fdom_split(const char *fdom
, const char **dom_r
, int *qf_r
,
115 char *ownflags
, int ownflags_l
) {
119 qf
= strtoul(fdom
,&ep
,0);
120 if (*ep
== ',' && strchr(ep
,'/')) {
123 if (--ownflags_l
<= 0) { fputs("too many flags\n",stderr
); exit(3); }
127 if (*ep
!= '/') { *dom_r
= fdom
; *qf_r
= 0; }
128 else { *dom_r
= ep
+1; *qf_r
= qf
; }
132 static int consistsof(const char *string
, const char *accept
) {
133 return strspn(string
,accept
) == strlen(string
);
136 int main(int argc
, char *const *argv
) {
145 struct myctx
*mcs
, *mc
, *mcw
;
148 const char *initstring
, *rrtn
, *fmtn
;
149 const char *const *fdomlist
, *domain
;
151 int len
, i
, qc
, qi
, tc
, ti
, ch
, qflags
, initflagsnum
;
154 const adns_rrtype
*types
;
156 adns_rrtype
*types_a
;
159 const char *initflags
, *owninitflags
;
161 if (argv
[0] && argv
[1] && argv
[1][0] == '-') {
162 initflags
= argv
[1]+1;
167 if (argv
[0] && argv
[1] && argv
[1][0] == '/') {
168 initstring
= argv
[1]+1;
174 initflagsnum
= strtoul(initflags
,&ep
,0);
177 if (!consistsof(owninitflags
,"ps")) usageerr("unknown owninitflag");
181 usageerr("bad <initflagsnum>[,<owninitflags>]");
184 if (argv
[0] && argv
[1] && argv
[1][0] == ':') {
185 for (cp
= argv
[1]+1, tc
=1; (ch
= *cp
); cp
++)
187 types_a
= malloc(sizeof(*types_a
)*(tc
+1));
188 if (!types_a
) { perror("malloc types"); exit(3); }
189 for (cp
= argv
[1]+1, ti
=0; ti
<tc
; ti
++) {
190 types_a
[ti
]= strtoul(cp
,&cp
,10);
192 if (ch
!= ',') usageerr("unexpected char (not comma) in or between types");
203 if (!(argv
[0] && argv
[1])) usageerr("no query domains supplied");
204 fdomlist
= (const char *const*)argv
+1;
206 for (qc
=0; fdomlist
[qc
]; qc
++);
207 for (tc
=0; types
[tc
] != adns_r_none
; tc
++);
208 mcs
= malloc(sizeof(*mcs
)*qc
*tc
);
209 if (!mcs
) { perror("malloc mcs"); exit(3); }
212 r
= adns_init_strcfg(&ads
,
213 (adns_if_debug
|adns_if_noautosys
|adns_if_checkc_freq
)
218 (adns_if_debug
|adns_if_noautosys
)^initflagsnum
,
221 if (r
) failure_errno("init",r
);
223 for (qi
=0; qi
<qc
; qi
++) {
224 fdom_split(fdomlist
[qi
],&domain
,&qflags
,ownflags
,sizeof(ownflags
));
225 if (!consistsof(ownflags
,"a")) usageerr("unknown ownqueryflag");
226 for (ti
=0; ti
<tc
; ti
++) {
229 mc
->fdom
= fdomlist
[qi
];
231 fprintf(stdout
,"%s flags %d type %d",domain
,qflags
,types
[ti
]);
232 r
= adns_submit(ads
,domain
,types
[ti
],qflags
,mc
,&mc
->qu
);
234 fprintf(stdout
," not implemented\n");
238 failure_errno("submit",r
);
240 ri
= adns_rr_info(types
[ti
], &rrtn
,&fmtn
,0, 0,0);
242 dumptype(ri
,rrtn
,fmtn
);
243 fprintf(stdout
," submitted\n");
249 for (qi
=0; qi
<qc
; qi
++) {
250 for (ti
=0; ti
<tc
; ti
++) {
255 for (adns_forallqueries_begin(ads
);
256 (qu
= adns_forallqueries_next(ads
,&mcr
));
259 assert(qu
== mc
->qu
);
260 assert(!mc
->doneyet
);
264 for (qi
=0; qi
<qc
; qi
++) {
265 for (ti
=0; ti
<tc
; ti
++) {
267 if (mc
->doneyet
) continue;
274 if (strchr(owninitflags
,'s')) {
282 if (strchr(owninitflags
,'p')) {
283 r
= adns_wait_poll(ads
,&qu
,&ans
,&mcr
);
285 r
= adns_wait(ads
,&qu
,&ans
,&mcr
);
287 if (r
) failure_errno("wait/check",r
);
289 if (mc
) assert(mcr
==mc
);
292 assert(!mc
->doneyet
);
294 fdom_split(mc
->fdom
,&domain
,&qflags
,ownflags
,sizeof(ownflags
));
296 if (gettimeofday(&now
,0)) { perror("gettimeofday"); exit(3); }
298 ri
= adns_rr_info(ans
->type
, &rrtn
,&fmtn
,&len
, 0,0);
299 fprintf(stdout
, "%s flags %d type ",domain
,qflags
);
300 dumptype(ri
,rrtn
,fmtn
);
301 fprintf(stdout
, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
302 ownflags
[0] ?
" ownflags=" : "", ownflags
,
304 ?
adns_errabbrev(ans
->status
)
305 : adns_strerror(ans
->status
),
307 ans
->cname ? ans
->cname
: "$",
308 ans
->owner ? ans
->owner
: "$",
309 (long)ans
->expires
- (long)now
.tv_sec
);
312 for (i
=0; i
<ans
->nrrs
; i
++) {
313 ri
= adns_rr_info(ans
->type
, 0,0,0, ans
->rrs
.bytes
+ i
*len
, &show
);
314 if (ri
) failure_status("info",ri
);
315 fprintf(stdout
," %s\n",show
);