New poll(2) stuff etc. Does not work yet, but compiles.
[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 #include <string.h>
29
30 #ifndef OUTPUTSTREAM
31 # define OUTPUTSTREAM stdout
32 #endif
33
34 #include "adns.h"
35
36 static void failure_status(const char *what, adns_status st) {
37 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
38 exit(2);
39 }
40
41 static void failure_errno(const char *what, int errnoval) {
42 fprintf(stderr,"adns failure: %s: errno=%d\n",what,errnoval);
43 exit(2);
44 }
45
46 static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
47
48 static const adns_rrtype defaulttypes[]= {
49 adns_r_a,
50 adns_r_ns_raw,
51 adns_r_cname,
52 adns_r_soa_raw,
53 adns_r_ptr_raw,
54 adns_r_hinfo,
55 adns_r_mx_raw,
56 adns_r_txt,
57 adns_r_rp_raw,
58
59 adns_r_addr,
60 adns_r_ns,
61 adns_r_ptr,
62 adns_r_mx,
63
64 adns_r_soa,
65 adns_r_rp,
66
67 adns_r_none
68 };
69
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) : "");
74 }
75
76 static void fdom_split(const char *fdom, const char **dom_r, int *qf_r,
77 char *ownflags, int ownflags_l) {
78 int qf;
79 char *ep;
80
81 qf= strtoul(fdom,&ep,0);
82 if (*ep == ',' && strchr(ep,'/')) {
83 ep++;
84 while (*ep != '/') {
85 if (--ownflags_l <= 0) { fputs("too many flags\n",stderr); exit(3); }
86 *ownflags++= *ep++;
87 }
88 }
89 if (*ep != '/') { *dom_r= fdom; *qf_r= 0; }
90 else { *dom_r= ep+1; *qf_r= qf; }
91 *ownflags= 0;
92 }
93
94 int main(int argc, char *const *argv) {
95 adns_state ads;
96 adns_query *qus, qu;
97 adns_answer *ans;
98 const char *initstring, *rrtn, *fmtn;
99 const char *const *fdomlist, *domain;
100 char *show, *cp;
101 int len, i, qc, qi, tc, ti, ch, qflags;
102 adns_status r, ri;
103 const adns_rrtype *types;
104 struct timeval now;
105 adns_rrtype *types_a;
106 char ownflags[10];
107
108 if (argv[0] && argv[1] && argv[1][0] == '/') {
109 initstring= argv[1]+1;
110 argv++;
111 } else {
112 initstring= 0;
113 }
114
115 if (argv[0] && argv[1] && argv[1][0] == ':') {
116 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
117 if (ch==',') tc++;
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);
122 if ((ch= *cp)) {
123 if (ch != ',') {
124 fputs("usage: dtest [/<initstring>] [:<typenum>,...] [<domain> ...]\n",stderr);
125 exit(4);
126 }
127 cp++;
128 }
129 }
130 *cp++= adns_r_none;
131 types= types_a;
132 argv++;
133 } else {
134 types= defaulttypes;
135 }
136
137 if (argv[0] && argv[1]) fdomlist= (const char *const*)argv+1;
138 else fdomlist= defaultargv;
139
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); }
144
145 if (initstring) {
146 r= adns_init_strcfg(&ads,adns_if_debug|adns_if_noautosys,stdout,initstring);
147 } else {
148 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
149 }
150 if (r) failure_errno("init",r);
151
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");
159 qus[qi*tc+ti]= 0;
160 } else if (r) {
161 failure_errno("submit",r);
162 } else {
163 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
164 putc(' ',stdout);
165 dumptype(ri,rrtn,fmtn);
166 fprintf(stdout," submitted\n");
167 }
168 }
169 }
170
171 for (qi=0; qi<qc; qi++) {
172 fdom_split(fdomlist[qi],&domain,&qflags,ownflags,sizeof(ownflags));
173
174 for (ti=0; ti<tc; ti++) {
175 qu= qus[qi*tc+ti];
176 if (!qu) continue;
177
178 r= adns_wait(ads,&qu,&ans,0);
179 if (r) failure_errno("wait",r);
180
181 if (gettimeofday(&now,0)) { perror("gettimeofday"); exit(3); }
182
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),
190 ans->nrrs,
191 ans->cname ? ans->cname : "$",
192 ans->owner ? ans->owner : "$",
193 (long)ans->expires - (long)now.tv_sec);
194 if (ans->nrrs) {
195 assert(!ri);
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);
200 free(show);
201 }
202 }
203 free(ans);
204 }
205 }
206
207 free(qus);
208 adns_finish(ads);
209
210 exit(0);
211 }