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))); |
66 | assert(!(qu->tcpfailed & (~0UL << ads->nservers))); |
67 | assert(qu->udpretries <= UDPMAXRETRIES); |
68 | assert(qu->search_pos <= ads->nsearchlist); |
28de6442 |
69 | if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.); |
3e2e5fab |
70 | } |
71 | |
72 | static void checkc_global(adns_state ads) { |
73 | int i; |
74 | |
75 | assert(ads->udpsocket >= 0); |
76 | |
77 | for (i=0; i<ads->nsortlist; i++) |
78 | assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr)); |
79 | |
80 | assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers); |
81 | |
82 | switch (ads->tcpstate) { |
83 | case server_connecting: |
84 | assert(ads->tcpsocket >= 0); |
85 | case server_disconnected: /* fall through */ |
86 | assert(!ads->tcprecv.used); |
87 | assert(!ads->tcpsend.used); |
88 | break; |
89 | case server_ok: |
90 | assert(ads->tcpsocket >= 0); |
91 | break; |
92 | default: |
93 | assert(!"ads->tcpstate value"); |
94 | } |
95 | |
96 | assert(ads->searchlist || !ads->nsearchlist); |
97 | } |
98 | |
99 | static void checkc_queue_timew(adns_state ads) { |
100 | adns_query qu; |
101 | |
102 | DLIST_CHECK(ads->timew, qu, , { |
103 | switch (qu->state) { |
104 | case query_tosend: |
105 | assert(qu->udpsent); |
106 | assert(!qu->tcpfailed); |
107 | break; |
108 | case query_tcpwait: |
109 | assert(ads->tcpstate != server_ok); |
110 | break; |
111 | case query_tcpsent: |
112 | break; |
113 | default: |
114 | assert(!"timew state"); |
115 | } |
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_childw(adns_state ads) { |
123 | adns_query parent, child; |
124 | |
125 | DLIST_CHECK(ads->childw, parent, , { |
126 | assert(parent->state == query_child); |
127 | assert(parent->children.head); |
128 | DLIST_CHECK(parent->children, child, siblings., { |
129 | assert(child->parent == parent); |
130 | assert(child->state != query_done); |
131 | }); |
132 | checkc_query(ads,parent); |
133 | checkc_query_alloc(ads,parent); |
134 | }); |
135 | } |
136 | |
137 | static void checkc_queue_output(adns_state ads) { |
138 | adns_query qu; |
139 | |
140 | DLIST_CHECK(ads->output, qu, , { |
141 | assert(qu->state == query_done); |
142 | assert(!qu->children.head && !qu->children.tail); |
143 | assert(!qu->parent); |
bfc2c80e |
144 | assert(!qu->allocations.head && !qu->allocations.tail); |
3e2e5fab |
145 | checkc_query(ads,qu); |
146 | }); |
147 | } |
148 | |
28de6442 |
149 | void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) { |
150 | adns_query search; |
151 | |
3e2e5fab |
152 | switch (cc) { |
153 | case cc_user: |
154 | break; |
155 | case cc_entex: |
156 | if (!(ads->iflags & adns_if_checkc_entex)) return; |
157 | break; |
158 | case cc_freq: |
159 | if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return; |
160 | break; |
161 | default: |
162 | abort(); |
163 | } |
164 | |
165 | checkc_global(ads); |
166 | checkc_queue_timew(ads); |
167 | checkc_queue_childw(ads); |
168 | checkc_queue_output(ads); |
28de6442 |
169 | |
170 | if (qu) { |
171 | switch (qu->state) { |
172 | case query_tosend: |
173 | case query_tcpwait: |
174 | case query_tcpsent: |
175 | DLIST_ASSERTON(qu, search, ads->timew, ); |
176 | break; |
177 | case query_child: |
178 | DLIST_ASSERTON(qu, search, ads->childw, ); |
179 | break; |
180 | case query_done: |
181 | DLIST_ASSERTON(qu, search, ads->output, ); |
182 | break; |
183 | default: |
184 | assert(!"specific query state"); |
185 | } |
186 | } |
3e2e5fab |
187 | } |