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