changesets.c 18.6 KB
Newer Older
1
/*  Copyright (C) 2019 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

    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
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
Lubos Slovak's avatar
Lubos Slovak committed
15 16
 */

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 Inits changeset iterator with given tries. */
57
static int changeset_iter_init(changeset_iter_t *ch_it, size_t tries, ...)
Jan Kadlec's avatar
Jan Kadlec committed
58 59 60 61 62 63
{
	memset(ch_it, 0, sizeof(*ch_it));

	va_list args;
	va_start(args, tries);

64
	assert(tries <= sizeof(ch_it->trees) / sizeof(*ch_it->trees));
Jan Kadlec's avatar
Jan Kadlec committed
65
	for (size_t i = 0; i < tries; ++i) {
66
		zone_tree_t *t = va_arg(args, zone_tree_t *);
67 68 69 70
		if (t == NULL) {
			continue;
		}

71
		ch_it->trees[ch_it->n_trees++] = t;
Jan Kadlec's avatar
Jan Kadlec committed
72 73 74 75
	}

	va_end(args);

76 77
	assert(ch_it->n_trees);
	return zone_tree_it_begin(ch_it->trees[0], &ch_it->it);
Jan Kadlec's avatar
Jan Kadlec committed
78 79
}

80 81 82
// 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)
83
{
84 85 86 87
	if (fixed_rr != NULL) {
		*fixed_rr = knot_rrset_copy(rr, NULL);
	}

88 89
	zone_node_t *node = zone_contents_find_node_for_rr(counterpart, rr);
	if (node == NULL) {
90
		return;
91 92 93
	}

	if (!node_rrtype_exists(node, rr->type)) {
94
		return;
95 96 97
	}

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

101 102
	if (fixed_rr != NULL && *fixed_rr != NULL &&
	    ((*fixed_rr)->ttl == rrs_ttl || rr->type == KNOT_RRTYPE_RRSIG)) {
103
		int ret = knot_rdataset_subtract(&(*fixed_rr)->rrs, rrs, NULL);
104 105 106 107 108
		if (ret != KNOT_EOK) {
			return;
		}
	}

109 110
	// TTL of RRSIGs is better determined by original_ttl field, which is compared as part of rdata anyway
	if (rr->ttl == rrs_ttl || rr->type == KNOT_RRTYPE_RRSIG) {
111 112 113 114
		int ret = knot_rdataset_subtract(rrs, &rr->rrs, NULL);
		if (ret != KNOT_EOK) {
			return;
		}
115 116
	}

117
	if (knot_rdataset_size(rrs) == 0) {
118 119 120
		// Remove empty type.
		node_remove_rdataset(node, rr->type);

121
		if (node->rrset_count == 0 && node != counterpart->apex) {
122 123
			// Remove empty node.
			zone_tree_t *t = knot_rrset_is_nsec3rel(rr) ?
Daniel Salzman's avatar
Daniel Salzman committed
124 125
			                 counterpart->nsec3_nodes : counterpart->nodes;
			zone_tree_delete_empty(t, node);
126
		}
127 128
	}

129
	return;
130 131
}

Jan Kadlec's avatar
Jan Kadlec committed
132 133 134
int changeset_init(changeset_t *ch, const knot_dname_t *apex)
{
	memset(ch, 0, sizeof(changeset_t));
135

136 137
	// Init local changes
	ch->add = zone_contents_new(apex);
Jan Kadlec's avatar
Jan Kadlec committed
138 139 140
	if (ch->add == NULL) {
		return KNOT_ENOMEM;
	}
141
	ch->remove = zone_contents_new(apex);
Jan Kadlec's avatar
Jan Kadlec committed
142
	if (ch->remove == NULL) {
143
		zone_contents_free(ch->add);
Jan Kadlec's avatar
Jan Kadlec committed
144 145
		return KNOT_ENOMEM;
	}
Jan Kadlec's avatar
Jan Kadlec committed
146

Jan Kadlec's avatar
Jan Kadlec committed
147
	return KNOT_EOK;
148 149
}

Jan Kadlec's avatar
Jan Kadlec committed
150
changeset_t *changeset_new(const knot_dname_t *apex)
Jan Kadlec's avatar
Jan Kadlec committed
151
{
Jan Kadlec's avatar
Jan Kadlec committed
152
	changeset_t *ret = malloc(sizeof(changeset_t));
153
	if (ret == NULL) {
Jan Kadlec's avatar
Jan Kadlec committed
154 155 156
		return NULL;
	}

Jan Kadlec's avatar
Jan Kadlec committed
157 158 159 160 161 162
	if (changeset_init(ret, apex) == KNOT_EOK) {
		return ret;
	} else {
		free(ret);
		return NULL;
	}
163 164
}

165
bool changeset_empty(const changeset_t *ch)
166
{
167
	if (ch == NULL || ch->add == NULL || ch->remove == NULL) {
168
		return true;
169 170
	}

171 172 173
	if (ch->soa_to) {
		return false;
	}
174

175
	changeset_iter_t itt;
176
	changeset_iter_all(&itt, ch);
177

178 179
	knot_rrset_t rr = changeset_iter_next(&itt);
	changeset_iter_clear(&itt);
180 181

	return knot_rrset_empty(&rr);
182 183
}

Lubos Slovak's avatar
Lubos Slovak committed
184
size_t changeset_size(const changeset_t *ch)
185
{
Jan Kadlec's avatar
Jan Kadlec committed
186
	if (ch == NULL) {
187 188
		return 0;
	}
189

190
	changeset_iter_t itt;
191
	changeset_iter_all(&itt, ch);
192 193

	size_t size = 0;
194
	knot_rrset_t rr = changeset_iter_next(&itt);
195 196
	while(!knot_rrset_empty(&rr)) {
		++size;
197
		rr = changeset_iter_next(&itt);
198
	}
199
	changeset_iter_clear(&itt);
200

Jan Kadlec's avatar
Jan Kadlec committed
201 202 203 204 205 206 207
	if (!knot_rrset_empty(ch->soa_from)) {
		size += 1;
	}
	if (!knot_rrset_empty(ch->soa_to)) {
		size += 1;
	}

208
	return size;
209 210
}

211
int changeset_add_addition(changeset_t *ch, const knot_rrset_t *rrset, changeset_flag_t flags)
Jan Kadlec's avatar
Jan Kadlec committed
212
{
213 214 215 216 217 218 219 220 221
	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);
	}

222 223
	knot_rrset_t *rrset_cancelout = NULL;

224 225
	/* Check if there's any removal and remove that, then add this
	 * addition anyway. Required to change TTLs. */
226
	if (flags & CHANGESET_CHECK) {
227 228 229 230 231 232
		/* 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;
		}

233 234
		check_redundancy(ch->remove, rrset,
				 ((flags & CHANGESET_CHECK_CANCELOUT) ? &rrset_cancelout : NULL));
235
	}
236

237 238
	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);
239

240
	if (flags & CHANGESET_CHECK) {
241
		knot_rrset_free((knot_rrset_t *)rrset, NULL);
242
	}
243
	knot_rrset_free(rrset_cancelout, NULL);
244 245

	return ret;
Jan Kadlec's avatar
Jan Kadlec committed
246 247
}

248
int changeset_add_removal(changeset_t *ch, const knot_rrset_t *rrset, changeset_flag_t flags)
Jan Kadlec's avatar
Jan Kadlec committed
249
{
250 251 252 253 254 255 256 257 258
	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);
	}

259 260
	knot_rrset_t *rrset_cancelout = NULL;

261 262
	/* Check if there's any addition and remove that, then add this
	 * removal anyway. */
263
	if (flags & CHANGESET_CHECK) {
264 265 266 267 268 269
		/* 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;
		}

270 271
		check_redundancy(ch->add, rrset,
				 ((flags & CHANGESET_CHECK_CANCELOUT) ? &rrset_cancelout : NULL));
272
	}
273

274
	const knot_rrset_t *to_remove = (rrset_cancelout == NULL ? rrset : rrset_cancelout);
275
	int ret = (knot_rrset_empty(to_remove) || ch->remove == NULL) ? KNOT_EOK : add_rr_to_contents(ch->remove, to_remove);
276

277
	if (flags & CHANGESET_CHECK) {
278
		knot_rrset_free((knot_rrset_t *)rrset, NULL);
279
	}
280
	knot_rrset_free(rrset_cancelout, NULL);
281

282
	return ret;
Jan Kadlec's avatar
Jan Kadlec committed
283 284
}

285 286 287 288 289
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) {
290
			knot_rrset_free(ch->soa_to, NULL);
291 292 293 294 295 296 297 298 299 300 301 302 303 304
			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) {
305
			knot_rrset_free(ch->soa_from, NULL);
306 307 308 309 310 311 312 313 314
			ch->soa_from = NULL;
		}
		return KNOT_EOK;
	}

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

315
int changeset_merge(changeset_t *ch1, const changeset_t *ch2, int flags)
316
{
317
	changeset_iter_t itt;
318
	changeset_iter_rem(&itt, ch2);
319

320
	knot_rrset_t rrset = changeset_iter_next(&itt);
321
	while (!knot_rrset_empty(&rrset)) {
322
		int ret = changeset_add_removal(ch1, &rrset, CHANGESET_CHECK | flags);
323
		if (ret != KNOT_EOK) {
324 325
			changeset_iter_clear(&itt);
			return ret;
326
		}
327
		rrset = changeset_iter_next(&itt);
328
	}
329
	changeset_iter_clear(&itt);
330

331
	changeset_iter_add(&itt, ch2);
332

333
	rrset = changeset_iter_next(&itt);
334
	while (!knot_rrset_empty(&rrset)) {
335
		int ret = changeset_add_addition(ch1, &rrset, CHANGESET_CHECK | flags);
336
		if (ret != KNOT_EOK) {
337 338
			changeset_iter_clear(&itt);
			return ret;
339
		}
340
		rrset = changeset_iter_next(&itt);
341
	}
342
	changeset_iter_clear(&itt);
343

344 345
	// Use soa_to and serial from the second changeset
	// soa_to from the first changeset is redundant, delete it
346 347 348 349
	if (ch2->soa_to == NULL && ch2->soa_from == NULL) {
		// but not if ch2 has no soa change
		return KNOT_EOK;
	}
Jan Kadlec's avatar
Jan Kadlec committed
350
	knot_rrset_t *soa_copy = knot_rrset_copy(ch2->soa_to, NULL);
Jan Kadlec's avatar
Jan Kadlec committed
351
	if (soa_copy == NULL && ch2->soa_to) {
Jan Kadlec's avatar
Jan Kadlec committed
352 353
		return KNOT_ENOMEM;
	}
354
	knot_rrset_free(ch1->soa_to, NULL);
Jan Kadlec's avatar
Jan Kadlec committed
355
	ch1->soa_to = soa_copy;
356 357

	return KNOT_EOK;
358 359
}

360 361 362 363 364 365 366 367 368 369
uint32_t changeset_from(const changeset_t *ch)
{
	return ch->soa_from == NULL ? 0 : knot_soa_serial(ch->soa_from->rrs.rdata);
}

uint32_t changeset_to(const changeset_t *ch)
{
	return ch->soa_to == NULL ? 0 : knot_soa_serial(ch->soa_to->rrs.rdata);
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383
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;
	}
384

385 386
	knot_rrset_t *fixrrset;
	if (adding) {
387 388
		fixrrset = knot_rrset_new(apply->owner, apply->type, apply->rclass,
		                          apply->ttl, ctx->mm);
389 390 391 392 393 394
	} else {
		fixrrset = knot_rrset_copy(apply, ctx->mm);
	}
	if (fixrrset == NULL) {
		return KNOT_ENOMEM;
	}
395 396

	int ret = KNOT_EOK;
397 398 399
	if (adding) {
		ret = knot_rdataset_intersect(zrdataset, &apply->rrs, &fixrrset->rrs, ctx->mm);
	} else {
400 401 402
		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);
403 404 405 406 407 408 409 410 411
		}
	}
	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);
		}
	}
412

413
	knot_rrset_free(fixrrset, ctx->mm);
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	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);
}

432
int changeset_preapply_fix(const zone_contents_t *zone, changeset_t *ch)
433
{
434
	if (zone == NULL || ch == NULL) {
435 436 437 438 439 440 441 442 443 444
		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 };
445
	ret = changeset_walk(ch, preapply_fix_rrset, (void *)&ctx);
446
	if (ret == KNOT_EOK) {
447
		ret = subtract(ch, &fixing);
448 449 450 451 452
	}
	changeset_clear(&fixing);
	return ret;
}

453
int changeset_cancelout(changeset_t *ch)
454
{
455
	if (ch == NULL) {
456 457 458 459
		return KNOT_EINVAL;
	}

	changeset_t fixing;
460
	int ret = changeset_init(&fixing, ch->add->apex->owner);
461 462 463 464
	if (ret != KNOT_EOK) {
		return ret;
	}

465 466
	preapply_fix_ctx ctx = { .zone = ch->remove, .fixing = &fixing, .mm = NULL };
	ret = changeset_walk(ch, preapply_fix_rrset, (void *)&ctx);
467 468 469 470
	if (ret == KNOT_EOK) {
		assert(zone_contents_is_empty(fixing.add));
		zone_contents_t *fixing_add_bck = fixing.add;
		fixing.add = fixing.remove;
471
		ret = subtract(ch, &fixing);
472 473 474 475 476 477
		fixing.add = fixing_add_bck;
	}
	changeset_clear(&fixing);
	return ret;
}

478 479 480 481 482 483 484
bool changeset_differs_just_serial(const changeset_t *ch)
{
	if (ch == NULL || ch->soa_from == NULL || ch->soa_to == NULL) {
		return false;
	}

	knot_rrset_t *soa_to_cpy = knot_rrset_copy(ch->soa_to, NULL);
485
	knot_soa_serial_set(soa_to_cpy->rrs.rdata, knot_soa_serial(ch->soa_from->rrs.rdata));
486

487
	bool ret = knot_rrset_equal(ch->soa_from, soa_to_cpy, KNOT_RRSET_COMPARE_WHOLE);
488 489
	knot_rrset_free(soa_to_cpy, NULL);

490 491 492 493 494 495 496 497 498 499 500 501 502 503
	changeset_iter_t itt;
	changeset_iter_all(&itt, ch);

	knot_rrset_t rrset = changeset_iter_next(&itt);
	while (!knot_rrset_empty(&rrset) && ret) {
		if (rrset.type != KNOT_RRTYPE_RRSIG || rrset.rrs.count != 1 ||
		    knot_rrsig_type_covered(rrset.rrs.rdata) != KNOT_RRTYPE_SOA) {
			ret = false;
		}
		rrset = changeset_iter_next(&itt);
	}
	changeset_iter_clear(&itt);

	return ret;
504 505
}

506
int changeset_to_contents(changeset_t *ch, zone_contents_t **out)
507 508 509
{
	assert(ch->soa_from == NULL);
	assert(zone_contents_is_empty(ch->remove));
510
	assert(out != NULL);
511

512 513
	*out = ch->add;
	int ret = add_rr_to_contents(*out, ch->soa_to);
514
	knot_rrset_free(ch->soa_to, NULL);
515
	if (ret != KNOT_EOK) {
516
		zone_contents_deep_free(*out);
517
	}
518

519
	zone_contents_deep_free(ch->remove);
520
	return ret;
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
}

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);

537
	zone_contents_deep_free(res->add);
538
	res->add = copy;
539 540
	zone_contents_deep_free(res->remove);
	res->remove = NULL;
541 542 543 544 545 546 547
	return res;
}

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

550
	update_free_zone(ch->add);
551

552
	zone_contents_deep_free(ch->remove);
553 554
	knot_rrset_free(ch->soa_from, NULL);
	knot_rrset_free(ch->soa_to, NULL);
555 556 557 558
	free(ch->data);
	free(ch);
}

559 560 561 562 563 564 565 566 567 568 569 570 571
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)
572
{
Jan Kadlec's avatar
Jan Kadlec committed
573
	if (chgs) {
574 575
		changeset_t *chg, *nxt;
		WALK_LIST_DELSAFE(chg, nxt, *chgs) {
576
			rem_node(&chg->n);
577
			changeset_free(chg);
578
		}
579
		init_list(chgs);
580
	}
581 582
}

Jan Kadlec's avatar
Jan Kadlec committed
583
void changeset_clear(changeset_t *ch)
584
{
Jan Kadlec's avatar
Jan Kadlec committed
585 586
	if (ch == NULL) {
		return;
587
	}
588

Jan Kadlec's avatar
Jan Kadlec committed
589
	// Delete RRSets in lists, in case there are any left
590 591 592 593
	zone_contents_deep_free(ch->add);
	zone_contents_deep_free(ch->remove);
	ch->add = NULL;
	ch->remove = NULL;
594

595 596 597 598
	knot_rrset_free(ch->soa_from, NULL);
	knot_rrset_free(ch->soa_to, NULL);
	ch->soa_from = NULL;
	ch->soa_to = NULL;
599

Jan Kadlec's avatar
Jan Kadlec committed
600 601 602
	// Delete binary data
	free(ch->data);
}
603

604 605 606 607 608 609
changeset_t *changeset_clone(const changeset_t *ch)
{
	if (ch == NULL) {
		return NULL;
	}

610
	changeset_t *res = changeset_new(ch->add->apex->owner);
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	if (res == NULL) {
		return NULL;
	}

	res->soa_from = knot_rrset_copy(ch->soa_from, NULL);
	res->soa_to = knot_rrset_copy(ch->soa_to, NULL);

	int ret = KNOT_EOK;
	changeset_iter_t itt;

	changeset_iter_rem(&itt, ch);
	knot_rrset_t rr = changeset_iter_next(&itt);
	while (!knot_rrset_empty(&rr) && ret == KNOT_EOK) {
		ret = changeset_add_removal(res, &rr, 0);
		rr = changeset_iter_next(&itt);
	}
	changeset_iter_clear(&itt);

	changeset_iter_add(&itt, ch);
	rr = changeset_iter_next(&itt);
	while (!knot_rrset_empty(&rr) && ret == KNOT_EOK) {
		ret = changeset_add_addition(res, &rr, 0);
		rr = changeset_iter_next(&itt);
	}
	changeset_iter_clear(&itt);

	if ((ch->soa_from != NULL && res->soa_from == NULL) ||
	    (ch->soa_to != NULL && res->soa_to == NULL) ||
	    ret != KNOT_EOK) {
		changeset_free(res);
		return NULL;
	}

	return res;
}

Jan Kadlec's avatar
Jan Kadlec committed
647 648 649 650
void changeset_free(changeset_t *ch)
{
	changeset_clear(ch);
	free(ch);
651 652
}

653
int changeset_iter_add(changeset_iter_t *itt, const changeset_t *ch)
654
{
655
	return changeset_iter_init(itt, 2, ch->add->nodes, ch->add->nsec3_nodes);
656 657
}

658
int changeset_iter_rem(changeset_iter_t *itt, const changeset_t *ch)
659
{
660
	return changeset_iter_init(itt, 2, ch->remove->nodes, ch->remove->nsec3_nodes);
661 662
}

663
int changeset_iter_all(changeset_iter_t *itt, const changeset_t *ch)
664
{
665
	return changeset_iter_init(itt, 4, ch->add->nodes, ch->add->nsec3_nodes,
666
	                           ch->remove->nodes, ch->remove->nsec3_nodes);
667 668
}

669 670
knot_rrset_t changeset_iter_next(changeset_iter_t *it)
{
671
	assert(it);
672

673
	knot_rrset_t rr;
674 675 676
	while (it->node == NULL || it->node_pos >= it->node->rrset_count) {
		if (it->node != NULL) {
			zone_tree_it_next(&it->it);
677
		}
678 679 680 681 682 683 684 685 686 687 688
		while (zone_tree_it_finished(&it->it)) {
			zone_tree_it_free(&it->it);
			if (--it->n_trees > 0) {
				for (size_t i = 0; i < it->n_trees; i++) {
					it->trees[i] = it->trees[i + 1];
				}
				(void)zone_tree_it_begin(it->trees[0], &it->it);
			} else {
				knot_rrset_init_empty(&rr);
				return rr;
			}
689
		}
690 691
		it->node = zone_tree_it_val(&it->it);
		it->node_pos = 0;
692
	}
693 694
	rr = node_rrset_at(it->node, it->node_pos++);
	assert(!knot_rrset_empty(&rr));
695
	return rr;
696 697
}

698
void changeset_iter_clear(changeset_iter_t *it)
699
{
Jan Kadlec's avatar
Jan Kadlec committed
700
	if (it) {
701
		zone_tree_it_free(&it->it);
702 703
		it->node = NULL;
		it->node_pos = 0;
Jan Kadlec's avatar
Jan Kadlec committed
704
	}
705
}
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

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;
}
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

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);
}