chiark / gitweb /
+ * In answers, quote everything except alphanums and - _ / + and document
[adns.git] / src / check.c
index 80c5ac390e4c30272aaced44a1719fd00f17c55a..f0f1eeca35499059970c4d786770708c51319562 100644 (file)
@@ -3,7 +3,12 @@
  * - consistency checks
  */
 /*
- *  This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
+ *  This file is
+ *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
+ *
+ *  It is part of adns, which is
+ *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
+ *    Copyright (C) 1999 Tony Finch <dot@dotat.at>
  *  
  *  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
@@ -22,7 +27,9 @@
 
 #include "internal.h"
 
-void adns_checkconsistency(adns_state ads) { adns__consistency(ads,cc_user); }
+void adns_checkconsistency(adns_state ads, adns_query qu) {
+  adns__consistency(ads,qu,cc_user);
+}
 
 #define DLIST_CHECK(list, nodevar, part, body)                                 \
   if ((list).head) {                                                           \
@@ -35,6 +42,15 @@ void adns_checkconsistency(adns_state ads) { adns__consistency(ads,cc_user); }
     }                                                                          \
   }
 
+#define DLIST_ASSERTON(node, nodevar, list, part)                              \
+  do {                                                                         \
+    for ((nodevar)= (list).head;                                               \
+        (nodevar) != (node);                                                   \
+        (nodevar)= (nodevar)->part next) {                                     \
+      assert((nodevar));                                                       \
+    }                                                                          \
+  } while(0)
+
 static void checkc_query_alloc(adns_state ads, adns_query qu) {
   allocnode *an;
 
@@ -43,11 +59,18 @@ static void checkc_query_alloc(adns_state ads, adns_query qu) {
 }
 
 static void checkc_query(adns_state ads, adns_query qu) {
+  adns_query child;
+
   assert(qu->udpnextserver < ads->nservers);
   assert(!(qu->udpsent & (~0UL << ads->nservers)));
-  assert(!(qu->tcpfailed & (~0UL << ads->nservers)));
-  assert(qu->udpretries <= UDPMAXRETRIES);
   assert(qu->search_pos <= ads->nsearchlist);
+  if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.);
+}
+
+static void checkc_notcpbuf(adns_state ads) {
+  assert(!ads->tcpsend.used);
+  assert(!ads->tcprecv.used);
+  assert(!ads->tcprecv_skip);
 }
 
 static void checkc_global(adns_state ads) {
@@ -63,12 +86,16 @@ static void checkc_global(adns_state ads) {
   switch (ads->tcpstate) {
   case server_connecting:
     assert(ads->tcpsocket >= 0);
-  case server_disconnected: /* fall through */
-    assert(!ads->tcprecv.used);
-    assert(!ads->tcpsend.used);
+    checkc_notcpbuf(ads);
+    break;
+  case server_disconnected:
+  case server_broken:
+    assert(ads->tcpsocket == -1);
+    checkc_notcpbuf(ads);
     break;
   case server_ok:
     assert(ads->tcpsocket >= 0);
+    assert(ads->tcprecv_skip <= ads->tcprecv.used);
     break;
   default:
     assert(!"ads->tcpstate value");
@@ -77,34 +104,36 @@ static void checkc_global(adns_state ads) {
   assert(ads->searchlist || !ads->nsearchlist);
 }
 
-static void checkc_queue_timew(adns_state ads) {
+static void checkc_queue_udpw(adns_state ads) {
   adns_query qu;
   
-  DLIST_CHECK(ads->timew, qu, , {
-    switch (qu->state) {
-    case query_tosend:
-      assert(qu->udpsent);
-      assert(!qu->tcpfailed);
-      break;
-    case query_tcpwait:
-      assert(ads->tcpstate != server_ok);
-      break;
-    case query_tcpsent:
-      break;
-    default:
-      assert(!"timew state");
-    }
+  DLIST_CHECK(ads->udpw, qu, , {
+    assert(qu->state==query_tosend);
+    assert(qu->retries <= UDPMAXRETRIES);
+    assert(qu->udpsent);
     assert(!qu->children.head && !qu->children.tail);
     checkc_query(ads,qu);
     checkc_query_alloc(ads,qu);
   });
 }
 
+static void checkc_queue_tcpw(adns_state ads) {
+  adns_query qu;
+  
+  DLIST_CHECK(ads->tcpw, qu, , {
+    assert(qu->state==query_tcpw);
+    assert(!qu->children.head && !qu->children.tail);
+    assert(qu->retries <= ads->nservers+1);
+    checkc_query(ads,qu);
+    checkc_query_alloc(ads,qu);
+  });
+}
+
 static void checkc_queue_childw(adns_state ads) {
   adns_query parent, child;
 
   DLIST_CHECK(ads->childw, parent, , {
-    assert(parent->state == query_child);
+    assert(parent->state == query_childw);
     assert(parent->children.head);
     DLIST_CHECK(parent->children, child, siblings., {
       assert(child->parent == parent);
@@ -122,11 +151,14 @@ static void checkc_queue_output(adns_state ads) {
     assert(qu->state == query_done);
     assert(!qu->children.head && !qu->children.tail);
     assert(!qu->parent);
+    assert(!qu->allocations.head && !qu->allocations.tail);
     checkc_query(ads,qu);
   });
 }
 
-void adns__consistency(adns_state ads, consistency_checks cc) {
+void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
+  adns_query search;
+  
   switch (cc) {
   case cc_user:
     break;
@@ -141,7 +173,27 @@ void adns__consistency(adns_state ads, consistency_checks cc) {
   }
 
   checkc_global(ads);
-  checkc_queue_timew(ads);
+  checkc_queue_udpw(ads);
+  checkc_queue_tcpw(ads);
   checkc_queue_childw(ads);
   checkc_queue_output(ads);
+
+  if (qu) {
+    switch (qu->state) {
+    case query_tosend:
+      DLIST_ASSERTON(qu, search, ads->udpw, );
+      break;
+    case query_tcpw:
+      DLIST_ASSERTON(qu, search, ads->tcpw, );
+      break;
+    case query_childw:
+      DLIST_ASSERTON(qu, search, ads->childw, );
+      break;
+    case query_done:
+      DLIST_ASSERTON(qu, search, ads->output, );
+      break;
+    default:
+      assert(!"specific query state");
+    }
+  }
 }