chiark / gitweb /
Parse searchlist; beginnings of paying attention.
[adns.git] / src / query.c
index 5605b34aed59ac5eabed9215a9f1d78c2a898dc4..f710d3e7baa32095c27030a6f7bbe897b52d3ab3 100644 (file)
@@ -5,7 +5,7 @@
  * - query submission and cancellation (user-visible and internal)
  */
 /*
- *  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
@@ -64,6 +64,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r,
   qu->udpsent= qu->tcpfailed= 0;
   timerclear(&qu->timeout);
   memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
+  qu->expires= now.tv_sec + MAXTTLBELIEVE;
 
   qu->answer->status= adns_s_ok;
   qu->answer->cname= 0;
@@ -78,7 +79,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r,
   if (qumsg_vb->used) {
     qu->query_dgram= malloc(qumsg_vb->used);
     if (!qu->query_dgram) {
-      adns__query_fail(qu,adns_s_nolocalmem);
+      adns__query_fail(qu,adns_s_nomemory);
       return adns_s_ok;
     }
     memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
@@ -101,7 +102,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r,
   free(qu);
  x_nomemory:
   adns__vbuf_free(qumsg_vb);
-  return adns_s_nolocalmem;
+  return adns_s_nomemory;
 }
 
 int adns_submit(adns_state ads,
@@ -112,13 +113,13 @@ int adns_submit(adns_state ads,
                adns_query *query_r) {
   qcontext ctx;
   int id, r, ol;
-  vbuf vb;
+  vbuf vb, search_vb;
   adns_status stat;
   const typeinfo *typei;
   struct timeval now;
 
   typei= adns__findtype(type);
-  if (!typei) return adns_s_notimplemented;
+  if (!typei) return adns_s_unknownrrtype;
   
   ctx.ext= context;
   ctx.callback= 0;
@@ -130,9 +131,9 @@ int adns_submit(adns_state ads,
   adns__vbuf_init(&vb);
 
   ol= strlen(owner);
-  if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; }
+  if (ol>DNS_MAXDOMAIN+1) { stat= adns_s_querydomaintoolong; goto xit; }
                                 
-  if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
+  if (ol>=2 && owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
 
   stat= adns__mkquery(ads,&vb,&id, owner,ol, typei,flags);
                        
@@ -192,6 +193,8 @@ void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t
 
   from->interim_allocd -= sz;
   to->interim_allocd += sz;
+
+  if (to->expires > from->expires) to->expires= from->expires;
 }
 
 void *adns__alloc_final(adns_query qu, size_t sz) {
@@ -255,6 +258,15 @@ void adns_cancel(adns_query qu) {
   free(qu);
 }
 
+void adns__update_expires(adns_query qu, unsigned long ttl, struct timeval now) {
+  time_t max;
+
+  assert(ttl <= MAXTTLBELIEVE);
+  max= now.tv_sec + ttl;
+  if (qu->expires < max) return;
+  qu->expires= max;
+}
+
 static void makefinal_query(adns_query qu) {
   adns_answer *ans;
   int rrn;
@@ -276,12 +288,12 @@ static void makefinal_query(adns_query qu) {
     for (rrn=0; rrn<ans->nrrs; rrn++)
       qu->typei->makefinal(qu, ans->rrs.bytes + rrn*ans->rrsz);
   }
-    
+  
   free_query_allocs(qu);
   return;
   
  x_nomem:
-  qu->answer->status= adns_s_nolocalmem;
+  qu->answer->status= adns_s_nomemory;
   qu->answer->cname= 0;
   adns__reset_cnameonly(qu);
   free_query_allocs(qu);
@@ -296,13 +308,16 @@ void adns__query_done(adns_query qu) {
 
   if (ans->nrrs && qu->typei->diff_needswap) {
     if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
-      adns__query_fail(qu,adns_s_nolocalmem);
+      adns__query_fail(qu,adns_s_nomemory);
       return;
     }
     adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
-               qu->vb.buf, qu->typei->diff_needswap);
+               qu->vb.buf,
+               (int(*)(void*, const void*, const void*))qu->typei->diff_needswap,
+               qu->ads);
   }
 
+  ans->expires= qu->expires;
   parent= qu->parent;
   if (parent) {
     LIST_UNLINK_PART(parent->children,qu,siblings.);