zone-update.c 18.7 KB
Newer Older
1
/*  Copyright (C) 2017 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

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

17
#include "knot/common/log.h"
18
#include "knot/dnssec/zone-events.h"
Daniel Salzman's avatar
Daniel Salzman committed
19
#include "knot/updates/zone-update.h"
20
#include "knot/zone/serial.h"
21
#include "knot/zone/zone-diff.h"
22
#include "contrib/mempattern.h"
23
#include "contrib/ucw/lists.h"
24
#include "contrib/ucw/mempool.h"
25

26 27
#include <urcu.h>

28 29
static int init_incremental(zone_update_t *update, zone_t *zone)
{
30 31 32
	if (zone->contents == NULL) {
		return KNOT_EINVAL;
	}
33

34 35 36 37
	int ret = changeset_init(&update->change, zone->name);
	if (ret != KNOT_EOK) {
		return ret;
	}
38

39 40
	update->new_cont_deep_copy = false;

41 42 43 44 45 46
	ret = apply_prepare_zone_copy(zone->contents, &update->new_cont);
	if (ret != KNOT_EOK) {
		changeset_clear(&update->change);
		return ret;
	}

47 48
	uint32_t apply_flags = update->flags & UPDATE_STRICT ? APPLY_STRICT : 0;
	apply_init_ctx(&update->a_ctx, update->new_cont, apply_flags);
49

50
	/* Copy base SOA RR. */
51
	update->change.soa_from =
52
		node_create_rrset(zone->contents->apex, KNOT_RRTYPE_SOA);
53
	if (update->change.soa_from == NULL) {
54
		zone_contents_free(&update->new_cont);
55
		changeset_clear(&update->change);
56 57
		return KNOT_ENOMEM;
	}
58 59 60 61 62 63 64 65 66 67 68

	return KNOT_EOK;
}

static int init_full(zone_update_t *update, zone_t *zone)
{
	update->new_cont = zone_contents_new(zone->name);
	if (update->new_cont == NULL) {
		return KNOT_ENOMEM;
	}

69 70
	update->new_cont_deep_copy = true;

71 72
	apply_init_ctx(&update->a_ctx, update->new_cont, 0);

73 74 75
	return KNOT_EOK;
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static int replace_soa(zone_contents_t *contents, const knot_rrset_t *rr)
{
	/* SOA possible only within apex. */
	if (!knot_dname_is_equal(rr->owner, contents->apex->owner)) {
		return KNOT_EDENIED;
	}

	knot_rrset_t old_soa = node_rrset(contents->apex, KNOT_RRTYPE_SOA);
	zone_node_t *n = contents->apex;
	int ret = zone_contents_remove_rr(contents, &old_soa, &n);
	if (ret != KNOT_EOK && ret != KNOT_EINVAL) {
		return ret;
	}

	return zone_contents_add_rr(contents, rr, &n);
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/* ------------------------------- API -------------------------------------- */

int zone_update_init(zone_update_t *update, zone_t *zone, zone_update_flags_t flags)
{
	if (update == NULL || zone == NULL) {
		return KNOT_EINVAL;
	}

	memset(update, 0, sizeof(*update));
	update->zone = zone;

	mm_ctx_mempool(&update->mm, MM_DEFAULT_BLKSIZE);
	update->flags = flags;

	if (flags & UPDATE_INCREMENTAL) {
		return init_incremental(update, zone);
	} else if (flags & UPDATE_FULL) {
		return init_full(update, zone);
	} else {
		return KNOT_EINVAL;
	}
}

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
int zone_update_from_differences(zone_update_t *update, zone_t *zone,
                                 zone_contents_t *new_cont, zone_update_flags_t flags)
{
	if (update == NULL || zone == NULL || new_cont == NULL ||
	    !(flags & UPDATE_INCREMENTAL) || (flags & UPDATE_FULL)) {
		return KNOT_EINVAL;
	}

	memset(update, 0, sizeof(*update));
	update->zone = zone;

	mm_ctx_mempool(&update->mm, MM_DEFAULT_BLKSIZE);
	update->flags = flags;

	update->new_cont = new_cont;
	update->new_cont_deep_copy = true;

	int ret = changeset_init(&update->change, zone->name);
	if (ret != KNOT_EOK) {
		return ret;
	}

	ret = zone_contents_diff(zone->contents, new_cont, &update->change);
	if (ret != KNOT_EOK) {
		changeset_clear(&update->change);
		return ret;
	}

	uint32_t apply_flags = update->flags & UPDATE_STRICT ? APPLY_STRICT : 0;
	apply_init_ctx(&update->a_ctx, update->new_cont, apply_flags);

	return KNOT_EOK;
}

int zone_update_from_contents(zone_update_t *update, zone_t *zone_without_contents,
                              zone_contents_t *new_cont, zone_update_flags_t flags)
{
	if (update == NULL || zone_without_contents == NULL || new_cont == NULL) {
		return KNOT_EINVAL;
	}

	memset(update, 0, sizeof(*update));
	update->zone = zone_without_contents;

	mm_ctx_mempool(&update->mm, MM_DEFAULT_BLKSIZE);
	update->flags = flags;

	update->new_cont = new_cont;
	update->new_cont_deep_copy = true;

	if (flags & UPDATE_INCREMENTAL) {
		int ret = changeset_init(&update->change, zone_without_contents->name);
		if (ret != KNOT_EOK) {
			return ret;
		}

		update->change.soa_from = node_create_rrset(new_cont->apex, KNOT_RRTYPE_SOA);
		if (update->change.soa_from == NULL) {
			changeset_clear(&update->change);
			return KNOT_ENOMEM;
		}
	}

	uint32_t apply_flags = update->flags & UPDATE_STRICT ? APPLY_STRICT : 0;
	apply_init_ctx(&update->a_ctx, update->new_cont, apply_flags);

	return KNOT_EOK;
}

185 186 187 188 189 190
const zone_node_t *zone_update_get_node(zone_update_t *update, const knot_dname_t *dname)
{
	if (update == NULL || dname == NULL) {
		return NULL;
	}

191
	return zone_contents_find_node(update->new_cont, dname);
192 193
}

194 195
const zone_node_t *zone_update_get_apex(zone_update_t *update)
{
196 197 198 199
	if (update == NULL) {
		return NULL;
	}

200 201 202 203 204 205
	return zone_update_get_node(update, update->zone->name);
}

uint32_t zone_update_current_serial(zone_update_t *update)
{
	const zone_node_t *apex = zone_update_get_apex(update);
206
	if (apex != NULL) {
207 208 209 210 211 212 213 214
		return knot_soa_serial(node_rdataset(apex, KNOT_RRTYPE_SOA));
	} else {
		return 0;
	}
}

const knot_rdataset_t *zone_update_from(zone_update_t *update)
{
215 216 217 218
	if (update == NULL) {
		return NULL;
	}

219 220 221 222 223 224
	if (update->flags & UPDATE_INCREMENTAL) {
		const zone_node_t *apex = update->zone->contents->apex;
		return node_rdataset(apex, KNOT_RRTYPE_SOA);
	}

	return NULL;
225 226 227 228
}

const knot_rdataset_t *zone_update_to(zone_update_t *update)
{
229 230 231
	if (update == NULL) {
		return NULL;
	}
232

233 234 235 236 237 238 239 240
	if (update->flags & UPDATE_FULL) {
		const zone_node_t *apex = update->new_cont->apex;
		return node_rdataset(apex, KNOT_RRTYPE_SOA);
	} else if (update->flags & UPDATE_INCREMENTAL) {
		if (update->change.soa_to == NULL) {
			return NULL;
		}
		return &update->change.soa_to->rrs;
241 242
	}

243
	return NULL;
244 245
}

246 247
void zone_update_clear(zone_update_t *update)
{
248 249
	if (update == NULL) {
		return;
250
	}
251 252 253 254

	if (update->flags & UPDATE_INCREMENTAL) {
		/* Revert any changes on error, do nothing on success. */
		update_rollback(&update->a_ctx);
255 256 257 258 259
		if (update->new_cont_deep_copy) {
			zone_contents_deep_free(&update->new_cont);
		} else {
			update_free_zone(&update->new_cont);
		}
260 261
		changeset_clear(&update->change);
	} else if (update->flags & UPDATE_FULL) {
262
		assert(update->new_cont_deep_copy);
263 264 265 266
		zone_contents_deep_free(&update->new_cont);
	}
	mp_delete(update->mm.ctx);
	memset(update, 0, sizeof(*update));
267
}
268

269 270
int zone_update_add(zone_update_t *update, const knot_rrset_t *rrset)
{
271
	if (update == NULL || rrset == NULL) {
272 273 274
		return KNOT_EINVAL;
	}

275
	if (update->flags & UPDATE_INCREMENTAL) {
276 277 278 279 280 281 282
		int ret = changeset_add_addition(&update->change, rrset, CHANGESET_CHECK);
		if (ret != KNOT_EOK) {
			return ret;
		}

		if (rrset->type == KNOT_RRTYPE_SOA) {
			/* replace previous SOA */
283
			ret = apply_replace_soa(&update->a_ctx, &update->change);
284 285 286 287 288 289
			if (ret != KNOT_EOK) {
				changeset_remove_addition(&update->change, rrset);
			}
			return ret;
		}

290
		ret = apply_add_rr(&update->a_ctx, rrset);
291 292 293 294 295 296
		if (ret != KNOT_EOK) {
			changeset_remove_addition(&update->change, rrset);
			return ret;
		}

		return KNOT_EOK;
297
	} else if (update->flags & UPDATE_FULL) {
298 299 300 301 302
		if (rrset->type == KNOT_RRTYPE_SOA) {
			/* replace previous SOA */
			return replace_soa(update->new_cont, rrset);
		}

303 304 305 306 307 308 309 310 311
		zone_node_t *n = NULL;
		return zone_contents_add_rr(update->new_cont, rrset, &n);
	} else {
		return KNOT_EINVAL;
	}
}

int zone_update_remove(zone_update_t *update, const knot_rrset_t *rrset)
{
312
	if (update == NULL || rrset == NULL) {
313 314 315
		return KNOT_EINVAL;
	}

316
	if (update->flags & UPDATE_INCREMENTAL) {
317 318 319 320 321 322 323 324 325 326
		int ret = changeset_add_removal(&update->change, rrset, CHANGESET_CHECK);
		if (ret != KNOT_EOK) {
			return ret;
		}

		if (rrset->type == KNOT_RRTYPE_SOA) {
			/* SOA is replaced with addition */
			return KNOT_EOK;
		}

327
		ret = apply_remove_rr(&update->a_ctx, rrset);
328 329 330 331 332 333
		if (ret != KNOT_EOK) {
			changeset_remove_removal(&update->change, rrset);
			return ret;
		}

		return KNOT_EOK;
334 335
	} else if (update->flags & UPDATE_FULL) {
		zone_node_t *n = NULL;
336 337 338 339
		knot_rrset_t *rrs_copy = knot_rrset_copy(rrset, &update->mm);
		int ret = zone_contents_remove_rr(update->new_cont, rrs_copy, &n);
		knot_rrset_free(&rrs_copy, &update->mm);
		return ret;
340
	} else {
341
		return KNOT_EINVAL;
342 343 344
	}
}

345 346 347 348 349 350 351 352
int zone_update_remove_rrset(zone_update_t *update, knot_dname_t *owner, uint16_t type)
{
	if (update == NULL || owner == NULL) {
		return KNOT_EINVAL;
	}

	if (update->flags & UPDATE_INCREMENTAL) {
		/* Remove the RRSet from the original node */
353
		const zone_node_t *node = zone_contents_find_node(update->new_cont, owner);
354 355
		if (node != NULL) {
			knot_rrset_t rrset = node_rrset(node, type);
356 357 358
			if (rrset.owner == NULL) {
				return KNOT_ENOENT;
			}
Daniel Salzman's avatar
Daniel Salzman committed
359 360
			int ret = changeset_add_removal(&update->change, &rrset,
			                                CHANGESET_CHECK);
361 362 363 364
			if (ret != KNOT_EOK) {
				return ret;
			}

365 366 367 368 369
			if (type == KNOT_RRTYPE_SOA) {
				/* SOA is replaced with addition */
				return KNOT_EOK;
			}

370
			ret = apply_remove_rr(&update->a_ctx, &rrset);
371 372 373
			if (ret != KNOT_EOK) {
				return ret;
			}
374
		} else {
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
			return KNOT_ENONODE;
		}
	} else if (update->flags & UPDATE_FULL) {
		/* Remove the RRSet from the non-synthesized new node */
		const zone_node_t *node = zone_contents_find_node(update->new_cont, owner);
		if (node == NULL) {
			return KNOT_ENONODE;
		}

		knot_rrset_t rrset = node_rrset(node, type);
		int ret = zone_update_remove(update, &rrset);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

int zone_update_remove_node(zone_update_t *update, const knot_dname_t *owner)
{
	if (update == NULL || owner == NULL) {
		return KNOT_EINVAL;
	}

	if (update->flags & UPDATE_INCREMENTAL) {
401 402
		/* Remove all RRSets from the new node */
		const zone_node_t *node = zone_contents_find_node(update->new_cont, owner);
403 404 405 406
		if (node != NULL) {
			size_t rrset_count = node->rrset_count;
			for (int i = 0; i < rrset_count; ++i) {
				knot_rrset_t rrset = node_rrset_at(node, rrset_count - 1 - i);
Daniel Salzman's avatar
Daniel Salzman committed
407 408
				int ret = changeset_add_removal(&update->change, &rrset,
				                                CHANGESET_CHECK);
409 410 411 412
				if (ret != KNOT_EOK) {
					return ret;
				}

413 414
				if (rrset.type == KNOT_RRTYPE_SOA) {
					/* SOA is replaced with addition */
415
					continue;
416 417
				}

418
				ret = apply_remove_rr(&update->a_ctx, &rrset);
419 420 421 422
				if (ret != KNOT_EOK) {
					return ret;
				}
			}
423
		} else {
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
			return KNOT_ENONODE;
		}
	} else if (update->flags & UPDATE_FULL) {
		/* Remove all RRSets from the non-synthesized new node */
		const zone_node_t *node = zone_contents_find_node(update->new_cont, owner);
		if (node == NULL) {
			return KNOT_ENONODE;
		}

		size_t rrset_count = node->rrset_count;
		for (int i = 0; i < rrset_count; ++i) {
			knot_rrset_t rrset = node_rrset_at(node, rrset_count - 1 - i);
			int ret = zone_update_remove(update, &rrset);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
	}

	return KNOT_EOK;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
int zone_update_apply_changeset(zone_update_t *update, const changeset_t *changes)
{
	int ret = KNOT_EOK;
	if (update->flags & UPDATE_INCREMENTAL) {
		ret = changeset_merge(&update->change, changes);
	}
	if (ret == KNOT_EOK) {
		ret = apply_changeset_directly(&update->a_ctx, changes);
	}
	return ret;
}

int zone_update_apply_changeset_fix(zone_update_t *update, changeset_t *changes)
{
	int ret = changeset_cancelout(changes);
	if (ret == KNOT_EOK) {
		ret = changeset_preapply_fix(update->new_cont, changes);
	}
464 465
	if (ret == KNOT_EOK) {
		ret = zone_update_apply_changeset(update, changes);
466
	}
467
	return ret;
468 469
}

470
static int set_new_soa(zone_update_t *update, unsigned serial_policy)
471
{
472 473
	assert(update);

Daniel Salzman's avatar
Daniel Salzman committed
474 475
	knot_rrset_t *soa_cpy = node_create_rrset(zone_update_get_apex(update),
	                                          KNOT_RRTYPE_SOA);
476 477 478 479
	if (soa_cpy == NULL) {
		return KNOT_ENOMEM;
	}

480 481 482 483 484
	int ret = zone_update_remove(update, soa_cpy);
	if (ret != KNOT_EOK) {
		knot_rrset_free(&soa_cpy, NULL);
	}

485
	uint32_t old_serial = knot_soa_serial(&soa_cpy->rrs);
486
	uint32_t new_serial = serial_next(old_serial, serial_policy);
487 488 489
	if (serial_compare(old_serial, new_serial) >= 0) {
		log_zone_warning(update->zone->name, "updated serial is lower "
		                 "than current, serial %u -> %u",
490
		                 old_serial, new_serial);
491 492 493 494
	}

	knot_soa_serial_set(&soa_cpy->rrs, new_serial);

495 496 497 498 499 500 501 502 503 504 505 506 507 508
	ret = zone_update_add(update, soa_cpy);
	knot_rrset_free(&soa_cpy, NULL);

	return ret;
}

int zone_update_increment_soa(zone_update_t *update, conf_t *conf)
{
	if (update == NULL || conf == NULL) {
		return KNOT_EINVAL;
	}

	conf_val_t val = conf_zone_get(conf, C_SERIAL_POLICY, update->zone->name);
	return set_new_soa(update, conf_opt(&val));
509 510
}

Daniel Salzman's avatar
Daniel Salzman committed
511 512
static int commit_incremental(conf_t *conf, zone_update_t *update,
                              zone_contents_t **contents_out)
513
{
514
	assert(update);
515
	assert(contents_out);
516

517 518
	if (changeset_empty(&update->change)) {
		changeset_clear(&update->change);
519 520 521
		if (update->zone->contents == NULL) {
			*contents_out = update->new_cont;
		}
522 523 524
		return KNOT_EOK;
	}

525
	zone_contents_t *new_contents = update->new_cont;
526
	int ret = KNOT_EOK;
527
	if (zone_update_to(update) == NULL) {
528
		/* No SOA in the update, create one according to the current policy */
529
		ret = zone_update_increment_soa(update, conf);
530
		if (ret != KNOT_EOK) {
531
			zone_update_clear(update);
532 533
			return ret;
		}
534
	}
535

536 537 538 539 540
	ret = apply_finalize(&update->a_ctx);
	if (ret != KNOT_EOK) {
		zone_update_clear(update);
		return ret;
	}
541

542 543 544
	/* Write changes to journal if all went well. */
	if (update->zone->journal_db != NULL) { // TODO this should be better
		ret = zone_change_store(conf, update->zone, &update->change);
545
		if (ret != KNOT_EOK) {
546
			/* Recoverable error. */
547 548
			return ret;
		}
549
	}
550

551 552
	*contents_out = new_contents;

553 554 555
	return KNOT_EOK;
}

556 557
static int commit_full(conf_t *conf, zone_update_t *update, zone_contents_t **contents_out)
{
558 559 560 561 562 563 564 565 566
	assert(update);
	assert(contents_out);

	/* Check if we have SOA. We might consider adding full semantic check here.
	 * But if we wanted full sem-check I'd consider being it controlled by a flag
	 * - to enable/disable it on demand. */
	if (!node_rrtype_exists(update->new_cont->apex, KNOT_RRTYPE_SOA)) {
		return KNOT_ESEMCHECK;
	}
567

568 569 570 571 572
	int ret = zone_contents_adjust_full(update->new_cont);
	if (ret != KNOT_EOK) {
		zone_update_clear(update);
		return ret;
	}
573

574 575 576
	/* Store new zone contents in journal. */
	if (update->zone->journal_db != NULL) { // TODO this should be better
		ret = zone_in_journal_store(conf, update->zone, update->new_cont);
577
		if (ret != KNOT_EOK && ret != KNOT_ENOTSUP) {
578 579 580 581 582
			return ret;
		}
	}

	*contents_out = update->new_cont;
583

584 585 586
	return KNOT_EOK;
}

587
int zone_update_commit(conf_t *conf, zone_update_t *update)
588
{
589
	if (conf == NULL || update == NULL) {
590 591 592 593 594
		return KNOT_EINVAL;
	}

	int ret = KNOT_EOK;
	zone_contents_t *new_contents = NULL;
595
	if (update->flags & UPDATE_INCREMENTAL) {
596 597
		ret = commit_incremental(conf, update, &new_contents);
	} else {
598
		ret = commit_full(conf, update, &new_contents);
599 600 601
	}
	if (ret != KNOT_EOK) {
		return ret;
602 603
	}

604 605 606 607 608 609
	/* If there is anything to change. */
	if (new_contents == NULL) {
		return KNOT_EOK;
	}

	/* Check the zone size. */
610
	conf_val_t val = conf_zone_get(conf, C_MAX_ZONE_SIZE, update->zone->name);
611
	size_t size_limit = conf_int(&val);
612

613 614
	if (new_contents->size > size_limit) {
		/* Recoverable error. */
615 616 617
		return KNOT_EZONESIZE;
	}

618
	/* Switch zone contents. */
619 620
	zone_contents_t *old_contents;
	old_contents = zone_switch_contents(update->zone, new_contents);
621 622 623 624

	/* Sync RCU. */
	synchronize_rcu();
	if (update->flags & UPDATE_FULL) {
625
		assert(update->new_cont_deep_copy);
626 627
		zone_contents_deep_free(&old_contents);
	} else if (update->flags & UPDATE_INCREMENTAL) {
628 629 630 631 632
		if (update->new_cont_deep_copy) {
			zone_contents_deep_free(&old_contents);
		} else {
			update_free_zone(&old_contents);
		}
633
		changeset_clear(&update->change);
634
	}
635 636
	update_cleanup(&update->a_ctx);
	update->new_cont = NULL;
637 638

	return KNOT_EOK;
639 640
}

641 642 643 644
static int iter_init_tree_iters(zone_update_iter_t *it, zone_update_t *update,
                                bool nsec3)
{
	/* Set zone iterator. */
645
	zone_contents_t *_contents = update->new_cont;
646 647

	/* Begin iteration. We can safely assume _contents is a valid pointer. */
648
	zone_tree_t *tree = nsec3 ? _contents->nsec3_nodes : _contents->nodes;
649
	it->tree_it = trie_it_begin(tree);
650
	if (it->tree_it == NULL) {
651 652 653
		return KNOT_ENOMEM;
	}

654
	it->cur_node = (zone_node_t *)(*trie_it_val(it->tree_it));
655

656 657 658
	return KNOT_EOK;
}

659
static int iter_get_next_node(zone_update_iter_t *it)
660
{
661 662 663
	trie_it_next(it->tree_it);
	if (trie_it_finished(it->tree_it)) {
		trie_it_free(it->tree_it);
664 665
		it->tree_it = NULL;
		it->cur_node = NULL;
666 667 668
		return KNOT_ENOENT;
	}

669
	it->cur_node = (zone_node_t *)(*trie_it_val(it->tree_it));
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

	return KNOT_EOK;
}

static int iter_init(zone_update_iter_t *it, zone_update_t *update, const bool nsec3)
{
	memset(it, 0, sizeof(*it));

	it->update = update;
	it->nsec3 = nsec3;
	int ret = iter_init_tree_iters(it, update, nsec3);
	if (ret != KNOT_EOK) {
		return ret;
	}

685
	it->cur_node = (zone_node_t *)(*trie_it_val(it->tree_it));
686 687 688 689 690 691

	return KNOT_EOK;
}

int zone_update_iter(zone_update_iter_t *it, zone_update_t *update)
{
692 693 694 695
	if (it == NULL || update == NULL) {
		return KNOT_EINVAL;
	}

696 697 698 699 700
	return iter_init(it, update, false);
}

int zone_update_iter_nsec3(zone_update_iter_t *it, zone_update_t *update)
{
701 702 703 704
	if (it == NULL || update == NULL) {
		return KNOT_EINVAL;
	}

705 706
	if (update->flags & UPDATE_FULL) {
		if (update->new_cont->nsec3_nodes == NULL) {
707
			/* No NSEC3 tree. */
708 709 710 711 712
			return KNOT_ENOENT;
		}
	} else {
		if (update->change.add->nsec3_nodes == NULL &&
		    update->change.remove->nsec3_nodes == NULL) {
713
			/* No NSEC3 changes. */
714 715 716 717 718 719 720 721 722 723 724 725 726
			return KNOT_ENOENT;
		}
	}

	return iter_init(it, update, true);
}

int zone_update_iter_next(zone_update_iter_t *it)
{
	if (it == NULL) {
		return KNOT_EINVAL;
	}

727 728
	if (it->tree_it != NULL) {
		int ret = iter_get_next_node(it);
729 730 731 732 733 734 735 736 737 738
		if (ret != KNOT_EOK && ret != KNOT_ENOENT) {
			return ret;
		}
	}

	return KNOT_EOK;
}

const zone_node_t *zone_update_iter_val(zone_update_iter_t *it)
{
739
	if (it != NULL) {
740
		return it->cur_node;
741 742 743 744 745
	} else {
		return NULL;
	}
}

746
void zone_update_iter_finish(zone_update_iter_t *it)
747
{
748
	if (it == NULL) {
749
		return;
750 751
	}

752
	trie_it_free(it->tree_it);
753 754
}

755
bool zone_update_no_change(zone_update_t *update)
756
{
757 758 759 760
	if (update == NULL) {
		return true;
	}

761 762 763
	if (update->flags & UPDATE_INCREMENTAL) {
		return changeset_empty(&update->change);
	} else {
764 765 766
		/* This branch does not make much sense and FULL update will most likely
		 * be a change every time anyway, just return false. */
		return false;
767
	}
768
}