Proper spec. of NULL handling.
[adns] / client / adnstest.c
CommitLineData
e576be50 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 */
964344fc 22
23#include <stdio.h>
350d37d9 24#include <unistd.h>
86e7b8d9 25#include <assert.h>
26#include <stdlib.h>
964344fc 27
28#include "adns.h"
29
86e7b8d9 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
ffbda80c 37static const adns_rrtype defaulttypes[]= {
38 adns_r_a,
39 adns_r_ns_raw,
40 adns_r_cname,
41 adns_r_ptr_raw,
1b644113 42 adns_r_txt,
ffbda80c 43 adns_r_none
44};
45
86e7b8d9 46int main(int argc, const char *const *argv) {
964344fc 47 adns_state ads;
86e7b8d9 48 adns_query *qus, qu;
4353a5c4 49 adns_answer *ans;
86e7b8d9 50 const char *rrtn, *fmtn;
51 char *show;
ffbda80c 52 int len, i, qc, qi, tc, ti;
86e7b8d9 53 adns_status r, ri;
ffbda80c 54 const adns_rrtype *types;
86e7b8d9 55
56 if (argv[0] && argv[1]) argv++;
57 else argv= defaultargv;
58
ffbda80c 59 types= defaulttypes;
60
86e7b8d9 61 for (qc=0; qc[argv]; qc++);
ffbda80c 62 for (tc=0; types[tc] != adns_r_none; tc++);
63 qus= malloc(sizeof(qus)*qc*tc);
86e7b8d9 64 if (!qus) { perror("malloc qus"); exit(3); }
964344fc 65
4353a5c4 66 r= adns_init(&ads,adns_if_debug|adns_if_noautosys,0);
86e7b8d9 67 if (r) failure("init",r);
68
69 for (qi=0; qi<qc; qi++) {
ffbda80c 70 for (ti=0; ti<tc; ti++) {
916c16ca 71 fprintf(stdout,"%s type %d",argv[qi],types[ti]);
ffbda80c 72 r= adns_submit(ads,argv[qi],types[ti],0,0,&qus[qi*tc+ti]);
916c16ca 73 if (r == adns_s_notimplemented) {
74 fprintf(stdout," not implemented\n");
75 qus[qi*tc+ti]= 0;
76 } else if (r) {
77 failure("submit",r);
78 } else {
79 fprintf(stdout," submitted\n");
80 }
ffbda80c 81 }
86e7b8d9 82 }
350d37d9 83
86e7b8d9 84 for (qi=0; qi<qc; qi++) {
ffbda80c 85 for (ti=0; ti<tc; ti++) {
86 qu= qus[qi*tc+ti];
916c16ca 87 if (!qu) continue;
ffbda80c 88 r= adns_wait(ads,&qu,&ans,0);
89 if (r) failure("wait",r);
4353a5c4 90
ffbda80c 91 ri= adns_rr_info(ans->type, &rrtn,&fmtn,&len, 0,0);
1b644113 92 fprintf(stdout, "%s type %s(%s)%s%s: ",
93 argv[qi],
ffbda80c 94 ri ? "?" : rrtn, ri ? "?" : fmtn ? fmtn : "-",
1b644113 95 ri ? " " : "", ri ? adns_strerror(ri) : "");
96 fprintf(stdout, "%s; nrrs=%d; cname=%s\n",
97 adns_strerror(ans->status),
98 ans->nrrs, ans->cname ? ans->cname : "$");
ffbda80c 99 if (ans->nrrs) {
100 assert(!ri);
101 for (i=0; i<ans->nrrs; i++) {
102 r= adns_rr_info(ans->type, 0,0,0, ans->rrs.bytes+i*len,&show);
103 if (r) failure("info",r);
104 printf(" %s\n",show);
105 free(show);
106 }
86e7b8d9 107 }
ffbda80c 108 free(ans);
86e7b8d9 109 }
86e7b8d9 110 }
4353a5c4 111
86e7b8d9 112 free(qus);
964344fc 113 exit(0);
114}