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
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE 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/**
108269257Sdes * Find forward zone exactly by name
109269257Sdes * @param fwd: forward storage.
110269257Sdes * @param qname: The qname of the query.
111269257Sdes * @param qclass: The qclass of the query.
112269257Sdes * @return: A delegation point or null.
113269257Sdes */
114269257Sdesstruct delegpt* forwards_find(struct iter_forwards* fwd, uint8_t* qname,
115269257Sdes	uint16_t qclass);
116269257Sdes
117269257Sdes/**
118238106Sdes * Find forward zone information
119238106Sdes * For this qname/qclass find forward zone information, returns delegation
120238106Sdes * point with server names and addresses, or NULL if no forwarding is needed.
121238106Sdes *
122238106Sdes * @param fwd: forward storage.
123238106Sdes * @param qname: The qname of the query.
124238106Sdes * @param qclass: The qclass of the query.
125238106Sdes * @return: A delegation point if the query has to be forwarded to that list,
126238106Sdes *         otherwise null.
127238106Sdes */
128238106Sdesstruct delegpt* forwards_lookup(struct iter_forwards* fwd,
129238106Sdes	uint8_t* qname, uint16_t qclass);
130238106Sdes
131238106Sdes/**
132238106Sdes * Same as forwards_lookup, but for the root only
133238106Sdes * @param fwd: forward storage.
134238106Sdes * @param qclass: The qclass of the query.
135238106Sdes * @return: A delegation point if root forward exists, otherwise null.
136238106Sdes */
137238106Sdesstruct delegpt* forwards_lookup_root(struct iter_forwards* fwd,
138238106Sdes	uint16_t qclass);
139238106Sdes
140238106Sdes/**
141238106Sdes * Find next root item in forwards lookup tree.
142238106Sdes * @param fwd: the forward storage
143238106Sdes * @param qclass: class to look at next, or higher.
144238106Sdes * @return false if none found, or if true stored in qclass.
145238106Sdes */
146238106Sdesint forwards_next_root(struct iter_forwards* fwd, uint16_t* qclass);
147238106Sdes
148238106Sdes/**
149238106Sdes * Get memory in use by forward storage
150238106Sdes * @param fwd: forward storage.
151238106Sdes * @return bytes in use
152238106Sdes */
153238106Sdessize_t forwards_get_mem(struct iter_forwards* fwd);
154238106Sdes
155238106Sdes/** compare two fwd entries */
156238106Sdesint fwd_cmp(const void* k1, const void* k2);
157238106Sdes
158238106Sdes/**
159238106Sdes * Add zone to forward structure. For external use since it recalcs
160238106Sdes * the tree parents.
161238106Sdes * @param fwd: the forward data structure
162238106Sdes * @param c: class of zone
163238106Sdes * @param dp: delegation point with name and target nameservers for new
164238106Sdes *	forward zone. malloced.
165238106Sdes * @return false on failure (out of memory);
166238106Sdes */
167238106Sdesint forwards_add_zone(struct iter_forwards* fwd, uint16_t c,
168238106Sdes	struct delegpt* dp);
169238106Sdes
170238106Sdes/**
171238106Sdes * Remove zone from forward structure. For external use since it
172238106Sdes * recalcs the tree parents.
173238106Sdes * @param fwd: the forward data structure
174238106Sdes * @param c: class of zone
175238106Sdes * @param nm: name of zone (in uncompressed wireformat).
176238106Sdes */
177238106Sdesvoid forwards_delete_zone(struct iter_forwards* fwd, uint16_t c, uint8_t* nm);
178238106Sdes
179238106Sdes/**
180238106Sdes * Add stub hole (empty entry in forward table, that makes resolution skip
181238106Sdes * a forward-zone because the stub zone should override the forward zone).
182238106Sdes * Does not add one if not necessary.
183238106Sdes * @param fwd: the forward data structure
184238106Sdes * @param c: class of zone
185238106Sdes * @param nm: name of zone (in uncompressed wireformat).
186238106Sdes * @return false on failure (out of memory);
187238106Sdes */
188238106Sdesint forwards_add_stub_hole(struct iter_forwards* fwd, uint16_t c, uint8_t* nm);
189238106Sdes
190238106Sdes/**
191238106Sdes * Remove stub hole, if one exists.
192238106Sdes * @param fwd: the forward data structure
193238106Sdes * @param c: class of zone
194238106Sdes * @param nm: name of zone (in uncompressed wireformat).
195238106Sdes */
196238106Sdesvoid forwards_delete_stub_hole(struct iter_forwards* fwd, uint16_t c,
197238106Sdes	uint8_t* nm);
198238106Sdes
199238106Sdes#endif /* ITERATOR_ITER_FWD_H */
200