zone-update.h 10.1 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

    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/>.
 */

#pragma once

19
#include "knot/updates/apply.h"
20
#include "knot/conf/conf.h"
21 22
#include "knot/updates/changesets.h"
#include "knot/zone/contents.h"
23
#include "knot/zone/zone.h"
24
#include "libknot/mm_ctx.h"
25

26
/*! \brief Structure for zone contents updating / querying. */
27
typedef struct zone_update {
28 29
	zone_t *zone;                /*!< Zone being updated. */
	zone_contents_t *new_cont;   /*!< New zone contents for full updates. */
30
	bool new_cont_deep_copy;     /*!< On update_clear, perform deep free instead of shallow. */
31
	changeset_t change;          /*!< Changes we want to apply. */
32
	apply_ctx_t *a_ctx;          /*!< Context for applying changesets. */
33
	uint32_t flags;              /*!< Zone update flags. */
34
	knot_mm_t mm;                /*!< Memory context used for intermediate nodes. */
35 36
} zone_update_t;

37 38
typedef struct {
	zone_update_t *update;          /*!< The update we're iterating over. */
39
	trie_it_t *tree_it;             /*!< Iterator for the new zone. */
40
	const zone_node_t *cur_node;    /*!< Current node in the new zone. */
41 42 43
	bool nsec3;                     /*!< Set when we're using the NSEC3 node tree. */
} zone_update_iter_t;

44
typedef enum {
45 46 47 48
	UPDATE_FULL           = 1 << 0, /*!< Replace the old zone by a complete new one. */
	UPDATE_INCREMENTAL    = 1 << 1, /*!< Apply changes to the old zone. */
	UPDATE_SIGN           = 1 << 2, /*!< Sign the resulting zone. */
	UPDATE_DIFF           = 1 << 3, /*!< In the case of full update, create a diff for journal. */
49
	UPDATE_STRICT         = 1 << 4, /*!< Apply changes strictly, i.e. fail when removing nonexistent RR. */
50 51
} zone_update_flags_t;

52 53 54 55 56
/*!
 * \brief Inits given zone update structure, new memory context is created.
 *
 * \param update  Zone update structure to init.
 * \param zone    Init with this zone.
57
 * \param flags   Flags to control the behavior of the update.
58 59
 *
 * \return KNOT_E*
60
 */
61
int zone_update_init(zone_update_t *update, zone_t *zone, zone_update_flags_t flags);
62

63 64 65 66 67 68 69 70 71
/*!
 * \brief Inits update structure, the update is built like IXFR from differences.
 *
 * The existing zone with its own contents is taken as a base,
 * the new candidate zone contents are taken as new contents,
 * the diff is calculated, so that this update is INCREMENTAL.
 *
 * \param update   Zone update structure to init.
 * \param zone     Init with this zone.
72
 * \param old_cont The current zone contents the diff will be against. Probably zone->contents.
73 74 75 76 77
 * \param new_cont New zone contents. Will be taken over (and later freed) by zone update.
 * \param flags    Flags for update. Must be UPDATE_INCREMENTAL.
 *
 * \return KNOT_E*
 */
78
int zone_update_from_differences(zone_update_t *update, zone_t *zone, zone_contents_t *old_cont,
79
                                 zone_contents_t *new_cont, zone_update_flags_t flags, bool ignore_dnssec);
80 81 82 83 84 85 86 87 88 89 90 91 92 93

/*!
 * \brief Inits a zone update based on new zone contents.
 *
 * \param update                 Zone update structure to init.
 * \param zone_without_contents  Init with this zone. Its contents may be NULL.
 * \param new_cont               New zone contents. Will be taken over (and later freed) by zone update.
 * \param flags                  Flags for update.
 *
 * \return KNOT_E*
 */
int zone_update_from_contents(zone_update_t *update, zone_t *zone_without_contents,
                              zone_contents_t *new_cont, zone_update_flags_t flags);

94 95 96
/*!
 * \brief Returns node that would be in the zone after updating it.
 *
97
 * \note Returned node is either zone original or synthesized, do *not* free
98
 *       or modify. Returned node is allocated on local mempool.
99 100 101 102
 *
 * \param update  Zone update.
 * \param dname   Dname to search for.
 *
103
 * \return   Node after zone update.
104 105 106
 */
const zone_node_t *zone_update_get_node(zone_update_t *update,
                                        const knot_dname_t *dname);
107 108 109 110 111 112 113 114 115 116 117

/*!
 * \brief Returns updated zone apex.
 *
 * \note Returned node is either zone original or synthesized, do *not* free
 *       or modify.
 *
 * \param update  Zone update.
 *
 * \return   Returns apex after update.
 */
118
const zone_node_t *zone_update_get_apex(zone_update_t *update);
119 120 121 122 123 124 125 126

/*!
 * \brief Returns the serial from the current apex.
 *
 * \param update  Zone update.
 *
 * \return   0 if no apex was found, its serial otherwise.
 */
127 128
uint32_t zone_update_current_serial(zone_update_t *update);

129 130 131 132 133 134 135
/*!
 * \brief Returns the SOA rdataset we're updating from.
 *
 * \param update  Zone update.
 *
 * \return   The original SOA rdataset.
 */
136
const knot_rdataset_t *zone_update_from(zone_update_t *update);
137 138 139 140 141 142 143 144

/*!
 * \brief Returns the SOA rdataset we're updating to.
 *
 * \param update  Zone update.
 *
 * \return   NULL if no new SOA has been added, new SOA otherwise.
 */
145
const knot_rdataset_t *zone_update_to(zone_update_t *update);
146

147 148 149 150 151
/*!
 * \brief Clear data allocated by given zone update structure.
 *
 * \param  update Zone update to clear.
 */
152 153
void zone_update_clear(zone_update_t *update);

154 155 156
/*!
 * \brief Adds an RRSet to the zone.
 *
157 158 159 160
 * \warning Do not edit the zone_update when any iterator is active. Any
 *          zone_update modifications will invalidate the trie iterators
 *          in the zone_update iterator(s).
 *
161
 * \param update  Zone update.
162
 * \param rrset   RRSet to add.
163 164 165
 *
 * \return KNOT_E*
 */
166
int zone_update_add(zone_update_t *update, const knot_rrset_t *rrset);
167 168 169 170

/*!
 * \brief Removes an RRSet from the zone.
 *
171 172 173 174
 * \warning Do not edit the zone_update when any iterator is active. Any
 *          zone_update modifications will invalidate the trie iterators
 *          in the zone_update iterator(s).
 *
175
 * \param update  Zone update.
176
 * \param rrset   RRSet to remove.
177 178 179
 *
 * \return KNOT_E*
 */
180
int zone_update_remove(zone_update_t *update, const knot_rrset_t *rrset);
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
/*!
 * \brief Removes a whole RRSet of specified type from the zone.
 *
 * \warning Do not edit the zone_update when any iterator is active. Any
 *          zone_update modifications will invalidate the trie iterators
 *          in the zone_update iterator(s).
 *
 * \param update  Zone update.
 * \param owner   Node name to remove.
 * \param type    RRSet type to remove.
 *
 * \return KNOT_E*
 */
int zone_update_remove_rrset(zone_update_t *update, knot_dname_t *owner, uint16_t type);

/*!
 * \brief Removes a whole node from the zone.
 *
 * \warning Do not edit the zone_update when any iterator is active. Any
 *          zone_update modifications will invalidate the trie iterators
 *          in the zone_update iterator(s).
 *
 * \param update  Zone update.
 * \param owner   Node name to remove.
 *
 * \return KNOT_E*
 */
int zone_update_remove_node(zone_update_t *update, const knot_dname_t *owner);

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
/*!
 * \brief Adds and removes RRsets to/from the zone according to the changeset.
 *
 * \param update  Zone update.
 * \param changes Changes to be made in zone.
 *
 * \return KNOT_E*
 */
int zone_update_apply_changeset(zone_update_t *update, const changeset_t *changes);

/*!
 * \brief Applies a changeset to zone, the changeset is modified to contain only really added/removed rdata.
 *
 * \param update  Zone update.
 * \param changes In: changes to be made in zone; out: changes really made in zone.
 *
 * \return KNOT_E*
 */
int zone_update_apply_changeset_fix(zone_update_t *update, changeset_t *changes);

231 232 233 234 235 236 237 238 239 240
/*!
 * \brief Applies the changeset in reverse, rsets from REM section are added and from ADD section removed.
 *
 * \param update   Zone update.
 * \param changes  Changes to be un-done.
 *
 * \return KNOT_E*
 */
int zone_update_apply_changeset_reverse(zone_update_t *update, const changeset_t *changes);

241 242 243 244 245 246 247 248 249 250
/*!
 * \brief Increment SOA serial (according to cofigured policy) in the update.
 *
 * \param update  Update to be modified.
 * \param conf    Configuration.
 *
 * \return KNOT_E*
 */
int zone_update_increment_soa(zone_update_t *update, conf_t *conf);

251 252 253
/*!
 * \brief Commits all changes to the zone, signs it, saves changes to journal.
 *
254
 * \param conf          Configuration.
255
 * \param update        Zone update.
256 257 258
 *
 * \return KNOT_E*
 */
259
int zone_update_commit(conf_t *conf, zone_update_t *update);
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
/*!
 * \brief Setup a zone_update iterator for both FULL and INCREMENTAL updates.
 *
 * \warning Do not init or use iterators when the zone is edited. Any
 *          zone_update modifications will invalidate the trie iterators
 *          in the zone_update iterator.
 *
 * \param it       Iterator.
 * \param update   Zone update.
 *
 * \return KNOT_E*
 */
int zone_update_iter(zone_update_iter_t *it, zone_update_t *update);

/*!
 * \brief Setup a zone_update iterator for both FULL and INCREMENTAL updates.
 *        Version for iterating over nsec3 nodes.
 *
 * \warning Do not init or use iterators when the zone is edited. Any
 *          zone_update modifications will invalidate the trie iterators
 *          in the zone_update iterator.
 *
 *
 * \param it       Iterator.
 * \param update   Zone update.
 *
 * \return KNOT_E*
 */
int zone_update_iter_nsec3(zone_update_iter_t *it, zone_update_t *update);

/*!
 * \brief Move the iterator to the next item.
 *
 * \param it  Iterator.
 *
 * \return KNOT_E*
 */
int zone_update_iter_next(zone_update_iter_t *it);

/*!
 * \brief Get the value of the iterator.
 *
 * \param it  Iterator.
 *
 * \return A (synthesized or added) node with all its current data.
 */
const zone_node_t *zone_update_iter_val(zone_update_iter_t *it);

/*!
 * \brief Finish the iterator and clean it up.
 *
 * \param it  Iterator.
 */
314
void zone_update_iter_finish(zone_update_iter_t *it);
315

316 317 318 319 320
/*!
 * \brief Returns bool whether there are any changes at all.
 *
 * \param update  Zone update.
 */
321
bool zone_update_no_change(zone_update_t *update);