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
|
/* Copyright (C) 2018 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
*
* \brief Domain name structure and API for manipulating it.
*
* \addtogroup dname
* @{
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "libknot/attribute.h"
#include "libknot/consts.h"
#include "libknot/mm_ctx.h"
/*! \brief Type representing a domain name in wire format. */
typedef uint8_t knot_dname_t;
/*! \brief Local domain name storage. */
typedef uint8_t knot_dname_storage_t[KNOT_DNAME_MAXLEN];
/*!
* \brief Check dname on the wire for constraints.
*
* If the name passes such checks, it is safe to be used in rest of the functions.
*
* \param name Name on the wire.
* \param endp Name boundary.
* \param pkt Wire.
*
* \retval (compressed) size of the domain name.
* \retval KNOT_EINVAL
* \retval KNOT_EMALF
*/
_pure_ _mustcheck_
int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
const uint8_t *pkt);
/*!
* \brief Duplicates the given domain name to a local storage.
*
* \param dst Destination storage.
* \param name Domain name to be copied.
*
* \retval size of the domain name.
* \retval 0 if invalid argument.
*/
_mustcheck_
size_t knot_dname_store(knot_dname_storage_t dst, const knot_dname_t *name);
/*!
* \brief Duplicates the given domain name.
*
* \param name Domain name to be copied.
* \param mm Memory context.
*
* \return New domain name which is an exact copy of \a name.
*/
_mustcheck_
knot_dname_t *knot_dname_copy(const knot_dname_t *name, knot_mm_t *mm);
/*!
* \brief Copy name to wire as is, no compression pointer expansion will be done.
*
* \param dst Destination wire.
* \param src Source name.
* \param maxlen Maximum wire length.
*
* \return the number of bytes written or negative error code
*/
int knot_dname_to_wire(uint8_t *dst, const knot_dname_t *src, size_t maxlen);
/*!
* \brief Write unpacked name (i.e. compression pointers expanded)
*
* \note The function is very similar to the knot_dname_to_wire(), except
* it expands compression pointers. E.g. you want to use knot_dname_unpack()
* if you copy a dname from incoming packet to some persistent storage.
* And you want to use knot_dname_to_wire() if you know the name is not
* compressed or you want to copy it 1:1.
*
* \param dst Destination wire.
* \param src Source name.
* \param maxlen Maximum destination wire size.
* \param pkt Name packet wire (for compression pointers).
*
* \return number of bytes written
*/
int knot_dname_unpack(uint8_t *dst, const knot_dname_t *src,
size_t maxlen, const uint8_t *pkt);
/*!
* \brief Converts the given domain name to its string representation.
*
* \note Output buffer is allocated automatically if dst is NULL.
*
* \param dst Output buffer.
* \param name Domain name to be converted.
* \param maxlen Output buffer length.
*
* \return 0-terminated string if successful, NULL if error.
*/
char *knot_dname_to_str(char *dst, const knot_dname_t *name, size_t maxlen);
/*!
* \brief This function is a shortcut for \ref knot_dname_to_str with
* no output buffer parameters.
*/
_mustcheck_
static inline char *knot_dname_to_str_alloc(const knot_dname_t *name)
{
return knot_dname_to_str(NULL, name, 0);
}
/*!
* \brief Creates a dname structure from domain name given in presentation
* format.
*
* \note The resulting FQDN is stored in the wire format.
* \note Output buffer is allocated automatically if dst is NULL.
*
* \param dst Output buffer.
* \param name Domain name in presentation format (labels separated by dots,
* '\0' terminated).
* \param maxlen Output buffer length.
*
* \return New dname if successful, NULL if error.
*/
knot_dname_t *knot_dname_from_str(uint8_t *dst, const char *name, size_t maxlen);
/*!
* \brief This function is a shortcut for \ref knot_dname_from_str with
* no output buffer parameters.
*/
_mustcheck_
static inline knot_dname_t *knot_dname_from_str_alloc(const char *name)
{
return knot_dname_from_str(NULL, name, 0);
}
/*!
* \brief Convert name to lowercase.
*
* \param name Domain name to be converted.
*/
void knot_dname_to_lower(knot_dname_t *name);
/*!
* \brief Returns size of the given domain name.
*
* \note If the domain name is compressed, the length of not compressed part
* is returned.
*
* \param name Domain name to get the size of.
*
* \retval size of the domain name.
* \retval 0 if invalid argument.
*/
_pure_
size_t knot_dname_size(const knot_dname_t *name);
/*!
* \brief Returns full size of the given domain name (expanded compression ptrs).
*
* \param name Domain name to get the size of.
* \param pkt Related packet (or NULL if unpacked)
*
* \retval size of the domain name.
* \retval 0 if invalid argument.
*/
_pure_
size_t knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt);
/*!
* \brief Checks if the domain name is a wildcard.
*
* \param name Domain name to check.
*
* \retval true if \a dname is a wildcard domain name.
* \retval false otherwise.
*/
static inline
bool knot_dname_is_wildcard(const knot_dname_t *name)
{
return name != NULL && name[0] == 1 && name[1] == '*';
}
/*!
* \brief Returns the number of labels common for the two domain names (counted
* from the rightmost label.
*
* \param d1 First domain name.
* \param d2 Second domain name.
*
* \return Number of labels common for the two domain names.
*/
_pure_
size_t knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Replaces the suffix of given size in one domain name with other domain
* name.
*
* \param name Domain name where to replace the suffix.
* \param labels Size of the suffix to be replaced.
* \param suffix New suffix to be used as a replacement.
* \param mm Memory context.
*
* \return New domain name created by replacing suffix of \a dname of size
* \a size with \a suffix.
*/
_mustcheck_
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name, unsigned labels,
const knot_dname_t *suffix, knot_mm_t *mm);
/*!
* \brief Destroys the given domain name.
*
* \param name Domain name to be destroyed.
* \param mm Memory context.
*/
void knot_dname_free(knot_dname_t *name, knot_mm_t *mm);
/*!
* \brief Compares two domain names by labels (case sensitive).
*
* \param d1 First domain name.
* \param d2 Second domain name.
*
* \retval < 0 if \a d1 goes before \a d2 in canonical order.
* \retval > 0 if \a d1 goes after \a d2 in canonical order.
* \retval 0 if the domain names are identical.
*/
_pure_
int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Compares two domain names (case sensitive).
*
* \param d1 First domain name.
* \param d2 Second domain name.
*
* \retval true if the domain names are identical
* \retval false if the domain names are NOT identical
*/
_pure_
bool knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Count length of the N first labels.
*
* \param name Domain name.
* \param nlabels First N labels.
* \param pkt Related packet (or NULL if not compressed).
*
* \return Length of the prefix.
*/
_pure_
size_t knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *pkt);
/*!
* \brief Return number of labels in the domain name.
*
* Terminal nullbyte is not counted.
*
* \param name Domain name.
* \param pkt Related packet (or NULL if not compressed).
*
* \return Number of labels.
*/
_pure_
size_t knot_dname_labels(const uint8_t *name, const uint8_t *pkt);
/*!
* \brief Convert domain name from wire to the lookup format.
*
* Formats names from rightmost label to the leftmost, separated by the lowest
* possible character (\\x00). Sorting such formatted names also gives
* correct canonical order (for NSEC/NSEC3). The first byte of the output
* contains length of the output.
*
* Examples:
* Name: lake.example.com.
* Wire: \\x04lake\\x07example\\x03com\\x00
* Lookup: \\x11com\\x00example\\x00lake\\x00
*
* Name: .
* Wire: \\x00
* Lookup: \\x00
*
* \param src Source domain name.
* \param storage Memory to store converted name into. Don't use directly!
*
* \retval Lookup format if successful (pointer into the storage).
* \retval NULL on invalid parameters.
*/
uint8_t *knot_dname_lf(const knot_dname_t *src, knot_dname_storage_t storage);
/*!
* \brief Check whether a domain name is under another one and how deep.
*
* \param name The longer name to check.
* \param bailiwick The shorter name to check.
*
* \retval >=0 a subdomain nested this many labels.
* \retval <0 not a subdomain (KNOT_EOUTOFZONE) or another error (KNOT_EINVAL).
*/
int knot_dname_in_bailiwick(const knot_dname_t *name, const knot_dname_t *bailiwick);
/*! @} */
|