chiark / gitweb /
src/: Fix internals to carry around address families.
[adns.git] / src / general.c
index 0168a4ae4ea252fb6fa8263b71f1a8e3e68a08f7..052c7e1556a77ad067b9b49b0fe13fd6cddd203e 100644 (file)
@@ -4,12 +4,11 @@
  * - vbuf handling
  */
 /*
- *  This file is
- *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
- *
- *  It is part of adns, which is
- *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
- *    Copyright (C) 1999 Tony Finch <dot@dotat.at>
+ *  This file is part of adns, which is
+ *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
+ *    Copyright (C) 1999-2000,2003,2006  Tony Finch
+ *    Copyright (C) 1991 Massachusetts Institute of Technology
+ *  (See the file INSTALL for full details.)
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 /* Core diagnostic functions */
 
+void adns__vlprintf(adns_state ads, const char *fmt, va_list al) {
+  ads->logfn(ads,ads->logfndata,fmt,al);
+}
+
+void adns__lprintf(adns_state ads, const char *fmt, ...) {
+  va_list al;
+  va_start(al,fmt);
+  adns__vlprintf(ads,fmt,al);
+  va_end(al);
+}
+
 void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
                 int serv, adns_query qu, const char *fmt, va_list al) {
   const char *bef, *aft;
   vbuf vb;
   
-  if (!ads->diagfile ||
-      (!(ads->iflags & adns_if_debug) && (!prevent || (ads->iflags & prevent))))
+  if (!ads->logfn ||
+      (!(ads->iflags & adns_if_debug)
+       && (!prevent || (ads->iflags & prevent))))
     return;
 
   if (ads->iflags & adns_if_logpid) {
-    fprintf(ads->diagfile,"adns%s [%ld]: ",pfx,(long)getpid());
+    adns__lprintf(ads,"adns%s [%ld]: ",pfx,(long)getpid());
   } else {
-    fprintf(ads->diagfile,"adns%s: ",pfx);
+    adns__lprintf(ads,"adns%s: ",pfx);
   }
 
-  vfprintf(ads->diagfile,fmt,al);
+  adns__vlprintf(ads,fmt,al);
 
   bef= " (";
   aft= "\n";
 
   if (qu && qu->query_dgram) {
     adns__vbuf_init(&vb);
-    fprintf(ads->diagfile,"%sQNAME=%s, QTYPE=%s",
+    adns__lprintf(ads,"%sQNAME=%s, QTYPE=%s",
            bef,
            adns__diag_domain(qu->ads,-1,0, &vb,
                              qu->query_dgram,qu->query_dglen,DNS_HDRSIZE),
            qu->typei ? qu->typei->rrtname : "<unknown>");
     if (qu->typei && qu->typei->fmtname)
-      fprintf(ads->diagfile,"(%s)",qu->typei->fmtname);
+      adns__lprintf(ads,"(%s)",qu->typei->fmtname);
     bef=", "; aft=")\n";
+    adns__vbuf_free(&vb);
   }
   
   if (serv>=0) {
-    fprintf(ads->diagfile,"%sNS=%s",bef,inet_ntoa(ads->servers[serv].addr));
+    assert(ads->servers[serv].addr.sa.sa_family==AF_INET);
+    adns__lprintf(ads,"%sNS=%s",bef,
+                 inet_ntoa(ads->servers[serv].addr.inet.sin_addr));
     bef=", "; aft=")\n";
   }
 
-  fputs(aft,ads->diagfile);
+  adns__lprintf(ads,"%s",aft);
 }
 
-void adns__debug(adns_state ads, int serv, adns_query qu, const char *fmt, ...) {
+void adns__debug(adns_state ads, int serv, adns_query qu,
+                const char *fmt, ...) {
   va_list al;
 
   va_start(al,fmt);
@@ -86,15 +101,18 @@ void adns__debug(adns_state ads, int serv, adns_query qu, const char *fmt, ...)
   va_end(al);
 }
 
-void adns__warn(adns_state ads, int serv, adns_query qu, const char *fmt, ...) {
+void adns__warn(adns_state ads, int serv, adns_query qu,
+               const char *fmt, ...) {
   va_list al;
 
   va_start(al,fmt);
-  adns__vdiag(ads," warning",adns_if_noerrprint|adns_if_noserverwarn,serv,qu,fmt,al);
+  adns__vdiag(ads," warning",
+             adns_if_noerrprint|adns_if_noserverwarn, serv,qu,fmt,al);
   va_end(al);
 }
 
-void adns__diag(adns_state ads, int serv, adns_query qu, const char *fmt, ...) {
+void adns__diag(adns_state ads, int serv, adns_query qu,
+               const char *fmt, ...) {
   va_list al;
 
   va_start(al,fmt);
@@ -155,10 +173,12 @@ void adns__vbuf_free(vbuf *vb) {
 /* Additional diagnostic functions */
 
 const char *adns__diag_domain(adns_state ads, int serv, adns_query qu,
-                             vbuf *vb, const byte *dgram, int dglen, int cbyte) {
+                             vbuf *vb, const byte *dgram,
+                             int dglen, int cbyte) {
   adns_status st;
 
-  st= adns__parse_domain(ads,serv,qu,vb, pdf_quoteok, dgram,dglen,&cbyte,dglen);
+  st= adns__parse_domain(ads,serv,qu,vb, pdf_quoteok,
+                        dgram,dglen,&cbyte,dglen);
   if (st == adns_s_nomemory) {
     return "<cannot report domain... out of memory>";
   }
@@ -178,6 +198,9 @@ const char *adns__diag_domain(adns_state ads, int serv, adns_query qu,
   return vb->buf;
 }
 
+int adns__getrrsz_default(const typeinfo *typei, adns_rrtype type)
+  { return typei->fixed_rrsz; }
+
 adns_status adns_rr_info(adns_rrtype type,
                         const char **rrtname_r, const char **fmtname_r,
                         int *len_r,
@@ -191,7 +214,7 @@ adns_status adns_rr_info(adns_rrtype type,
 
   if (rrtname_r) *rrtname_r= typei->rrtname;
   if (fmtname_r) *fmtname_r= typei->fmtname;
-  if (len_r) *len_r= typei->rrsz;
+  if (len_r) *len_r= typei->getrrsz(typei, type);
 
   if (!datap) return adns_s_ok;
   
@@ -217,36 +240,36 @@ static const struct sinfo {
   const char *abbrev;
   const char *string;
 } sinfos[]= {
-  SINFO(  ok,                  "OK"                                            ),
-
-  SINFO(  nomemory,            "Out of memory"                                 ),
-  SINFO(  unknownrrtype,       "Query not implemented in DNS library"          ),
-  SINFO(  systemfail,          "General resolver or system failure"            ),
-
-  SINFO(  timeout,             "DNS query timed out"                           ),
-  SINFO(  allservfail,         "All nameservers failed"                        ),
-  SINFO(  norecurse,           "Recursion denied by nameserver"                ),
-  SINFO(  invalidresponse,     "Nameserver sent bad response"                  ),
-  SINFO(  unknownformat,       "Nameserver used unknown format"                ),
-
-  SINFO(  rcodeservfail,       "Nameserver reports failure"                    ),
-  SINFO(  rcodeformaterror,    "Query not understood by nameserver"            ),
-  SINFO(  rcodenotimplemented, "Query not implemented by nameserver"           ),
-  SINFO(  rcoderefused,        "Query refused by nameserver"                   ),
-  SINFO(  rcodeunknown,        "Nameserver sent unknown response code"         ),
-  
-  SINFO(  inconsistent,        "Inconsistent resource records in DNS"          ),
-  SINFO(  prohibitedcname,     "DNS alias found where canonical name wanted"   ),
-  SINFO(  answerdomaininvalid, "Found syntactically invalid domain name"       ),
-  SINFO(  answerdomaintoolong, "Found overly-long domain name"                 ),
-  SINFO(  invaliddata,         "Found invalid DNS data"                        ),
-
-  SINFO(  querydomainwrong,    "Domain invalid for particular DNS query type"  ),
-  SINFO(  querydomaininvalid,  "Domain name is syntactically invalid"          ),
-  SINFO(  querydomaintoolong,  "Domain name is too long"                       ),
-
-  SINFO(  nxdomain,            "No such domain"                                ),
-  SINFO(  nodata,              "No such data"                                  )
+  SINFO( ok,                  "OK"                                           ),
+                                                                             
+  SINFO( nomemory,            "Out of memory"                                ),
+  SINFO( unknownrrtype,       "Query not implemented in DNS library"         ),
+  SINFO( systemfail,          "General resolver or system failure"           ),
+                                                                             
+  SINFO( timeout,             "DNS query timed out"                          ),
+  SINFO( allservfail,         "All nameservers failed"                       ),
+  SINFO( norecurse,           "Recursion denied by nameserver"               ),
+  SINFO( invalidresponse,     "Nameserver sent bad response"                 ),
+  SINFO( unknownformat,       "Nameserver used unknown format"               ),
+                                                                             
+  SINFO( rcodeservfail,       "Nameserver reports failure"                   ),
+  SINFO( rcodeformaterror,    "Query not understood by nameserver"           ),
+  SINFO( rcodenotimplemented, "Query not implemented by nameserver"          ),
+  SINFO( rcoderefused,        "Query refused by nameserver"                  ),
+  SINFO( rcodeunknown,        "Nameserver sent unknown response code"        ),
+                                                                             
+  SINFO( inconsistent,        "Inconsistent resource records in DNS"         ),
+  SINFO( prohibitedcname,     "DNS alias found where canonical name wanted"  ),
+  SINFO( answerdomaininvalid, "Found syntactically invalid domain name"      ),
+  SINFO( answerdomaintoolong, "Found overly-long domain name"                ),
+  SINFO( invaliddata,         "Found invalid DNS data"                       ),
+                                                                             
+  SINFO( querydomainwrong,    "Domain invalid for particular DNS query type" ),
+  SINFO( querydomaininvalid,  "Domain name is syntactically invalid"         ),
+  SINFO( querydomaintoolong,  "Domain name or component is too long"         ),
+                                                                             
+  SINFO( nxdomain,            "No such domain"                               ),
+  SINFO( nodata,              "No such data"                                 )
 };
 
 static int si_compar(const void *key, const void *elem) {
@@ -257,7 +280,8 @@ static int si_compar(const void *key, const void *elem) {
 }
 
 static const struct sinfo *findsinfo(adns_status st) {
-  return bsearch(&st,sinfos,sizeof(sinfos)/sizeof(*sinfos),sizeof(*sinfos),si_compar);
+  return bsearch(&st,sinfos, sizeof(sinfos)/sizeof(*sinfos),
+                sizeof(*sinfos), si_compar);
 }
 
 const char *adns_strerror(adns_status st) {
@@ -306,7 +330,8 @@ static int sti_compar(const void *key, const void *elem) {
 const char *adns_errtypeabbrev(adns_status st) {
   const struct stinfo *sti;
 
-  sti= bsearch(&st,stinfos,sizeof(stinfos)/sizeof(*stinfos),sizeof(*stinfos),sti_compar);
+  sti= bsearch(&st,stinfos, sizeof(stinfos)/sizeof(*stinfos),
+              sizeof(*stinfos), sti_compar);
   return sti->abbrev;
 }