iter_fwd.h revision 1.1
1/*
2 * iterator/iter_fwd.h - iterative resolver module forward zones.
3 *
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 *
6 * This software is open source.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36/**
37 * \file
38 *
39 * This file contains functions to assist the iterator module.
40 * Keep track of forward zones, and read those from config.
41 */
42
43#ifndef ITERATOR_ITER_FWD_H
44#define ITERATOR_ITER_FWD_H
45#include "util/rbtree.h"
46struct config_file;
47struct delegpt;
48struct regional;
49
50/**
51 * Iterator forward zones structure
52 */
53struct iter_forwards {
54	/** regional where forward zone server addresses are allocated */
55	struct regional* region;
56	/**
57	 * Zones are stored in this tree. Sort order is specially chosen.
58	 * first sorted on qclass. Then on dname in nsec-like order, so that
59	 * a lookup on class, name will return an exact match or the closest
60	 * match which gives the ancestor needed.
61	 * contents of type iter_forward_zone.
62	 */
63	rbtree_t* tree;
64};
65
66/**
67 * Iterator forward servers for a particular zone.
68 */
69struct iter_forward_zone {
70	/** redblacktree node, key is this structure: class and name */
71	rbnode_t node;
72	/** name */
73	uint8_t* name;
74	/** length of name */
75	size_t namelen;
76	/** number of labels in name */
77	int namelabs;
78	/** delegation point with forward server information for this zone.
79	 * If NULL then this forward entry is used to indicate that a
80	 * stub-zone with the same name exists, and should be used. */
81	struct delegpt* dp;
82	/** pointer to parent in tree (or NULL if none) */
83	struct iter_forward_zone* parent;
84	/** class. host order. */
85	uint16_t dclass;
86};
87
88/**
89 * Create forwards
90 * @return new forwards or NULL on error.
91 */
92struct iter_forwards* forwards_create(void);
93
94/**
95 * Delete forwards.
96 * @param fwd: to delete.
97 */
98void forwards_delete(struct iter_forwards* fwd);
99
100/**
101 * Process forwards config.
102 * @param fwd: where to store.
103 * @param cfg: config options.
104 * @return 0 on error.
105 */
106int forwards_apply_cfg(struct iter_forwards* fwd, struct config_file* cfg);
107
108/**
109 * Find forward zone information
110 * For this qname/qclass find forward zone information, returns delegation
111 * point with server names and addresses, or NULL if no forwarding is needed.
112 *
113 * @param fwd: forward storage.
114 * @param qname: The qname of the query.
115 * @param qclass: The qclass of the query.
116 * @return: A delegation point if the query has to be forwarded to that list,
117 *         otherwise null.
118 */
119struct delegpt* forwards_lookup(struct iter_forwards* fwd,
120	uint8_t* qname, uint16_t qclass);
121
122/**
123 * Same as forwards_lookup, but for the root only
124 * @param fwd: forward storage.
125 * @param qclass: The qclass of the query.
126 * @return: A delegation point if root forward exists, otherwise null.
127 */
128struct delegpt* forwards_lookup_root(struct iter_forwards* fwd,
129	uint16_t qclass);
130
131/**
132 * Find next root item in forwards lookup tree.
133 * @param fwd: the forward storage
134 * @param qclass: class to look at next, or higher.
135 * @return false if none found, or if true stored in qclass.
136 */
137int forwards_next_root(struct iter_forwards* fwd, uint16_t* qclass);
138
139/**
140 * Get memory in use by forward storage
141 * @param fwd: forward storage.
142 * @return bytes in use
143 */
144size_t forwards_get_mem(struct iter_forwards* fwd);
145
146/** compare two fwd entries */
147int fwd_cmp(const void* k1, const void* k2);
148
149/**
150 * Add zone to forward structure. For external use since it recalcs
151 * the tree parents.
152 * @param fwd: the forward data structure
153 * @param c: class of zone
154 * @param dp: delegation point with name and target nameservers for new
155 *	forward zone. This delegation point and all its data must be
156 *	malloced in the fwd->region. (then it is freed when the fwd is
157 *	deleted).
158 * @return false on failure (out of memory);
159 */
160int forwards_add_zone(struct iter_forwards* fwd, uint16_t c,
161	struct delegpt* dp);
162
163/**
164 * Remove zone from forward structure. For external use since it
165 * recalcs the tree parents. Does not actually release any memory, the region
166 * is unchanged.
167 * @param fwd: the forward data structure
168 * @param c: class of zone
169 * @param nm: name of zone (in uncompressed wireformat).
170 */
171void forwards_delete_zone(struct iter_forwards* fwd, uint16_t c, uint8_t* nm);
172
173#endif /* ITERATOR_ITER_FWD_H */
174