chiark / gitweb /
Consistency checks: Distinguish "entry" from exit
[adns.git] / src / check.c
1 /*
2  * check.c
3  * - consistency checks
4  */
5 /*
6  *  This file is part of adns, which is
7  *    Copyright (C) 1997-2000,2003,2006,2014-2016  Ian Jackson
8  *    Copyright (C) 2014  Mark Wooding
9  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
10  *    Copyright (C) 1991 Massachusetts Institute of Technology
11  *  (See the file INSTALL for full details.)
12  *  
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 3, or (at your option)
16  *  any later version.
17  *  
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *  
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software Foundation.
25  */
26
27 #include "internal.h"
28
29 void adns_checkconsistency(adns_state ads, adns_query qu) {
30   adns__consistency(ads,qu,cc_user);
31 }
32
33 #define DLIST_CHECK(list, nodevar, part, body)                  \
34   if ((list).head) {                                            \
35     assert(! (list).head->part back);                           \
36     for ((nodevar)= (list).head;                                \
37          (nodevar);                                             \
38          (nodevar)= (nodevar)->part next) {                     \
39       assert((nodevar)->part next                               \
40              ? (nodevar) == (nodevar)->part next->part back     \
41              : (nodevar) == (list).tail);                       \
42       body                                                      \
43     }                                                           \
44   }
45
46 #define DLIST_ASSERTON(node, nodevar, list, part)       \
47   do {                                                  \
48     for ((nodevar)= (list).head;                        \
49          (nodevar) != (node);                           \
50          (nodevar)= (nodevar)->part next) {             \
51       assert((nodevar));                                \
52     }                                                   \
53   } while(0)
54
55 static void checkc_query_alloc(adns_state ads, adns_query qu) {
56   allocnode *an;
57
58   DLIST_CHECK(qu->allocations, an, , {
59   });
60 }
61
62 static void checkc_query(adns_state ads, adns_query qu) {
63   adns_query child;
64
65   assert(qu->udpnextserver < ads->nservers);
66   assert(!(qu->udpsent & (~0UL << ads->nservers)));
67   assert(qu->search_pos <= ads->nsearchlist);
68   if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.);
69 }
70
71 static void checkc_notcpbuf(adns_state ads) {
72   assert(!ads->tcpsend.used);
73   assert(!ads->tcprecv.used);
74   assert(!ads->tcprecv_skip);
75 }
76
77 static void checkc_global(adns_state ads) {
78   const struct sortlist *sl;
79   int i;
80   
81   assert(ads->udpsockets >= 0);
82
83   for (i=0; i<ads->nsortlist; i++) {
84     sl= &ads->sortlist[i];
85     assert(adns__addr_matches(sl->base.sa.sa_family,
86                               adns__sockaddr_addr(&sl->base.sa),
87                               &sl->base,&sl->mask));
88   }
89
90   assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
91   
92   switch (ads->tcpstate) {
93   case server_connecting:
94     assert(ads->tcpsocket >= 0);
95     checkc_notcpbuf(ads);
96     break;
97   case server_disconnected:
98   case server_broken:
99     assert(ads->tcpsocket == -1);
100     checkc_notcpbuf(ads);
101     break;
102   case server_ok:
103     assert(ads->tcpsocket >= 0);
104     assert(ads->tcprecv_skip <= ads->tcprecv.used);
105     break;
106   default:
107     assert(!"ads->tcpstate value");
108   }
109
110   assert(ads->searchlist || !ads->nsearchlist);
111 }
112
113 static void checkc_queue_udpw(adns_state ads) {
114   adns_query qu;
115   
116   DLIST_CHECK(ads->udpw, qu, , {
117     assert(qu->state==query_tosend);
118     assert(qu->retries <= UDPMAXRETRIES);
119     assert(qu->udpsent);
120     assert(!qu->children.head && !qu->children.tail);
121     checkc_query(ads,qu);
122     checkc_query_alloc(ads,qu);
123   });
124 }
125
126 static void checkc_queue_tcpw(adns_state ads) {
127   adns_query qu;
128   
129   DLIST_CHECK(ads->tcpw, qu, , {
130     assert(qu->state==query_tcpw);
131     assert(!qu->children.head && !qu->children.tail);
132     assert(qu->retries <= ads->nservers+1);
133     checkc_query(ads,qu);
134     checkc_query_alloc(ads,qu);
135   });
136 }
137
138 static void checkc_queue_childw(adns_state ads) {
139   adns_query parent, child, search;
140
141   DLIST_CHECK(ads->childw, parent, , {
142     assert(parent->state == query_childw);
143     assert(parent->children.head);
144     DLIST_CHECK(parent->children, child, siblings., {
145       assert(child->parent == parent);
146       if (child->state == query_done) {
147         for (search= ads->intdone.head; search; search= search->next)
148           if (search==child) goto child_done_ok;
149         assert(!"done child not on intdone");
150       child_done_ok:;
151       }
152     });
153     checkc_query(ads,parent);
154     checkc_query_alloc(ads,parent);
155   });
156 }
157
158 static void checkc_query_done(adns_state ads, adns_query qu) {
159   assert(qu->state == query_done);
160   assert(!qu->children.head && !qu->children.tail);
161   checkc_query(ads,qu);
162 }
163
164 static void checkc_queue_output(adns_state ads) {
165   adns_query qu;
166   
167   DLIST_CHECK(ads->output, qu, , {
168     assert(!qu->parent);
169     assert(!qu->allocations.head && !qu->allocations.tail);
170     checkc_query_done(ads,qu);
171   });
172 }
173
174 static void checkc_queue_intdone(adns_state ads) {
175   adns_query qu;
176   
177   DLIST_CHECK(ads->intdone, qu, , {
178     assert(qu->parent);
179     assert(qu->ctx.callback);
180     checkc_query_done(ads,qu);
181   });
182 }
183
184 void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
185   adns_query search;
186   
187   switch (cc) {
188   case cc_user:
189     break;
190   case cc_enter:
191     if (!(ads->iflags & adns_if_checkc_entex)) return;
192     break;
193   case cc_exit:
194     if (!(ads->iflags & adns_if_checkc_entex)) return;
195     assert(!ads->intdone.head);
196     break;
197   case cc_freq:
198     if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
199     break;
200   default:
201     abort();
202   }
203
204   checkc_global(ads);
205   checkc_queue_udpw(ads);
206   checkc_queue_tcpw(ads);
207   checkc_queue_childw(ads);
208   checkc_queue_output(ads);
209   checkc_queue_intdone(ads);
210
211   if (qu) {
212     switch (qu->state) {
213     case query_tosend:
214       DLIST_ASSERTON(qu, search, ads->udpw, );
215       break;
216     case query_tcpw:
217       DLIST_ASSERTON(qu, search, ads->tcpw, );
218       break;
219     case query_childw:
220       DLIST_ASSERTON(qu, search, ads->childw, );
221       break;
222     case query_done:
223       if (qu->parent)
224         DLIST_ASSERTON(qu, search, ads->intdone, );
225       else
226         DLIST_ASSERTON(qu, search, ads->output, );
227       break;
228     default:
229       assert(!"specific query state");
230     }
231   }
232 }