Include TTL (well, actually, expiry time) in answers.
[adns] / client / adnstest.c
1 /*
2 * dtest.c
3 * - simple test program, not part of the library
4 */
5 /*
6 * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
7 *
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)
11 * any later version.
12 *
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.
17 *
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.
21 */
22
23 #include <stdio.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <assert.h>
27 #include <stdlib.h>
28
29 #ifndef OUTPUTSTREAM
30 # define OUTPUTSTREAM stdout
31 #endif
32
33 #include "adns.h"
34
35 static void failure(const char *what, adns_status st) {
36 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
37 exit(2);
38 }
39
40 static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
41
42 static const adns_rrtype defaulttypes[]= {
43 adns_r_a,
44 adns_r_ns_raw,
45 adns_r_cname,
46 adns_r_soa_raw,
47 adns_r_ptr_raw,
48 adns_r_hinfo,
49 adns_r_mx_raw,
50 adns_r_txt,
51 adns_r_rp_raw,
52
53 adns_r_addr,
54 adns_r_ns,
55 adns_r_ptr,
56 adns_r_mx,
57
58 adns_r_soa,
59 adns_r_rp,
60
61 adns_r_none
62 };
63
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) : "");
68 }
69
70 int main(int argc, char *const *argv) {
71 adns_state ads;
72 adns_query *qus, qu;
73 adns_answer *ans;
74 const char *initstring, *rrtn, *fmtn;
75 const char *const *domlist;
76 char *show, *cp;
77 int len, i, qc, qi, tc, ti, ch;
78 adns_status r, ri;
79 const adns_rrtype *types;
80 struct timeval now;
81 adns_rrtype *types_a;
82
83 if (argv[0] && argv[1] && argv[1][0] == '/') {
84 initstring= argv[1]+1;
85 argv++;
86 } else {
87 initstring= 0;
88 }
89
90 if (argv[0] && argv[1] && argv[1][0] == ':') {
91 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
92 if (ch==',') tc++;
93 types_a= malloc(sizeof(*types_a)*tc);
94 if (!types_a) { perror("malloc types"); exit(3); }
95 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
96 types_a[ti]= strtoul(cp,&cp,10);
97 if ((ch= *cp)) {
98 if (ch != ',') {
99 fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr);
100 exit(4);
101 }
102 cp++;
103 }
104 }
105 types= types_a;
106 argv++;
107 } else {
108 types= defaulttypes;
109 }
110
111 if (argv[0] && argv[1]) domlist= (const char *const*)argv+1;
112 else domlist= defaultargv;
113
114 for (qc=0; qc[domlist]; qc++);
115 for (tc=0; types[tc] != adns_r_none; tc++);
116 qus= malloc(sizeof(qus)*qc*tc);
117 if (!qus) { perror("malloc qus"); exit(3); }
118
119 if (initstring) {
120 r= adns_init_strcfg(&ads,adns_if_debug|adns_if_noautosys,stdout,initstring);
121 } else {
122 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
123 }
124 if (r) failure("init",r);
125
126 for (qi=0; qi<qc; qi++) {
127 for (ti=0; ti<tc; ti++) {
128 fprintf(stdout,"%s type %d",domlist[qi],types[ti]);
129 r= adns_submit(ads,domlist[qi],types[ti],0,0,&qus[qi*tc+ti]);
130 if (r == adns_s_unknownrrtype) {
131 fprintf(stdout," not implemented\n");
132 qus[qi*tc+ti]= 0;
133 } else if (r) {
134 failure("submit",r);
135 } else {
136 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
137 putc(' ',stdout);
138 dumptype(ri,rrtn,fmtn);
139 fprintf(stdout," submitted\n");
140 }
141 }
142 }
143
144 for (qi=0; qi<qc; qi++) {
145 for (ti=0; ti<tc; ti++) {
146 qu= qus[qi*tc+ti];
147 if (!qu) continue;
148 r= adns_wait(ads,&qu,&ans,0);
149 if (r) failure("wait",r);
150
151 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
152
153 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
154 fprintf(stdout, "%s type ", domlist[qi]);
155 dumptype(ri,rrtn,fmtn);
156 fprintf(stdout, ": %s; nrrs=%d; cname=%s; ttl=%ld\n",
157 adns_strerror(ans->status),
158 ans->nrrs, ans->cname ? ans->cname : "$",
159 (long)ans->expires - (long)now.tv_sec);
160 if (ans->nrrs) {
161 assert(!ri);
162 for (i=0; i<ans->nrrs; i++) {
163 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
164 if (r) failure("info",r);
165 fprintf(stdout," %s\n",show);
166 free(show);
167 }
168 }
169 free(ans);
170 }
171 }
172
173 free(qus);
174 adns_finish(ads);
175
176 exit(0);
177 }