chiark / gitweb /
Halfway through A lookups; need to restructure pa_... argument passing.
[adns] / client / adnstest.c
CommitLineData
98db6da3 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, 1998 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 */
e7afa27f 22
23#include <stdio.h>
31d3a9d0 24#include <unistd.h>
1e9efa71 25#include <assert.h>
26#include <stdlib.h>
e7afa27f 27
28#include "adns.h"
29
1e9efa71 30static void failure(const char *what, adns_status st) {
31 fprintf(stderr,"adns failure: %s: %s\n",what,adns_strerror(st));
32 exit(2);
33}
34
35static const char *defaultargv[]= { "ns.chiark.greenend.org.uk", 0 };
36
cfdca685 37static const adns_rrtype defaulttypes[]= {
38 adns_r_a,
39 adns_r_ns_raw,
40 adns_r_cname,
41 adns_r_ptr_raw,
f2ad23ee 42 adns_r_mx_raw,
43 adns_r_rp_raw,
d4c7b0b1 44 adns_r_txt,
cd363ffd 45 adns_r_addr,
cfdca685 46 adns_r_none
47};
48
f2ad23ee 49static void dumptype(adns_status ri, const char *rrtn, const char *fmtn) {
50 fprintf(stdout, "%s(%s)%s%s",
51 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
52 ri ? " " : "", ri ? adns_strerror(ri) : "");
53}
54
55int main(int argc, char *const *argv) {
e7afa27f 56 adns_state ads;
1e9efa71 57 adns_query *qus, qu;
d05cc330 58 adns_answer *ans;
f2ad23ee 59 const char *rrtn, *fmtn, *const *domlist;
60 char *show, *cp;
61 int len, i, qc, qi, tc, ti, ch;
1e9efa71 62 adns_status r, ri;
cfdca685 63 const adns_rrtype *types;
f2ad23ee 64 adns_rrtype *types_a;
1e9efa71 65
f2ad23ee 66 if (argv[0] && argv[1] && argv[1][0] == ':') {
67 for (cp= argv[1]+1, tc=1; (ch= *cp); cp++)
68 if (ch==',') tc++;
69 types_a= malloc(sizeof(*types_a)*tc);
70 if (!types_a) { perror("malloc types"); exit(3); }
71 for (cp= argv[1]+1, ti=0; ti<tc; ti++) {
72 types_a[ti]= strtoul(cp,&cp,10);
73 if ((ch= *cp)) {
74 if (ch != ':') {
75 fputs("usage: dtest [:<typenum>,...] [<domain> ...]",stderr);
76 exit(4);
77 }
78 cp++;
79 }
80 }
81 types= types_a;
82 argv++;
83 } else {
84 types= defaulttypes;
85 }
86
87 if (argv[0] && argv[1]) domlist= (const char *const*)argv+1;
88 else domlist= defaultargv;
cfdca685 89
f2ad23ee 90 for (qc=0; qc[domlist]; qc++);
cfdca685 91 for (tc=0; types[tc] != adns_r_none; tc++);
92 qus= malloc(sizeof(qus)*qc*tc);
1e9efa71 93 if (!qus) { perror("malloc qus"); exit(3); }
e7afa27f 94
d05cc330 95 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
1e9efa71 96 if (r) failure("init",r);
97
98 for (qi=0; qi<qc; qi++) {
cfdca685 99 for (ti=0; ti<tc; ti++) {
f2ad23ee 100 fprintf(stdout,"%s type %d",domlist[qi],types[ti]);
101 r= adns_submit(ads,domlist[qi],types[ti],0,0,&qus[qi*tc+ti]);
b8358304 102 if (r == adns_s_notimplemented) {
103 fprintf(stdout," not implemented\n");
104 qus[qi*tc+ti]= 0;
105 } else if (r) {
106 failure("submit",r);
107 } else {
f2ad23ee 108 ri= adns_rr_info(types[ti], &rrtn,&fmtn,0, 0,0);
109 putchar(' ');
110 dumptype(ri,rrtn,fmtn);
b8358304 111 fprintf(stdout," submitted\n");
112 }
cfdca685 113 }
1e9efa71 114 }
31d3a9d0 115
1e9efa71 116 for (qi=0; qi<qc; qi++) {
cfdca685 117 for (ti=0; ti<tc; ti++) {
118 qu= qus[qi*tc+ti];
b8358304 119 if (!qu) continue;
cfdca685 120 r= adns_wait(ads,&qu,&ans,0);
121 if (r) failure("wait",r);
d05cc330 122
cfdca685 123 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
f2ad23ee 124 fprintf(stdout, "%s type ", domlist[qi]);
125 dumptype(ri,rrtn,fmtn);
126 fprintf(stdout, ": %s; nrrs=%d; cname=%s\n",
d4c7b0b1 127 adns_strerror(ans->status),
128 ans->nrrs, ans->cname ? ans->cname : "$");
cfdca685 129 if (ans->nrrs) {
130 assert(!ri);
131 for (i=0; i<ans->nrrs; i++) {
132 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
133 if (r) failure("info",r);
134 printf(" %s\n",show);
135 free(show);
136 }
1e9efa71 137 }
cfdca685 138 free(ans);
1e9efa71 139 }
1e9efa71 140 }
d05cc330 141
1e9efa71 142 free(qus);
e7afa27f 143 exit(0);
144}