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.
33 # define OUTPUTSTREAM stdout
38 static void failure_status(const char *what
, adns_status st
) {
39 fprintf(stderr
,"adns failure: %s: %s\n",what
,adns_strerror(st
));
43 static void failure_errno(const char *what
, int errnoval
) {
44 fprintf(stderr
,"adns failure: %s: errno=%d\n",what
,errnoval
);
48 static const char *defaultargv
[]= { "ns.chiark.greenend.org.uk", 0 };
50 static const adns_rrtype defaulttypes
[]= {
72 static void dumptype(adns_status ri
, const char *rrtn
, const char *fmtn
) {
73 fprintf(stdout
, "%s(%s)%s%s",
74 ri ?
"?" : rrtn
, ri ?
"?" : fmtn ? fmtn
: "-",
75 ri ?
" " : "", ri ?
adns_strerror(ri
) : "");
78 static void fdom_split(const char *fdom
, const char **dom_r
, int *qf_r
,
79 char *ownflags
, int ownflags_l
) {
83 qf
= strtoul(fdom
,&ep
,0);
84 if (*ep
== ',' && strchr(ep
,'/')) {
87 if (--ownflags_l
<= 0) { fputs("too many flags\n",stderr
); exit(3); }
91 if (*ep
!= '/') { *dom_r
= fdom
; *qf_r
= 0; }
92 else { *dom_r
= ep
+1; *qf_r
= qf
; }
96 static int consistsof(const char *string
, const char *accept
) {
97 return strspn(string
,accept
) == strlen(string
);
100 int main(int argc
, char *const *argv
) {
109 struct myctx
*mcs
, *mc
, *mcw
;
112 const char *initstring
, *rrtn
, *fmtn
;
113 const char *const *fdomlist
, *domain
;
115 int len
, i
, qc
, qi
, tc
, ti
, ch
, qflags
, initflagsnum
, npollfds
, npollfdsavail
, timeout
;
116 struct pollfd
*pollfds
;
118 const adns_rrtype
*types
;
120 adns_rrtype
*types_a
;
123 const char *initflags
, *owninitflags
;
125 if (argv
[0] && argv
[1] && argv
[1][0] == '-') {
126 initflags
= argv
[1]+1;
131 if (argv
[0] && argv
[1] && argv
[1][0] == '/') {
132 initstring
= argv
[1]+1;
138 initflagsnum
= strtoul(initflags
,&ep
,0);
141 if (!consistsof(owninitflags
,"p")) {
142 fputs("unknown owninitflag\n",stderr
);
148 fputs("bad <initflagsnum>[,<owninitflags>]\n",stderr
);
152 if (argv
[0] && argv
[1] && argv
[1][0] == ':') {
153 for (cp
= argv
[1]+1, tc
=1; (ch
= *cp
); cp
++)
155 types_a
= malloc(sizeof(*types_a
)*(tc
+1));
156 if (!types_a
) { perror("malloc types"); exit(3); }
157 for (cp
= argv
[1]+1, ti
=0; ti
<tc
; ti
++) {
158 types_a
[ti
]= strtoul(cp
,&cp
,10);
161 fputs("usage: adnstest [-<initflagsnum>[,<owninitflags>]] [/<initstring>]\n"
162 " [ :<typenum>,... ]\n"
163 " [ [<queryflagsnum>[,<ownqueryflags>]/]<domain> ... ]\n"
164 "initflags: p use poll(2) instead of select(2)\n"
165 "queryflags: a print status abbrevs instead of strings\n",
179 if (argv
[0] && argv
[1]) fdomlist
= (const char *const*)argv
+1;
180 else fdomlist
= defaultargv
;
182 for (qc
=0; fdomlist
[qc
]; qc
++);
183 for (tc
=0; types
[tc
] != adns_r_none
; tc
++);
184 mcs
= malloc(sizeof(*mcs
)*qc
*tc
);
185 if (!mcs
) { perror("malloc mcs"); exit(3); }
188 r
= adns_init_strcfg(&ads
,
189 (adns_if_debug
|adns_if_noautosys
)^initflagsnum
,
193 (adns_if_debug
|adns_if_noautosys
)^initflagsnum
,
196 if (r
) failure_errno("init",r
);
201 for (qi
=0; qi
<qc
; qi
++) {
202 fdom_split(fdomlist
[qi
],&domain
,&qflags
,ownflags
,sizeof(ownflags
));
203 if (!consistsof(ownflags
,"a")) {
204 fputs("unknown ownqueryflag\n",stderr
);
207 for (ti
=0; ti
<tc
; ti
++) {
210 mc
->fdom
= fdomlist
[qi
];
212 fprintf(stdout
,"%s flags %d type %d",domain
,qflags
,types
[ti
]);
213 r
= adns_submit(ads
,domain
,types
[ti
],qflags
,mc
,&mc
->qu
);
214 if (r
== adns_s_unknownrrtype
) {
215 fprintf(stdout
," not implemented\n");
219 failure_errno("submit",r
);
221 ri
= adns_rr_info(types
[ti
], &rrtn
,&fmtn
,0, 0,0);
223 dumptype(ri
,rrtn
,fmtn
);
224 fprintf(stdout
," submitted\n");
230 for (qi
=0; qi
<qc
; qi
++) {
231 for (ti
=0; ti
<tc
; ti
++) {
236 for (adns_forallqueries_begin(ads
);
237 (qu
= adns_forallqueries_next(ads
,&mcr
));
240 assert(qu
== mc
->qu
);
241 assert(!mc
->doneyet
);
245 for (qi
=0; qi
<qc
; qi
++) {
246 for (ti
=0; ti
<tc
; ti
++) {
248 if (mc
->doneyet
) continue;
258 if (strchr(owninitflags
,'p')) {
260 r
= adns_check(ads
,&qu
,&ans
,&mcr
);
261 if (r
!= EWOULDBLOCK
) break;
263 npollfds
= npollfdsavail
;
265 r
= adns_beforepoll(ads
, pollfds
, &npollfds
, &timeout
, 0);
266 if (r
!= ERANGE
) break;
267 pollfds
= realloc(pollfds
,sizeof(*pollfds
)*npollfds
);
268 if (!pollfds
) failure_errno("realloc pollfds",errno
);
269 npollfdsavail
= npollfds
;
271 if (r
) failure_errno("beforepoll",r
);
272 r
= poll(pollfds
,npollfds
,timeout
);
273 if (r
== -1) failure_errno("poll",errno
);
274 adns_afterpoll(ads
,pollfds
, r?npollfds
:0, 0);
277 r
= adns_wait(ads
,&qu
,&ans
,&mcr
);
279 if (r
) failure_errno("wait/check",r
);
282 fdom_split(mc
->fdom
,&domain
,&qflags
,ownflags
,sizeof(ownflags
));
284 if (gettimeofday(&now
,0)) { perror("gettimeofday"); exit(3); }
286 ri
= adns_rr_info(ans
->type
, &rrtn
,&fmtn
,&len
, 0,0);
287 fprintf(stdout
, "%s flags %d type ",domain
,qflags
);
288 dumptype(ri
,rrtn
,fmtn
);
289 fprintf(stdout
, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
290 ownflags
[0] ?
" ownflags=" : "", ownflags
,
292 ?
adns_errabbrev(ans
->status
)
293 : adns_strerror(ans
->status
),
295 ans
->cname ? ans
->cname
: "$",
296 ans
->owner ? ans
->owner
: "$",
297 (long)ans
->expires
- (long)now
.tv_sec
);
300 for (i
=0; i
<ans
->nrrs
; i
++) {
301 r
= adns_rr_info(ans
->type
, 0,0,0, ans
->rrs
.bytes
+ i
*len
, &show
);
302 if (r
) failure_status("info",r
);
303 fprintf(stdout
," %s\n",show
);