| 1 | /* |
| 2 | * check.c |
| 3 | * - consistency checks |
| 4 | */ |
| 5 | /* |
| 6 | * This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software Foundation, |
| 20 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include "internal.h" |
| 24 | |
| 25 | void adns_checkconsistency(adns_state ads, adns_query qu) { |
| 26 | adns__consistency(ads,qu,cc_user); |
| 27 | } |
| 28 | |
| 29 | #define DLIST_CHECK(list, nodevar, part, body) \ |
| 30 | if ((list).head) { \ |
| 31 | assert(! (list).head->part back); \ |
| 32 | for ((nodevar)= (list).head; (nodevar); (nodevar)= (nodevar)->part next) { \ |
| 33 | assert((nodevar)->part next \ |
| 34 | ? (nodevar) == (nodevar)->part next->part back \ |
| 35 | : (nodevar) == (list).tail); \ |
| 36 | body \ |
| 37 | } \ |
| 38 | } |
| 39 | |
| 40 | #define DLIST_ASSERTON(node, nodevar, list, part) \ |
| 41 | do { \ |
| 42 | for ((nodevar)= (list).head; \ |
| 43 | (nodevar) != (node); \ |
| 44 | (nodevar)= (nodevar)->part next) { \ |
| 45 | assert((nodevar)); \ |
| 46 | } \ |
| 47 | } while(0) |
| 48 | |
| 49 | static void checkc_query_alloc(adns_state ads, adns_query qu) { |
| 50 | allocnode *an; |
| 51 | |
| 52 | DLIST_CHECK(qu->allocations, an, , { |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | static void checkc_query(adns_state ads, adns_query qu) { |
| 57 | adns_query child; |
| 58 | |
| 59 | assert(qu->udpnextserver < ads->nservers); |
| 60 | assert(!(qu->udpsent & (~0UL << ads->nservers))); |
| 61 | assert(!(qu->tcpfailed & (~0UL << ads->nservers))); |
| 62 | assert(qu->udpretries <= UDPMAXRETRIES); |
| 63 | assert(qu->search_pos <= ads->nsearchlist); |
| 64 | if (qu->parent) DLIST_ASSERTON(qu, child, qu->parent->children, siblings.); |
| 65 | } |
| 66 | |
| 67 | static void checkc_global(adns_state ads) { |
| 68 | int i; |
| 69 | |
| 70 | assert(ads->udpsocket >= 0); |
| 71 | |
| 72 | for (i=0; i<ads->nsortlist; i++) |
| 73 | assert(!(ads->sortlist[i].base.s_addr & ~ads->sortlist[i].mask.s_addr)); |
| 74 | |
| 75 | assert(ads->tcpserver >= 0 && ads->tcpserver < ads->nservers); |
| 76 | |
| 77 | switch (ads->tcpstate) { |
| 78 | case server_connecting: |
| 79 | assert(ads->tcpsocket >= 0); |
| 80 | case server_disconnected: /* fall through */ |
| 81 | assert(!ads->tcprecv.used); |
| 82 | assert(!ads->tcpsend.used); |
| 83 | break; |
| 84 | case server_ok: |
| 85 | assert(ads->tcpsocket >= 0); |
| 86 | break; |
| 87 | default: |
| 88 | assert(!"ads->tcpstate value"); |
| 89 | } |
| 90 | |
| 91 | assert(ads->searchlist || !ads->nsearchlist); |
| 92 | } |
| 93 | |
| 94 | static void checkc_queue_timew(adns_state ads) { |
| 95 | adns_query qu; |
| 96 | |
| 97 | DLIST_CHECK(ads->timew, qu, , { |
| 98 | switch (qu->state) { |
| 99 | case query_tosend: |
| 100 | assert(qu->udpsent); |
| 101 | assert(!qu->tcpfailed); |
| 102 | break; |
| 103 | case query_tcpwait: |
| 104 | assert(ads->tcpstate != server_ok); |
| 105 | break; |
| 106 | case query_tcpsent: |
| 107 | break; |
| 108 | default: |
| 109 | assert(!"timew state"); |
| 110 | } |
| 111 | assert(!qu->children.head && !qu->children.tail); |
| 112 | checkc_query(ads,qu); |
| 113 | checkc_query_alloc(ads,qu); |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | static void checkc_queue_childw(adns_state ads) { |
| 118 | adns_query parent, child; |
| 119 | |
| 120 | DLIST_CHECK(ads->childw, parent, , { |
| 121 | assert(parent->state == query_child); |
| 122 | assert(parent->children.head); |
| 123 | DLIST_CHECK(parent->children, child, siblings., { |
| 124 | assert(child->parent == parent); |
| 125 | assert(child->state != query_done); |
| 126 | }); |
| 127 | checkc_query(ads,parent); |
| 128 | checkc_query_alloc(ads,parent); |
| 129 | }); |
| 130 | } |
| 131 | |
| 132 | static void checkc_queue_output(adns_state ads) { |
| 133 | adns_query qu; |
| 134 | |
| 135 | DLIST_CHECK(ads->output, qu, , { |
| 136 | assert(qu->state == query_done); |
| 137 | assert(!qu->children.head && !qu->children.tail); |
| 138 | assert(!qu->parent); |
| 139 | assert(!qu->allocations.head && !qu->allocations.tail); |
| 140 | checkc_query(ads,qu); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | void adns__consistency(adns_state ads, adns_query qu, consistency_checks cc) { |
| 145 | adns_query search; |
| 146 | |
| 147 | switch (cc) { |
| 148 | case cc_user: |
| 149 | break; |
| 150 | case cc_entex: |
| 151 | if (!(ads->iflags & adns_if_checkc_entex)) return; |
| 152 | break; |
| 153 | case cc_freq: |
| 154 | if ((ads->iflags & adns_if_checkc_freq) != adns_if_checkc_freq) return; |
| 155 | break; |
| 156 | default: |
| 157 | abort(); |
| 158 | } |
| 159 | |
| 160 | checkc_global(ads); |
| 161 | checkc_queue_timew(ads); |
| 162 | checkc_queue_childw(ads); |
| 163 | checkc_queue_output(ads); |
| 164 | |
| 165 | if (qu) { |
| 166 | switch (qu->state) { |
| 167 | case query_tosend: |
| 168 | case query_tcpwait: |
| 169 | case query_tcpsent: |
| 170 | DLIST_ASSERTON(qu, search, ads->timew, ); |
| 171 | break; |
| 172 | case query_child: |
| 173 | DLIST_ASSERTON(qu, search, ads->childw, ); |
| 174 | break; |
| 175 | case query_done: |
| 176 | DLIST_ASSERTON(qu, search, ads->output, ); |
| 177 | break; |
| 178 | default: |
| 179 | assert(!"specific query state"); |
| 180 | } |
| 181 | } |
| 182 | } |