summaryrefslogtreecommitdiff
path: root/src/libknot/updates/changesets.c
blob: 54057269fe513933b119611a6425c84e878a3119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
314
315
316
317
318
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

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

#include <string.h>
#include <stdlib.h>
#include <assert.h>

#include "updates/changesets.h"
#include "libknot/common.h"
#include "rrset.h"

static const size_t KNOT_CHANGESET_COUNT = 5;
static const size_t KNOT_CHANGESET_STEP = 5;
static const size_t KNOT_CHANGESET_RRSET_COUNT = 5;
static const size_t KNOT_CHANGESET_RRSET_STEP = 5;

/*----------------------------------------------------------------------------*/

static int knot_changeset_check_count(knot_rrset_t ***rrsets, size_t count,
                                        size_t *allocated)
{
	/* Check if allocated is sufficient. */
	if (count <= *allocated) {
		return KNOT_EOK;
	}

	/* How many steps is needed to content count? */
	size_t extra = (count - *allocated) % KNOT_CHANGESET_RRSET_STEP;
	extra = (extra + 1) * KNOT_CHANGESET_RRSET_STEP;

	/* Allocate new memory block. */
	const size_t item_len = sizeof(knot_rrset_t *);
	const size_t new_count = *allocated + extra;
	knot_rrset_t **rrsets_new = malloc(new_count * item_len);
	if (rrsets_new == NULL) {
		return KNOT_ENOMEM;
	}

	/* Clear old memory block and copy old data. */
	memset(rrsets_new, 0, new_count * item_len);
	memcpy(rrsets_new, *rrsets, (*allocated) * item_len);

	/* Replace old rrsets. */
	free(*rrsets);
	*rrsets = rrsets_new;
	*allocated = new_count;

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

static int knot_changeset_rrsets_match(const knot_rrset_t *rrset1,
                                         const knot_rrset_t *rrset2)
{
	return knot_rrset_match(rrset1, rrset2, KNOT_RRSET_COMPARE_HEADER)
	       && (knot_rrset_type(rrset1) != KNOT_RRTYPE_RRSIG
	           || knot_rdata_rrsig_type_covered(
	                    knot_rrset_rdata(rrset1))
	              == knot_rdata_rrsig_type_covered(
	                    knot_rrset_rdata(rrset2)));
}

/*----------------------------------------------------------------------------*/

int knot_changeset_allocate(knot_changesets_t **changesets,
                            uint32_t flags)
{
	// create new changesets
	*changesets = (knot_changesets_t *)(malloc(sizeof(knot_changesets_t)));
	if (*changesets == NULL) {
		return KNOT_ENOMEM;
	}

	memset(*changesets, 0, sizeof(knot_changesets_t));
	(*changesets)->flags = flags;

	if (knot_changesets_check_size(*changesets) != KNOT_EOK) {
		free(*changesets);
		*changesets = NULL;
		return KNOT_ENOMEM;
	}
	
	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

int knot_changeset_add_rrset(knot_rrset_t ***rrsets,
                              size_t *count, size_t *allocated,
                              knot_rrset_t *rrset)
{
	int ret = knot_changeset_check_count(rrsets, *count + 1, allocated);
	if (ret != KNOT_EOK) {
		return ret;
	}

	(*rrsets)[*count] = rrset;
	*count = *count + 1;

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

int knot_changeset_add_rr(knot_rrset_t ***rrsets, size_t *count,
                           size_t *allocated, knot_rrset_t *rr)
{
	// try to find the RRSet in the list of RRSets, but search backwards
	// as it is probable that the last RRSet is the one to which the RR
	// belongs

	// Just check the last RRSet. If the RR belongs to it, merge it,
	// otherwise just add the RR to the end of the list

	if (*count > 0
	    && knot_changeset_rrsets_match((*rrsets)[*count - 1], rr)) {
		// Create changesets exactly as they came, with possibly
		// duplicate records
		if (knot_rrset_merge((void **)&(*rrsets)[*count - 1],
		                     (void **)&rr) != KNOT_EOK) {
			return KNOT_ERROR;
		}

		knot_rrset_free(&rr);
		return KNOT_EOK;
	} else {
		return knot_changeset_add_rrset(rrsets, count, allocated, rr);
	}
}

/*----------------------------------------------------------------------------*/

int knot_changeset_add_new_rr(knot_changeset_t *changeset,
                               knot_rrset_t *rrset,
                               knot_changeset_part_t part)
{
	knot_rrset_t ***rrsets = NULL;
	size_t *count = NULL;
	size_t *allocated = NULL;

	switch (part) {
	case KNOT_CHANGESET_ADD:
		rrsets = &changeset->add;
		count = &changeset->add_count;
		allocated = &changeset->add_allocated;
		break;
	case KNOT_CHANGESET_REMOVE:
		rrsets = &changeset->remove;
		count = &changeset->remove_count;
		allocated = &changeset->remove_allocated;
		break;
	default:
		assert(0);
	}

	assert(rrsets != NULL);
	assert(count != NULL);
	assert(allocated != NULL);

	int ret = knot_changeset_add_rr(rrsets, count, allocated, rrset);
	if (ret != KNOT_EOK) {
		return ret;
	}

	return ret;
}

/*----------------------------------------------------------------------------*/

knot_rrset_t *knot_changeset_remove_rr(knot_rrset_t **rrsets, size_t *count,
                                       int pos)
{
	if (pos >= *count || *count == 0) {
		return NULL;
	}

	knot_rrset_t *removed = rrsets[pos];

	// shift all RRSets from pos+1 one cell to the left
	for (int i = pos; i < *count - 1; ++i) {
		rrsets[i] = rrsets[i + 1];
	}

	// just to be sure, set the last previously occupied position to NULL
	rrsets[*count - 1] = NULL;
	*count -= 1;

	return removed;
}

/*----------------------------------------------------------------------------*/

void knot_changeset_store_soa(knot_rrset_t **chg_soa,
                               uint32_t *chg_serial, knot_rrset_t *soa)
{
	*chg_soa = soa;
	*chg_serial = knot_rdata_soa_serial(knot_rrset_rdata(soa));
}

/*----------------------------------------------------------------------------*/

int knot_changeset_add_soa(knot_changeset_t *changeset, knot_rrset_t *soa,
                            knot_changeset_part_t part)
{
	switch (part) {
	case KNOT_CHANGESET_ADD:
		knot_changeset_store_soa(&changeset->soa_to,
		                          &changeset->serial_to, soa);
		break;
	case KNOT_CHANGESET_REMOVE:
		knot_changeset_store_soa(&changeset->soa_from,
		                          &changeset->serial_from, soa);
		break;
	default:
		assert(0);
	}

	/*! \todo Remove return value? */
	return KNOT_EOK;
}

/*---------------------------------------------------------------------------*/

int knot_changesets_check_size(knot_changesets_t *changesets)
{
	/* Check if allocated is sufficient. */
	if (changesets->count < changesets->allocated) {
		return KNOT_EOK;
	}

	/* How many steps is needed to content count? */
	size_t extra = (changesets->count - changesets->allocated)
	                % KNOT_CHANGESET_STEP;
	extra = (extra + 1) * KNOT_CHANGESET_STEP;

	/* Allocate new memory block. */
	const size_t item_len = sizeof(knot_changeset_t);
	size_t new_count = (changesets->allocated + extra);
	knot_changeset_t *sets = malloc(new_count * item_len);
	if (sets == NULL) {
		return KNOT_ENOMEM;
	}

	/* Clear new memory block and copy old data. */
	memset(sets, 0, new_count * item_len);
	memcpy(sets, changesets->sets, changesets->allocated * item_len);

	/* Set type to all newly allocated changesets. */
	for (int i = changesets->allocated; i < new_count; ++i) {
		sets[i].flags = changesets->flags;
	}

	/* Replace old changesets. */
	free(changesets->sets);
	changesets->sets = sets;
	changesets->allocated = new_count;

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

void knot_changeset_set_flags(knot_changeset_t *changeset,
                             uint32_t flags)
{
	changeset->flags = flags;
}

/*----------------------------------------------------------------------------*/

uint32_t knot_changeset_flags(knot_changeset_t *changeset)
{
	return changeset->flags;
}

/*----------------------------------------------------------------------------*/

int knot_changeset_is_empty(const knot_changeset_t *changeset)
{
	if (changeset == NULL) {
		return 0;
	}

	return (changeset->add_count == 0 && changeset->remove_count == 0);
}

/*----------------------------------------------------------------------------*/

void knot_free_changeset(knot_changeset_t **changeset)
{
	assert((*changeset)->add_allocated >= (*changeset)->add_count);
	assert((*changeset)->remove_allocated >= (*changeset)->remove_count);
	assert((*changeset)->allocated >= (*changeset)->size);

	int j;
	for (j = 0; j < (*changeset)->add_count; ++j) {
		knot_rrset_deep_free(&(*changeset)->add[j], 1, 1, 1);
	}
	free((*changeset)->add);

	for (j = 0; j < (*changeset)->remove_count; ++j) {
		knot_rrset_deep_free(&(*changeset)->remove[j], 1, 1, 1);
	}
	free((*changeset)->remove);

	knot_rrset_deep_free(&(*changeset)->soa_from, 1, 1, 1);
	knot_rrset_deep_free(&(*changeset)->soa_to, 1, 1, 1);

	free((*changeset)->data);


	*changeset = NULL;
}

/*----------------------------------------------------------------------------*/

void knot_free_changesets(knot_changesets_t **changesets)
{
	if (changesets == NULL || *changesets == NULL) {
		return;
	}

	assert((*changesets)->allocated >= (*changesets)->count);

	for (int i = 0; i < (*changesets)->count; ++i) {
		knot_changeset_t *ch = &(*changesets)->sets[i];
		knot_free_changeset(&ch);
	}

	free((*changesets)->sets);
	
	knot_rrset_deep_free(&(*changesets)->first_soa, 1, 1, 1);
	
	assert((*changesets)->changes == NULL);

	free(*changesets);
	*changesets = NULL;
}

/*----------------------------------------------------------------------------*/
/* knot_changes_t manipulation                                                */
/*----------------------------------------------------------------------------*/

int knot_changes_rrsets_reserve(knot_rrset_t ***rrsets,
                              int *count, int *allocated, int to_add)
{
	if (rrsets == NULL || count == NULL || allocated == NULL) {
		return KNOT_EINVAL;
	}
	
	if (*count + to_add <= *allocated) {
		return KNOT_EOK;
	}

	int new_count = (*allocated == 0) ? 2 : *allocated * 2;
	while (new_count < *count + to_add) {
		new_count *= 2;
	}

	/* Allocate new memory block. */
	knot_rrset_t **rrsets_new = malloc(new_count * sizeof(knot_rrset_t *));
	if (rrsets_new == NULL) {
		return KNOT_ENOMEM;
	}

	/* Initialize new memory and copy old data. */
	memset(rrsets_new, 0, new_count * sizeof(knot_rrset_t *));
	memcpy(rrsets_new, *rrsets, (*allocated) * sizeof(knot_rrset_t *));

	/* Free old nodes and switch pointers. */
	free(*rrsets);
	*rrsets = rrsets_new;
	*allocated = new_count;

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

int knot_changes_nodes_reserve(knot_node_t ***nodes,
                             int *count, int *allocated)
{
	if (nodes == NULL || count == NULL || allocated == NULL) {
		return KNOT_EINVAL;
	}
	
	if (*count + 2 <= *allocated) {
		return KNOT_EOK;
	}

	int new_count = (*allocated == 0) ? 2 : *allocated * 2;

	/* Allocate new memory block. */
	const size_t node_len = sizeof(knot_node_t *);
	knot_node_t **nodes_new = malloc(new_count * node_len);
	if (nodes_new == NULL) {
		return KNOT_ENOMEM;
	}

	/* Clear memory block and copy old data. */
	memset(nodes_new, 0, new_count * node_len);
	memcpy(nodes_new, *nodes, (*allocated) * node_len);

	/* Free old nodes and switch pointers. */
	free(*nodes);
	*nodes = nodes_new;
	*allocated = new_count;

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

int knot_changes_rdata_reserve(knot_rdata_t ***rdatas, uint16_t **types,
                             int count, int *allocated, int to_add)
{
	if (rdatas == NULL || types == NULL || allocated == NULL) {
		return KNOT_EINVAL;
	}
	
	if (count + to_add <= *allocated) {
		return KNOT_EOK;
	}

	int new_count = (*allocated == 0) ? 2 : *allocated * 2;
	while (new_count < count + to_add) {
		new_count *= 2;
	}

	/* Allocate new memory block. */
	knot_rdata_t **rdatas_new = malloc(new_count * sizeof(knot_rdata_t *));
	if (rdatas_new == NULL) {
		return KNOT_ENOMEM;
	}

	uint16_t *types_new = malloc(new_count * sizeof(uint));
	if (types_new == NULL) {
		free(rdatas_new);
		return KNOT_ENOMEM;
	}

	/* Initialize new memory and copy old data. */
	memset(rdatas_new, 0, new_count * sizeof(knot_rdata_t *));
	memcpy(rdatas_new, *rdatas, (*allocated) * sizeof(knot_rdata_t *));

	memset(types_new, 0, new_count * sizeof(uint));
	memcpy(types_new, *types, (*allocated) * sizeof(uint));

	/* Free old rdatas and switch pointers. */
	free(*rdatas);
	free(*types);
	*rdatas = rdatas_new;
	*types = types_new;
	*allocated = new_count;

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

void knot_changes_add_rdata(knot_rdata_t **rdatas, uint16_t *types,
                            int *count, knot_rdata_t *rdata, uint16_t type)
{
	if (rdatas == NULL || types == NULL || count == NULL || rdata == NULL) {
		return;
	}

	// Add all RDATAs from the chain!!

	knot_rdata_t *r = rdata;
	do {
//		dbg_xfrin_detail("Adding RDATA to RDATA list: %p\n", r);
		rdatas[*count] = r;
		types[*count] = type;
		++*count;

		r = r->next;
	} while (r != NULL && r != rdata);
}

/*----------------------------------------------------------------------------*/

int knot_changes_add_old_rrsets(knot_rrset_t **rrsets, int count,
                                knot_changes_t *changes, int add_rdata)
{
	if (rrsets == NULL || changes == NULL) {
		return KNOT_EINVAL;
	}

	if (count == 0) {
		return KNOT_EOK;
	}

	/* Reserve twice the space, to have enough space for RRSIGs if
	 * there are some.
	 */
	int ret = knot_changes_rrsets_reserve(&changes->old_rrsets,
	                                      &changes->old_rrsets_count,
	                                      &changes->old_rrsets_allocated,
	                                      2 * count);
	if (ret != KNOT_EOK) {
//		dbg_xfrin("Failed to reserve changes rrsets.\n");
		return ret;
	}

	/* Mark RRsets and RDATA for removal. */
	for (unsigned i = 0; i < count; ++i) {
		if (rrsets[i] == NULL) {
			continue;
		}
		
		knot_rrset_t *rrsigs = knot_rrset_get_rrsigs(rrsets[i]);
		
		if (add_rdata) {
			
			/* RDATA count in the RRSet. */
			int rdata_count = knot_rrset_rdata_rr_count(rrsets[i]);
			
			if (rrsigs != NULL) {
				/* Increment the RDATA count by the count of 
				 * RRSIGs. */
				rdata_count += knot_rrset_rdata_rr_count(
				                        rrsigs);
			}
	
			/* Remove old RDATA. */
			
			ret = knot_changes_rdata_reserve(&changes->old_rdata,
			                          &changes->old_rdata_types,
			                          changes->old_rdata_count,
			                          &changes->old_rdata_allocated,
			                          rdata_count);
			if (ret != KNOT_EOK) {
//				dbg_xfrin("Failed to reserve changes rdata.\n");
				return ret;
			}

			knot_changes_add_rdata(changes->old_rdata,
			                       changes->old_rdata_types,
			                       &changes->old_rdata_count,
			                       knot_rrset_get_rdata(rrsets[i]),
			                       knot_rrset_type(rrsets[i]));
			
			knot_changes_add_rdata(changes->old_rdata,
			                       changes->old_rdata_types,
			                       &changes->old_rdata_count,
			                       knot_rrset_get_rdata(rrsigs),
			                       KNOT_RRTYPE_RRSIG);
		}
		
		/* Disconnect RRsigs from rrset. */
		knot_rrset_set_rrsigs(rrsets[i], NULL);
		changes->old_rrsets[changes->old_rrsets_count++] = rrsets[i];
		changes->old_rrsets[changes->old_rrsets_count++] = rrsigs;
	}

	return KNOT_EOK;
}

/*----------------------------------------------------------------------------*/

int knot_changes_add_new_rrsets(knot_rrset_t **rrsets, int count,
                                knot_changes_t *changes, int add_rdata)
{
	if (rrsets == NULL || changes == NULL) {
		return KNOT_EINVAL;
	}

	if (count == 0) {
		return KNOT_EOK;
	}

	int ret = knot_changes_rrsets_reserve(&changes->new_rrsets,
	                                      &changes->new_rrsets_count,
	                                      &changes->new_rrsets_allocated,
	                                      count);
	if (ret != KNOT_EOK) {
		return ret;
	}

	/* Mark RRsets and RDATA for removal. */
	for (unsigned i = 0; i < count; ++i) {
		if (rrsets[i] == NULL) {
			continue;
		}
		
		if (add_rdata) {
			int rdata_count = knot_rrset_rdata_rr_count(rrsets[i]);
			ret = knot_changes_rdata_reserve(&changes->new_rdata,
			                          &changes->new_rdata_types,
			                          changes->new_rdata_count,
			                          &changes->new_rdata_allocated,
			                          rdata_count);
			if (ret != KNOT_EOK) {
				return ret;
			}
			
			knot_changes_add_rdata(changes->new_rdata,
			                       changes->new_rdata_types,
			                       &changes->new_rdata_count,
			                       knot_rrset_get_rdata(rrsets[i]),
			                       knot_rrset_type(rrsets[i]));
		}	
		
		changes->new_rrsets[changes->new_rrsets_count++] = rrsets[i];
	}

	return KNOT_EOK;
}