6 * This file is part of adns, which is
7 * Copyright (C) 1997-2000,2003,2006,2014 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 3, 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.
28 void adns_checkconsistency(adns_state ads
, adns_query qu
) {
29 adns__consistency(ads
,qu
,cc_user
);
32 #define DLIST_CHECK(list, nodevar, part, body) \
34 assert(! (list).head->part back); \
35 for ((nodevar)= (list).head; \
37 (nodevar)= (nodevar)->part next) { \
38 assert((nodevar)->part next \
39 ? (nodevar) == (nodevar)->part next->part back \
40 : (nodevar) == (list).tail); \
45 #define DLIST_ASSERTON(node, nodevar, list, part) \
47 for ((nodevar)= (list).head; \
48 (nodevar) != (node); \
49 (nodevar)= (nodevar)->part next) { \
54 static void checkc_query_alloc(adns_state ads
, adns_query qu
) {
57 DLIST_CHECK(qu
->allocations
, an
, , {
61 static void checkc_query(adns_state ads
, adns_query qu
) {
64 assert(qu
->udpnextserver
< ads
->nservers
);
65 assert(!(qu
->udpsent
& (~0UL << ads
->nservers
)));
66 assert(qu
->search_pos
<= ads
->nsearchlist
);
67 if (qu
->parent
) DLIST_ASSERTON(qu
, child
, qu
->parent
->children
, siblings
.);
70 static void checkc_notcpbuf(adns_state ads
) {
71 assert(!ads
->tcpsend
.used
);
72 assert(!ads
->tcprecv
.used
);
73 assert(!ads
->tcprecv_skip
);
76 static void checkc_global(adns_state ads
) {
77 const struct sortlist
*sl
;
80 assert(ads
->udpsockets
>= 0);
82 for (i
=0; i
<ads
->nsortlist
; i
++) {
83 sl
= &ads
->sortlist
[i
];
84 assert(adns__addr_matches(sl
->base
.sa
.sa_family
,
85 adns__sockaddr_addr(&sl
->base
.sa
),
86 &sl
->base
,&sl
->mask
));
89 assert(ads
->tcpserver
>= 0 && ads
->tcpserver
< ads
->nservers
);
91 switch (ads
->tcpstate
) {
92 case server_connecting
:
93 assert(ads
->tcpsocket
>= 0);
96 case server_disconnected
:
98 assert(ads
->tcpsocket
== -1);
102 assert(ads
->tcpsocket
>= 0);
103 assert(ads
->tcprecv_skip
<= ads
->tcprecv
.used
);
106 assert(!"ads->tcpstate value");
109 assert(ads
->searchlist
|| !ads
->nsearchlist
);
112 static void checkc_queue_udpw(adns_state ads
) {
115 DLIST_CHECK(ads
->udpw
, qu
, , {
116 assert(qu
->state
==query_tosend
);
117 assert(qu
->retries
<= UDPMAXRETRIES
);
119 assert(!qu
->children
.head
&& !qu
->children
.tail
);
120 checkc_query(ads
,qu
);
121 checkc_query_alloc(ads
,qu
);
125 static void checkc_queue_tcpw(adns_state ads
) {
128 DLIST_CHECK(ads
->tcpw
, qu
, , {
129 assert(qu
->state
==query_tcpw
);
130 assert(!qu
->children
.head
&& !qu
->children
.tail
);
131 assert(qu
->retries
<= ads
->nservers
+1);
132 checkc_query(ads
,qu
);
133 checkc_query_alloc(ads
,qu
);
137 static void checkc_queue_childw(adns_state ads
) {
138 adns_query parent
, child
;
140 DLIST_CHECK(ads
->childw
, parent
, , {
141 assert(parent
->state
== query_childw
);
142 assert(parent
->children
.head
);
143 DLIST_CHECK(parent
->children
, child
, siblings
., {
144 assert(child
->parent
== parent
);
145 assert(child
->state
!= query_done
);
147 checkc_query(ads
,parent
);
148 checkc_query_alloc(ads
,parent
);
152 static void checkc_query_done(adns_state ads
, adns_query qu
) {
153 assert(qu
->state
== query_done
);
154 assert(!qu
->children
.head
&& !qu
->children
.tail
);
155 checkc_query(ads
,qu
);
158 static void checkc_queue_output(adns_state ads
) {
161 DLIST_CHECK(ads
->output
, qu
, , {
163 assert(!qu
->allocations
.head
&& !qu
->allocations
.tail
);
164 checkc_query_done(ads
,qu
);
168 static void checkc_queue_intdone(adns_state ads
) {
171 DLIST_CHECK(ads
->intdone
, qu
, , {
173 assert(qu
->ctx
.callback
);
174 checkc_query_done(ads
,qu
);
178 void adns__consistency(adns_state ads
, adns_query qu
, consistency_checks cc
) {
185 if (!(ads
->iflags
& adns_if_checkc_entex
)) return;
186 assert(!ads
->intdone
.head
);
189 if ((ads
->iflags
& adns_if_checkc_freq
) != adns_if_checkc_freq
) return;
196 checkc_queue_udpw(ads
);
197 checkc_queue_tcpw(ads
);
198 checkc_queue_childw(ads
);
199 checkc_queue_output(ads
);
200 checkc_queue_intdone(ads
);
205 DLIST_ASSERTON(qu
, search
, ads
->udpw
, );
208 DLIST_ASSERTON(qu
, search
, ads
->tcpw
, );
211 DLIST_ASSERTON(qu
, search
, ads
->childw
, );
215 DLIST_ASSERTON(qu
, search
, ads
->intdone
, );
217 DLIST_ASSERTON(qu
, search
, ads
->output
, );
220 assert(!"specific query state");