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.
30 # define OUTPUTSTREAM stdout
35 static void failure(const char *what, adns_status st) {
36 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
40 static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
42 static const adns_rrtype defaulttypes[]= {
64 static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
65 fprintf(stdout, "%s(%s)%s%s",
66 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
67 ri ? " " : "", ri ? adns_strerror(ri) : "");
70 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r) {
74 qf= strtoul(fdom,&ep,0);
75 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
76 else { *dom_r= ep+1; *qf_r= qf; }
79 int main(int argc, char *const *argv) {
83 const char *initstring, *rrtn, *fmtn;
84 const char *const *fdomlist, *domain;
86 int len, i, qc, qi, tc, ti, ch, qflags;
88 const adns_rrtype *types;
92 if (argv[0] && argv[1] && argv[1][0] == '/') {
93 initstring= argv[1]+1;
99 if (argv[0] && argv[1] && argv[1][0] == ':') {
100 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
102 types_a= malloc(sizeof(*types_a)*tc);
103 if (!types_a) { perror("malloc types"); exit(3); }
104 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
105 types_a[ti]= strtoul(cp,&cp,10);
108 fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr);
120 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
121 else fdomlist= defaultargv;
123 for (qc=0; fdomlist[qc]; qc++);
124 for (tc=0; types[tc] != adns_r_none; tc++);
125 qus= malloc(sizeof(qus)*qc*tc);
126 if (!qus) { perror("malloc qus"); exit(3); }
129 r= adns_init_strcfg(&ads,adns_if_debug|adns_if_noautosys,stdout,initstring);
131 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
133 if (r) failure("init",r);
135 for (qi=0; qi<qc; qi++) {
136 fdom_split(fdomlist[qi],&domain,&qflags);
137 for (ti=0; ti<tc; ti++) {
138 fprintf(stdout,"%s flags %d type %d",domain,qflags,types[ti]);
139 r= adns_submit(ads,domain,types[ti],qflags,0,&qus[qi*tc+ti]);
140 if (r == adns_s_unknownrrtype) {
141 fprintf(stdout," not implemented\n");
146 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
148 dumptype(ri,rrtn,fmtn);
149 fprintf(stdout," submitted\n");
154 for (qi=0; qi<qc; qi++) {
155 fdom_split(fdomlist[qi],&domain,&qflags);
157 for (ti=0; ti<tc; ti++) {
161 r= adns_wait(ads,&qu,&ans,0);
162 if (r) failure("wait",r);
164 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
166 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
167 fprintf(stdout, "%s flags %d type ",domain,qflags);
168 dumptype(ri,rrtn,fmtn);
169 fprintf(stdout, ": %s; nrrs=%d; cname=%s; owner=%s; ttl=%ld\n",
170 adns_strerror(ans->status),
172 ans->cname ? ans->cname : "$",
173 ans->owner ? ans->owner : "$",
174 (long)ans->expires - (long)now.tv_sec);
177 for (i=0; i<ans->nrrs; i++) {
178 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
179 if (r) failure("info",r);
180 fprintf(stdout," %s\n",show);