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.)
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)
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.
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.
29 void adns_checkconsistency(adns_state ads
, adns_query qu
) {
30 adns__consistency(ads
,qu
,cc_user
);
33 #define DLIST_CHECK(list, nodevar, part, body) \
35 assert(! (list).head->part back); \
36 for ((nodevar)= (list).head; \
38 (nodevar)= (nodevar)->part next) { \
39 assert((nodevar)->part next \
40 ? (nodevar) == (nodevar)->part next->part back \
41 : (nodevar) == (list).tail); \
46 #define DLIST_ASSERTON(node, nodevar, list, part) \
48 for ((nodevar)= (list).head; \
49 (nodevar) != (node); \
50 (nodevar)= (nodevar)->part next) { \
55 static void checkc_query_alloc(adns_state ads
, adns_query qu
) {
58 DLIST_CHECK(qu
->allocations
, an
, , {
62 static void checkc_query(adns_state ads
, adns_query qu
) {
65 assert(qu
->udpnextserver
< ads
->nservers
);
66 assert(!(qu
->udpsent
& (~0UL << ads
->nservers
)));
67 assert(qu
->search_pos
<= ads
->nsearchlist
);
68 if (qu
->parent
) DLIST_ASSERTON(qu
, child
, qu
->parent
->children
, siblings
.);
71 static void checkc_notcpbuf(adns_state ads
) {
72 assert(!ads
->tcpsend
.used
);
73 assert(!ads
->tcprecv
.used
);
74 assert(!ads
->tcprecv_skip
);
77 static void checkc_global(adns_state ads
) {
78 const struct sortlist
*sl
;
81 assert(ads
->udpsockets
>= 0);
83 for (i
=0; i
<ads
->nsortlist
; i
++) {
84 sl
= &ads
->sortlist
[i
];
85 assert(adns__addr_matches(sl
->base
.sa
.sa_family
,
86 adns__sockaddr_addr(&sl
->base
.sa
),
87 &sl
->base
,&sl
->mask
));
90 assert(ads
->tcpserver
>= 0 && ads
->tcpserver
< ads
->nservers
);
92 switch (ads
->tcpstate
) {
93 case server_connecting
:
94 assert(ads
->tcpsocket
>= 0);
97 case server_disconnected
:
99 assert(ads
->tcpsocket
== -1);
100 checkc_notcpbuf(ads
);
103 assert(ads
->tcpsocket
>= 0);
104 assert(ads
->tcprecv_skip
<= ads
->tcprecv
.used
);
107 assert(!"ads->tcpstate value");
110 assert(ads
->searchlist
|| !ads
->nsearchlist
);
113 static void checkc_queue_udpw(adns_state ads
) {
116 DLIST_CHECK(ads
->udpw
, qu
, , {
117 assert(qu
->state
==query_tosend
);
118 assert(qu
->retries
<= UDPMAXRETRIES
);
120 assert(!qu
->children
.head
&& !qu
->children
.tail
);
121 checkc_query(ads
,qu
);
122 checkc_query_alloc(ads
,qu
);
126 static void checkc_queue_tcpw(adns_state ads
) {
129 DLIST_CHECK(ads
->tcpw
, qu
, , {
130 assert(qu
->state
==query_tcpw
);
131 assert(!qu
->children
.head
&& !qu
->children
.tail
);
132 assert(qu
->retries
<= ads
->nservers
+1);
133 checkc_query(ads
,qu
);
134 checkc_query_alloc(ads
,qu
);
138 static void checkc_queue_childw(adns_state ads
) {
139 adns_query parent
, child
;
141 DLIST_CHECK(ads
->childw
, parent
, , {
142 assert(parent
->state
== query_childw
);
143 assert(parent
->children
.head
);
144 DLIST_CHECK(parent
->children
, child
, siblings
., {
145 assert(child
->parent
== parent
);
146 assert(child
->state
!= query_done
);
148 checkc_query(ads
,parent
);
149 checkc_query_alloc(ads
,parent
);
153 static void checkc_query_done(adns_state ads
, adns_query qu
) {
154 assert(qu
->state
== query_done
);
155 assert(!qu
->children
.head
&& !qu
->children
.tail
);
156 checkc_query(ads
,qu
);
159 static void checkc_queue_output(adns_state ads
) {
162 DLIST_CHECK(ads
->output
, qu
, , {
164 assert(!qu
->allocations
.head
&& !qu
->allocations
.tail
);
165 checkc_query_done(ads
,qu
);
169 static void checkc_queue_intdone(adns_state ads
) {
172 DLIST_CHECK(ads
->intdone
, qu
, , {
174 assert(qu
->ctx
.callback
);
175 checkc_query_done(ads
,qu
);
179 void adns__consistency(adns_state ads
, adns_query qu
, consistency_checks cc
) {
186 if (!(ads
->iflags
& adns_if_checkc_entex
)) return;
187 assert(!ads
->intdone
.head
);
190 if ((ads
->iflags
& adns_if_checkc_freq
) != adns_if_checkc_freq
) return;
197 checkc_queue_udpw(ads
);
198 checkc_queue_tcpw(ads
);
199 checkc_queue_childw(ads
);
200 checkc_queue_output(ads
);
201 checkc_queue_intdone(ads
);
206 DLIST_ASSERTON(qu
, search
, ads
->udpw
, );
209 DLIST_ASSERTON(qu
, search
, ads
->tcpw
, );
212 DLIST_ASSERTON(qu
, search
, ads
->childw
, );
216 DLIST_ASSERTON(qu
, search
, ads
->intdone
, );
218 DLIST_ASSERTON(qu
, search
, ads
->output
, );
221 assert(!"specific query state");