chiark / gitweb /
Remove "junk".
[adns.git] / src / reply.c
index ceeaceeeb4d7390523c9b734ae46946515fc3d7f..ab7c8ff4aadbc37c6cdb589baf43b49aa384cc88 100644 (file)
@@ -3,7 +3,7 @@
  * - 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 part of adns, which is Copyright (C) 1997-1999 Ian Jackson
  *  
  *  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
@@ -33,6 +33,7 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
   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;
@@ -111,22 +112,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;
   }
 
@@ -138,7 +139,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;
@@ -155,9 +156,12 @@ 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 CNAMEs */
        qu->cname_begin= rdstart;
        qu->cname_dglen= dglen;
        st= adns__parse_domain(ads,serv,qu, &qu->vb,
@@ -167,13 +171,14 @@ void adns__procdgram(adns_state ads, const byte *dgram, int dglen,
        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; }
+       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
@@ -200,18 +205,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) {
@@ -220,12 +220,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;
     }
@@ -246,7 +259,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;
   }
@@ -254,7 +267,7 @@ 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;
@@ -268,16 +281,18 @@ void adns__procdgram(adns_state ads, const byte *dgram, int 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;
+    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;
@@ -300,7 +315,7 @@ 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;
@@ -314,7 +329,7 @@ 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;