3e2e5fab |
1 | /* |
2 | * check.c |
3 | * - consistency checks |
4 | */ |
5 | /* |
ae8cc977 |
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.) |
3e2e5fab |
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 | |
28de6442 |
29 | void adns_checkconsistency(adns_state ads, adns_query qu) { |
30 | adns__consistency(ads,qu,cc_user); |
31 | } |
3e2e5fab |
32 | |
609133ee |
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 | } \ |
3e2e5fab |
44 | } |
45 | |
609133ee |
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 | } \ |
28de6442 |
53 | } while(0) |
54 | |
3e2e5fab |
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) { |
28de6442 |
63 | adns_query child; |
64 | |
3e2e5fab |
65 | assert(qu->udpnextserver < ads->nservers); |
66 | assert(!(qu->udpsent & (~0UL << ads->nservers))); |
3e2e5fab |
67 | assert(qu->search_pos <= ads->nsearchlist); |
28de6442 |
68 | if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.); |
3e2e5fab |
69 | } |
70 | |
f7f83b4a |
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 | |
3e2e5fab |
77 | static void checkc_global(adns_state ads) { |
78 | int i; |
79 | |
80 | assert(ads->udpsocket >= 0); |
81 | |
82 | for (i=0; i<ads->nsortlist; i++) |
83 | assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr)); |
84 | |
85 | assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers); |
86 | |
87 | switch (ads->tcpstate) { |
88 | case server_connecting: |
89 | assert(ads->tcpsocket >= 0); |
f7f83b4a |
90 | checkc_notcpbuf(ads); |
91 | break; |
92 | case server_disconnected: |
93 | case server_broken: |
94 | assert(ads->tcpsocket == -1); |
95 | checkc_notcpbuf(ads); |
3e2e5fab |
96 | break; |
97 | case server_ok: |
98 | assert(ads->tcpsocket >= 0); |
70ad7a2a |
99 | assert(ads->tcprecv_skip <= ads->tcprecv.used); |
3e2e5fab |
100 | break; |
101 | default: |
102 | assert(!"ads->tcpstate value"); |
103 | } |
104 | |
105 | assert(ads->searchlist || !ads->nsearchlist); |
106 | } |
107 | |
f7f83b4a |
108 | static void checkc_queue_udpw(adns_state ads) { |
3e2e5fab |
109 | adns_query qu; |
110 | |
f7f83b4a |
111 | DLIST_CHECK(ads->udpw, qu, , { |
112 | assert(qu->state==query_tosend); |
113 | assert(qu->retries <= UDPMAXRETRIES); |
114 | assert(qu->udpsent); |
3e2e5fab |
115 | assert(!qu->children.head && !qu->children.tail); |
116 | checkc_query(ads,qu); |
117 | checkc_query_alloc(ads,qu); |
118 | }); |
119 | } |
120 | |
f7f83b4a |
121 | static void checkc_queue_tcpw(adns_state ads) { |
122 | adns_query qu; |
123 | |
124 | DLIST_CHECK(ads->tcpw, qu, , { |
125 | assert(qu->state==query_tcpw); |
126 | assert(!qu->children.head && !qu->children.tail); |
127 | assert(qu->retries <= ads->nservers+1); |
128 | checkc_query(ads,qu); |
129 | checkc_query_alloc(ads,qu); |
130 | }); |
131 | } |
132 | |
3e2e5fab |
133 | static void checkc_queue_childw(adns_state ads) { |
134 | adns_query parent, child; |
135 | |
136 | DLIST_CHECK(ads->childw, parent, , { |
f7f83b4a |
137 | assert(parent->state == query_childw); |
3e2e5fab |
138 | assert(parent->children.head); |
139 | DLIST_CHECK(parent->children, child, siblings., { |
140 | assert(child->parent == parent); |
141 | assert(child->state != query_done); |
142 | }); |
143 | checkc_query(ads,parent); |
144 | checkc_query_alloc(ads,parent); |
145 | }); |
146 | } |
147 | |
148 | static void checkc_queue_output(adns_state ads) { |
149 | adns_query qu; |
150 | |
151 | DLIST_CHECK(ads->output, qu, , { |
152 | assert(qu->state == query_done); |
153 | assert(!qu->children.head && !qu->children.tail); |
154 | assert(!qu->parent); |
bfc2c80e |
155 | assert(!qu->allocations.head && !qu->allocations.tail); |
3e2e5fab |
156 | checkc_query(ads,qu); |
157 | }); |
158 | } |
159 | |
28de6442 |
160 | void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) { |
161 | adns_query search; |
162 | |
3e2e5fab |
163 | switch (cc) { |
164 | case cc_user: |
165 | break; |
166 | case cc_entex: |
167 | if (!(ads->iflags & adns_if_checkc_entex)) return; |
168 | break; |
169 | case cc_freq: |
170 | if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return; |
171 | break; |
172 | default: |
173 | abort(); |
174 | } |
175 | |
176 | checkc_global(ads); |
f7f83b4a |
177 | checkc_queue_udpw(ads); |
178 | checkc_queue_tcpw(ads); |
3e2e5fab |
179 | checkc_queue_childw(ads); |
180 | checkc_queue_output(ads); |
28de6442 |
181 | |
182 | if (qu) { |
183 | switch (qu->state) { |
184 | case query_tosend: |
f7f83b4a |
185 | DLIST_ASSERTON(qu, search, ads->udpw, ); |
186 | break; |
187 | case query_tcpw: |
188 | DLIST_ASSERTON(qu, search, ads->tcpw, ); |
28de6442 |
189 | break; |
f7f83b4a |
190 | case query_childw: |
28de6442 |
191 | DLIST_ASSERTON(qu, search, ads->childw, ); |
192 | break; |
193 | case query_done: |
194 | DLIST_ASSERTON(qu, search, ads->output, ); |
195 | break; |
196 | default: |
197 | assert(!"specific query state"); |
198 | } |
199 | } |
3e2e5fab |
200 | } |