chiark / gitweb /
Add a type hook for reporting the output record size.
[adns] / src / query.c
index a54451853f1e2ba0d14d244065c1843678a94679..30108f2a6b7a24e2e3f8196ce8dbb0f4d72f83c7 100644 (file)
@@ -83,7 +83,7 @@ static adns_query query_alloc(adns_state ads,
   qu->answer->expires= -1;
   qu->answer->nrrs= 0;
   qu->answer->rrs.untyped= 0;
-  qu->answer->rrsz= typei->rrsz;
+  qu->answer->rrsz= typei->getrrsz ? typei->getrrsz(type) : typei->rrsz;
 
   return qu;
 }
@@ -104,18 +104,23 @@ static void query_submit(adns_state ads, adns_query qu,
   qu->id= id;
   qu->query_dglen= qu->vb.used;
   memcpy(qu->query_dgram,qu->vb.buf,qu->vb.used);
-  
-  adns__query_send(qu,now);
+
+  if (flags & adns__qf_nosend)
+    ;
+  else if (typei->query_send && !(flags & adns__qf_senddirect))
+    typei->query_send(qu,now);
+  else
+    adns__query_send(qu, now);
 }
 
 adns_status adns__internal_submit(adns_state ads, adns_query *query_r,
-                                 const typeinfo *typei, vbuf *qumsg_vb,
-                                 int id,
+                                 const typeinfo *typei, adns_rrtype type,
+                                 vbuf *qumsg_vb, int id,
                                  adns_queryflags flags, struct timeval now,
                                  const qcontext *ctx) {
   adns_query qu;
 
-  qu= query_alloc(ads,typei,typei->typekey,flags,now);
+  qu= query_alloc(ads,typei,type,flags & ~adns__qf_nosend,now);
   if (!qu) { adns__vbuf_free(qumsg_vb); return adns_s_nomemory; }
   *query_r= qu;
 
@@ -222,6 +227,9 @@ int adns_submit(adns_state ads,
 
   adns__consistency(ads,0,cc_entex);
 
+  if (!(type & adns__qtf_bigaddr) || !(type & adns__qtf_manyaf))
+    flags = (flags & ~adns__qf_afmask) | adns_qf_ipv4_only;
+
   typei= adns__findtype(type);
   if (!typei) return ENOSYS;
 
@@ -358,6 +366,7 @@ static void *alloc_common(adns_query qu, size_t sz) {
   an= malloc(MEM_ROUND(MEM_ROUND(sizeof(*an)) + sz));
   if (!an) return 0;
   LIST_LINK_TAIL(qu->allocations,an);
+  an->sz = sz;
   return (byte*)an + MEM_ROUND(sizeof(*an));
 }
 
@@ -381,16 +390,36 @@ void *adns__alloc_preserved(adns_query qu, size_t sz) {
   return rv;
 }
 
+static allocnode *alloc_info(adns_query qu, void *p, size_t *sz_r)
+{
+  allocnode *an;
+
+  if (!p || p == qu) { *sz_r = 0; return 0; }
+  an = (allocnode *)((byte *)p - MEM_ROUND(sizeof(allocnode)));
+  *sz_r = MEM_ROUND(an->sz);
+  return an;
+}
+
+void adns__free_interim(adns_query qu, void *p) {
+  size_t sz;
+  allocnode *an = alloc_info(qu, p, &sz);
+
+  if (!an) return;
+  assert(!qu->final_allocspace);
+  LIST_UNLINK(qu->allocations, an);
+  free(an);
+  qu->interim_allocd -= sz;
+}
+
 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;
+void adns__transfer_interim(adns_query from, adns_query to, void *block) {
+  size_t sz;
+  allocnode *an = alloc_info(from, block, &sz);
 
-  if (!block) return;
-  an= (void*)((byte*)block - MEM_ROUND(sizeof(*an)));
+  if (!an) return;
 
   assert(!to->final_allocspace);
   assert(!from->final_allocspace);
@@ -398,7 +427,6 @@ void adns__transfer_interim(adns_query from, adns_query to,
   LIST_UNLINK(from->allocations,an);
   LIST_LINK_TAIL(to->allocations,an);
 
-  sz= MEM_ROUND(sz);
   from->interim_allocd -= sz;
   to->interim_allocd += sz;
 
@@ -421,7 +449,7 @@ void *adns__alloc_final(adns_query qu, size_t sz) {
   return rp;
 }
 
-static void cancel_children(adns_query qu) {
+void adns__cancel_children(adns_query qu) {
   adns_query cqu, ncqu;
 
   for (cqu= qu->children.head; cqu; cqu= ncqu) {
@@ -432,7 +460,7 @@ static void cancel_children(adns_query qu) {
 
 void adns__reset_preserved(adns_query qu) {
   assert(!qu->final_allocspace);
-  cancel_children(qu);
+  adns__cancel_children(qu);
   qu->answer->nrrs= 0;
   qu->answer->rrs.untyped= 0;
   qu->interim_allocd= qu->preserved_allocd;
@@ -441,7 +469,7 @@ void adns__reset_preserved(adns_query qu) {
 static void free_query_allocs(adns_query qu) {
   allocnode *an, *ann;
 
-  cancel_children(qu);
+  adns__cancel_children(qu);
   for (an= qu->allocations.head; an; an= ann) { ann= an->next; free(an); }
   LIST_INIT(qu->allocations);
   adns__vbuf_free(&qu->vb);
@@ -529,7 +557,7 @@ void adns__query_done(adns_query qu) {
   adns_answer *ans;
   adns_query parent;
 
-  cancel_children(qu);
+  adns__cancel_children(qu);
 
   qu->id= -1;
   ans= qu->answer;
@@ -542,7 +570,7 @@ void adns__query_done(adns_query qu) {
   }
 
   if (ans->nrrs && qu->typei->diff_needswap) {
-    if (!adns__vbuf_ensure(&qu->vb,qu->typei->rrsz)) {
+    if (!adns__vbuf_ensure(&qu->vb,ans->rrsz)) {
       adns__query_fail(qu,adns_s_nomemory);
       return;
     }