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