changesets.c 17.5 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
Lubos Slovak's avatar
Lubos Slovak committed
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 <assert.h>
Daniel Salzman's avatar
Daniel Salzman committed
18
#include <stdlib.h>
19
#include <stdarg.h>
20

21
#include "knot/updates/changesets.h"
22
#include "knot/updates/apply.h"
Daniel Salzman's avatar
Daniel Salzman committed
23
#include "libknot/libknot.h"
24
#include "knot/zone/zone-dump.h"
25
#include "contrib/macros.h"
26
#include "contrib/mempattern.h"
27

28
static int handle_soa(knot_rrset_t **soa, const knot_rrset_t *rrset)
29
{
30 31
	assert(soa);
	assert(rrset);
32

33
	if (*soa != NULL) {
34
		knot_rrset_free(*soa, NULL);
35 36
	}

37 38 39 40 41 42 43 44 45 46 47
	*soa = knot_rrset_copy(rrset, NULL);
	if (*soa == NULL) {
		return KNOT_ENOMEM;
	}

	return KNOT_EOK;
}

/*! \brief Adds RRSet to given zone. */
static int add_rr_to_contents(zone_contents_t *z, const knot_rrset_t *rrset)
{
48 49
	zone_node_t *n = NULL;
	int ret = zone_contents_add_rr(z, rrset, &n);
50
	UNUSED(n);
51 52 53

	// We don't care of TTLs.
	return ret == KNOT_ETTL ? KNOT_EOK : ret;
54 55
}

56
/*! \brief Cleans up trie iterations. */
57
static void cleanup_iter_list(list_t *l)
58
{
59 60
	ptrnode_t *n, *nxt;
	WALK_LIST_DELSAFE(n, nxt, *l) {
61 62
		trie_it_t *it = (trie_it_t *)n->d;
		trie_it_free(it);
63 64 65 66 67 68
		rem_node(&n->n);
		free(n);
	}
	init_list(l);
}

69
/*! \brief Inits changeset iterator with given tries. */
70
static int changeset_iter_init(changeset_iter_t *ch_it, size_t tries, ...)
71 72 73 74 75 76 77 78
{
	memset(ch_it, 0, sizeof(*ch_it));
	init_list(&ch_it->iters);

	va_list args;
	va_start(args, tries);

	for (size_t i = 0; i < tries; ++i) {
79
		trie_t *t = va_arg(args, trie_t *);
80 81 82 83
		if (t == NULL) {
			continue;
		}

84
		trie_it_t *it = trie_it_begin(t);
85 86 87 88 89 90 91 92 93 94
		if (it == NULL) {
			cleanup_iter_list(&ch_it->iters);
			va_end(args);
			return KNOT_ENOMEM;
		}

		if (ptrlist_add(&ch_it->iters, it, NULL) == NULL) {
			cleanup_iter_list(&ch_it->iters);
			va_end(args);
			return KNOT_ENOMEM;
95 96 97 98 99 100 101 102
		}
	}

	va_end(args);

	return KNOT_EOK;
}

103
/*! \brief Gets next node from trie iterators. */
104
static void iter_next_node(changeset_iter_t *ch_it, trie_it_t *t_it)
105
{
106
	assert(!trie_it_finished(t_it));
107 108
	// Get next node, but not for the very first call.
	if (ch_it->node) {
109
		trie_it_next(t_it);
110
	}
111
	if (trie_it_finished(t_it)) {
112 113 114 115
		ch_it->node = NULL;
		return;
	}

116
	ch_it->node = (zone_node_t *)*trie_it_val(t_it);
117 118 119
	assert(ch_it->node);
	while (ch_it->node && ch_it->node->rrset_count == 0) {
		// Skip empty non-terminals.
120 121
		trie_it_next(t_it);
		if (trie_it_finished(t_it)) {
122 123
			ch_it->node = NULL;
		} else {
124
			ch_it->node = (zone_node_t *)*trie_it_val(t_it);
125 126 127
			assert(ch_it->node);
		}
	}
128

129 130 131
	ch_it->node_pos = 0;
}

132
/*! \brief Gets next RRSet from trie iterators. */
133
static knot_rrset_t get_next_rr(changeset_iter_t *ch_it, trie_it_t *t_it)
134 135 136 137
{
	if (ch_it->node == NULL || ch_it->node_pos == ch_it->node->rrset_count) {
		iter_next_node(ch_it, t_it);
		if (ch_it->node == NULL) {
138
			assert(trie_it_finished(t_it));
139 140 141 142 143 144 145 146 147
			knot_rrset_t rr;
			knot_rrset_init_empty(&rr);
			return rr;
		}
	}

	return node_rrset_at(ch_it->node, ch_it->node_pos++);
}

148 149 150
// removes from counterpart what is in rr.
// fixed_rr is an output parameter, holding a copy of rr without what has been removed from counterpart
static void check_redundancy(zone_contents_t *counterpart, const knot_rrset_t *rr, knot_rrset_t **fixed_rr)
151
{
152 153 154 155
	if (fixed_rr != NULL) {
		*fixed_rr = knot_rrset_copy(rr, NULL);
	}

156 157
	zone_node_t *node = zone_contents_find_node_for_rr(counterpart, rr);
	if (node == NULL) {
158
		return;
159 160 161
	}

	if (!node_rrtype_exists(node, rr->type)) {
162
		return;
163 164 165
	}

	// Subtract the data from node's RRSet.
166
	knot_rdataset_t *rrs = node_rdataset(node, rr->type);
167
	uint32_t rrs_ttl = node_rrset(node, rr->type).ttl;
168

169 170
	if (fixed_rr != NULL && *fixed_rr != NULL && (*fixed_rr)->ttl == rrs_ttl) {
		int ret = knot_rdataset_subtract(&(*fixed_rr)->rrs, rrs, NULL);
171 172 173 174 175
		if (ret != KNOT_EOK) {
			return;
		}
	}

176 177 178 179 180
	if (rr->ttl == rrs_ttl) {
		int ret = knot_rdataset_subtract(rrs, &rr->rrs, NULL);
		if (ret != KNOT_EOK) {
			return;
		}
181 182
	}

183
	if (knot_rdataset_size(rrs) == 0) {
184 185 186
		// Remove empty type.
		node_remove_rdataset(node, rr->type);

187
		if (node->rrset_count == 0 && node != counterpart->apex) {
188 189
			// Remove empty node.
			zone_tree_t *t = knot_rrset_is_nsec3rel(rr) ?
Daniel Salzman's avatar
Daniel Salzman committed
190 191
			                 counterpart->nsec3_nodes : counterpart->nodes;
			zone_tree_delete_empty(t, node);
192
		}
193 194
	}

195
	return;
196 197
}

198 199 200
int changeset_init(changeset_t *ch, const knot_dname_t *apex)
{
	memset(ch, 0, sizeof(changeset_t));
201

202 203
	// Init local changes
	ch->add = zone_contents_new(apex);
204 205 206
	if (ch->add == NULL) {
		return KNOT_ENOMEM;
	}
207
	ch->remove = zone_contents_new(apex);
208
	if (ch->remove == NULL) {
209
		zone_contents_free(ch->add);
210 211
		return KNOT_ENOMEM;
	}
Jan Kadlec's avatar
Jan Kadlec committed
212

213
	return KNOT_EOK;
214 215
}

216
changeset_t *changeset_new(const knot_dname_t *apex)
217
{
218
	changeset_t *ret = malloc(sizeof(changeset_t));
219
	if (ret == NULL) {
220 221 222
		return NULL;
	}

223 224 225 226 227 228
	if (changeset_init(ret, apex) == KNOT_EOK) {
		return ret;
	} else {
		free(ret);
		return NULL;
	}
229 230
}

231
bool changeset_empty(const changeset_t *ch)
232
{
233
	if (ch == NULL || ch->add == NULL || ch->remove == NULL) {
234
		return true;
235 236
	}

237 238 239
	if (ch->soa_to) {
		return false;
	}
240

241
	changeset_iter_t itt;
242
	changeset_iter_all(&itt, ch);
243

244 245
	knot_rrset_t rr = changeset_iter_next(&itt);
	changeset_iter_clear(&itt);
246 247

	return knot_rrset_empty(&rr);
248 249
}

250
size_t changeset_size(const changeset_t *ch)
251
{
252
	if (ch == NULL) {
253 254
		return 0;
	}
255

256
	changeset_iter_t itt;
257
	changeset_iter_all(&itt, ch);
258 259

	size_t size = 0;
260
	knot_rrset_t rr = changeset_iter_next(&itt);
261 262
	while(!knot_rrset_empty(&rr)) {
		++size;
263
		rr = changeset_iter_next(&itt);
264
	}
265
	changeset_iter_clear(&itt);
266

Jan Kadlec's avatar
Jan Kadlec committed
267 268 269 270 271 272 273
	if (!knot_rrset_empty(ch->soa_from)) {
		size += 1;
	}
	if (!knot_rrset_empty(ch->soa_to)) {
		size += 1;
	}

274
	return size;
275 276
}

277
int changeset_add_addition(changeset_t *ch, const knot_rrset_t *rrset, changeset_flag_t flags)
278
{
279 280 281 282 283 284 285 286 287
	if (!ch || !rrset) {
		return KNOT_EINVAL;
	}

	if (rrset->type == KNOT_RRTYPE_SOA) {
		/* Do not add SOAs into actual contents. */
		return handle_soa(&ch->soa_to, rrset);
	}

288 289
	knot_rrset_t *rrset_cancelout = NULL;

290 291
	/* Check if there's any removal and remove that, then add this
	 * addition anyway. Required to change TTLs. */
292
	if (flags & CHANGESET_CHECK) {
293 294 295 296 297 298
		/* If we delete the rrset, we need to hold a copy to add it later */
		rrset = knot_rrset_copy(rrset, NULL);
		if (rrset == NULL) {
			return KNOT_ENOMEM;
		}

299 300
		check_redundancy(ch->remove, rrset,
				 ((flags & CHANGESET_CHECK_CANCELOUT) ? &rrset_cancelout : NULL));
301
	}
302

303 304
	const knot_rrset_t *to_add = (rrset_cancelout == NULL ? rrset : rrset_cancelout);
	int ret = knot_rrset_empty(to_add) ? KNOT_EOK : add_rr_to_contents(ch->add, to_add);
305

306
	if (flags & CHANGESET_CHECK) {
307
		knot_rrset_free((knot_rrset_t *)rrset, NULL);
308
	}
309
	knot_rrset_free(rrset_cancelout, NULL);
310 311

	return ret;
312 313
}

314
int changeset_add_removal(changeset_t *ch, const knot_rrset_t *rrset, changeset_flag_t flags)
315
{
316 317 318 319 320 321 322 323 324
	if (!ch || !rrset) {
		return KNOT_EINVAL;
	}

	if (rrset->type == KNOT_RRTYPE_SOA) {
		/* Do not add SOAs into actual contents. */
		return handle_soa(&ch->soa_from, rrset);
	}

325 326
	knot_rrset_t *rrset_cancelout = NULL;

327 328
	/* Check if there's any addition and remove that, then add this
	 * removal anyway. */
329
	if (flags & CHANGESET_CHECK) {
330 331 332 333 334 335
		/* If we delete the rrset, we need to hold a copy to add it later */
		rrset = knot_rrset_copy(rrset, NULL);
		if (rrset == NULL) {
			return KNOT_ENOMEM;
		}

336 337
		check_redundancy(ch->add, rrset,
				 ((flags & CHANGESET_CHECK_CANCELOUT) ? &rrset_cancelout : NULL));
338
	}
339

340 341
	const knot_rrset_t *to_remove = (rrset_cancelout == NULL ? rrset : rrset_cancelout);
	int ret = knot_rrset_empty(to_remove) ? KNOT_EOK : add_rr_to_contents(ch->remove, to_remove);
342

343
	if (flags & CHANGESET_CHECK) {
344
		knot_rrset_free((knot_rrset_t *)rrset, NULL);
345
	}
346
	knot_rrset_free(rrset_cancelout, NULL);
347

348
	return ret;
349 350
}

351 352 353 354 355
int changeset_remove_addition(changeset_t *ch, const knot_rrset_t *rrset)
{
	if (rrset->type == KNOT_RRTYPE_SOA) {
		/* Do not add SOAs into actual contents. */
		if (ch->soa_to != NULL) {
356
			knot_rrset_free(ch->soa_to, NULL);
357 358 359 360 361 362 363 364 365 366 367 368 369 370
			ch->soa_to = NULL;
		}
		return KNOT_EOK;
	}

	zone_node_t *n = NULL;
	return zone_contents_remove_rr(ch->add, rrset, &n);
}

int changeset_remove_removal(changeset_t *ch, const knot_rrset_t *rrset)
{
	if (rrset->type == KNOT_RRTYPE_SOA) {
		/* Do not add SOAs into actual contents. */
		if (ch->soa_from != NULL) {
371
			knot_rrset_free(ch->soa_from, NULL);
372 373 374 375 376 377 378 379 380
			ch->soa_from = NULL;
		}
		return KNOT_EOK;
	}

	zone_node_t *n = NULL;
	return zone_contents_remove_rr(ch->remove, rrset, &n);
}

381
int changeset_merge(changeset_t *ch1, const changeset_t *ch2, int flags)
382
{
383
	changeset_iter_t itt;
384
	changeset_iter_rem(&itt, ch2);
385

386
	knot_rrset_t rrset = changeset_iter_next(&itt);
387
	while (!knot_rrset_empty(&rrset)) {
388
		int ret = changeset_add_removal(ch1, &rrset, CHANGESET_CHECK | flags);
389
		if (ret != KNOT_EOK) {
390 391
			changeset_iter_clear(&itt);
			return ret;
392
		}
393
		rrset = changeset_iter_next(&itt);
394
	}
395
	changeset_iter_clear(&itt);
396

397
	changeset_iter_add(&itt, ch2);
398

399
	rrset = changeset_iter_next(&itt);
400
	while (!knot_rrset_empty(&rrset)) {
401
		int ret = changeset_add_addition(ch1, &rrset, CHANGESET_CHECK | flags);
402
		if (ret != KNOT_EOK) {
403 404
			changeset_iter_clear(&itt);
			return ret;
405
		}
406
		rrset = changeset_iter_next(&itt);
407
	}
408
	changeset_iter_clear(&itt);
409

410 411
	// Use soa_to and serial from the second changeset
	// soa_to from the first changeset is redundant, delete it
412 413 414 415
	if (ch2->soa_to == NULL && ch2->soa_from == NULL) {
		// but not if ch2 has no soa change
		return KNOT_EOK;
	}
416
	knot_rrset_t *soa_copy = knot_rrset_copy(ch2->soa_to, NULL);
Jan Kadlec's avatar
Jan Kadlec committed
417
	if (soa_copy == NULL && ch2->soa_to) {
418 419
		return KNOT_ENOMEM;
	}
420
	knot_rrset_free(ch1->soa_to, NULL);
421
	ch1->soa_to = soa_copy;
422 423

	return KNOT_EOK;
424 425
}

426 427 428 429 430 431 432 433 434 435 436 437 438 439
typedef struct {
	const zone_contents_t *zone;
	changeset_t *fixing;
	knot_mm_t *mm;
} preapply_fix_ctx;

static int preapply_fix_rrset(const knot_rrset_t *apply, bool adding, void *data)
{
	preapply_fix_ctx *ctx  = (preapply_fix_ctx *)data;
	const zone_node_t *znode = zone_contents_find_node(ctx->zone, apply->owner);
	const knot_rdataset_t *zrdataset = node_rdataset(znode, apply->type);
	if (adding && zrdataset == NULL) {
		return KNOT_EOK;
	}
440

441 442
	knot_rrset_t *fixrrset;
	if (adding) {
443 444
		fixrrset = knot_rrset_new(apply->owner, apply->type, apply->rclass,
		                          apply->ttl, ctx->mm);
445 446 447 448 449 450
	} else {
		fixrrset = knot_rrset_copy(apply, ctx->mm);
	}
	if (fixrrset == NULL) {
		return KNOT_ENOMEM;
	}
451 452

	int ret = KNOT_EOK;
453 454 455
	if (adding) {
		ret = knot_rdataset_intersect(zrdataset, &apply->rrs, &fixrrset->rrs, ctx->mm);
	} else {
456 457 458
		uint32_t zrrset_ttl = node_rrset(znode, apply->type).ttl;
		if (zrdataset != NULL && fixrrset->ttl == zrrset_ttl) {
			ret = knot_rdataset_subtract(&fixrrset->rrs, zrdataset, ctx->mm);
459 460 461 462 463 464 465 466 467
		}
	}
	if (ret == KNOT_EOK && !knot_rrset_empty(fixrrset)) {
		if (adding) {
			ret = changeset_add_removal(ctx->fixing, fixrrset, 0);
		} else {
			ret = changeset_add_addition(ctx->fixing, fixrrset, 0);
		}
	}
468

469
	knot_rrset_free(fixrrset, ctx->mm);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	return ret;
}

static int subtract_callback(const knot_rrset_t *rrset, bool addition, void *subtractfrom)
{
	changeset_t *chsf = (changeset_t *)subtractfrom;
	if (addition) {
		return changeset_remove_removal(chsf, rrset);
	} else {
		return changeset_remove_addition(chsf, rrset);
	}
}

static int subtract(changeset_t *from, const changeset_t *what)
{
	return changeset_walk(what, subtract_callback, (void *)from);
}

488
int changeset_preapply_fix(const zone_contents_t *zone, changeset_t *ch)
489
{
490
	if (zone == NULL || ch == NULL) {
491 492 493 494 495 496 497 498 499 500
		return KNOT_EINVAL;
	}

	changeset_t fixing;
	int ret = changeset_init(&fixing, zone->apex->owner);
	if (ret != KNOT_EOK) {
		return ret;
	}

	preapply_fix_ctx ctx = { .zone = zone, .fixing = &fixing, .mm = NULL };
501
	ret = changeset_walk(ch, preapply_fix_rrset, (void *)&ctx);
502
	if (ret == KNOT_EOK) {
503
		ret = subtract(ch, &fixing);
504 505 506 507 508
	}
	changeset_clear(&fixing);
	return ret;
}

509
int changeset_cancelout(changeset_t *ch)
510
{
511
	if (ch == NULL) {
512 513 514 515
		return KNOT_EINVAL;
	}

	changeset_t fixing;
516
	int ret = changeset_init(&fixing, ch->add->apex->owner);
517 518 519 520
	if (ret != KNOT_EOK) {
		return ret;
	}

521 522
	preapply_fix_ctx ctx = { .zone = ch->remove, .fixing = &fixing, .mm = NULL };
	ret = changeset_walk(ch, preapply_fix_rrset, (void *)&ctx);
523 524 525 526
	if (ret == KNOT_EOK) {
		assert(zone_contents_is_empty(fixing.add));
		zone_contents_t *fixing_add_bck = fixing.add;
		fixing.add = fixing.remove;
527
		ret = subtract(ch, &fixing);
528 529 530 531 532 533
		fixing.add = fixing_add_bck;
	}
	changeset_clear(&fixing);
	return ret;
}

534
int changeset_to_contents(changeset_t *ch, zone_contents_t **out)
535 536 537
{
	assert(ch->soa_from == NULL);
	assert(zone_contents_is_empty(ch->remove));
538
	assert(out != NULL);
539

540 541
	*out = ch->add;
	int ret = add_rr_to_contents(*out, ch->soa_to);
542
	knot_rrset_free(ch->soa_to, NULL);
543
	if (ret != KNOT_EOK) {
544
		zone_contents_deep_free(*out);
545
	}
546

547
	zone_contents_deep_free(ch->remove);
548 549
	free(ch->data);
	free(ch);
550
	return ret;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
}

changeset_t *changeset_from_contents(const zone_contents_t *contents)
{
	zone_contents_t *copy = NULL;
	if (zone_contents_shallow_copy(contents, &copy) != KNOT_EOK) {
		return NULL;
	}

	changeset_t *res = changeset_new(copy->apex->owner);

	knot_rrset_t soa_rr = node_rrset(copy->apex, KNOT_RRTYPE_SOA);;
	res->soa_to = knot_rrset_copy(&soa_rr, NULL);

	node_remove_rdataset(copy->apex, KNOT_RRTYPE_SOA);

567
	zone_contents_deep_free(res->add);
568 569 570 571 572 573 574 575 576 577
	res->add = copy;
	return res;
}

void changeset_from_contents_free(changeset_t *ch)
{
	assert(ch);
	assert(ch->soa_from == NULL);
	assert(zone_contents_is_empty(ch->remove));

578
	update_free_zone(ch->add);
579

580
	zone_contents_deep_free(ch->remove);
581 582
	knot_rrset_free(ch->soa_from, NULL);
	knot_rrset_free(ch->soa_to, NULL);
583 584 585 586
	free(ch->data);
	free(ch);
}

587 588 589 590 591 592 593 594 595 596 597 598 599
void changesets_clear(list_t *chgs)
{
	if (chgs) {
		changeset_t *chg, *nxt;
		WALK_LIST_DELSAFE(chg, nxt, *chgs) {
			changeset_clear(chg);
			rem_node(&chg->n);
		}
		init_list(chgs);
	}
}

void changesets_free(list_t *chgs)
600
{
601
	if (chgs) {
602 603
		changeset_t *chg, *nxt;
		WALK_LIST_DELSAFE(chg, nxt, *chgs) {
604
			rem_node(&chg->n);
605
			changeset_free(chg);
606
		}
607
		init_list(chgs);
608
	}
609 610
}

611
void changeset_clear(changeset_t *ch)
612
{
613 614
	if (ch == NULL) {
		return;
615
	}
616

617
	// Delete RRSets in lists, in case there are any left
618 619 620 621
	zone_contents_deep_free(ch->add);
	zone_contents_deep_free(ch->remove);
	ch->add = NULL;
	ch->remove = NULL;
622

623 624 625 626
	knot_rrset_free(ch->soa_from, NULL);
	knot_rrset_free(ch->soa_to, NULL);
	ch->soa_from = NULL;
	ch->soa_to = NULL;
627

628 629 630
	// Delete binary data
	free(ch->data);
}
631

632 633 634 635
void changeset_free(changeset_t *ch)
{
	changeset_clear(ch);
	free(ch);
636 637
}

638
int changeset_iter_add(changeset_iter_t *itt, const changeset_t *ch)
639
{
640
	return changeset_iter_init(itt, 2, ch->add->nodes, ch->add->nsec3_nodes);
641 642
}

643
int changeset_iter_rem(changeset_iter_t *itt, const changeset_t *ch)
644
{
645
	return changeset_iter_init(itt, 2, ch->remove->nodes, ch->remove->nsec3_nodes);
646 647
}

648
int changeset_iter_all(changeset_iter_t *itt, const changeset_t *ch)
649
{
650
	return changeset_iter_init(itt, 4, ch->add->nodes, ch->add->nsec3_nodes,
651
	                           ch->remove->nodes, ch->remove->nsec3_nodes);
652 653
}

654 655
knot_rrset_t changeset_iter_next(changeset_iter_t *it)
{
656
	assert(it);
657 658 659 660
	ptrnode_t *n = NULL;
	knot_rrset_t rr;
	knot_rrset_init_empty(&rr);
	WALK_LIST(n, it->iters) {
661 662
		trie_it_t *t_it = (trie_it_t *)n->d;
		if (trie_it_finished(t_it)) {
663 664 665 666 667 668 669 670
			continue;
		}

		rr = get_next_rr(it, t_it);
		if (!knot_rrset_empty(&rr)) {
			// Got valid RRSet.
			return rr;
		}
671 672
	}

673
	return rr;
674 675
}

676
void changeset_iter_clear(changeset_iter_t *it)
677
{
678
	if (it) {
679
		cleanup_iter_list(&it->iters);
680 681
		it->node = NULL;
		it->node_pos = 0;
682
	}
683
}
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721

int changeset_walk(const changeset_t *changeset, changeset_walk_callback callback, void *ctx)
{
	changeset_iter_t it;
	int ret = changeset_iter_rem(&it, changeset);
	if (ret != KNOT_EOK) {
		return ret;
	}

	knot_rrset_t rrset = changeset_iter_next(&it);
	while (!knot_rrset_empty(&rrset)) {
		ret = callback(&rrset, false, ctx);
		if (ret != KNOT_EOK) {
			changeset_iter_clear(&it);
			return ret;
		}
		rrset = changeset_iter_next(&it);
	}
	changeset_iter_clear(&it);

	ret = changeset_iter_add(&it, changeset);
	if (ret != KNOT_EOK) {
		return ret;
	}

	rrset = changeset_iter_next(&it);
	while (!knot_rrset_empty(&rrset)) {
		ret = callback(&rrset, true, ctx);
		if (ret != KNOT_EOK) {
			changeset_iter_clear(&it);
			return ret;
		}
		rrset = changeset_iter_next(&it);
	}
	changeset_iter_clear(&it);

	return KNOT_EOK;
}
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

void changeset_print(const changeset_t *changeset, FILE *outfile, bool color)
{
	const char * RED = "\x1B[31m", * GRN = "\x1B[32m", * RESET = "\x1B[0m";
	size_t buflen = 1024;
	char *buff = malloc(buflen);

	if (changeset->soa_from != NULL || !zone_contents_is_empty(changeset->remove)) {
		fprintf(outfile, "%s;;Removed\n", color ? RED : "");
	}
	if (changeset->soa_from != NULL && buff != NULL) {
		(void)knot_rrset_txt_dump(changeset->soa_from, &buff, &buflen, &KNOT_DUMP_STYLE_DEFAULT);
		fprintf(outfile, "%s", buff);
	}
	(void)zone_dump_text(changeset->remove, outfile, false);

	if (changeset->soa_to != NULL || !zone_contents_is_empty(changeset->add)) {
		fprintf(outfile, "%s;;Added\n", color ? GRN : "");
	}
	if (changeset->soa_to != NULL && buff != NULL) {
		(void)knot_rrset_txt_dump(changeset->soa_to, &buff, &buflen, &KNOT_DUMP_STYLE_DEFAULT);
		fprintf(outfile, "%s", buff);
	}
	(void)zone_dump_text(changeset->add, outfile, false);

	if (color) {
		printf("%s", RESET);
	}
	free(buff);
}