changesets.c 11.5 KB
Newer Older
1
/*  Copyright (C) 2016 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 "contrib/macros.h"
25
#include "contrib/mempattern.h"
26

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

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

36
37
38
39
40
41
42
43
44
45
46
	*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)
{
47
48
	zone_node_t *n = NULL;
	int ret = zone_contents_add_rr(z, rrset, &n);
49
	UNUSED(n);
50
	return ret;
51
52
}

53
/*! \brief Cleans up trie iterations. */
Jan Kadlec's avatar
Jan Kadlec committed
54
static void cleanup_iter_list(list_t *l)
55
{
Jan Kadlec's avatar
Jan Kadlec committed
56
57
	ptrnode_t *n, *nxt;
	WALK_LIST_DELSAFE(n, nxt, *l) {
58
59
		trie_it_t *it = (trie_it_t *)n->d;
		trie_it_free(it);
Jan Kadlec's avatar
Jan Kadlec committed
60
61
62
63
64
65
		rem_node(&n->n);
		free(n);
	}
	init_list(l);
}

66
/*! \brief Inits changeset iterator with given tries. */
67
static int changeset_iter_init(changeset_iter_t *ch_it, size_t tries, ...)
Jan Kadlec's avatar
Jan Kadlec committed
68
69
70
71
72
73
74
75
{
	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) {
76
		trie_t *t = va_arg(args, trie_t *);
77
78
79
80
		if (t == NULL) {
			continue;
		}

81
		trie_it_t *it = trie_it_begin(t);
82
83
84
85
86
87
88
89
90
91
		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;
Jan Kadlec's avatar
Jan Kadlec committed
92
93
94
95
96
97
98
99
		}
	}

	va_end(args);

	return KNOT_EOK;
}

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

113
	ch_it->node = (zone_node_t *)*trie_it_val(t_it);
Jan Kadlec's avatar
Jan Kadlec committed
114
115
116
	assert(ch_it->node);
	while (ch_it->node && ch_it->node->rrset_count == 0) {
		// Skip empty non-terminals.
117
118
		trie_it_next(t_it);
		if (trie_it_finished(t_it)) {
Jan Kadlec's avatar
Jan Kadlec committed
119
120
			ch_it->node = NULL;
		} else {
121
			ch_it->node = (zone_node_t *)*trie_it_val(t_it);
Jan Kadlec's avatar
Jan Kadlec committed
122
123
124
			assert(ch_it->node);
		}
	}
125

Jan Kadlec's avatar
Jan Kadlec committed
126
127
128
	ch_it->node_pos = 0;
}

129
/*! \brief Gets next RRSet from trie iterators. */
130
static knot_rrset_t get_next_rr(changeset_iter_t *ch_it, trie_it_t *t_it)
Jan Kadlec's avatar
Jan Kadlec committed
131
132
133
134
{
	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) {
135
			assert(trie_it_finished(t_it));
Jan Kadlec's avatar
Jan Kadlec committed
136
137
138
139
140
141
142
143
144
			knot_rrset_t rr;
			knot_rrset_init_empty(&rr);
			return rr;
		}
	}

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

145
static void check_redundancy(zone_contents_t *counterpart, const knot_rrset_t *rr)
146
147
148
{
	zone_node_t *node = zone_contents_find_node_for_rr(counterpart, rr);
	if (node == NULL) {
149
		return;
150
151
152
	}

	if (!node_rrtype_exists(node, rr->type)) {
153
		return;
154
155
156
	}

	// Subtract the data from node's RRSet.
157
158
	knot_rdataset_t *rrs = node_rdataset(node, rr->type);
	int ret = knot_rdataset_subtract(rrs, &rr->rrs, NULL);
159
	if (ret != KNOT_EOK) {
160
		return;
161
162
	}

163
	if (knot_rdataset_size(rrs) == 0) {
164
165
166
		// Remove empty type.
		node_remove_rdataset(node, rr->type);

167
168
169
170
		if (node->rrset_count == 0) {
			// Remove empty node.
			zone_tree_t *t = knot_rrset_is_nsec3rel(rr) ?
								 counterpart->nsec3_nodes : counterpart->nodes;
171
			zone_tree_delete_empty_node(t, node);
172
		}
173
174
	}

175
	return;
176
177
}

Jan Kadlec's avatar
Jan Kadlec committed
178
179
180
int changeset_init(changeset_t *ch, const knot_dname_t *apex)
{
	memset(ch, 0, sizeof(changeset_t));
181

182
183
	// Init local changes
	ch->add = zone_contents_new(apex);
Jan Kadlec's avatar
Jan Kadlec committed
184
185
186
	if (ch->add == NULL) {
		return KNOT_ENOMEM;
	}
187
	ch->remove = zone_contents_new(apex);
Jan Kadlec's avatar
Jan Kadlec committed
188
189
190
191
	if (ch->remove == NULL) {
		zone_contents_free(&ch->add);
		return KNOT_ENOMEM;
	}
Jan Kadlec's avatar
Jan Kadlec committed
192

Jan Kadlec's avatar
Jan Kadlec committed
193
	return KNOT_EOK;
194
195
}

Jan Kadlec's avatar
Jan Kadlec committed
196
changeset_t *changeset_new(const knot_dname_t *apex)
Jan Kadlec's avatar
Jan Kadlec committed
197
{
Jan Kadlec's avatar
Jan Kadlec committed
198
	changeset_t *ret = malloc(sizeof(changeset_t));
199
	if (ret == NULL) {
Jan Kadlec's avatar
Jan Kadlec committed
200
201
202
		return NULL;
	}

Jan Kadlec's avatar
Jan Kadlec committed
203
204
205
206
207
208
	if (changeset_init(ret, apex) == KNOT_EOK) {
		return ret;
	} else {
		free(ret);
		return NULL;
	}
209
210
}

211
bool changeset_empty(const changeset_t *ch)
212
{
213
	if (ch == NULL || ch->add == NULL || ch->remove == NULL) {
214
		return true;
215
216
	}

217
218
219
	if (ch->soa_to) {
		return false;
	}
220

221
	changeset_iter_t itt;
222
	changeset_iter_all(&itt, ch);
223

224
225
	knot_rrset_t rr = changeset_iter_next(&itt);
	changeset_iter_clear(&itt);
226
227

	return knot_rrset_empty(&rr);
228
229
}

Lubos Slovak's avatar
Lubos Slovak committed
230
size_t changeset_size(const changeset_t *ch)
231
{
Jan Kadlec's avatar
Jan Kadlec committed
232
	if (ch == NULL) {
233
234
		return 0;
	}
235

236
	changeset_iter_t itt;
237
	changeset_iter_all(&itt, ch);
238
239

	size_t size = 0;
240
	knot_rrset_t rr = changeset_iter_next(&itt);
241
242
	while(!knot_rrset_empty(&rr)) {
		++size;
243
		rr = changeset_iter_next(&itt);
244
	}
245
	changeset_iter_clear(&itt);
246

Jan Kadlec's avatar
Jan Kadlec committed
247
248
249
250
251
252
253
	if (!knot_rrset_empty(ch->soa_from)) {
		size += 1;
	}
	if (!knot_rrset_empty(ch->soa_to)) {
		size += 1;
	}

254
	return size;
255
256
}

257
int changeset_add_addition(changeset_t *ch, const knot_rrset_t *rrset, unsigned flags)
Jan Kadlec's avatar
Jan Kadlec committed
258
{
259
260
261
262
263
264
265
266
267
	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);
	}

268
269
	/* Check if there's any removal and remove that, then add this
	 * addition anyway. Required to change TTLs. */
270
	if (flags & CHANGESET_CHECK) {
271
272
273
274
275
276
		/* 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;
		}

277
		check_redundancy(ch->remove, rrset);
278
	}
279

280
	int ret = add_rr_to_contents(ch->add, rrset);
281

282
	if (flags & CHANGESET_CHECK) {
283
284
285
286
		knot_rrset_free((knot_rrset_t **)&rrset, NULL);
	}

	return ret;
Jan Kadlec's avatar
Jan Kadlec committed
287
288
}

289
int changeset_add_removal(changeset_t *ch, const knot_rrset_t *rrset, unsigned flags)
Jan Kadlec's avatar
Jan Kadlec committed
290
{
291
292
293
294
295
296
297
298
299
	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);
	}

300
301
	/* Check if there's any addition and remove that, then add this
	 * removal anyway. */
302
	if (flags & CHANGESET_CHECK) {
303
304
305
306
307
308
		/* 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;
		}

309
		check_redundancy(ch->add, rrset);
310
	}
311

312
	int ret = add_rr_to_contents(ch->remove, rrset);
313

314
	if (flags & CHANGESET_CHECK) {
315
316
317
318
		knot_rrset_free((knot_rrset_t **)&rrset, NULL);
	}

	return ret;
Jan Kadlec's avatar
Jan Kadlec committed
319
320
}

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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) {
			knot_rrset_free(&ch->soa_to, NULL);
			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) {
			knot_rrset_free(&ch->soa_from, NULL);
			ch->soa_from = NULL;
		}
		return KNOT_EOK;
	}

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

Jan Kadlec's avatar
Jan Kadlec committed
351
int changeset_merge(changeset_t *ch1, const changeset_t *ch2)
352
{
353
	changeset_iter_t itt;
354
	changeset_iter_add(&itt, ch2);
355

356
	knot_rrset_t rrset = changeset_iter_next(&itt);
357
	while (!knot_rrset_empty(&rrset)) {
358
		int ret = changeset_add_addition(ch1, &rrset, CHANGESET_CHECK);
359
		if (ret != KNOT_EOK) {
360
361
			changeset_iter_clear(&itt);
			return ret;
362
		}
363
		rrset = changeset_iter_next(&itt);
364
	}
365
	changeset_iter_clear(&itt);
366

367
	changeset_iter_rem(&itt, ch2);
368

369
	rrset = changeset_iter_next(&itt);
370
	while (!knot_rrset_empty(&rrset)) {
371
		int ret = changeset_add_removal(ch1, &rrset, CHANGESET_CHECK);
372
		if (ret != KNOT_EOK) {
373
374
			changeset_iter_clear(&itt);
			return ret;
375
		}
376
		rrset = changeset_iter_next(&itt);
377
	}
378
	changeset_iter_clear(&itt);
379

380
381
	// Use soa_to and serial from the second changeset
	// soa_to from the first changeset is redundant, delete it
Jan Kadlec's avatar
Jan Kadlec committed
382
	knot_rrset_t *soa_copy = knot_rrset_copy(ch2->soa_to, NULL);
Jan Kadlec's avatar
Jan Kadlec committed
383
	if (soa_copy == NULL && ch2->soa_to) {
Jan Kadlec's avatar
Jan Kadlec committed
384
385
		return KNOT_ENOMEM;
	}
386
	knot_rrset_free(&ch1->soa_to, NULL);
Jan Kadlec's avatar
Jan Kadlec committed
387
	ch1->soa_to = soa_copy;
388
389

	return KNOT_EOK;
390
391
}

392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
zone_contents_t *changeset_to_contents(changeset_t *ch)
{
	assert(ch->soa_from == NULL);
	assert(zone_contents_is_empty(ch->remove));

	zone_contents_t *res = ch->add;
	add_rr_to_contents(res, ch->soa_to);
	knot_rrset_free(&ch->soa_to, NULL);

	zone_contents_deep_free(&ch->remove);
	free(ch->data);
	free(ch);
	return res;
}

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

	zone_contents_deep_free(&res->add);
	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));

	update_free_zone(&ch->add);

	zone_contents_deep_free(&ch->remove);
	knot_rrset_free(&ch->soa_from, NULL);
	knot_rrset_free(&ch->soa_to, NULL);
	free(ch->data);
	free(ch);
}

441
442
443
444
445
446
447
448
449
450
451
452
453
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)
454
{
Jan Kadlec's avatar
Jan Kadlec committed
455
	if (chgs) {
456
457
		changeset_t *chg, *nxt;
		WALK_LIST_DELSAFE(chg, nxt, *chgs) {
458
			rem_node(&chg->n);
459
			changeset_free(chg);
460
		}
461
		init_list(chgs);
462
	}
463
464
}

Jan Kadlec's avatar
Jan Kadlec committed
465
void changeset_clear(changeset_t *ch)
466
{
Jan Kadlec's avatar
Jan Kadlec committed
467
468
	if (ch == NULL) {
		return;
469
	}
470

Jan Kadlec's avatar
Jan Kadlec committed
471
472
473
	// Delete RRSets in lists, in case there are any left
	zone_contents_deep_free(&ch->add);
	zone_contents_deep_free(&ch->remove);
474

Jan Kadlec's avatar
Jan Kadlec committed
475
476
	knot_rrset_free(&ch->soa_from, NULL);
	knot_rrset_free(&ch->soa_to, NULL);
477

Jan Kadlec's avatar
Jan Kadlec committed
478
479
480
	// Delete binary data
	free(ch->data);
}
481

Jan Kadlec's avatar
Jan Kadlec committed
482
483
484
485
void changeset_free(changeset_t *ch)
{
	changeset_clear(ch);
	free(ch);
486
487
}

488
int changeset_iter_add(changeset_iter_t *itt, const changeset_t *ch)
489
{
490
	return changeset_iter_init(itt, 2, ch->add->nodes, ch->add->nsec3_nodes);
491
492
}

493
int changeset_iter_rem(changeset_iter_t *itt, const changeset_t *ch)
494
{
495
	return changeset_iter_init(itt, 2, ch->remove->nodes, ch->remove->nsec3_nodes);
496
497
}

498
int changeset_iter_all(changeset_iter_t *itt, const changeset_t *ch)
499
{
500
	return changeset_iter_init(itt, 4, ch->add->nodes, ch->add->nsec3_nodes,
501
	                           ch->remove->nodes, ch->remove->nsec3_nodes);
502
503
}

504
505
knot_rrset_t changeset_iter_next(changeset_iter_t *it)
{
506
	assert(it);
507
508
509
510
	ptrnode_t *n = NULL;
	knot_rrset_t rr;
	knot_rrset_init_empty(&rr);
	WALK_LIST(n, it->iters) {
511
512
		trie_it_t *t_it = (trie_it_t *)n->d;
		if (trie_it_finished(t_it)) {
513
514
515
516
517
518
519
520
			continue;
		}

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

523
	return rr;
524
525
}

526
void changeset_iter_clear(changeset_iter_t *it)
527
{
Jan Kadlec's avatar
Jan Kadlec committed
528
	if (it) {
529
		cleanup_iter_list(&it->iters);
530
531
		it->node = NULL;
		it->node_pos = 0;
Jan Kadlec's avatar
Jan Kadlec committed
532
	}
533
}