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
|
/* 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/>.
*/
/*!
* \file nsec-bitmap.h
*
* \author Jan Vcelak <jan.vcelak@nic.cz>
*
* \brief RR bitmap used in NSEC/NSEC3 records (RFC 4034).
*
* \addtogroup dnssec
* @{
*/
#ifndef _KNOT_DNSSEC_ZONE_NSEC_BITMAP_H_
#define _KNOT_DNSSEC_ZONE_NSEC_BITMAP_H_
#include <stdint.h>
#include <string.h>
#include "libknot/zone/node.h"
#include "libknot/rrset.h"
#include "common/descriptor.h"
#define BITMAP_WINDOW_SIZE 256
#define BITMAP_WINDOW_BYTES (BITMAP_WINDOW_SIZE/CHAR_BIT)
#define BITMAP_WINDOW_COUNT 256
/*!
* \brief One window of a bitmap.
*/
typedef struct {
uint8_t used;
uint8_t data[BITMAP_WINDOW_BYTES];
} bitmap_window_t;
/*!
* \brief Bitmap of RR types.
*/
typedef struct {
int used;
bitmap_window_t windows[BITMAP_WINDOW_COUNT];
} bitmap_t;
/*!
* \brief Add one RR type into the bitmap.
*/
inline static void bitmap_add_type(bitmap_t *bitmap, uint16_t type)
{
int win = type / BITMAP_WINDOW_SIZE;
int bit = type % BITMAP_WINDOW_SIZE;
if (bitmap->used <= win) {
bitmap->used = win + 1;
}
int win_byte = bit / CHAR_BIT;
int win_bit = bit % CHAR_BIT;
bitmap_window_t *window = &bitmap->windows[win];
window->data[win_byte] |= 0x80 >> win_bit;
if (window->used <= win_byte) {
window->used = win_byte + 1;
}
}
/*!
* \brief Add all RR types from a node into the bitmap.
*/
inline static void bitmap_add_node_rrsets(bitmap_t *bitmap,
const knot_node_t *node)
{
const knot_rrset_t **node_rrsets = knot_node_rrsets_no_copy(node);
for (int i = 0; i < node->rrset_count; i++) {
const knot_rrset_t *rr = node_rrsets[i];
if (rr->type != KNOT_RRTYPE_NSEC && rr->rdata_count > 0) {
bitmap_add_type(bitmap, node_rrsets[i]->type);
}
}
}
/*!
* \brief Compute the size of the bitmap in NSEC RDATA format.
*/
inline static size_t bitmap_size(const bitmap_t *bitmap)
{
size_t result = 0;
for (int i = 0; i < bitmap->used; i++) {
int used = bitmap->windows[i].used;
if (used == 0) {
continue;
}
result += 2 + used; // windows number, window size, data
}
return result;
}
/*!
* \brief Write bitmap in NSEC RDATA format.
*/
inline static void bitmap_write(const bitmap_t *bitmap, uint8_t *output)
{
uint8_t *write_ptr = output;
for (int win = 0; win < bitmap->used; win++) {
int used = bitmap->windows[win].used;
if (used == 0) {
continue;
}
*write_ptr = (uint8_t)win;
write_ptr += 1;
*write_ptr = (uint8_t)used;
write_ptr += 1;
memcpy(write_ptr, bitmap->windows[win].data, used);
write_ptr += used;
}
}
#endif // _KNOT_DNSSEC_ZONE_NSEC_BITMAP_H_
/*! @} */
|