summaryrefslogtreecommitdiffstats
path: root/src/knot/zone/zonedb.c
blob: 98cade558de218b4427a2fbec299b682f0e03599 (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
/*  Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
 */

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

#include "knot/journal/journal_metadata.h"
#include "knot/zone/zonedb.h"
#include "libknot/packet/wire.h"
#include "contrib/mempattern.h"
#include "contrib/ucw/mempool.h"

/*! \brief Discard zone in zone database. */
static void discard_zone(zone_t *zone, bool abort_txn)
{
	// Don't flush if removed zone (no previous configuration available).
	if (conf_rawid_exists(conf(), C_ZONE, zone->name, knot_dname_size(zone->name)) ||
	    catalog_has_member(conf()->catalog, zone->name)) {
		uint32_t journal_serial, zone_serial = zone_contents_serial(zone->contents);
		bool exists;

		// Flush if bootstrapped or if the journal doesn't exist.
		if (!zone->zonefile.exists || journal_info(
			zone_journal(zone), &exists, NULL, NULL, &journal_serial, NULL, NULL, NULL, NULL
		    ) != KNOT_EOK || !exists || journal_serial != zone_serial) {
			zone_flush_journal(conf(), zone, false);
		}
	}

	if (abort_txn) {
		zone_control_clear(zone);
	}
	zone_free(&zone);
}

knot_zonedb_t *knot_zonedb_new(void)
{
	knot_zonedb_t *db = calloc(1, sizeof(knot_zonedb_t));
	if (db == NULL) {
		return NULL;
	}

	mm_ctx_mempool(&db->mm, MM_DEFAULT_BLKSIZE);

	db->trie = trie_create(&db->mm);
	if (db->trie == NULL) {
		mp_delete(db->mm.ctx);
		free(db);
		return NULL;
	}

	return db;
}

int knot_zonedb_insert(knot_zonedb_t *db, zone_t *zone)
{
	if (db == NULL || zone == NULL) {
		return KNOT_EINVAL;
	}

	assert(zone->name);
	knot_dname_storage_t lf_storage;
	uint8_t *lf = knot_dname_lf(zone->name, lf_storage);
	assert(lf);

	*trie_get_ins(db->trie, lf + 1, *lf) = zone;

	return KNOT_EOK;
}

int knot_zonedb_del(knot_zonedb_t *db, const knot_dname_t *zone_name)
{
	if (db == NULL || zone_name == NULL) {
		return KNOT_EINVAL;
	}

	knot_dname_storage_t lf_storage;
	uint8_t *lf = knot_dname_lf(zone_name, lf_storage);
	assert(lf);

	trie_val_t *rval = trie_get_try(db->trie, lf + 1, *lf);
	if (rval == NULL) {
		return KNOT_ENOENT;
	}

	return trie_del(db->trie, lf + 1, *lf, NULL);
}

zone_t *knot_zonedb_find(knot_zonedb_t *db, const knot_dname_t *zone_name)
{
	if (db == NULL) {
		return NULL;
	}

	knot_dname_storage_t lf_storage;
	uint8_t *lf = knot_dname_lf(zone_name, lf_storage);
	assert(lf);

	trie_val_t *val = trie_get_try(db->trie, lf + 1, *lf);
	if (val == NULL) {
		return NULL;
	}

	return *val;
}

zone_t **knot_zonedb_find_ptr(knot_zonedb_t *db, const knot_dname_t *zone_name)
{
	if (db == NULL) {
		return NULL;
	}

	knot_dname_storage_t lf_storage;
	uint8_t *lf = knot_dname_lf(zone_name, lf_storage);
	assert(lf);

	trie_val_t *val = trie_get_try(db->trie, lf + 1, *lf);
	if (val == NULL) {
		return NULL;
	}

	return (zone_t **)val;
}

zone_t *knot_zonedb_find_suffix(knot_zonedb_t *db, const knot_dname_t *zone_name)
{
	if (db == NULL || zone_name == NULL) {
		return NULL;
	}

	while (true) {
		knot_dname_storage_t lf_storage;
		uint8_t *lf = knot_dname_lf(zone_name, lf_storage);
		assert(lf);

		trie_val_t *val = trie_get_try(db->trie, lf + 1, *lf);
		if (val != NULL) {
			return *val;
		} else if (zone_name[0] == 0) {
			return NULL;
		}

		zone_name = knot_wire_next_label(zone_name, NULL);
	}
}

size_t knot_zonedb_size(const knot_zonedb_t *db)
{
	if (db == NULL) {
		return 0;
	}

	return trie_weight(db->trie);
}

void knot_zonedb_free(knot_zonedb_t **db)
{
	if (db == NULL || *db == NULL) {
		return;
	}

	mp_delete((*db)->mm.ctx);
	free(*db);
	*db = NULL;
}

void knot_zonedb_deep_free(knot_zonedb_t **db, bool abort_txn)
{
	if (db == NULL || *db == NULL) {
		return;
	}

	knot_zonedb_foreach(*db, discard_zone, abort_txn);
	knot_zonedb_free(db);
}