summaryrefslogtreecommitdiff
path: root/src/check.c
blob: 41cdde5ff3592d09f86d3e0bd4b9d8abcc114896 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * check.c
 * - consistency checks
 */
/*
 *  This file is part of adns, which is
 *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
 *    Copyright (C) 1999-2000,2003,2006  Tony Finch
 *    Copyright (C) 1991 Massachusetts Institute of Technology
 *  (See the file INSTALL for full details.)
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 */

#include "internal.h"

void adns_checkconsistency(adns_state ads, adns_query qu) {
  adns__consistency(ads,qu,cc_user);
}

#define DLIST_CHECK(list, nodevar, part, body)			\
  if ((list).head) {						\
    assert(! (list).head->part back);				\
    for ((nodevar)= (list).head;				\
	 (nodevar);						\
	 (nodevar)= (nodevar)->part next) {			\
      assert((nodevar)->part next				\
	     ? (nodevar) == (nodevar)->part next->part back	\
	     : (nodevar) == (list).tail);			\
      body							\
    }								\
  }

#define DLIST_ASSERTON(node, nodevar, list, part)	\
  do {							\
    for ((nodevar)= (list).head;			\
	 (nodevar) != (node);				\
	 (nodevar)= (nodevar)->part next) {		\
      assert((nodevar));				\
    }							\
  } while(0)

static void checkc_query_alloc(adns_state ads, adns_query qu) {
  allocnode *an;

  DLIST_CHECK(qu->allocations, an, , {
  });
}

static void checkc_query(adns_state ads, adns_query qu) {
  adns_query child;

  assert(qu->udpnextserver < ads->nservers);
  assert(!(qu->udpsent & (~0UL << ads->nservers)));
  assert(qu->search_pos <= ads->nsearchlist);
  if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.);
}

static void checkc_notcpbuf(adns_state ads) {
  assert(!ads->tcpsend.used);
  assert(!ads->tcprecv.used);
  assert(!ads->tcprecv_skip);
}

static void checkc_global(adns_state ads) {
  int i;
  
  assert(ads->udpsocket >= 0);

  for (i=0; i<ads->nsortlist; i++)
    assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr));

  assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers);
  
  switch (ads->tcpstate) {
  case server_connecting:
    assert(ads->tcpsocket >= 0);
    checkc_notcpbuf(ads);
    break;
  case server_disconnected:
  case server_broken:
    assert(ads->tcpsocket == -1);
    checkc_notcpbuf(ads);
    break;
  case server_ok:
    assert(ads->tcpsocket >= 0);
    assert(ads->tcprecv_skip <= ads->tcprecv.used);
    break;
  default:
    assert(!"ads->tcpstate value");
  }

  assert(ads->searchlist || !ads->nsearchlist);
}

static void checkc_queue_udpw(adns_state ads) {
  adns_query qu;
  
  DLIST_CHECK(ads->udpw, qu, , {
    assert(qu->state==query_tosend);
    assert(qu->retries <= UDPMAXRETRIES);
    assert(qu->udpsent);
    assert(!qu->children.head && !qu->children.tail);
    checkc_query(ads,qu);
    checkc_query_alloc(ads,qu);
  });
}

static void checkc_queue_tcpw(adns_state ads) {
  adns_query qu;
  
  DLIST_CHECK(ads->tcpw, qu, , {
    assert(qu->state==query_tcpw);
    assert(!qu->children.head && !qu->children.tail);
    assert(qu->retries <= ads->nservers+1);
    checkc_query(ads,qu);
    checkc_query_alloc(ads,qu);
  });
}

static void checkc_queue_childw(adns_state ads) {
  adns_query parent, child;

  DLIST_CHECK(ads->childw, parent, , {
    assert(parent->state == query_childw);
    assert(parent->children.head);
    DLIST_CHECK(parent->children, child, siblings., {
      assert(child->parent == parent);
      assert(child->state != query_done);
    });
    checkc_query(ads,parent);
    checkc_query_alloc(ads,parent);
  });
}

static void checkc_queue_output(adns_state ads) {
  adns_query qu;
  
  DLIST_CHECK(ads->output, qu, , {
    assert(qu->state == query_done);
    assert(!qu->children.head && !qu->children.tail);
    assert(!qu->parent);
    assert(!qu->allocations.head && !qu->allocations.tail);
    checkc_query(ads,qu);
  });
}

void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) {
  adns_query search;
  
  switch (cc) {
  case cc_user:
    break;
  case cc_entex:
    if (!(ads->iflags & adns_if_checkc_entex)) return;
    break;
  case cc_freq:
    if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return;
    break;
  default:
    abort();
  }

  checkc_global(ads);
  checkc_queue_udpw(ads);
  checkc_queue_tcpw(ads);
  checkc_queue_childw(ads);
  checkc_queue_output(ads);

  if (qu) {
    switch (qu->state) {
    case query_tosend:
      DLIST_ASSERTON(qu, search, ads->udpw, );
      break;
    case query_tcpw:
      DLIST_ASSERTON(qu, search, ads->tcpw, );
      break;
    case query_childw:
      DLIST_ASSERTON(qu, search, ads->childw, );
      break;
    case query_done:
      DLIST_ASSERTON(qu, search, ads->output, );
      break;
    default:
      assert(!"specific query state");
    }
  }
}