6 * This file is part of adns, which is Copyright Ian Jackson
7 * and contributors (see the file INSTALL for full details).
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation.
25 void adns_checkconsistency(adns_state ads, adns_query qu) {
26 adns__consistency(ads,qu,cc_user);
29 #define DLIST_CHECK(list, nodevar, part, body) \
31 assert(! (list).head->part back); \
32 for ((nodevar)= (list).head; \
34 (nodevar)= (nodevar)->part next) { \
35 assert((nodevar)->part next \
36 ? (nodevar) == (nodevar)->part next->part back \
37 : (nodevar) == (list).tail); \
42 #define DLIST_ASSERTON(node, nodevar, list, part) \
44 for ((nodevar)= (list).head; \
45 (nodevar) != (node); \
46 (nodevar)= (nodevar)->part next) { \
51 static void checkc_query_alloc(adns_state ads, adns_query qu) {
54 DLIST_CHECK(qu->allocations, an, , {
58 static void checkc_query(adns_state ads, adns_query qu) {
61 assert(qu->udpnextserver < ads->nservers);
62 assert(!(qu->udpsent & (~0UL << ads->nservers)));
63 assert(qu->search_pos <= ads->nsearchlist);
64 if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.);
67 static void checkc_notcpbuf(adns_state ads) {
68 assert(!ads->tcpsend.used);
69 assert(!ads->tcprecv.used);
70 assert(!ads->tcprecv_skip);
73 static void checkc_global(adns_state ads) {
74 const struct sortlist *sl;
77 assert(ads->udpsockets >= 0);
79 for (i=0; i<ads->nsortlist; i++) {
80 sl= &ads->sortlist[i];
81 assert(adns__addr_matches(sl->base.sa.sa_family,
82 adns__sockaddr_addr(&sl->base.sa),
83 &sl->base,&sl->mask));
86 assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
88 switch (ads->tcpstate) {
89 case server_connecting:
90 assert(ads->tcpsocket >= 0);
93 case server_disconnected:
95 assert(ads->tcpsocket == -1);
99 assert(ads->tcpsocket >= 0);
100 assert(ads->tcprecv_skip <= ads->tcprecv.used);
103 assert(!"ads->tcpstate value");
106 assert(ads->searchlist || !ads->nsearchlist);
109 static void checkc_queue_udpw(adns_state ads) {
112 DLIST_CHECK(ads->udpw, qu, , {
113 assert(qu->state==query_tosend);
114 assert(qu->retries <= UDPMAXRETRIES);
116 assert(!qu->children.head && !qu->children.tail);
117 checkc_query(ads,qu);
118 checkc_query_alloc(ads,qu);
122 static void checkc_queue_tcpw(adns_state ads) {
125 DLIST_CHECK(ads->tcpw, qu, , {
126 assert(qu->state==query_tcpw);
127 assert(!qu->children.head && !qu->children.tail);
128 assert(qu->retries <= ads->nservers+1);
129 checkc_query(ads,qu);
130 checkc_query_alloc(ads,qu);
134 static void checkc_queue_childw(adns_state ads) {
135 adns_query parent, child, search;
137 DLIST_CHECK(ads->childw, parent, , {
138 assert(parent->state == query_childw);
139 assert(parent->children.head);
140 DLIST_CHECK(parent->children, child, siblings., {
141 assert(child->parent == parent);
142 if (child->state == query_done) {
143 for (search= ads->intdone.head; search; search= search->next)
144 if (search==child) goto child_done_ok;
145 assert(!"done child not on intdone");
149 checkc_query(ads,parent);
150 checkc_query_alloc(ads,parent);
154 static void checkc_query_done(adns_state ads, adns_query qu) {
155 assert(qu->state == query_done);
156 assert(!qu->children.head && !qu->children.tail);
157 checkc_query(ads,qu);
160 static void checkc_queue_output(adns_state ads) {
163 DLIST_CHECK(ads->output, qu, , {
165 assert(!qu->allocations.head && !qu->allocations.tail);
166 checkc_query_done(ads,qu);
170 static void checkc_queue_intdone(adns_state ads) {
173 DLIST_CHECK(ads->intdone, qu, , {
175 assert(qu->ctx.callback);
176 checkc_query_done(ads,qu);
180 void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
187 if (!(ads->iflags & adns_if_checkc_entex)) return;
190 if (!(ads->iflags & adns_if_checkc_entex)) return;
191 assert(!ads->intdone.head);
194 if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
201 checkc_queue_udpw(ads);
202 checkc_queue_tcpw(ads);
203 checkc_queue_childw(ads);
204 checkc_queue_output(ads);
205 checkc_queue_intdone(ads);
210 DLIST_ASSERTON(qu, search, ads->udpw, );
213 DLIST_ASSERTON(qu, search, ads->tcpw, );
216 DLIST_ASSERTON(qu, search, ads->childw, );
220 DLIST_ASSERTON(qu, search, ads->intdone, );
222 DLIST_ASSERTON(qu, search, ads->output, );
225 assert(!"specific query state");