3e2e5fab |
1 | /* |
2 | * check.c |
3 | * - consistency checks |
4 | */ |
5 | /* |
6 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2, or (at your option) |
11 | * any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software Foundation, |
20 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 | */ |
22 | |
23 | #include "internal.h" |
24 | |
28de6442 |
25 | void adns_checkconsistency(adns_state ads, adns_query qu) { |
26 | adns__consistency(ads,qu,cc_user); |
27 | } |
3e2e5fab |
28 | |
29 | #define DLIST_CHECK(list, nodevar, part, body) \ |
30 | if ((list).head) { \ |
31 | assert(! (list).head->part back); \ |
32 | for ((nodevar)= (list).head; (nodevar); (nodevar)= (nodevar)->part next) { \ |
33 | assert((nodevar)->part next \ |
34 | ? (nodevar) == (nodevar)->part next->part back \ |
35 | : (nodevar) == (list).tail); \ |
36 | body \ |
37 | } \ |
38 | } |
39 | |
28de6442 |
40 | #define DLIST_ASSERTON(node, nodevar, list, part) \ |
41 | do { \ |
42 | for ((nodevar)= (list).head; \ |
43 | (nodevar) != (node); \ |
44 | (nodevar)= (nodevar)->part next) { \ |
45 | assert((nodevar)); \ |
46 | } \ |
47 | } while(0) |
48 | |
3e2e5fab |
49 | static void checkc_query_alloc(adns_state ads, adns_query qu) { |
50 | allocnode *an; |
51 | |
52 | DLIST_CHECK(qu->allocations, an, , { |
53 | }); |
54 | } |
55 | |
56 | static void checkc_query(adns_state ads, adns_query qu) { |
28de6442 |
57 | adns_query child; |
58 | |
3e2e5fab |
59 | assert(qu->udpnextserver < ads->nservers); |
60 | assert(!(qu->udpsent & (~0UL << ads->nservers))); |
61 | assert(!(qu->tcpfailed & (~0UL << ads->nservers))); |
62 | assert(qu->udpretries <= UDPMAXRETRIES); |
63 | assert(qu->search_pos <= ads->nsearchlist); |
28de6442 |
64 | if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.); |
3e2e5fab |
65 | } |
66 | |
67 | static void checkc_global(adns_state ads) { |
68 | int i; |
69 | |
70 | assert(ads->udpsocket >= 0); |
71 | |
72 | for (i=0; i<ads->nsortlist; i++) |
73 | assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr)); |
74 | |
75 | assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers); |
76 | |
77 | switch (ads->tcpstate) { |
78 | case server_connecting: |
79 | assert(ads->tcpsocket >= 0); |
80 | case server_disconnected: /* fall through */ |
81 | assert(!ads->tcprecv.used); |
82 | assert(!ads->tcpsend.used); |
83 | break; |
84 | case server_ok: |
85 | assert(ads->tcpsocket >= 0); |
86 | break; |
87 | default: |
88 | assert(!"ads->tcpstate value"); |
89 | } |
90 | |
91 | assert(ads->searchlist || !ads->nsearchlist); |
92 | } |
93 | |
94 | static void checkc_queue_timew(adns_state ads) { |
95 | adns_query qu; |
96 | |
97 | DLIST_CHECK(ads->timew, qu, , { |
98 | switch (qu->state) { |
99 | case query_tosend: |
100 | assert(qu->udpsent); |
101 | assert(!qu->tcpfailed); |
102 | break; |
103 | case query_tcpwait: |
104 | assert(ads->tcpstate != server_ok); |
105 | break; |
106 | case query_tcpsent: |
107 | break; |
108 | default: |
109 | assert(!"timew state"); |
110 | } |
111 | assert(!qu->children.head && !qu->children.tail); |
112 | checkc_query(ads,qu); |
113 | checkc_query_alloc(ads,qu); |
114 | }); |
115 | } |
116 | |
117 | static void checkc_queue_childw(adns_state ads) { |
118 | adns_query parent, child; |
119 | |
120 | DLIST_CHECK(ads->childw, parent, , { |
121 | assert(parent->state == query_child); |
122 | assert(parent->children.head); |
123 | DLIST_CHECK(parent->children, child, siblings., { |
124 | assert(child->parent == parent); |
125 | assert(child->state != query_done); |
126 | }); |
127 | checkc_query(ads,parent); |
128 | checkc_query_alloc(ads,parent); |
129 | }); |
130 | } |
131 | |
132 | static void checkc_queue_output(adns_state ads) { |
133 | adns_query qu; |
134 | |
135 | DLIST_CHECK(ads->output, qu, , { |
136 | assert(qu->state == query_done); |
137 | assert(!qu->children.head && !qu->children.tail); |
138 | assert(!qu->parent); |
139 | checkc_query(ads,qu); |
140 | }); |
141 | } |
142 | |
28de6442 |
143 | void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) { |
144 | adns_query search; |
145 | |
3e2e5fab |
146 | switch (cc) { |
147 | case cc_user: |
148 | break; |
149 | case cc_entex: |
150 | if (!(ads->iflags & adns_if_checkc_entex)) return; |
151 | break; |
152 | case cc_freq: |
153 | if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return; |
154 | break; |
155 | default: |
156 | abort(); |
157 | } |
158 | |
159 | checkc_global(ads); |
160 | checkc_queue_timew(ads); |
161 | checkc_queue_childw(ads); |
162 | checkc_queue_output(ads); |
28de6442 |
163 | |
164 | if (qu) { |
165 | switch (qu->state) { |
166 | case query_tosend: |
167 | case query_tcpwait: |
168 | case query_tcpsent: |
169 | DLIST_ASSERTON(qu, search, ads->timew, ); |
170 | break; |
171 | case query_child: |
172 | DLIST_ASSERTON(qu, search, ads->childw, ); |
173 | break; |
174 | case query_done: |
175 | DLIST_ASSERTON(qu, search, ads->output, ); |
176 | break; |
177 | default: |
178 | assert(!"specific query state"); |
179 | } |
180 | } |
3e2e5fab |
181 | } |