iter_fwd.h revision 238106
1219820Sjeff/* 2219820Sjeff * iterator/iter_fwd.h - iterative resolver module forward zones. 3219820Sjeff * 4219820Sjeff * Copyright (c) 2007, NLnet Labs. All rights reserved. 5219820Sjeff * 6219820Sjeff * This software is open source. 7219820Sjeff * 8219820Sjeff * Redistribution and use in source and binary forms, with or without 9219820Sjeff * modification, are permitted provided that the following conditions 10219820Sjeff * are met: 11219820Sjeff * 12219820Sjeff * Redistributions of source code must retain the above copyright notice, 13219820Sjeff * this list of conditions and the following disclaimer. 14219820Sjeff * 15219820Sjeff * Redistributions in binary form must reproduce the above copyright notice, 16219820Sjeff * this list of conditions and the following disclaimer in the documentation 17219820Sjeff * and/or other materials provided with the distribution. 18219820Sjeff * 19219820Sjeff * Neither the name of the NLNET LABS nor the names of its contributors may 20219820Sjeff * be used to endorse or promote products derived from this software without 21219820Sjeff * specific prior written permission. 22219820Sjeff * 23219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24219820Sjeff * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25219820Sjeff * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26219820Sjeff * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27219820Sjeff * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28219820Sjeff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29219820Sjeff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30219820Sjeff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31219820Sjeff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32219820Sjeff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33219820Sjeff * POSSIBILITY OF SUCH DAMAGE. 34219820Sjeff */ 35219820Sjeff 36219820Sjeff/** 37219820Sjeff * \file 38219820Sjeff * 39219820Sjeff * This file contains functions to assist the iterator module. 40219820Sjeff * Keep track of forward zones, and read those from config. 41219820Sjeff */ 42219820Sjeff 43219820Sjeff#ifndef ITERATOR_ITER_FWD_H 44219820Sjeff#define ITERATOR_ITER_FWD_H 45219820Sjeff#include "util/rbtree.h" 46219820Sjeffstruct config_file; 47219820Sjeffstruct delegpt; 48219820Sjeff 49219820Sjeff/** 50219820Sjeff * Iterator forward zones structure 51219820Sjeff */ 52219820Sjeffstruct iter_forwards { 53219820Sjeff /** 54219820Sjeff * Zones are stored in this tree. Sort order is specially chosen. 55219820Sjeff * first sorted on qclass. Then on dname in nsec-like order, so that 56219820Sjeff * a lookup on class, name will return an exact match or the closest 57219820Sjeff * match which gives the ancestor needed. 58219820Sjeff * contents of type iter_forward_zone. 59219820Sjeff */ 60219820Sjeff rbtree_t* tree; 61219820Sjeff}; 62219820Sjeff 63219820Sjeff/** 64219820Sjeff * Iterator forward servers for a particular zone. 65219820Sjeff */ 66219820Sjeffstruct iter_forward_zone { 67219820Sjeff /** redblacktree node, key is this structure: class and name */ 68219820Sjeff rbnode_t node; 69219820Sjeff /** name */ 70219820Sjeff uint8_t* name; 71219820Sjeff /** length of name */ 72219820Sjeff size_t namelen; 73219820Sjeff /** number of labels in name */ 74219820Sjeff int namelabs; 75219820Sjeff /** delegation point with forward server information for this zone. 76219820Sjeff * If NULL then this forward entry is used to indicate that a 77219820Sjeff * stub-zone with the same name exists, and should be used. 78219820Sjeff * This delegation point is malloced. 79219820Sjeff */ 80219820Sjeff struct delegpt* dp; 81219820Sjeff /** pointer to parent in tree (or NULL if none) */ 82219820Sjeff struct iter_forward_zone* parent; 83219820Sjeff /** class. host order. */ 84219820Sjeff uint16_t dclass; 85219820Sjeff}; 86219820Sjeff 87219820Sjeff/** 88219820Sjeff * Create forwards 89219820Sjeff * @return new forwards or NULL on error. 90219820Sjeff */ 91219820Sjeffstruct iter_forwards* forwards_create(void); 92219820Sjeff 93219820Sjeff/** 94219820Sjeff * Delete forwards. 95219820Sjeff * @param fwd: to delete. 96219820Sjeff */ 97219820Sjeffvoid forwards_delete(struct iter_forwards* fwd); 98219820Sjeff 99219820Sjeff/** 100219820Sjeff * Process forwards config. 101219820Sjeff * @param fwd: where to store. 102219820Sjeff * @param cfg: config options. 103219820Sjeff * @return 0 on error. 104219820Sjeff */ 105219820Sjeffint forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg); 106219820Sjeff 107219820Sjeff/** 108219820Sjeff * Find forward zone information 109219820Sjeff * For this qname/qclass find forward zone information, returns delegation 110219820Sjeff * point with server names and addresses, or NULL if no forwarding is needed. 111219820Sjeff * 112219820Sjeff * @param fwd: forward storage. 113219820Sjeff * @param qname: The qname of the query. 114219820Sjeff * @param qclass: The qclass of the query. 115219820Sjeff * @return: A delegation point if the query has to be forwarded to that list, 116219820Sjeff * otherwise null. 117219820Sjeff */ 118219820Sjeffstruct delegpt* forwards_lookup(struct iter_forwards* fwd, 119219820Sjeff uint8_t* qname, uint16_t qclass); 120219820Sjeff 121219820Sjeff/** 122219820Sjeff * Same as forwards_lookup, but for the root only 123219820Sjeff * @param fwd: forward storage. 124219820Sjeff * @param qclass: The qclass of the query. 125219820Sjeff * @return: A delegation point if root forward exists, otherwise null. 126219820Sjeff */ 127219820Sjeffstruct delegpt* forwards_lookup_root(struct iter_forwards* fwd, 128219820Sjeff uint16_t qclass); 129219820Sjeff 130219820Sjeff/** 131219820Sjeff * Find next root item in forwards lookup tree. 132219820Sjeff * @param fwd: the forward storage 133219820Sjeff * @param qclass: class to look at next, or higher. 134219820Sjeff * @return false if none found, or if true stored in qclass. 135219820Sjeff */ 136219820Sjeffint forwards_next_root(struct iter_forwards* fwd, uint16_t* qclass); 137219820Sjeff 138219820Sjeff/** 139219820Sjeff * Get memory in use by forward storage 140219820Sjeff * @param fwd: forward storage. 141219820Sjeff * @return bytes in use 142219820Sjeff */ 143219820Sjeffsize_t forwards_get_mem(struct iter_forwards* fwd); 144219820Sjeff 145219820Sjeff/** compare two fwd entries */ 146219820Sjeffint fwd_cmp(const void* k1, const void* k2); 147219820Sjeff 148219820Sjeff/** 149219820Sjeff * Add zone to forward structure. For external use since it recalcs 150219820Sjeff * the tree parents. 151219820Sjeff * @param fwd: the forward data structure 152219820Sjeff * @param c: class of zone 153219820Sjeff * @param dp: delegation point with name and target nameservers for new 154219820Sjeff * forward zone. malloced. 155219820Sjeff * @return false on failure (out of memory); 156219820Sjeff */ 157219820Sjeffint forwards_add_zone(struct iter_forwards* fwd, uint16_t c, 158219820Sjeff struct delegpt* dp); 159219820Sjeff 160219820Sjeff/** 161219820Sjeff * Remove zone from forward structure. For external use since it 162219820Sjeff * recalcs the tree parents. 163219820Sjeff * @param fwd: the forward data structure 164219820Sjeff * @param c: class of zone 165219820Sjeff * @param nm: name of zone (in uncompressed wireformat). 166219820Sjeff */ 167219820Sjeffvoid forwards_delete_zone(struct iter_forwards* fwd, uint16_t c, uint8_t* nm); 168219820Sjeff 169219820Sjeff/** 170219820Sjeff * Add stub hole (empty entry in forward table, that makes resolution skip 171219820Sjeff * a forward-zone because the stub zone should override the forward zone). 172219820Sjeff * Does not add one if not necessary. 173219820Sjeff * @param fwd: the forward data structure 174219820Sjeff * @param c: class of zone 175219820Sjeff * @param nm: name of zone (in uncompressed wireformat). 176219820Sjeff * @return false on failure (out of memory); 177219820Sjeff */ 178219820Sjeffint forwards_add_stub_hole(struct iter_forwards* fwd, uint16_t c, uint8_t* nm); 179219820Sjeff 180219820Sjeff/** 181219820Sjeff * Remove stub hole, if one exists. 182219820Sjeff * @param fwd: the forward data structure 183219820Sjeff * @param c: class of zone 184219820Sjeff * @param nm: name of zone (in uncompressed wireformat). 185219820Sjeff */ 186219820Sjeffvoid forwards_delete_stub_hole(struct iter_forwards* fwd, uint16_t c, 187219820Sjeff uint8_t* nm); 188219820Sjeff 189219820Sjeff#endif /* ITERATOR_ITER_FWD_H */ 190219820Sjeff