about summary refs log tree commit diff
path: root/src/cache
diff options
context:
space:
mode:
Diffstat (limited to 'src/cache')
-rw-r--r--src/cache/deps-exe/shibari-cache1
-rw-r--r--src/cache/dns.c89
-rw-r--r--src/cache/log.c13
-rw-r--r--src/cache/query.c175
-rw-r--r--src/cache/shibari-cache-internal.h63
-rw-r--r--src/cache/shibari-cache.c75
-rw-r--r--src/cache/tcpconnection.c14
7 files changed, 270 insertions, 160 deletions
diff --git a/src/cache/deps-exe/shibari-cache b/src/cache/deps-exe/shibari-cache
index e363f19..44d74a0 100644
--- a/src/cache/deps-exe/shibari-cache
+++ b/src/cache/deps-exe/shibari-cache
@@ -1,6 +1,7 @@
 cache.o
 clientaccess.o
 conf.o
+dns.o
 log.o
 query.o
 tcpconnection.o
diff --git a/src/cache/dns.c b/src/cache/dns.c
new file mode 100644
index 0000000..f027180
--- /dev/null
+++ b/src/cache/dns.c
@@ -0,0 +1,89 @@
+/* ISC license. */
+
+#include <stdint.h>
+
+#include <skalibs/uint16.h>
+#include <skalibs/uint32.h>
+
+
+#include <s6-dns/s6dns.h>
+
+#include "shibari-cache-internal.h"
+
+int dns_ask (dcache_string *answer, uint8_t source, uint16_t sid, char const *q, uint16_t qlen, uint16_t qtype)
+{
+  uint32_t i ;
+  int r = dcache_searchnode_g(&g->cache, &i, q, qlen, qtype) ;
+  if (r > 0)
+  {
+    dcache_string data ;
+    dcache_node_get_data(&g->cache, i, answer) ;
+    return 1 ;
+  }
+  else if (!r)
+  {
+    dns_task_new()
+  }
+}
+
+uint16_t dnstask_new  (uint8_t source, uint16_t sid, char const *ip, uint16_t port, uint16_t qid, char const *q, uint16_t qlen, uint16_t qtype)
+{
+  uint16_t i = genset_new(&g->dnstasks) ;
+  dnstask *task = DNSTASK(i) ;
+  if (!stralloc_ready(&task->sa, 6 + qlen)) dienomem() ;
+  task->source = source ;
+  task->sid = sid ;
+  if (source < 2)
+  {
+    memcpy(task->ip, ip, source ? 16 : 4) ;
+    task->port = port ;
+  }
+  else
+  {
+    memset(task->ip, 0, SKALIBS_IP_SIZE) ;
+    task->port = 0 ;
+  }
+  task->spin = 0 ;
+  task->prefixlen = 0 ;
+  uint16_pack_big(task->sa.s, qtype) ;
+  memcpy(task->sa.s + 2, q, qlen) ;
+  uint32_pack_big(task->sa.s + 2 + qlen, 6 + qlen) ;
+  task->sa.len = 6 + qlen ;
+  return i ;
+}
+
+int dns_start_query (uint8_t source, uint16_t sid, char const *ip, uint16_t port, uint16_t qid, char const *q, uint16_t qlen, uint16_t qtype)
+{
+  uint16_t tid = dnstask_new(source, sid, ip, port, qid, q, qlen, qtype) ;
+  dnstask *task = DNSTASK(i) ;
+  
+}
+
+
+int dns_start (uint8_t source, uint16_t i, char const *ip, uint16_t port, char const *buf, uint16_t len)
+{
+  s6dns_message_header_t hdr ;
+  s6dns_message_counts_t counts ;
+  s6dns_domain_t name ;
+  unsigned int pos ;
+  unsigned int rcode ;
+  int r;
+  uint16_t qtype ;
+
+  if (!s6dns_message_parse_init(&hdr, &counts, buf, len, &pos)
+   || !s6dns_message_parse_question(&counts, &name, &qtype, buf, len, &pos)
+   || !s6dns_domain_encode(&name)
+   || counts.qd || counts.an || counts.ns || counts.nr)
+    return 0 ;
+  if (hdr.opcode) return dns_error(source, i, ip, port, &name, qtype, hdr.id, 4) ;
+  if (!hdr.rd) return dns_error(source, i, ip, port, &name, qtype, hdr.id, 9) ;
+
+  r = dns_ask(&data, source, i, name.s, name.len, qtype) ;
+  return r > 0 ?
+      dns_need_glue(name.s, name.len, qtype, data.s, data.len) ?
+        dns_start_glue(source, i, ip, port, hdr.id, name.s, name.len, qtype, data.s, data.len) :
+        dns_answer(source, i, ip, port, hdr.id, name.s, name.len, qtype, data.s, data.len) :
+    r < 0 ?
+      dns_start_query(source, i, ip, port, hdr.id, name.s, name.len, qtype) :
+      dns_start_wait(source, i, ip, port, hdr.id, name.s, name.len, qtype, (dcache_node *)data.s) ;
+}
diff --git a/src/cache/log.c b/src/cache/log.c
index 0e20f91..06da0ec 100644
--- a/src/cache/log.c
+++ b/src/cache/log.c
@@ -1,4 +1,6 @@
-/* ISC license. */
+/*  ISC license. */
+
+#include <stdint.h>
 
 #include <skalibs/fmtscan.h>
 #include <skalibs/strerr.h>
@@ -7,7 +9,6 @@
 #include "shibari-cache-internal.h"
 
 
-
 void log_udp4bad (char const *ip, uint16_t port)
 {
   if (g->verbosity >= 3)
@@ -53,3 +54,11 @@ void log_newtcp6 (char const *ip, uint16_t port)
 }
 
 #endif
+
+void log_warn_unexpected_answer(char const *q, uint16_t qlen, uint16_t qtype, int present)
+{
+  if (g->verbosity)
+  {
+  }
+}
+
diff --git a/src/cache/query.c b/src/cache/query.c
index 7fb27c4..7fa5af3 100644
--- a/src/cache/query.c
+++ b/src/cache/query.c
@@ -1,13 +1,25 @@
 /* ISC license. */
 
 #include <stdint.h>
+#include <string.h>
+#include <errno.h>
 
-#include <s6-dns/s6dns.h>
+#include <skalibs/bitarray.h>
+#include <skalibs/error.h>
+#include <skalibs/ip46.h>
+#include <skalibs/tai.h>
+#include <skalibs/random.h>
+#include <skalibs/gensetdyn.h>
 
-#include <shibari/constants.h>
+#include <s6-dns/s6dns-engine.h>
+#include <s6-dns/s6dns-ip46.h>
+
+#include <shibari/dcache.h>
 #include "shibari-cache-internal.h"
 
-static uint16_t query_delete (query *q)
+#include <skalibs/posixishard.h>
+
+static inline uint16_t query_delete (query *q)
 {
   uint16_t newi = q->prev ;
   QUERY(newi)->next = q->next ;
@@ -17,105 +29,88 @@ static uint16_t query_delete (query *q)
   return newi ;
 }
 
-uint16_t query_abort (uint16_t id)
-{
-  query *q = QUERY(id) ;
-  s6dns_engine_recycle(&q->dt) ;
-  return query_delete(q) ;
-}
-
-uint16_t query_fail (uint16_t id)
-{
-  query *q = QUERY(id) ;
-
-  if (q->source == 2) tcpconnection_removequery(TCPCONNECTION(q->i), id) ;
-  return query_delete(q) ;
-}
-
-uint16_t query_succeed (uint16_t id)
-{
-  query *q = QUERY(id) ;
-
-  if (q->source == 2) tcpconnection_removequery(TCPCONNECTION(q->i), id) ;
-  return query_delete(q) ;
-}
-
-int query_end (uint8_t source, uint16_t i, char const *ip, uint16_t port, char const *buf, uint16_t len)
+uint16_t query_event (uint16_t qid)
 {
-  return source < 2 ?
-    udpqueue_add(g->udpqueues[source] + i, source, ip, port, buf, len) :
-    tcpconnection_add(g->tcpconnections + i, buf, len) ;
-}
-
-int query_error (uint8_t source, uint16_t i, char const *ip, uint16_t port, s6dns_domain_t *name, uint16_t qtype, uint16_t id, unsigned int rcode)
-{
-  s6dns_message_header_t hdr = S6DNS_MESSAGE_HEADER_ZERO ;
-  unsigned int pos = 12 ;
-  char pkt[name->len + 16] ;
-  hdr.id = id ;
-  hdr.qr = 1 ;
-  hdr.ra = 1 ;
-  hdr.rcode = rcode ;
-  hdr.counts.qd = 1 ;
-  s6dns_message_header_pack(pkt, &hdr) ;
-  memcpy(pkt + pos, name->s, name->len) ; pos += name->len ;
-  uint16_pack_big(pkt + pos, qtype) ; pos += 2 ;
-  uint16_pack_big(pkt + pos, SHIBARI_C_IN) ; pos += 2 ;
-  return query_end(source, i, ip, port, pkt, pos) ;
-}
-
-static void query_init (query *q, uint8_t source, uint16_t i, char const *ip, uint16_t port, s6dns_domain_t const *name, uint16_t qtype)
-{
-  q->source = source ;
-  q->i = i ;
-  if (source < 2)
+  query *q = QUERY(qid) ;
+  dcache_string question ;
+  int r ;
+  uint32_t nodeid ;
+  uint16_t qtype ;
+  uint16_t rcode = 0 ;
+  switch (q->dt.status)
+  {
+    case EAGAIN :
+    case EWOULDBLOCK : return qid ;
+    case 0 : break ;
+    case EOPNOTSUP : rcode = 4 ; break ;
+    case EPROTO : rcode = 1 ; break ;
+    default : rcode = 2 ; break ;
+  }
+  s6dns_engine_query(&q->dt, &question.s, &question.len, &qtype) ;
+  r = dcache_searchnode_g(&g->dcache, &nodeid, question.s, question.len, qtype) ;
+  switch (r)
   {
-    memcpy(q->ip, ip, source ? 16 : 4) ;
-    q->port = port ;
+    case -1 :
+      log_warn_unexpected_answer(question.s, question.len, qtype, 0) ;
+      if (!rcode) dcache_add_new_answer(&g->dcache, question.s, question.len, qtype, s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt)) ;
+      break ;
+    case 1 :
+      log_warn_unexpected_answer(question.s, question.len, qtype, 1) ;
+      if (!rcode) dcache_refresh_answer(&g->dcache, nodeid, s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt)) ;
+      break ;
+    case 0 :
+    {
+      uint16_t n = dcache_get_taskn(&g->cache, nodeid) ;
+      uint16_t tasks[n ? n : 1] ;
+      dcache_get_tasks(&g->cache, nodeid, tasks, taskn) ;
+      if (rcode) dcache_delete(&g->cache, nodeid) ;
+      else
+      {
+        dcache_add_answer(&g->dcache, nodeid, s6dns_engine_packet(&q->dt), s6dns_engine_packetlen(&q->dt)) ;
+        s6dns_engine_recycle(&q->dt) ;
+      }
+      for (uint16_t i = 0 ; i < n ; i++) dnstask_wakeup(tasks[i], rcode, nodeid) ;
+      break ;
+    }
   }
-  q->port = port ;
-  if (!stralloc_catb(&q->qname, name->s, name->len)) dienomem() ;
-  q->qtype = qtype ;
-  q->prefixlen = 0 ;
+  return query_delete(q) ;
 }
 
-static query *query_new (uint8_t source, uint16_t i, char const *ip, uint16_t port, s6dns_domain_t const *name, uint16_t qtype)
+static inline uint16_t query_new (void)
 {
-  uint16_t n = genset_new(&g->queries) ;
+  uint32_t qid ;
+  if (!gensetdyn_new(&g->queries, &qid) || n > UINT16_MAX) dienomem() ;
   query *sentinel = QUERY(g->qsentinel) ;
-  query *q = QUERY(n) ;
-  query_init(q, source, i, ip, port, name, type) ;
+  query *q = QUERY(qid) ;
   q->prev = g->qsentinel ;
   q->next = sentinel->next ;
-  QUERY(sentinel->next)->prev = n ;
-  sentinel->next = n ;
-  return q ;
+  QUERY(sentinel->next)->prev = qid ;
+  sentinel->next = qid ;
+  return qid ;
 }
 
-int query_start (uint8_t source, uint16_t i, char const *ip, uint16_t port, char const *buf, uint16_t len)
+uint16_t query_start (uint16_t tid, char const *q, uint16_t qlen, uint16_t qtype, char const *ip4, uint16_t n4, char const *ip6, uint16_t n6, uint32_t flags)
 {
-  dcache_key_t data ;
-  s6dns_message_header_t hdr ;
-  s6dns_message_counts_t counts ;
-  s6dns_domain_t name ;
-  unsigned int pos ;
-  unsigned int rcode ;
-  uint16_t qtype ;
-
-  if (!s6dns_message_parse_init(&hdr, &counts, buf, len, &pos)
-   || !s6dns_message_parse_question(&counts, &name, &qtype, buf, len, &pos)
-   || !s6dns_domain_encode(&name)) return 0 ;
-  if (hdr.opcode) return query_error(source, i, ip, port, &name, qtype, hdr.id, 4) ;
-  if (!hdr.rd) return query_error(source, i, ip, port, &name, qtype, hdr.id, 9) ;
-
-  if (cache_search(&name, qtype, &data))
-    return query_end(source, i, ip, port, data.s, data.len) ;
-
+  query *p ;
+  tain qdeadline ;
+  uint16_t qid ;
+  uint16_t n = n4 + n6 ;
+  s6dns_ip46list_t servers = S6DNS_IP46LIST_ZERO ;  /* TODO: away with all this goofiness */
   {
-    uint16_t j = genset_new(&g->queries) ;
-    query *q = QUERY(j) ;
+    ip46 list[n] ;
+    for (uint16_t i = 0 ; i < n4 ; i++) ip46_from_ip4(list + i, ip4 + (i<<2)) ;
+    for (uint16_t i = 0 ; i < n6 ; i++) ip46_from_ip6(list + n4 + i, ip6 + (i<<4)) ;
+    random_unsort(list, n, sizeof(ip46)) ;
+    for (uint16_t i = 0 ; i < n ; i++)
+    {
+      memcpy(servers.ip + i * SKALIBS_IP_SIZE, list[i].ip, ip46_is6(list + i) ? 16 : 4) ;
+      if (ip46_is6(list + i)) bitarray_set(servers.is6, i) ;
+    }
   }
-
-  return 1 ;
+  tain_add_g(&qdeadline, &g->qtto) ;
+  qid = query_new() ;
+  p = QUERY(qid) ;
+  if (!dcache_add_new_entry(&g->cache, q, qlen, qtype, tid)) dienomem() ;
+  if (!s6dns_engine_init_g(&q->dt, servers, flags, q, qlen, qtype, &qdeadline)) dienewquery() ;
+  return qid ;
 }
-
diff --git a/src/cache/shibari-cache-internal.h b/src/cache/shibari-cache-internal.h
index 950bfdc..c4208f9 100644
--- a/src/cache/shibari-cache-internal.h
+++ b/src/cache/shibari-cache-internal.h
@@ -22,6 +22,7 @@
 #include <shibari/dcache.h>
 
 #define dienomem() strerr_diefu1sys(111, "concatenate data") ;
+#define dienewquery() strerr_diefu1sys(111, "prepare new query") ;
 
 
  /* cache */
@@ -50,6 +51,29 @@ extern int conf_get_uint64 (char const *, uint64_t *) ;
 extern char const *conf_get_string (char const *) ;
 
 
+ /* dns */
+
+typedef struct dnstask_s dnstask, *dnstask_ref ;
+struct dnstask_s
+{
+  stralloc sa ;
+  char ip[SKALIBS_IP_SIZE] ;
+  uint16_t qtype ;
+  uint16_t prefixlen ;
+  uint16_t state : 13 ;
+  uint16_t spin : 1 ;
+  uint16_t source : 2 ;
+  uint16_t sid ;
+  uint16 query[2] ;
+} ;
+#define DNSTASK_ZERO { .sa = STRALLOC_ZERO, .ip = { 0 }, .qtype = 0, .prefixlen = 0, .state = 0, .spin = 0, .source = 0, .sid = 0, query = { 0, 0 } }
+#define ntasks genset_n(&g->tasks)
+#define DNSTASK(i) genset_p(dnstask, &g->dnstasks, (i))
+
+extern void dnstask_wakeup (uint16_t, uint16_t, uint32_t) ;
+extern int dns_start (uint8_t, uint16_t, char const *, uint16_t, char const *, uint16_t) ;
+
+
  /* log */
 
 extern void log_udp4bad (char const *, uint16_t) ;
@@ -68,28 +92,27 @@ typedef struct query_s query, *query_ref ;
 struct query_s
 {
   s6dns_engine_t dt ;
-  stralloc qname ;
   uint16_t prev ;
   uint16_t next ;
   uint16_t xindex ;
-  uint16_t i ;
-  uint16_t port ;
-  uint16_t qtype ;
-  uint8_t source ;
-  char ip[SKALIBS_IP_SIZE] ;
 } ;
-#define QUERY_ZERO { .dt = S6DNS_ENGINE_ZERO, .qname = STRALLOC_ZERO, .prev = 0, .next = 0, .xindex = UINT16_MAX, .i = 0, .port = 0, qtype = 0, name = 0, .source = 0, .ip = { 0 } }
-#define nq (genset_n(&g->queries) - 1)
-#define QUERY(i) genset_p(query, &g->queries, (i))
+#define QUERY_ZERO { .dt = S6DNS_ENGINE_ZERO, .prev = 0, .next = 0, .xindex = UINT16_MAX }
+#define nq (gensetdyn_n(&g->queries) - 1)
+#define QUERY(i) GENSETDYN_P(query, &g->queries, (i))
 #define qstart (QUERY(g->qsentinel)->next)
 
 extern uint16_t query_abort (uint16_t) ;
-extern uint16_t query_fail (uint16_t) ;
-extern uint16_t query_succeed (uint16_t) ;
+extern uint16_t query_event (uint16_t) ;
 
-extern int query_start (uint8_t, uint16_t, char const *, uint16_t, char const *, uint16_t) ;
+extern int query_start (uint16_t, char const *, uint16_t, uint16_t, char const *, uint16_t, char const *, uint16_t, uint32_t) ;
 extern int query_end (uint8_t, uint16_t, char const *, uint16_t, char const *, uint16_t) ;
 
+
+ /* dns */
+
+extern void dns_start (query *) ;
+
+
  /* tcpconnection */
 
 typedef struct tcpconnection_s tcpconnection, *tcpconnection_ref ;
@@ -100,7 +123,7 @@ struct tcpconnection_s
   uint32_t instate ;
   tain rdeadline ;
   tain wdeadline ;
-  genalloc queries ;  /* uint16_t */
+  genalloc tasks ;  /* uint16_t */
   uint16_t prev ;
   uint16_t next ;
   uint16_t xindex ;
@@ -150,26 +173,30 @@ struct global_s
 {
   cdb confdb ;
   char const *dumpfile ;
-  uint16_t verbosity ;
   tain rtto ;
   tain wtto ;
+  tain qtto ;
   udpqueue *udpqueues[2] ;
-  genset tcpconnections ;  /* tcpconnection */
-  genset queries ;  /* query */
+  genset tcpconnections ;
+  genset dnstasks ;
+  gensetdyn queries ;
   uint16_t tcpsentinel ;
   uint16_t qsentinel ;
+  uint16_t verbosity ;
 } ;
 #define GLOBAL_ZERO { \
   .confdb = CDB_ZERO, \
   .dumpfile = 0, \
-  .verbosity = 1, \
   .rtto = TAIN_INFINITE, \
   .wtto = TAIN_INFINITE, \
+  .qtto = TAIN_INFINITE, \
   .udpqueues = { 0, 0 }, \
   .tcpconnections = GENSET_ZERO, \
-  .queries = GENSET_ZERO, \
+  .dnstasks = GENSET_ZERO, \
+  .queries = GENSETDYN_INIT(query, 3, 3, 8), \
   .tcpsentinel = 0, \
   .qsentinel = 0, \
+  .verbosity = 1, \
 }
 
 extern global *g ;
diff --git a/src/cache/shibari-cache.c b/src/cache/shibari-cache.c
index d9391f2..54abeae 100644
--- a/src/cache/shibari-cache.c
+++ b/src/cache/shibari-cache.c
@@ -45,7 +45,7 @@ static int flagwantfinaldump = 1 ;
 static unsigned int cont = 2 ;
 
 
-static inline void conf_init (char const *conffile, uint16_t *n4, uint16_t *n6, char const **ip4, char const **ip6, uint16_t *maxtcp, uint16_t *maxqueries)
+static inline void conf_init (char const *conffile, uint16_t *n4, uint16_t *n6, char const **ip4, char const **ip6, uint16_t *maxtcp, uint16_t *maxtasks)
 {
   cdb_data data ;
   uint32_t u ;
@@ -63,16 +63,19 @@ static inline void conf_init (char const *conffile, uint16_t *n4, uint16_t *n6,
     strerr_diefu4sys(102, "read ", "G:maxtcp", " configuration key from ", conffile) ;
   if (*maxtcp > 4096 || *maxtcp < 1)
     strerr_dief2x(102, "invalid G:maxtcp in ", conffile) ;
-  if (!conf_get_uint16("G:maxqueries", maxqueries))
-    strerr_diefu4sys(102, "read ", "G:maxqueries", " configuration key from ", conffile) ;
-  if (*maxqueries > 8192 || *maxqueries < 1)
-    strerr_dief2x(102, "invalid G:maxqueries in ", conffile) ;
+  if (!conf_get_uint16("G:maxtasks", maxtasks))
+    strerr_diefu4sys(102, "read ", "G:maxtasks", " configuration key from ", conffile) ;
+  if (*maxtasks > 8192 || *maxtasks < 1)
+    strerr_dief2x(102, "invalid G:maxtasks in ", conffile) ;
   if (!conf_get_uint32("G:rtimeout", &u))
   if (u) tain_from_millisecs(&g->rtto, u) ;
     strerr_diefu4sys(102, "read ", "G:rtimeout", " configuration key from ", conffile) ;
   if (!conf_get_uint32("G:wtimeout", &u))
     strerr_diefu4sys(102, "read ", "G:wtimeout", " configuration key from ", conffile) ;
   if (u) tain_from_millisecs(&g->wtto, u) ;
+  if (!conf_get_uint32("G:qtimeout", &u))
+    strerr_diefu4sys(102, "read ", "G:qtimeout", " configuration key from ", conffile) ;
+  if (u) tain_from_millisecs(&g->qtto, u) ;
   g->dumpfile = conf_get_string("G:cachefile") ;
   if (!g->dumpfile && errno != ENOENT)
     strerr_diefu4sys(102, "read ", "G:cachefile", " configuration key from ", conffile) ;
@@ -122,7 +125,7 @@ int main (int argc, char const *const *argv)
 {
   global globals = GLOBAL_ZERO ;
   char const *conffile = SHIBARI_SYSCONFPREFIX "/shibari-cache.conf.cdb" ;
-  uint16_t n4 = 0, n6 = 0, maxtcp, maxqueries ;
+  uint16_t n4 = 0, n6 = 0, maxtcp, maxtasks ;
   char const *ip4 = 0, *ip6 = 0 ;
   unsigned int cont = 2 ;
   int spfd = -1 ;
@@ -168,7 +171,7 @@ int main (int argc, char const *const *argv)
   close(1) ;
 
   if (!cdb_init(&g->confdb, conffile)) strerr_diefu2sys(111, "open ", conffile) ;
-  conf_init(conffile, &n4, &n6, &ip4, &ip6, &maxtcp, &maxqueries) ;
+  conf_init(conffile, &n4, &n6, &ip4, &ip6, &maxtcp, &maxtasks) ;
 
   spfd = selfpipe_init() ;
   if (spfd == -1) strerr_diefu1sys(111, "create selfpipe") ;
@@ -192,19 +195,18 @@ int main (int argc, char const *const *argv)
     uint16_t tcp6xindex[n4 ? n4 : 1] ;
     tcpconnection tcpconnection_storage[maxtcp + 1] ;
     uint32_t tcpconnection_freelist[maxtcp + 1] ;
-    query query_storage[maxqueries + 1] ;
-    uint32_t query_freelist[maxqueries + 1] ;
+    dnstask dnstask_storage[maxtasks] ;
+    uint32_t dnstask_freelist[maxtasks] ;
 
     memset(udpq4, 0, n4 * sizeof(udpqueue)) ;
     memset(udpq6, 0, n6 * sizeof(udpqueue)) ;
     g->udpqueues[0] = udpq4 ;
     g->udpqueues[1] = udpq6 ;
     memset(tcpconnection_storage, 0, (maxtcp + 1) * sizeof(tcpconnection)) ;
-    memset(query_storage, 0, (maxqueries + 1) * sizeof(query)) ;
+    memset(dnstasks_storage, 0, maxtasks * sizeof(dnstask)) ;
     GENSET_init(&g->tcpconnections, tcpconnection, tcpconnection_storage, tcpconnection_freelist, maxtcp + 1) ;
     g->tcpsentinel = genset_new(&g->tcpconnections) ;
-    GENSET_init(&g->queries, query, query_storage, query_freelist, maxqueries + 1) ;
-    g->qsentinel = genset_new(&g->queries) ;
+    GENSET_init(&g->dnstasks, dnstask, dnstask_storage, dnstask_freelist, maxtasks) ;
     {
       tcpconnection *p = TCPCONNECTION(g->tcpsentinel) ;
       query *q = QUERY(g->qsentinel) ;
@@ -285,7 +287,7 @@ int main (int argc, char const *const *argv)
       for (uint16_t i = 0 ; i < n4 ; i++)
       {
         x[j].fd = udpq4[i].fd ;
-        x[j].events = nq < maxqueries && cont >= 2 ? IOPAUSE_READ : 0 ;
+        x[j].events = ntasks < maxtasks && cont >= 2 ? IOPAUSE_READ : 0 ;
         if (genalloc_len(udp4msg, &udpq4[i].messages))
         {
           x[j].events |= IOPAUSE_WRITE ;
@@ -307,7 +309,7 @@ int main (int argc, char const *const *argv)
       for (uint16_t i = 0 ; i < n6 ; i++)
       {
         x[j].fd = udpq6[i].fd ;
-        x[j].events = nq < maxqueries && cont >= 2 ? IOPAUSE_READ : 0 ;
+        x[j].events = ntasks < maxtasks && cont >= 2 ? IOPAUSE_READ : 0 ;
         if (genalloc_len(udp6msg, &udpq6[i].messages))
         {
           x[j].events |= IOPAUSE_WRITE ;
@@ -330,7 +332,7 @@ int main (int argc, char const *const *argv)
       {
         tcpconnection *p = TCPCONNECTION(i) ;
         x[j].fd = bufalloc_fd(&p->out) ;
-        if (nq < maxqueries && cont >= 2)
+        if (ntasks < maxtasks && cont >= 2)
         {
           x[j].events = IOPAUSE_READ ;
           if (tain_less(&p->rdeadline, &deadline)) deadline = p->rdeadline ;
@@ -354,10 +356,7 @@ int main (int argc, char const *const *argv)
         if (x[j].events) p->xindex = j++ ; else p->xindex = UINT16_MAX ;
       }
 
-
-     /* normal exit condition */
-
-      if (cont < 2 && !r && !nq) break ;
+      if (cont < 2 && !r && !ntasks) break ;  /* normal exit */
 
 
      /* poll() */
@@ -370,9 +369,9 @@ int main (int argc, char const *const *argv)
 
       if (!r)
       {
-        if (cont == 1 && !tain_future(&lameduckt)) break ;  /* too lame */
+        if (cont == 1 && !tain_future(&lameduckt)) break ;  /* lameduck exit */
         for (uint16_t i = qstart ; i != g->qsentinel ; i = QUERY(i)->next)
-          if (s6dns_engine_timeout_g(&QUERY(i)->dt)) i = query_fail(i) ;
+          if (s6dns_engine_timeout_g(&QUERY(i)->dt)) i = query_event(i) ;
         for (uint16_t i = tcpstart ; i != g->tcpsentinel ; i = TCPCONNECTION(i)->next)
         {
           tcpconnection *p = TCPCONNECTION(i) ;
@@ -440,12 +439,8 @@ int main (int argc, char const *const *argv)
        /* process in-flight queries */
 
         for (uint16_t i = qstart ; i != g->qsentinel ; i = QUERY(i)->next)
-        {
-          if (QUERY(i)->xindex == UINT16_MAX) continue ;
-          r = s6dns_engine_event_g(&QUERY(i)->dt) ;
-          if (r < 0) i = query_fail(i) ;
-          else if (r > 0) i = query_succeed(i) ;
-        }
+          if (QUERY(i)->xindex < UINT16_MAX && s6dns_engine_event_g(&QUERY(i)->dt))
+            i = query_event(i) ;
 
 
        /* read udp */
@@ -454,11 +449,11 @@ int main (int argc, char const *const *argv)
         {
           if (udpq4[i].xindex < UINT16_MAX && x[udpq4[i].xindex].revents & IOPAUSE_READ)
           {
+            uint16_t port ;
             uint16_t n = MAXSAME ;
-            char buf[513] ;
             char ip[4] ;
-            uint16_t port ;
-            while (n-- && nq < maxqueries)
+            char buf[513] ;
+            while (n-- && ntasks < maxtasks)
             {
               ssize_t len = sanitize_read(socket_recv4(udpq4[i].fd, buf, 512, ip, &port)) ;
               if (len == -1)
@@ -470,10 +465,7 @@ int main (int argc, char const *const *argv)
               if (!len) break ;
               if (len < 12 || len > 512
                || !clientaccess_ip4(ip)
-               || !query_start(0, i, ip, port, buf, len))
-              {
-                if (g->verbosity >= 3) log_udpbad(ip, port) ;
-              }
+               || !dns_start(0, i, ip, port, buf, len)) log_udpbad(ip, port) ;
             }
           }
         }
@@ -483,11 +475,11 @@ int main (int argc, char const *const *argv)
         {
           if (udpq6[i].xindex < UINT16_MAX && x[udpq6[i].xindex].revents & IOPAUSE_READ)
           {
+            uint16_t port ;
             uint16_t n = MAXSAME ;
-            char buf[513] ;
             char ip[16] ;
-            uint16_t port ;
-            while (n-- && nq < maxqueries)
+            char buf[513] ;
+            while (n-- && ntasks < maxtasks)
             {
               ssize_t len = sanitize_read(socket_recv6(udpq6[i].fd, buf, 512, ip, &port)) ;
               if (len == -1)
@@ -499,10 +491,7 @@ int main (int argc, char const *const *argv)
               if (!len) break ;
               if (len < 12 || len > 512
                || !clientaccess_ip6(ip)
-               || !query_start(1, i, ip, port, buf, len))
-              {
-                if (g->verbosity >= 3) log_udpbad(ip, port) ;
-              }
+               || !dns_start(1, i, ip, port, buf, len)) log_udpbad(ip, port) ;
             }
           }
         }
@@ -523,9 +512,9 @@ int main (int argc, char const *const *argv)
               if (l == -1) { i = tcpconnection_delete(p) ; break ; }
               if (!l) break ;
               if (p->in.len < 12 || p->in.len > 65536
-               || !query_start(2, i, 0, 0, p->in.s, p->in.len))
+               || !dns_start(2, i, 0, 0, p->in.s, p->in.len))
               {
-                if (g->verbosity >= 3) log_tcpbad(i) ;
+                log_tcpbad(i) ;
                 i = tcpconnection_delete(p) ;
                 break ;
               }
diff --git a/src/cache/tcpconnection.c b/src/cache/tcpconnection.c
index 40c3035..87ac69c 100644
--- a/src/cache/tcpconnection.c
+++ b/src/cache/tcpconnection.c
@@ -13,15 +13,15 @@
 
 #include "shibari-cache-internal.h"
 
-void tcpconnection_removequery (tcpconnection *p, uint16_t id)
+void tcpconnection_removetask (tcpconnection *p, uint16_t id)
 {
-  uint16_t *tab = genalloc_s(uint16_t, &p->queries) ;
-  uint16_t n = genalloc_len(uint16_t, &p->queries) ;
+  uint16_t *tab = genalloc_s(uint16_t, &p->tasks) ;
+  uint16_t n = genalloc_len(uint16_t, &p->tasks) ;
   uint16_t i = 0 ;
   for (; i < n ; i++) if (id == tab[i]) break ;
   if (i >= n) return ;
   tab[i] = tab[--n] ;
-  genalloc_setlen(uint16_t, &p->queries, n) ;
+  genalloc_setlen(uint16_t, &p->tasks, n) ;
 }
 
 uint16_t tcpconnection_delete (tcpconnection *p)
@@ -31,9 +31,9 @@ uint16_t tcpconnection_delete (tcpconnection *p)
   p->in.len = 0 ;
   p->instate = 0 ;
   fd_close(p->out.fd) ;
-  for (uint16_t i = 0 ; i < genalloc_len(uint16_t, &p->queries) ; i++)
-    query_abort(genalloc_s(uint16_t, &p->queries)[i]) ;
-  genalloc_setlen(uint16_t, &p->queries, 0) ;
+  for (uint16_t i = 0 ; i < genalloc_len(uint16_t, &p->tasks) ; i++)
+    dnstask_abort(genalloc_s(uint16_t, &p->tasks)[i]) ;
+  genalloc_setlen(uint16_t, &p->tasks, 0) ;
   TCPCONNECTION(newi)->next = p->next ;
   TCPCONNECTION(p->next)->prev = p->prev ;
   p->xindex = UINT16_MAX ;