chiark / gitweb /
Implement SOA, RP, HINFO (but no mailbox quoting). Implement _finish.
[adns.git] / src / query.c
index 92bb2282a6265084ace73d820dd60c2db8018afa..5605b34aed59ac5eabed9215a9f1d78c2a898dc4 100644 (file)
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <string.h>
 
 #include <sys/time.h>
 
@@ -46,7 +47,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r,
   qu->back= qu->next= qu->parent= 0;
   LIST_INIT(qu->children);
   qu->siblings.next= qu->siblings.back= 0;
-  qu->allocations= 0;
+  LIST_INIT(qu->allocations);
   qu->interim_allocd= 0;
   qu->final_allocspace= 0;
 
@@ -62,7 +63,7 @@ int adns__internal_submit(adns_state ads, adns_query *query_r,
   qu->udpnextserver= 0;
   qu->udpsent= qu->tcpfailed= 0;
   timerclear(&qu->timeout);
-  memcpy(&qu->context,ctx,sizeof(qu->context));
+  memcpy(&qu->ctx,ctx,sizeof(qu->ctx));
 
   qu->answer->status= adns_s_ok;
   qu->answer->cname= 0;
@@ -120,13 +121,16 @@ int adns_submit(adns_state ads,
   if (!typei) return adns_s_notimplemented;
   
   ctx.ext= context;
+  ctx.callback= 0;
+  memset(&ctx.info,0,sizeof(ctx.info));
+  
   r= gettimeofday(&now,0); if (r) return errno;
   id= 0;
 
   adns__vbuf_init(&vb);
 
   ol= strlen(owner);
-  if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; }
+  if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_domaintoolong; goto xit; }
                                 
   if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
 
@@ -147,15 +151,10 @@ int adns_synchronous(adns_state ads,
   r= adns_submit(ads,owner,type,flags,0,&qu);
   if (r) return r;
 
-  do {
-    r= adns_wait(ads,&qu,answer_r,0);
-  } while (r==EINTR);
+  r= adns_wait(ads,&qu,answer_r,0);
   if (r) adns_cancel(qu);
-  return r;
-}
 
-void adns_cancel(adns_query query) {
-  abort(); /* fixme */
+  return r;
 }
 
 static void *alloc_common(adns_query qu, size_t sz) {
@@ -164,12 +163,8 @@ static void *alloc_common(adns_query qu, size_t sz) {
   if (!sz) return qu; /* Any old pointer will do */
   assert(!qu->final_allocspace);
   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
-  if (!an) {
-    adns__query_fail(qu,adns_s_nolocalmem);
-    return 0;
-  }
-  an->next= qu->allocations;
-  qu->allocations= an;
+  if (!an) return 0;
+  LIST_LINK_TAIL(qu->allocations,an);
   return (byte*)an + MEM_ROUND(sizeof(*an));
 }
 
@@ -183,6 +178,22 @@ void *adns__alloc_mine(adns_query qu, size_t sz) {
   return alloc_common(qu,MEM_ROUND(sz));
 }
 
+void adns__transfer_interim(adns_query from, adns_query to, void *block, size_t sz) {
+  allocnode *an;
+
+  if (!block) return;
+  an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
+
+  assert(!to->final_allocspace);
+  assert(!from->final_allocspace);
+  
+  LIST_UNLINK(from->allocations,an);
+  LIST_LINK_TAIL(to->allocations,an);
+
+  from->interim_allocd -= sz;
+  to->interim_allocd += sz;
+}
+
 void *adns__alloc_final(adns_query qu, size_t sz) {
   /* When we're in the _final stage, we _subtract_ from interim_alloc'd
    * each allocation, and use final_allocspace to point to the next free
@@ -199,36 +210,109 @@ void *adns__alloc_final(adns_query qu, size_t sz) {
   return rp;
 }
 
+static void cancel_children(adns_query qu) {
+  adns_query cqu, ncqu;
+
+  for (cqu= qu->children.head; cqu; cqu= ncqu) {
+    ncqu= cqu->siblings.next;
+    adns_cancel(cqu);
+  }
+  LIST_INIT(qu->children);
+}
+
 void adns__reset_cnameonly(adns_query qu) {
   assert(!qu->final_allocspace);
+  cancel_children(qu);
   qu->answer->nrrs= 0;
   qu->answer->rrs= 0;
   qu->interim_allocd= qu->answer->cname ? MEM_ROUND(strlen(qu->answer->cname)+1) : 0;
 }
 
-void adns__query_done(adns_query qu) {
-  adns_answer *ans;
+static void free_query_allocs(adns_query qu) {
   allocnode *an, *ann;
-  int i;
 
-  qu->answer= ans= realloc(qu->answer,
-                          MEM_ROUND(MEM_ROUND(sizeof(*ans)) +
-                                    qu->interim_allocd));
-  qu->final_allocspace= (byte*)qu->answer + MEM_ROUND(sizeof(*ans));
+  cancel_children(qu);
+  for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
+  adns__vbuf_free(&qu->vb);
+}
+
+void adns_cancel(adns_query qu) {
+  switch (qu->state) {
+  case query_udp: case query_tcpwait: case query_tcpsent:
+    LIST_UNLINK(qu->ads->timew,qu);
+    break;
+  case query_child:
+    LIST_UNLINK(qu->ads->childw,qu);
+    break;
+  case query_done:
+    LIST_UNLINK(qu->ads->output,qu);
+    break;
+  default:
+    abort();
+  }
+  free_query_allocs(qu);
+  free(qu->answer);
+  free(qu);
+}
+
+static void makefinal_query(adns_query qu) {
+  adns_answer *ans;
+  int rrn;
+
+  ans= qu->answer;
+
+  if (qu->interim_allocd) {
+    ans= realloc(qu->answer, MEM_ROUND(MEM_ROUND(sizeof(*ans)) + qu->interim_allocd));
+    if (!ans) goto x_nomem;
+    qu->answer= ans;
+  }
 
+  qu->final_allocspace= (byte*)ans + MEM_ROUND(sizeof(*ans));
   adns__makefinal_str(qu,&ans->cname);
+  
   if (ans->nrrs) {
-    adns__makefinal_block(qu,&ans->rrs.untyped,ans->rrsz*ans->nrrs);
-    for (i=0; i<ans->nrrs; i++)
-      qu->typei->makefinal(qu,ans->rrs.bytes+ans->rrsz*i);
+    adns__makefinal_block(qu, &ans->rrs.untyped, ans->nrrs*ans->rrsz);
+
+    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->cname= 0;
+  adns__reset_cnameonly(qu);
+  free_query_allocs(qu);
+}
 
-  for (an= qu->allocations; an; an= ann) { ann= an->next; free(an); }
+void adns__query_done(adns_query qu) {
+  adns_answer *ans;
+  adns_query parent;
 
-  adns__vbuf_free(&qu->vb);
-  
   qu->id= -1;
-  LIST_LINK_TAIL(qu->ads->output,qu);
+  ans= qu->answer;
+
+  if (ans->nrrs && qu->typei->diff_needswap) {
+    if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
+      adns__query_fail(qu,adns_s_nolocalmem);
+      return;
+    }
+    adns__isort(ans->rrs.bytes, ans->nrrs, ans->rrsz,
+               qu->vb.buf, qu->typei->diff_needswap);
+  }
+
+  parent= qu->parent;
+  if (parent) {
+    LIST_UNLINK_PART(parent->children,qu,siblings.);
+    qu->ctx.callback(parent,qu);
+    free_query_allocs(qu);
+    free(qu);
+  } else {
+    makefinal_query(qu);
+    LIST_LINK_TAIL(qu->ads->output,qu);
+  }
 }
 
 void adns__query_fail(adns_query qu, adns_status stat) {
@@ -258,4 +342,3 @@ void adns__makefinal_block(adns_query qu, void **blpp, size_t sz) {
   memcpy(after,before,sz);
   *blpp= after;
 }
-