/usr/include/libknot/dname.h is in libknot-dev 2.6.5-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* Copyright (C) 2017 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 libknot
* @{
*/
#pragma once
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "libknot/attribute.h"
#include "libknot/mm_ctx.h"
/*! \brief Type representing a domain name in wire format. */
typedef uint8_t knot_dname_t;
/*!
* \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_EMALF
* \retval KNOT_ESPACE
*/
_pure_ _mustcheck_
int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
const uint8_t *pkt);
/*!
* \brief Parse dname from wire.
*
* \param pkt Message in wire format.
* \param pos Position of the domain name on wire.
* \param maxpos Domain name length.
* \param mm Memory context.
*
* \return parsed domain name or NULL.
*/
_mustcheck_
knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos,
knot_mm_t *mm);
/*!
* \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 dname.
*/
_mustcheck_
knot_dname_t *knot_dname_copy(const knot_dname_t *name, knot_mm_t *mm);
/*!
* \brief Duplicates part of the given domain name.
*
* \param name Domain name to be copied.
* \param len Part length.
* \param mm Memory context.
*
* \return New domain name which is an partial copy of \a dname.
*/
_mustcheck_
knot_dname_t *knot_dname_copy_part(const knot_dname_t *name, unsigned len,
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 number of bytes written
*/
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.
*
* \note Name must not be compressed.
*
* \param name Domain name to be converted.
*
* \return KNOT_EOK
* \retval KNOT_EINVAL
*/
int 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 KNOT_EINVAL
*/
_pure_
int knot_dname_size(const knot_dname_t *name);
/*!
* \brief Returns wire 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 KNOT_EINVAL
*/
_pure_
int knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt);
/*!
* \brief Checks if one domain name is a subdomain of other.
*
* \param sub Domain name to be the possible subdomain.
* \param domain Domain name to be the possible parent domain.
*
* \retval true \a sub is a subdomain of \a domain.
* \retval false otherwise.
*/
_pure_
bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain);
/*!
* \brief Check if the domain name is a subdomain of or equal to other.
*
* \param domain Domain name to be the possible parent domain.
* \param sub Domain name to be the possible subdomain.
*
* \retval true \a sub us a subdomain or equal to \a domain.
* \retval false otherwise.
*/
_pure_
bool knot_dname_in(const knot_dname_t *domain, const knot_dname_t *sub);
/*!
* \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.
*/
_pure_
bool knot_dname_is_wildcard(const knot_dname_t *name);
/*!
* \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_
int 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.
*
* \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);
/*!
* \brief Destroys the given domain name.
*
* Frees also the data within the struct. This is somewhat different behaviour
* than that of RDATA and RRSet structures which do not deallocate their
* contents.
*
* Sets the given pointer to NULL.
*
* \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 insensitive).
*
* \warning Since it would be hard to catch errors, because negative value
* is also a good result, there are assertions that expect neither
* d1 or d2 to be NULL.
*
* \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).
*
* \warning d1 and d2 must not be NULL.
*
* \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 Compares two domain name labels (case insensitive).
*
* \warning label1 and label2 must not be NULL.
*
* \param label1 First label.
* \param label2 Second label.
*
* \retval true if the labels are identical
* \retval false if the labels are NOT identical
*/
_pure_
bool knot_dname_label_is_equal(const uint8_t *label1, const uint8_t *label2);
/*!
* \brief Concatenates two domain names.
*
* \param d1 First domain name (will be modified).
* \param d2 Second domain name (will not be modified).
*
* \return The concatenated domain name or NULL
*/
knot_dname_t *knot_dname_cat(knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Cound length of the N first labels.
*
* \param name Domain name.
* \param nlabels N first labels.
* \param pkt Related packet (or NULL if not compressed).
*
* \retval length of the prefix
*/
_pure_
int 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).
*/
_pure_
int knot_dname_labels(const uint8_t *name, const uint8_t *pkt);
/*!
* \brief Align name end-to-end and return number of common suffix labels.
*
* \param d1 Domain name.
* \param d1_labels Number of labels in d1.
* \param d2 Domain name.
* \param d2_labels Number of labels in d2.
* \param wire Packet wire related to names (or NULL).
*/
int knot_dname_align(const uint8_t **d1, uint8_t d1_labels,
const uint8_t **d2, uint8_t d2_labels,
uint8_t *wire);
/*!
* \brief Convert domain name from wire to 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).
*
* Example:
* Name: lake.example.com. Wire: \\x04lake\\x07example\\x03com\\x00
* Lookup format \\x11com\\x00example\\x00lake\\x00
*
* Maximum length of such a domain name is KNOT_DNAME_MAXLEN characters.
*
* \param dst Memory to store converted name into. dst[0] will contain the length.
* \param src Source domain name.
* \param pkt Source name packet (NULL if not any).
*
* \retval KNOT_EOK if successful
* \retval KNOT_EINVAL on invalid parameters
*/
int knot_dname_lf(uint8_t *dst, const knot_dname_t *src, const uint8_t *pkt);
/*! @} */
|