chiark / gitweb /
+ Bugfixes:
[adns.git] / client / adh-query.c
1 /*
2  * adh-query.c
3  * - useful general-purpose resolver client program
4  *   make queries and print answers
5  */
6 /*
7  *  This file is
8  *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12  *    Copyright (C) 1999 Tony Finch <dot@dotat.at>
13  *  
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *  
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *  
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
27  */
28
29 #include "adnshost.h"
30
31 adns_state ads;
32 struct outstanding_list outstanding;
33
34 static unsigned long idcounter;
35
36 void ensure_adns_init(void) {
37   int r;
38   
39   if (ads) return;
40
41   if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno);
42   r= adns_init(&ads,
43                adns_if_noautosys|adns_if_nosigpipe |
44                (ov_env ? 0 : adns_if_noenv) |
45                ov_verbose,
46                0);
47   if (r) sysfail("adns_init",r);
48 }
49
50 static void prep_query(struct query_node **qun_r, int *quflags_r) {
51   struct query_node *qun;
52   char idbuf[20];
53   
54   if (ov_pipe && !ads) usageerr("-f/--pipe not consistent with domains on command line");
55   ensure_adns_init();
56   
57   qun= malloc(sizeof(*qun));
58   qun->pqfr= ov_pqfr;
59   if (ov_id) {
60     qun->id= xstrsave(ov_id);
61   } else {
62     sprintf(idbuf,"%lu",idcounter++);
63     idcounter &= 0x0fffffffflu;
64     qun->id= xstrsave(idbuf);
65   }
66
67   *quflags_r=
68     (ov_search ? adns_qf_search : 0) |
69     (ov_tcp ? adns_qf_usevc : 0) |
70     (ov_pqfr.show_owner ? adns_qf_owner : 0) |
71     (ov_qc_query ? adns_qf_quoteok_query : 0) |
72     (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) |
73     (ov_qc_cname ? 0 : adns_qf_quoteok_cname) |
74     ov_cname,
75     
76   *qun_r= qun;
77 }
78   
79 void of_ptr(const struct optioninfo *oi, const char *arg) {
80   struct query_node *qun;
81   int quflags, r;
82   struct sockaddr_in sa;
83
84   memset(&sa,0,sizeof(sa));
85   sa.sin_family= AF_INET;
86   if (!inet_aton(arg,&sa.sin_addr)) usageerr("invalid IP address %s",arg);
87
88   prep_query(&qun,&quflags);
89   r= adns_submit_reverse(ads,
90                          (struct sockaddr*)&sa,
91                          ov_type == adns_r_none ? adns_r_ptr : ov_type,
92                          quflags,
93                          qun,
94                          &qun->qu);
95   if (r) sysfail("adns_submit_reverse",r);
96
97   LIST_LINK_TAIL(outstanding,qun);
98 }
99
100 void query_do(const char *domain) {
101   struct query_node *qun;
102   int quflags, r;
103
104   prep_query(&qun,&quflags);
105   r= adns_submit(ads, domain,
106                  ov_type == adns_r_none ? adns_r_addr : ov_type,
107                  quflags,
108                  qun,
109                  &qun->qu);
110   if (r) sysfail("adns_submit",r);
111
112   LIST_LINK_TAIL(outstanding,qun);
113 }
114
115 static void dequeue_query(struct query_node *qun) {
116   LIST_UNLINK(outstanding,qun);
117   free(qun->id);
118   free(qun);
119 }
120
121 static void print_withspace(const char *str) {
122   if (printf("%s ", str) == EOF) outerr();
123 }
124
125 static void print_ttl(struct query_node *qun, adns_answer *answer) {
126   unsigned long ttl;
127   time_t now;
128   
129   switch (qun->pqfr.ttl) {
130   case tm_none:
131     return;
132   case tm_rel:
133     if (time(&now) == (time_t)-1) sysfail("get current time",errno);
134     ttl= answer->expires < now ? 0 : answer->expires - now;
135     break;
136   case tm_abs:
137     ttl= answer->expires;
138     break;
139   default:
140     abort();
141   }
142   if (printf("%lu ",ttl) == EOF) outerr();
143 }
144
145 static void print_owner_ttl(struct query_node *qun, adns_answer *answer) {
146   if (qun->pqfr.show_owner) print_withspace(answer->owner);
147   print_ttl(qun,answer);
148 }
149
150 static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) {
151   static const adns_status statuspoints[]= {
152     adns_s_ok,
153     adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail,
154     adns_s_max_misconfig, adns_s_max_misquery
155   };
156
157   const adns_status *spp;
158   const char *statustypeabbrev, *statusabbrev, *statusstring;
159   int minrcode;
160
161   statustypeabbrev= adns_errtypeabbrev(st);
162   for (minrcode=0, spp=statuspoints;
163        spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0]));
164        spp++)
165     if (st > *spp) minrcode++;
166   if (rcode < minrcode) rcode= minrcode;
167
168   statusabbrev= adns_errabbrev(st);
169   statusstring= adns_strerror(st);
170   assert(!strchr(statusstring,'"'));
171
172   if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev)
173       == EOF) outerr();
174   print_owner_ttl(qun,answer);
175   if (qun->pqfr.show_cname)
176     print_withspace(answer->cname ? answer->cname : "$");
177   if (printf("\"%s\"\n", statusstring) == EOF) outerr();
178 }
179
180 void query_done(struct query_node *qun, adns_answer *answer) {
181   adns_status st, ist;
182   int rrn, nrrs;
183   const char *rrp, *realowner, *typename;
184   char *datastr;
185
186   st= answer->status;
187   nrrs= answer->nrrs;
188   if (ov_asynch) {
189     if (printf("%s %d ", qun->id, nrrs) == EOF) outerr();
190     print_status(st,qun,answer);
191   } else {
192     if (st) {
193       if (fputs("; failed ",stdout) == EOF) outerr();
194       print_status(st,qun,answer);
195     } else if (qun->pqfr.show_cname && answer->cname) {
196       print_owner_ttl(qun,answer);
197       if (printf("CNAME %s\n",answer->cname) == EOF) outerr();
198     }
199   }
200   if (qun->pqfr.show_owner) {
201     realowner= answer->cname ? answer->cname : answer->owner;
202     assert(realowner);
203   } else {
204     realowner= 0;
205   }
206   if (nrrs) {
207     for (rrn=0, rrp = answer->rrs.untyped;
208          rrn < nrrs;
209          rrn++, rrp += answer->rrsz) {
210       if (realowner) print_withspace(realowner);
211       print_ttl(qun,answer);
212       ist= adns_rr_info(answer->type, &typename, 0, 0, rrp, &datastr);
213       if (ist == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM);
214       assert(!ist);
215       if (qun->pqfr.show_type) print_withspace(typename);
216       if (printf("%s\n",datastr) == EOF) outerr();
217       free(datastr);
218     }
219   }
220   if (fflush(stdout)) outerr();
221   free(answer);
222   dequeue_query(qun);
223 }
224
225 void of_asynch_id(const struct optioninfo *oi, const char *arg) {
226   free(ov_id);
227   ov_id= xstrsave(arg);
228 }
229
230 void of_cancel_id(const struct optioninfo *oi, const char *arg) {
231   struct query_node *qun;
232
233   for (qun= outstanding.head;
234        qun && strcmp(qun->id,arg);
235        qun= qun->next);
236   if (!qun) return;
237   adns_cancel(qun->qu);
238   dequeue_query(qun);
239 }