1238106Sdes/* 2238106Sdes * iterator/iter_fwd.h - iterative resolver module forward zones. 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33238106Sdes * POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * This file contains functions to assist the iterator module. 40238106Sdes * Keep track of forward zones, and read those from config. 41238106Sdes */ 42238106Sdes 43238106Sdes#ifndef ITERATOR_ITER_FWD_H 44238106Sdes#define ITERATOR_ITER_FWD_H 45238106Sdes#include "util/rbtree.h" 46238106Sdesstruct config_file; 47238106Sdesstruct delegpt; 48238106Sdes 49238106Sdes/** 50238106Sdes * Iterator forward zones structure 51238106Sdes */ 52238106Sdesstruct iter_forwards { 53238106Sdes /** 54238106Sdes * Zones are stored in this tree. Sort order is specially chosen. 55238106Sdes * first sorted on qclass. Then on dname in nsec-like order, so that 56238106Sdes * a lookup on class, name will return an exact match or the closest 57238106Sdes * match which gives the ancestor needed. 58238106Sdes * contents of type iter_forward_zone. 59238106Sdes */ 60238106Sdes rbtree_t* tree; 61238106Sdes}; 62238106Sdes 63238106Sdes/** 64238106Sdes * Iterator forward servers for a particular zone. 65238106Sdes */ 66238106Sdesstruct iter_forward_zone { 67238106Sdes /** redblacktree node, key is this structure: class and name */ 68238106Sdes rbnode_t node; 69238106Sdes /** name */ 70238106Sdes uint8_t* name; 71238106Sdes /** length of name */ 72238106Sdes size_t namelen; 73238106Sdes /** number of labels in name */ 74238106Sdes int namelabs; 75238106Sdes /** delegation point with forward server information for this zone. 76238106Sdes * If NULL then this forward entry is used to indicate that a 77238106Sdes * stub-zone with the same name exists, and should be used. 78238106Sdes * This delegation point is malloced. 79238106Sdes */ 80238106Sdes struct delegpt* dp; 81238106Sdes /** pointer to parent in tree (or NULL if none) */ 82238106Sdes struct iter_forward_zone* parent; 83238106Sdes /** class. host order. */ 84238106Sdes uint16_t dclass; 85238106Sdes}; 86238106Sdes 87238106Sdes/** 88238106Sdes * Create forwards 89238106Sdes * @return new forwards or NULL on error. 90238106Sdes */ 91238106Sdesstruct iter_forwards* forwards_create(void); 92238106Sdes 93238106Sdes/** 94238106Sdes * Delete forwards. 95238106Sdes * @param fwd: to delete. 96238106Sdes */ 97238106Sdesvoid forwards_delete(struct iter_forwards* fwd); 98238106Sdes 99238106Sdes/** 100238106Sdes * Process forwards config. 101238106Sdes * @param fwd: where to store. 102238106Sdes * @param cfg: config options. 103238106Sdes * @return 0 on error. 104238106Sdes */ 105238106Sdesint forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg); 106238106Sdes 107238106Sdes/** 108238106Sdes * Find forward zone information 109238106Sdes * For this qname/qclass find forward zone information, returns delegation 110238106Sdes * point with server names and addresses, or NULL if no forwarding is needed. 111238106Sdes * 112238106Sdes * @param fwd: forward storage. 113238106Sdes * @param qname: The qname of the query. 114238106Sdes * @param qclass: The qclass of the query. 115238106Sdes * @return: A delegation point if the query has to be forwarded to that list, 116238106Sdes * otherwise null. 117238106Sdes */ 118238106Sdesstruct delegpt* forwards_lookup(struct iter_forwards* fwd, 119238106Sdes uint8_t* qname, uint16_t qclass); 120238106Sdes 121238106Sdes/** 122238106Sdes * Same as forwards_lookup, but for the root only 123238106Sdes * @param fwd: forward storage. 124238106Sdes * @param qclass: The qclass of the query. 125238106Sdes * @return: A delegation point if root forward exists, otherwise null. 126238106Sdes */ 127238106Sdesstruct delegpt* forwards_lookup_root(struct iter_forwards* fwd, 128238106Sdes uint16_t qclass); 129238106Sdes 130238106Sdes/** 131238106Sdes * Find next root item in forwards lookup tree. 132238106Sdes * @param fwd: the forward storage 133238106Sdes * @param qclass: class to look at next, or higher. 134238106Sdes * @return false if none found, or if true stored in qclass. 135238106Sdes */ 136238106Sdesint forwards_next_root(struct iter_forwards* fwd, uint16_t* qclass); 137238106Sdes 138238106Sdes/** 139238106Sdes * Get memory in use by forward storage 140238106Sdes * @param fwd: forward storage. 141238106Sdes * @return bytes in use 142238106Sdes */ 143238106Sdessize_t forwards_get_mem(struct iter_forwards* fwd); 144238106Sdes 145238106Sdes/** compare two fwd entries */ 146238106Sdesint fwd_cmp(const void* k1, const void* k2); 147238106Sdes 148238106Sdes/** 149238106Sdes * Add zone to forward structure. For external use since it recalcs 150238106Sdes * the tree parents. 151238106Sdes * @param fwd: the forward data structure 152238106Sdes * @param c: class of zone 153238106Sdes * @param dp: delegation point with name and target nameservers for new 154238106Sdes * forward zone. malloced. 155238106Sdes * @return false on failure (out of memory); 156238106Sdes */ 157238106Sdesint forwards_add_zone(struct iter_forwards* fwd, uint16_t c, 158238106Sdes struct delegpt* dp); 159238106Sdes 160238106Sdes/** 161238106Sdes * Remove zone from forward structure. For external use since it 162238106Sdes * recalcs the tree parents. 163238106Sdes * @param fwd: the forward data structure 164238106Sdes * @param c: class of zone 165238106Sdes * @param nm: name of zone (in uncompressed wireformat). 166238106Sdes */ 167238106Sdesvoid forwards_delete_zone(struct iter_forwards* fwd, uint16_t c, uint8_t* nm); 168238106Sdes 169238106Sdes/** 170238106Sdes * Add stub hole (empty entry in forward table, that makes resolution skip 171238106Sdes * a forward-zone because the stub zone should override the forward zone). 172238106Sdes * Does not add one if not necessary. 173238106Sdes * @param fwd: the forward data structure 174238106Sdes * @param c: class of zone 175238106Sdes * @param nm: name of zone (in uncompressed wireformat). 176238106Sdes * @return false on failure (out of memory); 177238106Sdes */ 178238106Sdesint forwards_add_stub_hole(struct iter_forwards* fwd, uint16_t c, uint8_t* nm); 179238106Sdes 180238106Sdes/** 181238106Sdes * Remove stub hole, if one exists. 182238106Sdes * @param fwd: the forward data structure 183238106Sdes * @param c: class of zone 184238106Sdes * @param nm: name of zone (in uncompressed wireformat). 185238106Sdes */ 186238106Sdesvoid forwards_delete_stub_hole(struct iter_forwards* fwd, uint16_t c, 187238106Sdes uint8_t* nm); 188238106Sdes 189238106Sdes#endif /* ITERATOR_ITER_FWD_H */ 190