chiark / gitweb /
src/types.c: Do not crash when one of several addr subqueries fails
[adns.git] / src / types.c
index 55a7709373532421d31f4363bf7f54e3e6531f3c..142a942c1a2f3fa003e30fe53f4ac81d4c560108 100644 (file)
@@ -4,14 +4,15 @@
  */
 /*
  *  This file is part of adns, which is
- *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
+ *    Copyright (C) 1997-2000,2003,2006,2014  Ian Jackson
+ *    Copyright (C) 2014  Mark Wooding
  *    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
- *  the Free Software Foundation; either version 2, or (at your option)
+ *  the Free Software Foundation; either version 3, or (at your option)
  *  any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
@@ -20,8 +21,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software Foundation,
- *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
+ *  along with this program; if not, write to the Free Software Foundation.
  */
 
 #include <stddef.h>
@@ -373,8 +373,10 @@ static unsigned addr_rrtypeflag(adns_rrtype type) {
   int i;
 
   type &= adns_rrt_typemask;
-  for (i=0; i<addr_nrrtypes && type!=addr_all_rrtypes[i]; i++);
-  return i < addr_nrrtypes ? 1 << i : 0;
+  for (i=0; i<addr_nrrtypes; i++)
+     if (type==addr_all_rrtypes[i])
+       return 1 << i;
+  return 0;
 }
 
 /* About CNAME handling in addr queries.
@@ -398,50 +400,46 @@ static unsigned addr_rrtypeflag(adns_rrtype type) {
  * adns__qf_addr_cname set so that we know that we're in the fixup state.
  */
 
-static adns_status pap_addr(const parseinfo *pai, int rrty, size_t rrsz,
-                           int *cbyte_io, int max, adns_rr_addr *storeto) {
-  const byte *dgram= pai->dgram;
-  int af, addrlen, salen;
+static adns_status pap_addr(const parseinfo *pai, int in_rrty, size_t out_rrsz,
+                           int *cbyte_io, int cbyte_max, adns_rr_addr *out) {
+  int in_addrlen;
+  int out_af, out_salen;
   struct in6_addr v6map;
-  const void *oaddr= dgram + *cbyte_io;
-  int avail= max - *cbyte_io;
-  int step= -1;
-  void *addrp= 0;
-
-  switch (rrty) {
-  case adns_r_a:
-    if ((pai->qu->flags & adns_qf_ipv6_mapv4) &&
-       (pai->qu->answer->type & adns__qtf_bigaddr)) {
-      if (avail < 4) return adns_s_invaliddata;
-      memset(v6map.s6_addr +  0, 0x00, 10);
-      memset(v6map.s6_addr + 10, 0xff,  2);
-      memcpy(v6map.s6_addr + 12, oaddr, 4);
-      oaddr= v6map.s6_addr; avail= sizeof(v6map.s6_addr);
-      if (step < 0) step= 4;
-      goto aaaa;
-    }
-    af= AF_INET; addrlen= 4;
-    addrp= &storeto->addr.inet.sin_addr;
-    salen= sizeof(storeto->addr.inet);
-    break;
-  case adns_r_aaaa:
-  aaaa:
-    af= AF_INET6; addrlen= 16;
-    addrp= storeto->addr.inet6.sin6_addr.s6_addr;
-    salen= sizeof(storeto->addr.inet6);
-    break;
+
+  const void *use_addr= pai->dgram + *cbyte_io;
+
+  switch (in_rrty) {
+  case adns_r_a:    in_addrlen= 4;  out_af= AF_INET;  break;
+  case adns_r_aaaa: in_addrlen= 16; out_af= AF_INET6; break;
+  default: abort();
   }
-  assert(addrp);
 
-  assert(offsetof(adns_rr_addr, addr) + salen <= rrsz);
-  if (addrlen < avail) return adns_s_invaliddata;
-  if (step < 0) step= addrlen;
-  *cbyte_io += step;
-  memset(&storeto->addr, 0, salen);
-  storeto->len= salen;
-  storeto->addr.sa.sa_family= af;
-  memcpy(addrp, oaddr, addrlen);
+  if ((*cbyte_io + in_addrlen) != cbyte_max) return adns_s_invaliddata;
 
+  if (out_af==AF_INET &&
+      (pai->qu->flags & adns_qf_ipv6_mapv4) &&
+      (pai->qu->answer->type & adns__qtf_bigaddr)) {
+    memset(v6map.s6_addr +  0, 0x00,    10);
+    memset(v6map.s6_addr + 10, 0xff,     2);
+    memcpy(v6map.s6_addr + 12, use_addr, 4);
+    use_addr= v6map.s6_addr;
+    out_af= AF_INET6;
+  }
+
+  switch (out_af) {
+  case AF_INET:  out_salen= sizeof(out->addr.inet);  break;
+  case AF_INET6: out_salen= sizeof(out->addr.inet6); break;
+  default: abort();
+  }
+
+  assert(offsetof(adns_rr_addr, addr) + out_salen <= out_rrsz);
+
+  memset(&out->addr, 0, out_salen);
+  out->len= out_salen;
+  out->addr.sa.sa_family= out_af;
+  adns__addr_inject(use_addr, &out->addr);
+
+  *cbyte_io += in_addrlen;
   return adns_s_ok;
 }
 
@@ -581,11 +579,11 @@ static adns_status addr_submit(adns_query parent, adns_query *query_r,
    * available record types.  The memory management and callback rules are
    * the same as for adns__internal_submit.
    *
-   * Some differences: the query is linked onto the parent's children list
-   * before exit (though the parent's state is not changed, and it is not
-   * linked into the childw list queue); and we fiddle with the `tinfo'
-   * portion of the context structure (yes, modifying *ctx), since this is,
-   * in fact, the main purpose of this function.
+   * Some differences: the query is linked onto the parent's children
+   * list before exit (though the parent's state is not changed, and
+   * it is not linked into the childw list queue); and we set the
+   * `tinfo' portion of the context structure (yes, modifying *ctx),
+   * since this is, in fact, the main purpose of this function.
    */
 
   adns_state ads= parent->ads;
@@ -652,7 +650,7 @@ static void icb_addr(adns_query parent, adns_query child) {
   struct timeval now;
   adns_status err;
   adns_queryflags qf;
-  int id;
+  int id, r;
 
   propagate_ttl(parent, child);
 
@@ -693,7 +691,7 @@ static void icb_addr(adns_query parent, adns_query child) {
      * settled on.
      */
     adns__cancel_children(parent);
-    if (gettimeofday(&now, 0)) goto x_gtod;
+    r= gettimeofday(&now, 0);  if (r) goto x_gtod;
     qf= adns__qf_addr_cname;
     if (!(parent->flags & adns_qf_cname_loose)) qf |= adns_qf_cname_forbid;
     addr_subqueries(parent, now, qf, child->vb.buf, child->vb.used);
@@ -714,7 +712,7 @@ static void icb_addr(adns_query parent, adns_query child) {
     adns__cancel_children(parent);
     adns__free_interim(parent, pans->rrs.bytes);
     pans->rrs.bytes= 0; pans->nrrs= 0;
-    if (gettimeofday(&now, 0)) goto x_gtod;
+    r= gettimeofday(&now, 0);  if (r) goto x_gtod;
     adns__search_next(ads, parent, now);
     return;
   }
@@ -736,6 +734,9 @@ static void icb_addr(adns_query parent, adns_query child) {
   return;
 
 x_gtod:
+  /* We have our own error handling, because adns__must_gettimeofday
+   * handles errors by calling adns_globalsystemfailure, which would
+   * reenter the query processing logic. */
   adns__diag(ads, -1, parent, "gettimeofday failed: %s", strerror(errno));
   err= adns_s_systemfail;
   goto x_err;
@@ -891,6 +892,7 @@ static void icb_hostaddr(adns_query parent, adns_query child) {
 done:
   if (st) {
     adns__free_interim(parent, rrp->addrs);
+    rrp->addrs = 0;
     rrp->naddrs= (st>0 && st<=adns_s_max_tempfail) ? -1 : 0;
   }
 
@@ -1692,6 +1694,10 @@ DEEP_TYPE(unknown,0, "unknown",byteblock,opaque,  0,     opaque            );
 const typeinfo *adns__findtype(adns_rrtype type) {
   const typeinfo *begin, *end, *mid;
 
+  if (type & ~(adns_rrtype)0x63ffffff)
+    /* 0x60000000 is reserved for `harmless' future expansion */
+    return 0;
+
   if (type & adns_r_unknown) return &typeinfo_unknown;
   type &= adns_rrt_reprmask;