summaryrefslogtreecommitdiff
path: root/src/libknot/zone/dname-table.h
blob: 945b6de1d381d0454f3184b064ce8f1a9b1af80e (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
/*!
 * \file dname-table.h
 *
 * \author Jan Kadlec <jan.kadlec.@nic.cz>
 *
 * \brief Structures representing dname table and functions for
 *        manipulating these structures.
 *
 * \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_DNAME_TABLE_H_
#define _KNOT_DNAME_TABLE_H_

#include <config.h>

#include "common/tree.h"

#include "dname.h"
#include "common.h"


/*!
 * \brief Structure encapsulating
 */
struct dname_table_node {
	knot_dname_t *dname; /*!< Dname stored in node. */
	TREE_ENTRY(dname_table_node) avl; /*!< Tree variables. */
};

/*!
 * \brief Tree structure.
 */
typedef TREE_HEAD(avl, dname_table_node) table_tree_t;

/*!
 * \brief Structure holding tree together with dname ID counter.
 */
struct knot_dname_table {
	unsigned int id_counter; /*!< ID counter (starts from 1) */
	table_tree_t *tree;  /*!< AVL tree */
};

typedef struct knot_dname_table knot_dname_table_t;

/*!
 * \brief Creates new empty domain name table.
 *
 * \retval Created table on success.
 * \retval NULL on memory error.
 */
knot_dname_table_t *knot_dname_table_new();

/*!
 * \brief Finds name in the domain name table.
 *
 * \note Reference count to dname will be incremented, caller is responsible
 *       for releasing it.
 *
 * \param table Domain name table to be searched.
 * \param dname Dname to be searched.
 *
 * \retval Pointer to found dname when dname is present in the table.
 * \retval NULL when dname is not present.
 */
knot_dname_t *knot_dname_table_find_dname(const knot_dname_table_t *table,
					      knot_dname_t *dname);

/*!
 * \brief Adds domain name to domain name table.
 *
 * \param table Domain name table to be added to.
 * \param dname Domain name to be added.
 *
 * \warning Function does not check for duplicates!
 *
 * \note This function encapsulates dname in a structure and saves it to a tree.
 *
 * \retval KNOT_EOK on success.
 * \retval KNOT_ENOMEM when memory runs out.
 */
int knot_dname_table_add_dname(knot_dname_table_t *table,
                               knot_dname_t *dname);

/*!
 * \brief Adds domain name to domain name table and checks for duplicates.
 *
 * \param table Domain name table to be added to.
 * \param dname Domain name to be added.
 *
 * \note This function encapsulates dname in a structure and saves it to a tree.
 * \note If a duplicate is found, \a dname is replaced by the name from table.
 *
 * \retval KNOT_EOK on success.
 * \retval KNOT_ENOMEM when memory runs out.
 */
int knot_dname_table_add_dname_check(knot_dname_table_t *table,
                                     knot_dname_t **dname);

/*!
 * \brief Creates a shallow copy of the domain name table.
 *
 * Expects an existing knot_dname_table_t structure to be passed via \a to,
 * and fills it with the same data (domain names) as the original. Actual
 * tree nodes are created, but domain names are not copied (just referenced).
 *
 * \param from Original domain name table.
 * \param to Copy of the domain name table.
 */
int knot_dname_table_shallow_copy(knot_dname_table_t *from,
                                    knot_dname_table_t *to);

/*!
 * \brief Frees dname table without its nodes. Sets pointer to NULL.
 *
 * \param table Table to be freed.
 */
void knot_dname_table_free(knot_dname_table_t **table);

/*!
 * \brief Frees dname table and all its nodes (and release dnames in the nodes)
 *        Sets pointer to NULL.
 *
 * \param table Table to be freed.
 */
void knot_dname_table_deep_free(knot_dname_table_t **table);

/*!
 * \brief Frees dname table and all its nodes (including dnames in the nodes)
 *        Sets pointer to NULL.
 *
 * \param table Table to be freed.
 */
void knot_dname_table_destroy(knot_dname_table_t **table);

/*!
 * \brief Encapsulation of domain name table tree traversal function.
 *
 * \param table Table containing tree to be traversed.
 * \param applied_function Function to be used to process nodes.
 * \param data Data to be passed to processing function.
 */
void knot_dname_table_tree_inorder_apply(const knot_dname_table_t *table,
            void (*applied_function)(knot_dname_t *dname,
                                     void *data),
            void *data);


/*!
 * \brief Dumps dname table to stderr.
 *
 * \param table Table to be dumped.
 */
void knot_dname_table_dump(const knot_dname_table_t *table);


#endif // _KNOT_DNAME_TABLE_H_

/*! @} */