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
|
/* 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_compare(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)
{
// 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));
return knot_changesets_check_size(*changesets);
}
/*----------------------------------------------------------------------------*/
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,
xfrin_changeset_part_t part)
{
knot_rrset_t ***rrsets = NULL;
size_t *count = NULL;
size_t *allocated = NULL;
switch (part) {
case XFRIN_CHANGESET_ADD:
rrsets = &changeset->add;
count = &changeset->add_count;
allocated = &changeset->add_allocated;
break;
case XFRIN_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;
}
/*----------------------------------------------------------------------------*/
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,
xfrin_changeset_part_t part)
{
switch (part) {
case XFRIN_CHANGESET_ADD:
knot_changeset_store_soa(&changeset->soa_to,
&changeset->serial_to, soa);
break;
case XFRIN_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 old memory block and copy old data. */
memset(sets, 0, new_count * item_len);
memcpy(sets, changesets->sets, changesets->allocated * item_len);
/* Replace old changesets. */
free(changesets->sets);
changesets->sets = sets;
changesets->allocated = new_count;
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
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;
}
|