summaryrefslogtreecommitdiff
path: root/src/libknot/zone/zone-contents.h
blob: ab33d5fa06f59b0800d97e317150c802468d9a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*!
 * \file zone-contents.h
 *
 * \author Lubos Slovak <lubos.slovak@nic.cz>
 *
 * \brief Zone contents structure and API for manipulating it.
 *
 * \addtogroup libknot
 * @{
 */
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _KNOT_ZONE_CONTENTS_H_
#define _KNOT_ZONE_CONTENTS_H_

#include "zone/node.h"
#include "dname.h"
#include "nsec3.h"

#include "zone-tree.h"

struct knot_zone;

/*----------------------------------------------------------------------------*/

typedef struct knot_zone_contents_t {
	knot_node_t *apex;       /*!< Apex node of the zone (holding SOA) */

	knot_zone_tree_t *nodes;
	knot_zone_tree_t *nsec3_nodes;

	struct knot_zone *zone;

	knot_nsec3_params_t nsec3_params;

	/*!
	 * \todo Unify the use of this field - authoritative nodes vs. all.
	 */
	size_t node_count;

	/*! \brief Various flags
	 *
	 * Two rightmost bits denote zone contents generation.
	 *
	 * Possible values:
	 * - 00 - Original version of the zone. Old nodes should be used.
	 * - 01 - New (updated) zone. New nodes should be used.
	 * - 10 - New (updated) zone, but exactly the stored nodes should be
	 *        used, no matter their generation.
	 *
	 * The third bit denotes whether ANY queries are enabled or disabled:
	 * - 1xx - ANY queries disabled
	 * - 0xx - ANY queries enabled
	 */
	uint8_t flags;
} knot_zone_contents_t;

/*!< \brief Helper linked list list for CNAME loop checking */
typedef struct cname_chain {
	const knot_node_t *node;
	struct cname_chain *next;
} cname_chain_t;

/*----------------------------------------------------------------------------*/

knot_zone_contents_t *knot_zone_contents_new(knot_node_t *apex,
                                             struct knot_zone *zone);

int knot_zone_contents_gen_is_old(const knot_zone_contents_t *contents);
int knot_zone_contents_gen_is_new(const knot_zone_contents_t *contents);
int knot_zone_contents_gen_is_finished(const knot_zone_contents_t *contents);


void knot_zone_contents_set_gen_old(knot_zone_contents_t *contents);
void knot_zone_contents_set_gen_new(knot_zone_contents_t *contents);
void knot_zone_contents_set_gen_new_finished(knot_zone_contents_t *contents);

int knot_zone_contents_any_disabled(const knot_zone_contents_t *contents);
void knot_zone_contents_disable_any(knot_zone_contents_t *contents);
void knot_zone_contents_enable_any(knot_zone_contents_t *contents);

uint16_t knot_zone_contents_class(const knot_zone_contents_t *contents);

/*!
 * \brief Adds a node to the given zone.
 *
 * Checks if the node belongs to the zone, i.e. if its owner is a subdomain of
 * the zone's apex. It thus also forbids adding node with the same name as the
 * zone apex.
 *
 * \warning This function may destroy domain names saved in the node, that
 *          are already present in the zone.
 *
 * \param zone Zone to add the node into.
 * \param node Node to add into the zone.
 *
 * \retval KNOT_EOK
 * \retval KNOT_EINVAL
 * \retval KNOT_EOUTOFZONE
 * \retval KNOT_EHASH
 */
int knot_zone_contents_add_node(knot_zone_contents_t *contents,
                                  knot_node_t *node, int create_parents,
                                  uint8_t flags);

/*!
 * \brief Create new node in the zone contents for given RRSet.
 *
 * \param contents Zone to add the node into.
 * \param rr Given RRSet.
 * \param node Returns created node.
 * \return
 */
int knot_zone_contents_create_node(knot_zone_contents_t *contents,
                                   const knot_rrset_t *rr,
                                   knot_node_t **node);

/*!
 * \brief Adds a RRSet to the given zone.
 *
 * Checks if the RRSet belongs to the zone, i.e. if its owner is a subdomain of
 * the zone's apex. The RRSet is inserted only if the node is given, or if
 * a node where the RRSet should belong is found in the zone.
 *
 * \warning The function does not check if the node is already inserted in the
 *          zone, just assumes that it is.
 * \warning This function may destroy domain names saved in the RRSet, that
 *          are already present in the zone.
 *
 * \param zone Zone to add the node into.
 * \param rrset RRSet to add into the zone.
 * \param node Node the RRSet should be inserted into. (Should be a node of the
 *             given zone.) If set to NULL, the function will find proper node
 *             and set it to this parameter.
 *
 * \retval KNOT_EOK
 * \retval KNOT_EINVAL
 * \retval KNOT_EOUTOFZONE
 */
int knot_zone_contents_add_rrset(knot_zone_contents_t *contents,
                          knot_rrset_t *rrset,
                          knot_node_t **node,
                          knot_rrset_dupl_handling_t dupl);

int knot_zone_contents_add_rrsigs(knot_zone_contents_t *contents,
                           knot_rrset_t *rrsigs,
                           knot_rrset_t **rrset, knot_node_t **node,
                           knot_rrset_dupl_handling_t dupl);

/*!
 * \brief Adds a node holding NSEC3 records to the given zone.
 *
 * Checks if the node belongs to the zone, i.e. if its owner is a subdomain of
 * the zone's apex. It does not check if the node really contains any NSEC3
 * records, nor if the name is a hash (as there is actually no way of
 * determining this).
 *
 * \param zone Zone to add the node into.
 * \param node Node to add into the zone.
 *
 * \retval KNOT_EOK
 * \retval KNOT_EINVAL
 * \retval KNOT_EOUTOFZONE
 */
int knot_zone_contents_add_nsec3_node(knot_zone_contents_t *contents,
                                        knot_node_t *node, int create_parents,
                                        uint8_t flags);

int knot_zone_contents_add_nsec3_rrset(knot_zone_contents_t *contents,
                                         knot_rrset_t *rrset,
                                         knot_node_t **node,
                                         knot_rrset_dupl_handling_t dupl);

int knot_zone_contents_remove_node(knot_zone_contents_t *contents,
	const knot_node_t *node, knot_node_t **removed_tree);

int knot_zone_contents_remove_nsec3_node(knot_zone_contents_t *contents,
	const knot_node_t *node, knot_node_t **removed);

/*!
 * \brief Tries to find a node with the specified name in the zone.
 *
 * \param zone Zone where the name should be searched for.
 * \param name Name to find.
 *
 * \return Corresponding node if found, NULL otherwise.
 */
knot_node_t *knot_zone_contents_get_node(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

/*!
 * \brief Tries to find a node with the specified name among the NSEC3 nodes
 *        of the zone.
 *
 * \param zone Zone where the name should be searched for.
 * \param name Name to find.
 *
 * \return Corresponding node if found, NULL otherwise.
 */
knot_node_t *knot_zone_contents_get_nsec3_node(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

/*!
 * \brief Tries to find a node with the specified name in the zone.
 *
 * \note This function is identical to knot_zone_contents_get_node(), only it returns
 *       constant reference.
 *
 * \param zone Zone where the name should be searched for.
 * \param name Name to find.
 *
 * \return Corresponding node if found, NULL otherwise.
 */
const knot_node_t *knot_zone_contents_find_node(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

/*!
 * \brief Tries to find domain name in the given zone using AVL tree.
 *
 * \param[in] zone Zone to search for the name.
 * \param[in] name Domain name to search for.
 * \param[out] node The found node (if it was found, otherwise it may contain
 *                  arbitrary node).
 * \param[out] closest_encloser Closest encloser of the given name in the zone.
 * \param[out] previous Previous domain name in canonical order.
 *
 * \retval KNOT_ZONE_NAME_FOUND if node with owner \a name was found.
 * \retval KNOT_ZONE_NAME_NOT_FOUND if it was not found.
 * \retval KNOT_EINVAL
 * \retval KNOT_EOUTOFZONE
 */
int knot_zone_contents_find_dname(const knot_zone_contents_t *contents,
                           const knot_dname_t *name,
                           const knot_node_t **node,
                           const knot_node_t **closest_encloser,
                           const knot_node_t **previous);

/*!
 * \brief Finds previous name in canonical order to the given name in the zone.
 *
 * \param zone Zone to search for the name.
 * \param name Domain name to find the previous domain name of.
 *
 * \return Previous node in canonical order, or NULL if some parameter is wrong.
 */
const knot_node_t *knot_zone_contents_find_previous(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

knot_node_t *knot_zone_contents_get_previous(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

const knot_node_t *knot_zone_contents_find_previous_nsec3(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

knot_node_t *knot_zone_contents_get_previous_nsec3(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

/*!
 * \brief Tries to find a node with the specified name among the NSEC3 nodes
 *        of the zone.
 *
 * \note This function is identical to knot_zone_contents_get_nsec3_node(), only it
 *       returns constant reference.
 *
 * \param zone Zone where the name should be searched for.
 * \param name Name to find.
 *
 * \return Corresponding node if found, NULL otherwise.
 */
const knot_node_t *knot_zone_contents_find_nsec3_node(
	const knot_zone_contents_t *contents, const knot_dname_t *name);

/*!
 * \brief Finds NSEC3 node and previous NSEC3 node in canonical order,
 *        corresponding to the given domain name.
 *
 * This functions creates a NSEC3 hash of \a name and tries to find NSEC3 node
 * with the hashed domain name as owner.
 *
 * \param[in] zone Zone to search in.
 * \param[in] name Domain name to get the corresponding NSEC3 nodes for.
 * \param[out] nsec3_node NSEC3 node corresponding to \a name (if found,
 *                        otherwise this may be an arbitrary NSEC3 node).
 * \param[out] nsec3_previous The NSEC3 node immediately preceding hashed domain
 *                            name corresponding to \a name in canonical order.
 *
 * \retval KNOT_ZONE_NAME_FOUND if the corresponding NSEC3 node was found.
 * \retval KNOT_ZONE_NAME_NOT_FOUND if it was not found.
 * \retval KNOT_EINVAL
 * \retval KNOT_ENSEC3PAR
 * \retval KNOT_ECRYPTO
 * \retval KNOT_ERROR
 */
int knot_zone_contents_find_nsec3_for_name(
                                    const knot_zone_contents_t *contents,
                                    const knot_dname_t *name,
                                    const knot_node_t **nsec3_node,
                                    const knot_node_t **nsec3_previous);
/*!
 * \brief Returns the apex node of the zone.
 *
 * \param zone Zone to get the apex of.
 *
 * \return Zone apex node.
 */
const knot_node_t *knot_zone_contents_apex(
	const knot_zone_contents_t *contents);

knot_node_t *knot_zone_contents_get_apex(
	const knot_zone_contents_t *contents);

/*!
 * \brief Optimizes zone by replacing domain names in RDATA with references to
 *        domain names present in zone (as node owners).
 * \param first_nsec3_node First node in NSEC3 tree - needed in sem. checks.
 *        Will not be saved if set to NULL.
 * \param last_nsec3_node Last node in NSEC3 tree - needed in sem. checks.
 *        Will not be saved if set to NULL.
 * \param zone Zone to adjust domain names in.
 */
int knot_zone_contents_adjust(knot_zone_contents_t *contents,
                              knot_node_t **first_nsec3_node,
                              knot_node_t **last_nsec3_node, int dupl_check);

int knot_zone_contents_check_loops(knot_zone_contents_t *zone);

/*!
 * \brief Parses the NSEC3PARAM record stored in the zone.
 *
 * This function properly fills in the nsec3_params field of the zone structure
 * according to data stored in the NSEC3PARAM record. This is necessary to do
 * before any NSEC3 operations on the zone are requested, otherwise they will
 * fail (error KNOT_ENSEC3PAR).
 *
 * \note If there is no NSEC3PARAM record in the zone, this function clears
 *       the nsec3_params field of the zone structure (fills it with zeros).
 *
 * \param zone Zone to get the NSEC3PARAM record from.
 */
int knot_zone_contents_load_nsec3param(knot_zone_contents_t *contents);

/*!
 * \brief Checks if the zone uses NSEC3.
 *
 * This function will return 0 if the NSEC3PARAM record was not parse prior to
 * calling it.
 *
 * \param zone Zone to check.
 *
 * \retval <> 0 if the zone uses NSEC3.
 * \retval 0 if it does not.
 *
 * \see knot_zone_contents_load_nsec3param()
 */
int knot_zone_contents_nsec3_enabled(const knot_zone_contents_t *contents);

/*!
 * \brief Returns the parsed NSEC3PARAM record of the zone.
 *
 * \note You must parse the NSEC3PARAM record prior to calling this function
 *       (knot_zone_contents_load_nsec3param()).
 *
 * \param zone Zone to get the NSEC3PARAM record from.
 *
 * \return Parsed NSEC3PARAM from the zone or NULL if the zone does not use
 *         NSEC3 or the record was not parsed before.
 *
 * \see knot_zone_contents_load_nsec3param()
 */
const knot_nsec3_params_t *knot_zone_contents_nsec3params(
	const knot_zone_contents_t *contents);

/*!
 * \brief Applies the given function to each regular node in the zone.
 *
 * This function uses in-order depth-first forward traversal, i.e. the function
 * is first recursively applied to left subtree, then to the root and then to
 * the right subtree.
 *
 * \note This implies that the zone is stored in a binary tree. Is there a way
 *       to make this traversal independent on the underlying structure?
 *
 * \param zone Nodes of this zone will be used as parameters for the function.
 * \param function Function to be applied to each node of the zone.
 * \param data Arbitrary data to be passed to the function.
 */
int knot_zone_contents_tree_apply_inorder(knot_zone_contents_t *contents,
			      void (*function)(knot_node_t *node, void *data),
                              void *data);

/*!
 * \brief Applies the given function to each regular node in the zone.
 *
 * This function uses in-order depth-first reverse traversal, i.e. the function
 * is first recursively applied to right subtree, then to the root and then to
 * the left subtree.
 *
 * \note This implies that the zone is stored in a binary tree. Is there a way
 *       to make this traversal independent on the underlying structure?
 *
 * \param zone Nodes of this zone will be used as parameters for the function.
 * \param function Function to be applied to each node of the zone.
 * \param data Arbitrary data to be passed to the function.
 */
int knot_zone_contents_tree_apply_inorder_reverse(
	knot_zone_contents_t *contents,
	void (*function)(knot_node_t *node, void *data), void *data);

/*!
 * \brief Applies the given function to each NSEC3 node in the zone.
 *
 * This function uses in-order depth-first forward traversal, i.e. the function
 * is first recursively applied to left subtree, then to the root and then to
 * the right subtree.
 *
 * \note This implies that the zone is stored in a binary tree. Is there a way
 *       to make this traversal independent on the underlying structure?
 *
 * \param zone NSEC3 nodes of this zone will be used as parameters for the
 *             function.
 * \param function Function to be applied to each node of the zone.
 * \param data Arbitrary data to be passed to the function.
 */
int knot_zone_contents_nsec3_apply_inorder(knot_zone_contents_t *contents,
			      void (*function)(knot_node_t *node, void *data),
                              void *data);

/*!
 * \brief Applies the given function to each NSEC3 node in the zone.
 *
 * This function uses in-order depth-first reverse traversal, i.e. the function
 * is first recursively applied to right subtree, then to the root and then to
 * the left subtree.
 *
 * \note This implies that the zone is stored in a binary tree. Is there a way
 *       to make this traversal independent on the underlying structure?
 *
 * \param zone NSEC3 nodes of this zone will be used as parameters for the
 *             function.
 * \param function Function to be applied to each node of the zone.
 * \param data Arbitrary data to be passed to the function.
 */
int knot_zone_contents_nsec3_apply_inorder_reverse(
	knot_zone_contents_t *contents,
	void (*function)(knot_node_t *node, void *data), void *data);

knot_zone_tree_t *knot_zone_contents_get_nodes(
		knot_zone_contents_t *contents);

knot_zone_tree_t *knot_zone_contents_get_nsec3_nodes(
		knot_zone_contents_t *contents);

int knot_zone_contents_dname_table_apply(knot_zone_contents_t *contents,
                                           void (*function)(knot_dname_t *,
                                                            void *),
                                           void *data);

/*!
 * \brief Creates a shallow copy of the zone (no stored data are copied).
 *
 * This function creates a new zone structure in \a to, creates new trees for
 * regular nodes and for NSEC3 nodes, creates new hash table and a new domain
 * table. It also fills these structures with the exact same data as the
 * original zone is - no copying of stored data is done, just pointers are
 * copied.
 *
 * \param from Original zone.
 * \param to Copy of the zone.
 *
 * \retval KNOT_EOK
 * \retval KNOT_EINVAL
 * \retval KNOT_ENOMEM
 */
int knot_zone_contents_shallow_copy(const knot_zone_contents_t *from,
                                    knot_zone_contents_t **to);

int knot_zone_contents_shallow_copy2(const knot_zone_contents_t *from,
                                     knot_zone_contents_t **to);

void knot_zone_contents_free(knot_zone_contents_t **contents);

void knot_zone_contents_deep_free(knot_zone_contents_t **contents);

int knot_zone_contents_integrity_check(const knot_zone_contents_t *contents);

const knot_dname_t *knot_zone_contents_find_dname_in_rdata(
	const knot_zone_contents_t *zone,
	const knot_dname_t *dname);

/*!
 * \brief Creates a NSEC3 hashed name for the given domain name.
 *
 * \note The zone's NSEC3PARAM record must be parsed prior to calling this
 *       function (see knot_zone_load_nsec3param()).
 *
 * \param zone Zone from which to take the NSEC3 parameters.
 * \param name Domain name to hash.
 * \param nsec3_name Hashed name.
 *
 * \retval KNOT_EOK
 * \retval KNOT_ENSEC3PAR
 * \retval KNOT_ECRYPTO
 * \retval KNOT_ERROR if an error occured while creating a new domain name
 *                      from the hash or concatenating it with the zone name.
 */
int knot_zone_contents_nsec3_name(const knot_zone_contents_t *zone,
                                           const knot_dname_t *name,
                                           knot_dname_t **nsec3_name);

void knot_zone_contents_insert_dname_into_table(knot_dname_t **in_dname,
                                                hattrie_t *lookup_tree);

/*!
 * \brief Fetch zone serial.
 *
 * \param zone Zone.
 *
 * \return serial or 0
 */
unsigned knot_zone_serial(const knot_zone_contents_t *zone);

#endif

/*! @} */