chiark / gitweb /
Fix typo in changelog entry for 1.6.1
[adns.git] / src / general.c
1 /*
2  * general.c
3  * - diagnostic functions
4  * - vbuf handling
5  */
6 /*
7  *  This file is part of adns, which is Copyright Ian Jackson
8  *  and contributors (see the file INSTALL for full details).
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 3, or (at your option)
13  *  any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software Foundation.
22  */
23
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
32 #include "internal.h"
33
34 /* Core diagnostic functions */
35
36 void adns__vlprintf(adns_state ads, const char *fmt, va_list al) {
37   ads->logfn(ads,ads->logfndata,fmt,al);
38 }
39
40 void adns__lprintf(adns_state ads, const char *fmt, ...) {
41   va_list al;
42   va_start(al,fmt);
43   adns__vlprintf(ads,fmt,al);
44   va_end(al);
45 }
46
47 void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
48                  int serv, adns_query qu, const char *fmt, va_list al) {
49   char buf[ADNS_ADDR2TEXT_BUFLEN];
50   const char *bef, *aft;
51   vbuf vb;
52   
53   if (!ads->logfn ||
54       (!(ads->iflags & adns_if_debug)
55        && (!prevent || (ads->iflags & prevent))))
56     return;
57
58   if (ads->iflags & adns_if_logpid) {
59     adns__lprintf(ads,"adns%s [%ld]: ",pfx,(long)getpid());
60   } else {
61     adns__lprintf(ads,"adns%s: ",pfx);
62   }
63
64   adns__vlprintf(ads,fmt,al);
65
66   bef= " (";
67   aft= "\n";
68
69   if (qu && qu->query_dgram) {
70     adns__vbuf_init(&vb);
71     adns__lprintf(ads,"%sQNAME=%s, QTYPE=%s",
72             bef,
73             adns__diag_domain(qu->ads,-1,0, &vb,
74                               qu->query_dgram,qu->query_dglen,DNS_HDRSIZE),
75             qu->typei ? qu->typei->rrtname : "<unknown>");
76     if (qu->typei && qu->typei->fmtname)
77       adns__lprintf(ads,"(%s)",qu->typei->fmtname);
78     bef=", "; aft=")\n";
79     adns__vbuf_free(&vb);
80   }
81   
82   if (serv>=0) {
83     adns__lprintf(ads,"%sNS=%s",bef,
84                   adns__sockaddr_ntoa(&ads->servers[serv].addr.sa, buf));
85     bef=", "; aft=")\n";
86   }
87
88   adns__lprintf(ads,"%s",aft);
89 }
90
91 void adns__debug(adns_state ads, int serv, adns_query qu,
92                  const char *fmt, ...) {
93   va_list al;
94
95   va_start(al,fmt);
96   adns__vdiag(ads," debug",0,serv,qu,fmt,al);
97   va_end(al);
98 }
99
100 void adns__warn(adns_state ads, int serv, adns_query qu,
101                 const char *fmt, ...) {
102   va_list al;
103
104   va_start(al,fmt);
105   adns__vdiag(ads," warning",
106               adns_if_noerrprint|adns_if_noserverwarn, serv,qu,fmt,al);
107   va_end(al);
108 }
109
110 void adns__diag(adns_state ads, int serv, adns_query qu,
111                 const char *fmt, ...) {
112   va_list al;
113
114   va_start(al,fmt);
115   adns__vdiag(ads,"",adns_if_noerrprint,serv,qu,fmt,al);
116   va_end(al);
117 }
118
119 /* vbuf functions */
120
121 void adns__vbuf_init(vbuf *vb) {
122   vb->used= vb->avail= 0; vb->buf= 0;
123 }
124
125 int adns__vbuf_ensure(vbuf *vb, int want) {
126   void *nb;
127   
128   if (vb->avail >= want) return 1;
129   nb= realloc(vb->buf,want); if (!nb) return 0;
130   vb->buf= nb;
131   vb->avail= want;
132   return 1;
133 }
134   
135 void adns__vbuf_appendq(vbuf *vb, const byte *data, int len) {
136   memcpy(vb->buf+vb->used,data,len);
137   vb->used+= len;
138 }
139
140 int adns__vbuf_append(vbuf *vb, const byte *data, int len) {
141   int newlen;
142   void *nb;
143
144   newlen= vb->used+len;
145   if (vb->avail < newlen) {
146     if (newlen<20) newlen= 20;
147     newlen <<= 1;
148     nb= realloc(vb->buf,newlen);
149     if (!nb) { newlen= vb->used+len; nb= realloc(vb->buf,newlen); }
150     if (!nb) return 0;
151     vb->buf= nb;
152     vb->avail= newlen;
153   }
154   adns__vbuf_appendq(vb,data,len);
155   return 1;
156 }
157
158 int adns__vbuf_appendstr(vbuf *vb, const char *data) {
159   int l;
160   l= strlen(data);
161   return adns__vbuf_append(vb,data,l);
162 }
163
164 void adns__vbuf_free(vbuf *vb) {
165   free(vb->buf);
166   adns__vbuf_init(vb);
167 }
168
169 /* Additional diagnostic functions */
170
171 const char *adns__diag_domain(adns_state ads, int serv, adns_query qu,
172                               vbuf *vb, const byte *dgram,
173                               int dglen, int cbyte) {
174   adns_status st;
175
176   st= adns__parse_domain(ads,serv,qu,vb, pdf_quoteok,
177                          dgram,dglen,&cbyte,dglen);
178   if (st == adns_s_nomemory) {
179     return "<cannot report domain... out of memory>";
180   }
181   if (st) {
182     vb->used= 0;
183     if (!(adns__vbuf_appendstr(vb,"<bad format... ") &&
184           adns__vbuf_appendstr(vb,adns_strerror(st)) &&
185           adns__vbuf_appendstr(vb,">") &&
186           adns__vbuf_append(vb,"",1))) {
187       return "<cannot report bad format... out of memory>";
188     }
189   }
190   if (!vb->used) {
191     adns__vbuf_appendstr(vb,"<truncated ...>");
192     adns__vbuf_append(vb,"",1);
193   }
194   return vb->buf;
195 }
196
197 int adns__getrrsz_default(const typeinfo *typei, adns_rrtype type)
198   { return typei->fixed_rrsz; }
199
200 adns_status adns_rr_info(adns_rrtype type,
201                          const char **rrtname_r, const char **fmtname_r,
202                          int *len_r,
203                          const void *datap, char **data_r) {
204   const typeinfo *typei;
205   vbuf vb;
206   adns_status st;
207
208   typei= adns__findtype(type);
209   if (!typei) return adns_s_unknownrrtype;
210
211   if (rrtname_r) *rrtname_r= typei->rrtname;
212   if (fmtname_r) *fmtname_r= typei->fmtname;
213   if (len_r) *len_r= typei->getrrsz(typei, type);
214
215   if (!datap) return adns_s_ok;
216   
217   adns__vbuf_init(&vb);
218   st= typei->convstring(&vb,type,datap);
219   if (st) goto x_freevb;
220   if (!adns__vbuf_append(&vb,"",1)) { st= adns_s_nomemory; goto x_freevb; }
221   assert(strlen(vb.buf) == vb.used-1);
222   *data_r= realloc(vb.buf,vb.used);
223   if (!*data_r) *data_r= vb.buf;
224   return adns_s_ok;
225
226  x_freevb:
227   adns__vbuf_free(&vb);
228   return st;
229 }
230
231
232 #define SINFO(n,s) { adns_s_##n, #n, s }
233
234 static const struct sinfo {
235   adns_status st;
236   const char *abbrev;
237   const char *string;
238 } sinfos[]= {
239   SINFO( ok,                  "OK"                                           ),
240                                                                               
241   SINFO( nomemory,            "Out of memory"                                ),
242   SINFO( unknownrrtype,       "Query not implemented in DNS library"         ),
243   SINFO( systemfail,          "General resolver or system failure"           ),
244                                                                               
245   SINFO( timeout,             "DNS query timed out"                          ),
246   SINFO( allservfail,         "All nameservers failed"                       ),
247   SINFO( norecurse,           "Recursion denied by nameserver"               ),
248   SINFO( invalidresponse,     "Nameserver sent bad response"                 ),
249   SINFO( unknownformat,       "Nameserver used unknown format"               ),
250                                                                               
251   SINFO( rcodeservfail,       "Nameserver reports failure"                   ),
252   SINFO( rcodeformaterror,    "Query not understood by nameserver"           ),
253   SINFO( rcodenotimplemented, "Query not implemented by nameserver"          ),
254   SINFO( rcoderefused,        "Query refused by nameserver"                  ),
255   SINFO( rcodeunknown,        "Nameserver sent unknown response code"        ),
256                                                                               
257   SINFO( inconsistent,        "Inconsistent resource records in DNS"         ),
258   SINFO( prohibitedcname,     "DNS alias found where canonical name wanted"  ),
259   SINFO( answerdomaininvalid, "Found syntactically invalid domain name"      ),
260   SINFO( answerdomaintoolong, "Found overly-long domain name"                ),
261   SINFO( invaliddata,         "Found invalid DNS data"                       ),
262                                                                               
263   SINFO( querydomainwrong,    "Domain invalid for particular DNS query type" ),
264   SINFO( querydomaininvalid,  "Domain name is syntactically invalid"         ),
265   SINFO( querydomaintoolong,  "Domain name or component is too long"         ),
266                                                                               
267   SINFO( nxdomain,            "No such domain"                               ),
268   SINFO( nodata,              "No such data"                                 )
269 };
270
271 static int si_compar(const void *key, const void *elem) {
272   const adns_status *st= key;
273   const struct sinfo *si= elem;
274
275   return *st < si->st ? -1 : *st > si->st ? 1 : 0;
276 }
277
278 static const struct sinfo *findsinfo(adns_status st) {
279   return bsearch(&st,sinfos, sizeof(sinfos)/sizeof(*sinfos),
280                  sizeof(*sinfos), si_compar);
281 }
282
283 const char *adns_strerror(adns_status st) {
284   const struct sinfo *si;
285
286   si= findsinfo(st);
287   return si ? si->string : 0;
288 }
289
290 const char *adns_errabbrev(adns_status st) {
291   const struct sinfo *si;
292
293   si= findsinfo(st);
294   return si ? si->abbrev : 0;
295 }
296
297
298 #define STINFO(max) { adns_s_max_##max, #max }
299
300 static const struct stinfo {
301   adns_status stmax;
302   const char *abbrev;
303 } stinfos[]= {
304   { adns_s_ok, "ok" },
305   STINFO(  localfail   ),
306   STINFO(  remotefail  ),
307   STINFO(  tempfail    ),
308   STINFO(  misconfig   ),
309   STINFO(  misquery    ),
310   STINFO(  permfail    )
311 };
312
313 static int sti_compar(const void *key, const void *elem) {
314   const adns_status *st= key;
315   const struct stinfo *sti= elem;
316
317   adns_status here, min, max;
318
319   here= *st;
320   min= (sti==stinfos) ? 0 : sti[-1].stmax+1;
321   max= sti->stmax;
322   
323   return here < min  ? -1 : here > max ? 1 : 0;
324 }
325
326 const char *adns_errtypeabbrev(adns_status st) {
327   const struct stinfo *sti;
328
329   sti= bsearch(&st,stinfos, sizeof(stinfos)/sizeof(*stinfos),
330                sizeof(*stinfos), sti_compar);
331   return sti ? sti->abbrev : 0;
332 }
333
334
335 void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
336                  int (*needswap)(void *context, const void *a, const void *b),
337                  void *context) {
338   byte *data= array;
339   int i, place;
340
341   for (i=0; i<nobjs; i++) {
342     for (place= i;
343          place>0 && needswap(context, data + (place-1)*sz, data + i*sz);
344          place--);
345     if (place != i) {
346       memcpy(tempbuf, data + i*sz, sz);
347       memmove(data + (place+1)*sz, data + place*sz, (i-place)*sz);
348       memcpy(data + place*sz, tempbuf, sz);
349     }
350   }
351 }
352
353 /* SIGPIPE protection. */
354
355 void adns__sigpipe_protect(adns_state ads) {
356   sigset_t toblock;
357   struct sigaction sa;
358   int r;
359
360   if (ads->iflags & adns_if_nosigpipe) return;
361
362   sigfillset(&toblock);
363   sigdelset(&toblock,SIGPIPE);
364
365   sa.sa_handler= SIG_IGN;
366   sigfillset(&sa.sa_mask);
367   sa.sa_flags= 0;
368   
369   r= sigprocmask(SIG_SETMASK,&toblock,&ads->stdsigmask); assert(!r);
370   r= sigaction(SIGPIPE,&sa,&ads->stdsigpipe); assert(!r);
371 }
372
373 void adns__sigpipe_unprotect(adns_state ads) {
374   int r;
375
376   if (ads->iflags & adns_if_nosigpipe) return;
377
378   r= sigaction(SIGPIPE,&ads->stdsigpipe,0); assert(!r);
379   r= sigprocmask(SIG_SETMASK,&ads->stdsigmask,0); assert(!r);
380 }