chiark / gitweb /
+ * Many memory leaks fixed.
[adns.git] / src / reply.c
index c0772b9aa509ecb44e3bd2964490160f51d939a2..246d52d44fe0a796018b0b15666be9710909fbe6 100644 (file)
@@ -3,7 +3,12 @@
  * - main handling and parsing routine for received datagrams
  */
 /*
- *  This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
+ *  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 program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  */
 
 #include <stdlib.h>
-#include <string.h>
 
 #include "internal.h"
     
 void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
-                    int serv, struct timeval now) {
+                    int serv, int viatcp, struct timeval now) {
   int cbyte, rrstart, wantedrrs, rri, foundsoa, foundns, cname_here;
   int id, f1, f2, qdcount, ancount, nscount, arcount;
   int flg_ra, flg_rd, flg_tc, flg_qr, opcode;
   int rrtype, rrclass, rdlength, rdstart;
   int anstart, nsstart, arstart;
   int ownermatched, l, nrrs;
+  unsigned long ttl, soattl;
   const typeinfo *typei;
   adns_query qu, nqu;
   dns_rcode rcode;
   adns_status st;
   vbuf tempvb;
   byte *newquery, *rrsdata;
+  parseinfo pai;
   
   if (dglen<DNS_HDRSIZE) {
     adns__diag(ads,serv,0,"received datagram too short for message header (%d)",dglen);
@@ -79,7 +85,7 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
               qdcount);
     return;
   }
-  for (qu= ads->timew.head; qu; qu= nqu) {
+  for (qu= viatcp ? ads->tcpw.head : ads->udpw.head; qu; qu= nqu) {
     nqu= qu->next;
     if (qu->id != id) continue;
     if (dglen < qu->query_dglen) continue;
@@ -87,6 +93,12 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
               dgram+DNS_HDRSIZE,
               qu->query_dglen-DNS_HDRSIZE))
       continue;
+    if (viatcp) {
+      assert(qu->state == query_tcpw);
+    } else {
+      assert(qu->state == query_tosend);
+      if (!(qu->udpsent & (1<<serv))) continue;
+    }
     break;
   }
   if (!qu) {
@@ -101,7 +113,8 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
   anstart= qu->query_dglen;
   arstart= -1;
 
-  LIST_UNLINK(ads->timew,qu);
+  if (viatcp) LIST_UNLINK(ads->tcpw,qu);
+  else LIST_UNLINK(ads->udpw,qu);
   /* We're definitely going to do something with this query now */
   
   switch (rcode) {
@@ -110,22 +123,22 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
     break;
   case rcode_formaterror:
     adns__warn(ads,serv,qu,"server cannot understand our query (Format Error)");
-    adns__query_fail(qu,adns_s_serverfaulty);
+    adns__query_fail(qu,adns_s_rcodeformaterror);
     return;
   case rcode_servfail:
-    adns__query_fail(qu,adns_s_servfail);
+    adns__query_fail(qu,adns_s_rcodeservfail);
     return;
   case rcode_notimp:
     adns__warn(ads,serv,qu,"server claims not to implement our query");
-    adns__query_fail(qu,adns_s_notimplemented);
+    adns__query_fail(qu,adns_s_rcodenotimplemented);
     return;
   case rcode_refused:
     adns__warn(ads,serv,qu,"server refused our query");
-    adns__query_fail(qu,adns_s_refused);
+    adns__query_fail(qu,adns_s_rcoderefused);
     return;
   default:
     adns__warn(ads,serv,qu,"server gave unknown response code %d",rcode);
-    adns__query_fail(qu,adns_s_reasonunknown);
+    adns__query_fail(qu,adns_s_rcodeunknown);
     return;
   }
 
@@ -137,7 +150,7 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
   for (rri= 0; rri<ancount; rri++) {
     rrstart= cbyte;
     st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
-                    &rrtype,&rrclass,&rdlength,&rdstart,
+                    &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
                     &ownermatched);
     if (st) { adns__query_fail(qu,st); return; }
     if (rrtype == -1) goto x_truncated;
@@ -154,9 +167,21 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
       }
       continue;
     }
-    if (rrtype == adns_r_cname && /* fixme - implement adns_qf_nocname */
+    if (rrtype == adns_r_cname &&
        (qu->typei->type & adns__rrt_typemask) != adns_r_cname) {
-      if (!qu->cname_dgram) { /* Ignore second and subsequent CNAMEs */
+      if (qu->flags & adns_qf_cname_forbid) {
+       adns__query_fail(qu,adns_s_prohibitedcname);
+       return;
+      } else if (qu->cname_dgram) { /* Ignore second and subsequent CNAME(s) */
+       adns__debug(ads,serv,qu,"allegedly canonical name %s is actually alias for %s",
+                   qu->answer->cname,
+                   adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rdstart));
+       adns__query_fail(qu,adns_s_prohibitedcname);
+       return;
+      } else if (wantedrrs) { /* Ignore CNAME(s) after RR(s). */
+       adns__debug(ads,serv,qu,"ignoring CNAME (to %s) coexisting with RR",
+                   adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rdstart));
+      } else {
        qu->cname_begin= rdstart;
        qu->cname_dglen= dglen;
        st= adns__parse_domain(ads,serv,qu, &qu->vb,
@@ -165,23 +190,20 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
        if (!qu->vb.used) goto x_truncated;
        if (st) { adns__query_fail(qu,st); return; }
        l= strlen(qu->vb.buf)+1;
-       qu->answer->cname= adns__alloc_interim(qu,l);
-       if (!qu->answer->cname) { adns__query_fail(qu,adns_s_nolocalmem); return; }
+       qu->answer->cname= adns__alloc_preserved(qu,l);
+       if (!qu->answer->cname) { adns__query_fail(qu,adns_s_nomemory); return; }
 
        qu->cname_dgram= adns__alloc_mine(qu,dglen);
        memcpy(qu->cname_dgram,dgram,dglen);
 
        memcpy(qu->answer->cname,qu->vb.buf,l);
        cname_here= 1;
+       adns__update_expires(qu,ttl,now);
        /* If we find the answer section truncated after this point we restart
         * the query at the CNAME; if beforehand then we obviously have to use
         * TCP.  If there is no truncation we can use the whole answer if
         * it contains the relevant info.
         */
-      } else {
-       adns__debug(ads,serv,qu,"ignoring duplicate CNAME (%s, as well as %s)",
-                   adns__diag_domain(ads,serv,qu, &qu->vb, dgram,dglen,rdstart),
-                   qu->answer->cname);
       }
     } else if (rrtype == (qu->typei->type & adns__rrt_typemask)) {
       wantedrrs++;
@@ -199,18 +221,13 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
 
   if (!wantedrrs) {
     /* Oops, NODATA or NXDOMAIN or perhaps a referral (which would be a problem) */
-    
-    if (rcode == rcode_nxdomain) {
-      adns__query_fail(qu,adns_s_nxdomain);
-      return;
-    }
 
     /* RFC2308: NODATA has _either_ a SOA _or_ _no_ NS records in authority section */
-    foundsoa= 0; foundns= 0;
+    foundsoa= 0; soattl= 0; foundns= 0;
     for (rri= 0; rri<nscount; rri++) {
       rrstart= cbyte;
       st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
-                      &rrtype,&rrclass,&rdlength,&rdstart, 0);
+                      &rrtype,&rrclass,&ttl, &rdlength,&rdstart, 0);
       if (st) { adns__query_fail(qu,st); return; }
       if (rrtype==-1) goto x_truncated;
       if (rrclass != DNS_CLASS_IN) {
@@ -219,12 +236,25 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
                   rrclass,DNS_CLASS_IN);
        continue;
       }
-      if (rrtype == adns_r_soa_raw) { foundsoa= 1; break; }
+      if (rrtype == adns_r_soa_raw) { foundsoa= 1; soattl= ttl; break; }
       else if (rrtype == adns_r_ns_raw) { foundns= 1; }
     }
+    
+    if (rcode == rcode_nxdomain) {
+      /* We still wanted to look for the SOA so we could find the TTL. */
+      adns__update_expires(qu,soattl,now);
+
+      if (qu->flags & adns_qf_search) {
+       adns__search_next(ads,qu,now);
+      } else {
+       adns__query_fail(qu,adns_s_nxdomain);
+      }
+      return;
+    }
 
     if (foundsoa || !foundns) {
       /* Aha !  A NODATA response, good. */
+      adns__update_expires(qu,soattl,now);
       adns__query_fail(qu,adns_s_nodata);
       return;
     }
@@ -245,7 +275,7 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
       adns__query_fail(qu,adns_s_norecurse);
     } else {
       adns__diag(ads,serv,qu,"server claims to do recursion, but gave us a referral");
-      adns__query_fail(qu,adns_s_serverfaulty);
+      adns__query_fail(qu,adns_s_invalidresponse);
     }
     return;
   }
@@ -253,24 +283,33 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
   /* Now, we have some RRs which we wanted. */
 
   qu->answer->rrs.untyped= adns__alloc_interim(qu,qu->typei->rrsz*wantedrrs);
-  if (!qu->answer->rrs.untyped) { adns__query_fail(qu,adns_s_nolocalmem); return; }
+  if (!qu->answer->rrs.untyped) { adns__query_fail(qu,adns_s_nomemory); return; }
 
   typei= qu->typei;
   cbyte= anstart;
   rrsdata= qu->answer->rrs.bytes;
-  
-  nrrs= 0;
-  for (rri=0; rri<ancount; rri++) {
+
+  pai.ads= qu->ads;
+  pai.qu= qu;
+  pai.serv= serv;
+  pai.dgram= dgram;
+  pai.dglen= dglen;
+  pai.nsstart= nsstart;
+  pai.nscount= nscount;
+  pai.arcount= arcount;
+  pai.now= now;
+
+  for (rri=0, nrrs=0; rri<ancount; rri++) {
     st= adns__findrr(qu,serv, dgram,dglen,&cbyte,
-                    &rrtype,&rrclass,&rdlength,&rdstart,
+                    &rrtype,&rrclass,&ttl, &rdlength,&rdstart,
                     &ownermatched);
     assert(!st); assert(rrtype != -1);
     if (rrclass != DNS_CLASS_IN ||
        rrtype != (qu->typei->type & adns__rrt_typemask) ||
        !ownermatched)
       continue;
-    st= typei->parse(qu,serv, dgram,dglen, rdstart,rdstart+rdlength,
-                    nsstart,&arstart, rrsdata+nrrs*typei->rrsz);
+    adns__update_expires(qu,ttl,now);
+    st= typei->parse(&pai, rdstart,rdstart+rdlength, rrsdata+nrrs*typei->rrsz);
     if (st) { adns__query_fail(qu,st); return; }
     if (rdstart==-1) goto x_truncated;
     nrrs++;
@@ -280,11 +319,10 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
 
   /* This may have generated some child queries ... */
   if (qu->children.head) {
-    qu->state= query_child;
+    qu->state= query_childw;
     LIST_LINK_TAIL(ads->childw,qu);
     return;
   }
-
   adns__query_done(qu);
   return;
 
@@ -292,13 +330,12 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
   
   if (!flg_tc) {
     adns__diag(ads,serv,qu,"server sent datagram which points outside itself");
-    adns__query_fail(qu,adns_s_serverfaulty);
+    adns__query_fail(qu,adns_s_invalidresponse);
     return;
   }
   qu->flags |= adns_qf_usevc;
   
  x_restartquery:
-  
   if (qu->cname_dgram) {
     st= adns__mkquery_frdgram(qu->ads,&qu->vb,&qu->id,
                              qu->cname_dgram, qu->cname_dglen, qu->cname_begin,
@@ -306,13 +343,15 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
     if (st) { adns__query_fail(qu,st); return; }
     
     newquery= realloc(qu->query_dgram,qu->vb.used);
-    if (!newquery) { adns__query_fail(qu,adns_s_nolocalmem); return; }
+    if (!newquery) { adns__query_fail(qu,adns_s_nomemory); return; }
     
     qu->query_dgram= newquery;
     qu->query_dglen= qu->vb.used;
     memcpy(newquery,qu->vb.buf,qu->vb.used);
   }
   
-  adns__reset_cnameonly(qu);
-  adns__query_udp(qu,now);
+  if (qu->state == query_tcpw) qu->state= query_tosend;
+  qu->retries= 0;
+  adns__reset_preserved(qu);
+  adns__query_send(qu,now);
 }