chiark / gitweb /
New internal consistency checking with assert if right options set.
[adns.git] / src / check.c
1 /*
2  * check.c
3  * - consistency checks
4  */
5 /*
6  *  This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software Foundation,
20  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
21  */
22
23 #include "internal.h"
24
25 void adns_checkconsistency(adns_state ads) { adns__consistency(ads,cc_user); }
26
27 #define DLIST_CHECK(list, nodevar, part, body)                                  \
28   if ((list).head) {                                                            \
29     assert(! (list).head->part back);                                           \
30     for ((nodevar)= (list).head; (nodevar); (nodevar)= (nodevar)->part next) {  \
31       assert((nodevar)->part next                                               \
32              ? (nodevar) == (nodevar)->part next->part back                     \
33              : (nodevar) == (list).tail);                                       \
34       body                                                                      \
35     }                                                                           \
36   }
37
38 static void checkc_query_alloc(adns_state ads, adns_query qu) {
39   allocnode *an;
40
41   DLIST_CHECK(qu->allocations, an, , {
42   });
43 }
44
45 static void checkc_query(adns_state ads, adns_query qu) {
46   assert(qu->udpnextserver < ads->nservers);
47   assert(!(qu->udpsent & (~0UL << ads->nservers)));
48   assert(!(qu->tcpfailed & (~0UL << ads->nservers)));
49   assert(qu->udpretries <= UDPMAXRETRIES);
50   assert(qu->search_pos <= ads->nsearchlist);
51 }
52
53 static void checkc_global(adns_state ads) {
54   int i;
55   
56   assert(ads->udpsocket >= 0);
57
58   for (i=0; i<ads->nsortlist; i++)
59     assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr));
60
61   assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
62   
63   switch (ads->tcpstate) {
64   case server_connecting:
65     assert(ads->tcpsocket >= 0);
66   case server_disconnected: /* fall through */
67     assert(!ads->tcprecv.used);
68     assert(!ads->tcpsend.used);
69     break;
70   case server_ok:
71     assert(ads->tcpsocket >= 0);
72     break;
73   default:
74     assert(!"ads->tcpstate value");
75   }
76
77   assert(ads->searchlist || !ads->nsearchlist);
78 }
79
80 static void checkc_queue_timew(adns_state ads) {
81   adns_query qu;
82   
83   DLIST_CHECK(ads->timew, qu, , {
84     switch (qu->state) {
85     case query_tosend:
86       assert(qu->udpsent);
87       assert(!qu->tcpfailed);
88       break;
89     case query_tcpwait:
90       assert(ads->tcpstate != server_ok);
91       break;
92     case query_tcpsent:
93       break;
94     default:
95       assert(!"timew state");
96     }
97     assert(!qu->children.head && !qu->children.tail);
98     checkc_query(ads,qu);
99     checkc_query_alloc(ads,qu);
100   });
101 }
102
103 static void checkc_queue_childw(adns_state ads) {
104   adns_query parent, child;
105
106   DLIST_CHECK(ads->childw, parent, , {
107     assert(parent->state == query_child);
108     assert(parent->children.head);
109     DLIST_CHECK(parent->children, child, siblings., {
110       assert(child->parent == parent);
111       assert(child->state != query_done);
112     });
113     checkc_query(ads,parent);
114     checkc_query_alloc(ads,parent);
115   });
116 }
117
118 static void checkc_queue_output(adns_state ads) {
119   adns_query qu;
120   
121   DLIST_CHECK(ads->output, qu, , {
122     assert(qu->state == query_done);
123     assert(!qu->children.head && !qu->children.tail);
124     assert(!qu->parent);
125     checkc_query(ads,qu);
126   });
127 }
128
129 void adns__consistency(adns_state ads, consistency_checks cc) {
130   switch (cc) {
131   case cc_user:
132     break;
133   case cc_entex:
134     if (!(ads->iflags & adns_if_checkc_entex)) return;
135     break;
136   case cc_freq:
137     if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
138     break;
139   default:
140     abort();
141   }
142
143   checkc_global(ads);
144   checkc_queue_timew(ads);
145   checkc_queue_childw(ads);
146   checkc_queue_output(ads);
147 }