chiark / gitweb /
Halfway through getting it to compile; about to move various bits of
[adns.git] / src / submit.c
index f2fd72c3f6fd7bf52919f3edc0822da407c7e1be..197381d8c5bec6f89495c4aebb84ee2ed56de703 100644 (file)
@@ -1,41 +1,88 @@
 /**/
 
-#include "adns-internal.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
 
-static adns_query allocquery(adns_state ads, const char *owner, int ol,
-                            int qml, int id, adns_rrtype type,
-                            adns_queryflags flags, void *context) {
+#include <sys/time.h>
+
+#include "internal.h"
+
+int adns__internal_submit(adns_state ads, adns_query *query_r,
+                         adns_rrtype type, vbuf *qumsg_vb, int id,
+                         adns_queryflags flags, struct timeval now,
+                         adns_status failstat, const qcontext *ctx) {
   adns_query qu;
-  unsigned char *qm;
-  
-  qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0;
-  qu->next= qu->back= qu->parent= 0;
-  qu->children.head= qu->children.tail= 0;
+  adns_status stat;
+  int ol, id, r;
+  struct timeval now;
+  const typeinfo *typei;
+  adns_query qu;
+
+  qu= malloc(sizeof(*qu)); if (!qu) goto x_nomemory;
+  qu->answer= malloc(sizeof(*qu->answer)); if (!qu->answer) goto x_freequ_nomemory;
+
+  qu->state= query_udp;
+  qu->back= qu->next= qu->parent= 0;
+  LIST_INIT(qu->children);
   qu->siblings.next= qu->siblings.back= 0;
+  qu->allocations= 0;
+  qu->interim_allocd= 0;
+  qu->perm_used= 0;
+
+  qu->typei= adns__findtype(type);
+  adns__vbuf_init(&qu->vb);
+
+  qu->cname_dgram= 0;
+  qu->cname_dglen= qu->cname_begin= 0;
+  
   qu->id= id;
-  qu->type= type;
-  qu->answer= 0;
   qu->flags= flags;
-  qu->context= context;
   qu->udpretries= 0;
-  qu->sentudp= qu->senttcp= 0;
-  qu->nextserver= 0;
+  qu->udpnextserver= 0;
+  qu->udpsent= qu->tcpfailed= 0;
+  timerclear(&qu->timeout);
+  memcpy(&qu->context,ctx,sizeof(qu->context));
   memcpy(qu->owner,owner,ol); qu->owner[ol]= 0;
-  qu->querymsg= qm= qu->owner+ol+1;
-  memcpy(qm,ads->qbuf,qml);
-  qu->querylen= qml;
-  return qu;
-}
 
-static int failsubmit(adns_state ads, void *context, adns_query *query_r,
-                     adns_rrtype type, adns_queryflags flags,
-                     int id, adns_status stat) {
-  adns_query qu;
+  qu->answer->status= adns_s_ok;
+  qu->answer->cname= 0;
+  qu->answer->type= type;
+  qu->answer->nrrs= 0;
+  qu->answer->rrs= 0;
 
-  qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno;
-  query_fail(ads,qu,stat);
+  if (qu->typei) {
+    qu->answer->rrsz= qu->rrsz;
+  } else {
+    qu->answer->rrsz= -1;
+    failstat= adns_s_notimplemented;
+  }
+  
   *query_r= qu;
+
+  qu->query_dgram= malloc(qumsg_vb->used);
+  if (!qu->query_dgram) {
+    adns__query_fail(ads,qu,adns_s_nomemory);
+    return;
+  }
+  memcpy(qu->query_dgram,qumsg_vb->buf,qumsg_vb->used);
+  qu->vb= *qumsg_vb;
+  adns__vbuf_init(qumsg_vb);
+  
+  if (failstat) {
+    adns__query_fail(ads,qu,failstat);
+    return;
+  }
+  adns__query_udp(ads,qu,now);
+  adns__autosys(ads,now);
+
   return 0;
+
+ x_freequ_nomemory:
+  free(qu);
+ x_nomemory:
+  free(query_dgram);
+  return adns_s_nomemory;
 }
 
 int adns_submit(adns_state ads,
@@ -44,30 +91,25 @@ int adns_submit(adns_state ads,
                adns_queryflags flags,
                void *context,
                adns_query *query_r) {
-  adns_query qu;
-  adns_status stat;
-  int ol, id, qml;
-
-  id= ads->nextid++;
+  qcontext ctx;
+  int id;
+  vbuf vb;
 
-  ol= strlen(owner);
-  if (ol<=1 || ol>MAXDNAME+1)
-    return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain);
-  if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; }
+  ctx.ext= context;
+  r= gettimeofday(&now,0); if (r) return errno;
+  id= 0;
 
-  stat= adns__mkquery(ads,owner,ol,id,type,flags,&qml);
-  if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat);
+  adns__vbuf_init(&vb);
 
-  qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno;
-  if (qu->flags & adns_f_usevc) qu->udpretries= -1;
-  LIST_LINK_TAIL(ads->tosend,qu);
-    
-  r= gettimeofday(&now,0); if (r) return;
-  quproc_tosend(ads,qu,now);
-  autosys(ads,now);
+  ol= strlen(owner);
+  if (ol<=1 || ol>DNS_MAXDOMAIN+1) { stat= adns_s_invaliddomain; goto xit; }
+                                
+  if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_qf_search; ol--; }
 
-  *query_r= qu;
-  return 0;
+  stat= adns__mkquery(ads,&vb, &id, owner,ol, typei,flags);
+                       
+ xit:
+  return adns__internal_submit(ads,query_r, type,&vb,id, flags,now, stat,&ctx);        
 }
 
 int adns_synchronous(adns_state ads,
@@ -89,5 +131,56 @@ int adns_synchronous(adns_state ads,
 }
 
 void adns_cancel(adns_state ads, adns_query query) {
-  abort(); /* FIXME */
+  abort(); /* fixme */
+}
+
+void adns__makefinal_str(adns_query qu, char **strp) {
+  int l;
+  char *before, *after;
+
+  before= *strp;
+  l= strlen(before)+1;
+  after= adns__alloc_final(qu,l);
+  memcpy(after,before,l);
+  *strp= after;  
+}
+
+void adns__makefinal_block(adns__query qu, void **blpp, size_t sz) {
+  void *after;
+
+  after= adns__alloc_final(qu,sz);
+  memcpy(after,*blpp,sz);
+  *blpp= after;
+}
+
+void *adns__alloc_interim(adns_state ads, adns_query qu, size_t sz) {
+  allocnode *an;
+
+  assert(!qu->final_allocspace);
+  sz= MEM_ROUND(sz);
+  an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
+  if (!an) {
+    adns__query_fail(ads,qu,adns_s_nolocalmem);
+    return 0;
+  }
+  qu->permalloclen += sz;
+  an->next= qu->allocations;
+  qu->allocations= an;
+  return (byte*)an + MEM_ROUND(sizeof(*an));
+}
+
+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
+   * bit.
+   */
+  void *rp;
+
+  sz= MEM_ROUND(sz);
+  rp= qu->final_allocspace;
+  assert(rp);
+  qu->interim_allocd -= sz;
+  assert(qu->interim_allocd>=0);
+  qu->final_allocspace= (byte*)rp + sz;
+  return rp;
 }