1 1.1 christos /* 2 1.1 christos * iterator/iter_hints.h - iterative resolver module stub and root hints. 3 1.1 christos * 4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 1.1 christos * 6 1.1 christos * This software is open source. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 12 1.1 christos * Redistributions of source code must retain the above copyright notice, 13 1.1 christos * this list of conditions and the following disclaimer. 14 1.1 christos * 15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice, 16 1.1 christos * this list of conditions and the following disclaimer in the documentation 17 1.1 christos * and/or other materials provided with the distribution. 18 1.1 christos * 19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may 20 1.1 christos * be used to endorse or promote products derived from this software without 21 1.1 christos * specific prior written permission. 22 1.1 christos * 23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.1 christos */ 35 1.1 christos 36 1.1 christos /** 37 1.1 christos * \file 38 1.1 christos * 39 1.1 christos * This file contains functions to assist the iterator module. 40 1.1 christos * Keep track of stub and root hints, and read those from config. 41 1.1 christos */ 42 1.1 christos 43 1.1 christos #ifndef ITERATOR_ITER_HINTS_H 44 1.1 christos #define ITERATOR_ITER_HINTS_H 45 1.1 christos #include "util/storage/dnstree.h" 46 1.1.1.3 christos #include "util/locks.h" 47 1.1 christos struct iter_env; 48 1.1 christos struct config_file; 49 1.1 christos struct delegpt; 50 1.1 christos 51 1.1 christos /** 52 1.1 christos * Iterator hints structure 53 1.1 christos */ 54 1.1 christos struct iter_hints { 55 1.1.1.3 christos /** lock on the forwards tree. 56 1.1.1.3 christos * When grabbing both this lock and the anchors.lock, this lock 57 1.1.1.3 christos * is grabbed first. */ 58 1.1.1.3 christos lock_rw_type lock; 59 1.1 christos /** 60 1.1 christos * Hints are stored in this tree. Sort order is specially chosen. 61 1.1 christos * first sorted on qclass. Then on dname in nsec-like order, so that 62 1.1 christos * a lookup on class, name will return an exact match or the closest 63 1.1 christos * match which gives the ancestor needed. 64 1.1 christos * contents of type iter_hints_stub. The class IN root is in here. 65 1.1 christos * uses name_tree_node from dnstree.h. 66 1.1 christos */ 67 1.1.1.2 christos rbtree_type tree; 68 1.1 christos }; 69 1.1 christos 70 1.1 christos /** 71 1.1 christos * Iterator hints for a particular stub. 72 1.1 christos */ 73 1.1 christos struct iter_hints_stub { 74 1.1 christos /** tree sorted by name, class */ 75 1.1 christos struct name_tree_node node; 76 1.1 christos /** delegation point with hint information for this stub. malloced. */ 77 1.1 christos struct delegpt* dp; 78 1.1 christos /** does the stub need to forego priming (like on other ports) */ 79 1.1 christos uint8_t noprime; 80 1.1 christos }; 81 1.1 christos 82 1.1 christos /** 83 1.1 christos * Create hints 84 1.1 christos * @return new hints or NULL on error. 85 1.1 christos */ 86 1.1 christos struct iter_hints* hints_create(void); 87 1.1 christos 88 1.1 christos /** 89 1.1 christos * Delete hints. 90 1.1 christos * @param hints: to delete. 91 1.1 christos */ 92 1.1 christos void hints_delete(struct iter_hints* hints); 93 1.1 christos 94 1.1 christos /** 95 1.1 christos * Process hints config. Sets default values for root hints if no config. 96 1.1 christos * @param hints: where to store. 97 1.1 christos * @param cfg: config options. 98 1.1 christos * @return 0 on error. 99 1.1 christos */ 100 1.1 christos int hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg); 101 1.1 christos 102 1.1 christos /** 103 1.1.1.3 christos * Find hints for the given class. 104 1.1.1.3 christos * The return value is contents of the hints structure. 105 1.1.1.3 christos * Caller should lock and unlock a readlock on the hints structure if nolock 106 1.1.1.3 christos * is set. 107 1.1.1.3 christos * Otherwise caller should unlock the readlock on the hints structure if a 108 1.1.1.3 christos * value was returned. 109 1.1 christos * @param hints: hint storage. 110 1.1.1.3 christos * @param qname: the qname that generated the delegation point. 111 1.1 christos * @param qclass: class for which root hints are requested. host order. 112 1.1.1.3 christos * @param nolock: Skip locking, locking is handled by the caller. 113 1.1 christos * @return: NULL if no hints, or a ptr to stored hints. 114 1.1 christos */ 115 1.1.1.3 christos struct delegpt* hints_find(struct iter_hints* hints, uint8_t* qname, 116 1.1.1.3 christos uint16_t qclass, int nolock); 117 1.1.1.3 christos 118 1.1.1.3 christos /** 119 1.1.1.3 christos * Same as hints_lookup, but for the root only. 120 1.1.1.3 christos * @param hints: hint storage. 121 1.1.1.3 christos * @param qclass: class for which root hints are requested. host order. 122 1.1.1.3 christos * @param nolock: Skip locking, locking is handled by the caller. 123 1.1.1.3 christos * @return: NULL if no hints, or a ptr to stored hints. 124 1.1.1.3 christos */ 125 1.1.1.3 christos struct delegpt* hints_find_root(struct iter_hints* hints, 126 1.1.1.3 christos uint16_t qclass, int nolock); 127 1.1 christos 128 1.1 christos /** 129 1.1 christos * Find next root hints (to cycle through all root hints). 130 1.1.1.3 christos * Handles its own locking unless nolock is set. In that case the caller 131 1.1.1.3 christos * should lock and unlock a readlock on the hints structure. 132 1.1 christos * @param hints: hint storage 133 1.1 christos * @param qclass: class for which root hints are sought. 134 1.1 christos * 0 means give the first available root hints class. 135 1.1 christos * x means, give class x or a higher class if any. 136 1.1 christos * returns the found class in this variable. 137 1.1.1.3 christos * @param nolock: Skip locking, locking is handled by the caller. 138 1.1 christos * @return true if a root hint class is found. 139 1.1 christos * false if not root hint class is found (qclass may have been changed). 140 1.1 christos */ 141 1.1.1.3 christos int hints_next_root(struct iter_hints* hints, uint16_t* qclass, int nolock); 142 1.1 christos 143 1.1 christos /** 144 1.1 christos * Given a qname/qclass combination, and the delegation point from the cache 145 1.1 christos * for this qname/qclass, determine if this combination indicates that a 146 1.1 christos * stub hint exists and must be primed. 147 1.1.1.3 christos * The return value is contents of the hints structure. 148 1.1.1.3 christos * Caller should lock and unlock a readlock on the hints structure if nolock 149 1.1.1.3 christos * is set. 150 1.1.1.3 christos * Otherwise caller should unlock the readlock on the hints structure if a 151 1.1.1.3 christos * value was returned. 152 1.1 christos * 153 1.1 christos * @param hints: hint storage. 154 1.1 christos * @param qname: The qname that generated the delegation point. 155 1.1 christos * @param qclass: The qclass that generated the delegation point. 156 1.1 christos * @param dp: The cache generated delegation point. 157 1.1.1.3 christos * @param nolock: Skip locking, locking is handled by the caller. 158 1.1 christos * @return: A priming delegation point if there is a stub hint that must 159 1.1 christos * be primed, otherwise null. 160 1.1 christos */ 161 1.1.1.3 christos struct iter_hints_stub* hints_lookup_stub(struct iter_hints* hints, 162 1.1.1.3 christos uint8_t* qname, uint16_t qclass, struct delegpt* dp, int nolock); 163 1.1 christos 164 1.1 christos /** 165 1.1 christos * Get memory in use by hints 166 1.1.1.3 christos * Locks and unlocks the structure. 167 1.1 christos * @param hints: hint storage. 168 1.1 christos * @return bytes in use 169 1.1 christos */ 170 1.1 christos size_t hints_get_mem(struct iter_hints* hints); 171 1.1 christos 172 1.1 christos /** 173 1.1 christos * Add stub to hints structure. For external use since it recalcs 174 1.1 christos * the tree parents. 175 1.1.1.3 christos * Handles its own locking unless nolock is set. In that case the caller 176 1.1.1.3 christos * should lock and unlock a writelock on the hints structure. 177 1.1 christos * @param hints: the hints data structure 178 1.1 christos * @param c: class of zone 179 1.1 christos * @param dp: delegation point with name and target nameservers for new 180 1.1 christos * hints stub. malloced. 181 1.1 christos * @param noprime: set noprime option to true or false on new hint stub. 182 1.1.1.3 christos * @param nolock: Skip locking, locking is handled by the caller. 183 1.1 christos * @return false on failure (out of memory); 184 1.1 christos */ 185 1.1 christos int hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp, 186 1.1.1.3 christos int noprime, int nolock); 187 1.1 christos 188 1.1 christos /** 189 1.1 christos * Remove stub from hints structure. For external use since it 190 1.1 christos * recalcs the tree parents. 191 1.1.1.3 christos * Handles its own locking unless nolock is set. In that case the caller 192 1.1.1.3 christos * should lock and unlock a writelock on the hints structure. 193 1.1 christos * @param hints: the hints data structure 194 1.1 christos * @param c: class of stub zone 195 1.1 christos * @param nm: name of stub zone (in uncompressed wireformat). 196 1.1.1.3 christos * @param nolock: Skip locking, locking is handled by the caller. 197 1.1.1.3 christos */ 198 1.1.1.3 christos void hints_delete_stub(struct iter_hints* hints, uint16_t c, 199 1.1.1.3 christos uint8_t* nm, int nolock); 200 1.1.1.3 christos 201 1.1.1.3 christos /** 202 1.1.1.3 christos * Swap internal tree with preallocated entries. Caller should manage 203 1.1.1.3 christos * the locks. 204 1.1.1.3 christos * @param hints: the hints data structure. 205 1.1.1.3 christos * @param data: the data structure used to take elements from. This contains 206 1.1.1.3 christos * the old elements on return. 207 1.1 christos */ 208 1.1.1.3 christos void hints_swap_tree(struct iter_hints* hints, struct iter_hints* data); 209 1.1 christos 210 1.1 christos #endif /* ITERATOR_ITER_HINTS_H */ 211