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