chiark / gitweb /
44f52b909e31cd1792144dab8d3939c8556aa9ef
[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  Ian Jackson
8  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
9  *    Copyright (C) 1991 Massachusetts Institute of Technology
10  *  (See the file INSTALL for full details.)
11  *  
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *  
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *  
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software Foundation,
24  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
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->udpsocket >= 0);
82
83   for (i=0; i<ads->nsortlist; i++) {
84     assert(ads->sortlist[i].af==AF_INET);
85     assert(!(ads->sortlist[i].base.v4.s_addr &
86              ~ads->sortlist[i].mask.v4.s_addr));
87     sl= &ads->sortlist[i];
88     assert(sl->af==AF_INET);
89     assert(!(sl->base.v4.s_addr & ~sl->mask.v4.s_addr));
90   }
91
92   assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
93   
94   switch (ads->tcpstate) {
95   case server_connecting:
96     assert(ads->tcpsocket >= 0);
97     checkc_notcpbuf(ads);
98     break;
99   case server_disconnected:
100   case server_broken:
101     assert(ads->tcpsocket == -1);
102     checkc_notcpbuf(ads);
103     break;
104   case server_ok:
105     assert(ads->tcpsocket >= 0);
106     assert(ads->tcprecv_skip <= ads->tcprecv.used);
107     break;
108   default:
109     assert(!"ads->tcpstate value");
110   }
111
112   assert(ads->searchlist || !ads->nsearchlist);
113 }
114
115 static void checkc_queue_udpw(adns_state ads) {
116   adns_query qu;
117   
118   DLIST_CHECK(ads->udpw, qu, , {
119     assert(qu->state==query_tosend);
120     assert(qu->retries <= UDPMAXRETRIES);
121     assert(qu->udpsent);
122     assert(!qu->children.head && !qu->children.tail);
123     checkc_query(ads,qu);
124     checkc_query_alloc(ads,qu);
125   });
126 }
127
128 static void checkc_queue_tcpw(adns_state ads) {
129   adns_query qu;
130   
131   DLIST_CHECK(ads->tcpw, qu, , {
132     assert(qu->state==query_tcpw);
133     assert(!qu->children.head && !qu->children.tail);
134     assert(qu->retries <= ads->nservers+1);
135     checkc_query(ads,qu);
136     checkc_query_alloc(ads,qu);
137   });
138 }
139
140 static void checkc_queue_childw(adns_state ads) {
141   adns_query parent, child;
142
143   DLIST_CHECK(ads->childw, parent, , {
144     assert(parent->state == query_childw);
145     assert(parent->children.head);
146     DLIST_CHECK(parent->children, child, siblings., {
147       assert(child->parent == parent);
148       assert(child->state != query_done);
149     });
150     checkc_query(ads,parent);
151     checkc_query_alloc(ads,parent);
152   });
153 }
154
155 static void checkc_queue_output(adns_state ads) {
156   adns_query qu;
157   
158   DLIST_CHECK(ads->output, qu, , {
159     assert(qu->state == query_done);
160     assert(!qu->children.head && !qu->children.tail);
161     assert(!qu->parent);
162     assert(!qu->allocations.head && !qu->allocations.tail);
163     checkc_query(ads,qu);
164   });
165 }
166
167 void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
168   adns_query search;
169   
170   switch (cc) {
171   case cc_user:
172     break;
173   case cc_entex:
174     if (!(ads->iflags & adns_if_checkc_entex)) return;
175     break;
176   case cc_freq:
177     if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
178     break;
179   default:
180     abort();
181   }
182
183   checkc_global(ads);
184   checkc_queue_udpw(ads);
185   checkc_queue_tcpw(ads);
186   checkc_queue_childw(ads);
187   checkc_queue_output(ads);
188
189   if (qu) {
190     switch (qu->state) {
191     case query_tosend:
192       DLIST_ASSERTON(qu, search, ads->udpw, );
193       break;
194     case query_tcpw:
195       DLIST_ASSERTON(qu, search, ads->tcpw, );
196       break;
197     case query_childw:
198       DLIST_ASSERTON(qu, search, ads->childw, );
199       break;
200     case query_done:
201       DLIST_ASSERTON(qu, search, ads->output, );
202       break;
203     default:
204       assert(!"specific query state");
205     }
206   }
207 }