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.
31 # define OUTPUTSTREAM stdout
36 static void failure_status(const char *what
, adns_status st
) {
37 fprintf(stderr
,"adns failure: %s: %s\n",what
,adns_strerror(st
));
41 static void failure_errno(const char *what
, int errnoval
) {
42 fprintf(stderr
,"adns failure: %s: errno=%d\n",what
,errnoval
);
46 static const char *defaultargv
[]= { "ns.chiark.greenend.org.uk", 0 };
48 static const adns_rrtype defaulttypes
[]= {
70 static void dumptype(adns_status ri
, const char *rrtn
, const char *fmtn
) {
71 fprintf(stdout
, "%s(%s)%s%s",
72 ri ?
"?" : rrtn
, ri ?
"?" : fmtn ? fmtn
: "-",
73 ri ?
" " : "", ri ?
adns_strerror(ri
) : "");
76 static void fdom_split(const char *fdom
, const char **dom_r
, int *qf_r
,
77 char *ownflags
, int ownflags_l
) {
81 qf
= strtoul(fdom
,&ep
,0);
82 if (*ep
== ',' && strchr(ep
,'/')) {
85 if (--ownflags_l
<= 0) { fputs("too many flags\n",stderr
); exit(3); }
89 if (*ep
!= '/') { *dom_r
= fdom
; *qf_r
= 0; }
90 else { *dom_r
= ep
+1; *qf_r
= qf
; }
94 int main(int argc
, char *const *argv
) {
98 const char *initstring
, *rrtn
, *fmtn
;
99 const char *const *fdomlist
, *domain
;
101 int len
, i
, qc
, qi
, tc
, ti
, ch
, qflags
;
103 const adns_rrtype
*types
;
105 adns_rrtype
*types_a
;
108 if (argv
[0] && argv
[1] && argv
[1][0] == '/') {
109 initstring
= argv
[1]+1;
115 if (argv
[0] && argv
[1] && argv
[1][0] == ':') {
116 for (cp
= argv
[1]+1, tc
=1; (ch
= *cp
); cp
++)
118 types_a
= malloc(sizeof(*types_a
)*(tc
+1));
119 if (!types_a
) { perror("malloc types"); exit(3); }
120 for (cp
= argv
[1]+1, ti
=0; ti
<tc
; ti
++) {
121 types_a
[ti
]= strtoul(cp
,&cp
,10);
124 fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr
);
137 if (argv
[0] && argv
[1]) fdomlist
= (const char *const*)argv
+1;
138 else fdomlist
= defaultargv
;
140 for (qc
=0; fdomlist
[qc
]; qc
++);
141 for (tc
=0; types
[tc
] != adns_r_none
; tc
++);
142 qus
= malloc(sizeof(qus
)*qc
*tc
);
143 if (!qus
) { perror("malloc qus"); exit(3); }
146 r
= adns_init_strcfg(&ads
,adns_if_debug
|adns_if_noautosys
,stdout
,initstring
);
148 r
= adns_init(&ads
,adns_if_debug
|adns_if_noautosys
,0);
150 if (r
) failure_errno("init",r
);
152 for (qi
=0; qi
<qc
; qi
++) {
153 fdom_split(fdomlist
[qi
],&domain
,&qflags
,ownflags
,sizeof(ownflags
));
154 for (ti
=0; ti
<tc
; ti
++) {
155 fprintf(stdout
,"%s flags %d type %d",domain
,qflags
,types
[ti
]);
156 r
= adns_submit(ads
,domain
,types
[ti
],qflags
,0,&qus
[qi
*tc
+ti
]);
157 if (r
== adns_s_unknownrrtype
) {
158 fprintf(stdout
," not implemented\n");
161 failure_errno("submit",r
);
163 ri
= adns_rr_info(types
[ti
], &rrtn
,&fmtn
,0, 0,0);
165 dumptype(ri
,rrtn
,fmtn
);
166 fprintf(stdout
," submitted\n");
171 for (qi
=0; qi
<qc
; qi
++) {
172 fdom_split(fdomlist
[qi
],&domain
,&qflags
,ownflags
,sizeof(ownflags
));
174 for (ti
=0; ti
<tc
; ti
++) {
178 r
= adns_wait(ads
,&qu
,&ans
,0);
179 if (r
) failure_errno("wait",r
);
181 if (gettimeofday(&now
,0)) { perror("gettimeofday"); exit(3); }
183 ri
= adns_rr_info(ans
->type
, &rrtn
,&fmtn
,&len
, 0,0);
184 fprintf(stdout
, "%s flags %d type ",domain
,qflags
);
185 dumptype(ri
,rrtn
,fmtn
);
186 fprintf(stdout
, "%s%s: %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
187 ownflags
[0] ?
" ownflags=" : "", ownflags
,
188 strchr(ownflags
,'a') ?
adns_errabbrev(ans
->status
)
189 : adns_strerror(ans
->status
),
191 ans
->cname ? ans
->cname
: "$",
192 ans
->owner ? ans
->owner
: "$",
193 (long)ans
->expires
- (long)now
.tv_sec
);
196 for (i
=0; i
<ans
->nrrs
; i
++) {
197 r
= adns_rr_info(ans
->type
, 0,0,0, ans
->rrs
.bytes
+ i
*len
, &show
);
198 if (r
) failure_status("info",r
);
199 fprintf(stdout
," %s\n",show
);