+ * Some better source code formatting/wrapping in a few places.
[adns] / src / check.c
1 /*
2 * check.c
3 * - consistency checks
4 */
5 /*
6 * This file is
7 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
8 *
9 * It is part of adns, which is
10 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
11 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
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 2, 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 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 #include "internal.h"
29
30 void adns_checkconsistency(adns_state ads, adns_query qu) {
31 adns__consistency(ads,qu,cc_user);
32 }
33
34 #define DLIST_CHECK(list, nodevar, part, body) \
35 if ((list).head) { \
36 assert(! (list).head->part back); \
37 for ((nodevar)= (list).head; \
38 (nodevar); \
39 (nodevar)= (nodevar)->part next) { \
40 assert((nodevar)->part next \
41 ? (nodevar) == (nodevar)->part next->part back \
42 : (nodevar) == (list).tail); \
43 body \
44 } \
45 }
46
47 #define DLIST_ASSERTON(node, nodevar, list, part) \
48 do { \
49 for ((nodevar)= (list).head; \
50 (nodevar) != (node); \
51 (nodevar)= (nodevar)->part next) { \
52 assert((nodevar)); \
53 } \
54 } while(0)
55
56 static void checkc_query_alloc(adns_state ads, adns_query qu) {
57 allocnode *an;
58
59 DLIST_CHECK(qu->allocations, an, , {
60 });
61 }
62
63 static void checkc_query(adns_state ads, adns_query qu) {
64 adns_query child;
65
66 assert(qu->udpnextserver < ads->nservers);
67 assert(!(qu->udpsent & (~0UL << ads->nservers)));
68 assert(qu->search_pos <= ads->nsearchlist);
69 if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.);
70 }
71
72 static void checkc_notcpbuf(adns_state ads) {
73 assert(!ads->tcpsend.used);
74 assert(!ads->tcprecv.used);
75 assert(!ads->tcprecv_skip);
76 }
77
78 static void checkc_global(adns_state ads) {
79 int i;
80
81 assert(ads->udpsocket >= 0);
82
83 for (i=0; i<ads->nsortlist; i++)
84 assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr));
85
86 assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
87
88 switch (ads->tcpstate) {
89 case server_connecting:
90 assert(ads->tcpsocket >= 0);
91 checkc_notcpbuf(ads);
92 break;
93 case server_disconnected:
94 case server_broken:
95 assert(ads->tcpsocket == -1);
96 checkc_notcpbuf(ads);
97 break;
98 case server_ok:
99 assert(ads->tcpsocket >= 0);
100 assert(ads->tcprecv_skip <= ads->tcprecv.used);
101 break;
102 default:
103 assert(!"ads->tcpstate value");
104 }
105
106 assert(ads->searchlist || !ads->nsearchlist);
107 }
108
109 static void checkc_queue_udpw(adns_state ads) {
110 adns_query qu;
111
112 DLIST_CHECK(ads->udpw, qu, , {
113 assert(qu->state==query_tosend);
114 assert(qu->retries <= UDPMAXRETRIES);
115 assert(qu->udpsent);
116 assert(!qu->children.head && !qu->children.tail);
117 checkc_query(ads,qu);
118 checkc_query_alloc(ads,qu);
119 });
120 }
121
122 static void checkc_queue_tcpw(adns_state ads) {
123 adns_query qu;
124
125 DLIST_CHECK(ads->tcpw, qu, , {
126 assert(qu->state==query_tcpw);
127 assert(!qu->children.head && !qu->children.tail);
128 assert(qu->retries <= ads->nservers+1);
129 checkc_query(ads,qu);
130 checkc_query_alloc(ads,qu);
131 });
132 }
133
134 static void checkc_queue_childw(adns_state ads) {
135 adns_query parent, child;
136
137 DLIST_CHECK(ads->childw, parent, , {
138 assert(parent->state == query_childw);
139 assert(parent->children.head);
140 DLIST_CHECK(parent->children, child, siblings., {
141 assert(child->parent == parent);
142 assert(child->state != query_done);
143 });
144 checkc_query(ads,parent);
145 checkc_query_alloc(ads,parent);
146 });
147 }
148
149 static void checkc_queue_output(adns_state ads) {
150 adns_query qu;
151
152 DLIST_CHECK(ads->output, qu, , {
153 assert(qu->state == query_done);
154 assert(!qu->children.head && !qu->children.tail);
155 assert(!qu->parent);
156 assert(!qu->allocations.head && !qu->allocations.tail);
157 checkc_query(ads,qu);
158 });
159 }
160
161 void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
162 adns_query search;
163
164 switch (cc) {
165 case cc_user:
166 break;
167 case cc_entex:
168 if (!(ads->iflags & adns_if_checkc_entex)) return;
169 break;
170 case cc_freq:
171 if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
172 break;
173 default:
174 abort();
175 }
176
177 checkc_global(ads);
178 checkc_queue_udpw(ads);
179 checkc_queue_tcpw(ads);
180 checkc_queue_childw(ads);
181 checkc_queue_output(ads);
182
183 if (qu) {
184 switch (qu->state) {
185 case query_tosend:
186 DLIST_ASSERTON(qu, search, ads->udpw, );
187 break;
188 case query_tcpw:
189 DLIST_ASSERTON(qu, search, ads->tcpw, );
190 break;
191 case query_childw:
192 DLIST_ASSERTON(qu, search, ads->childw, );
193 break;
194 case query_done:
195 DLIST_ASSERTON(qu, search, ads->output, );
196 break;
197 default:
198 assert(!"specific query state");
199 }
200 }
201 }