1238106Sdes/* 2238106Sdes * util/data/dname.h - domain name routines 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33238106Sdes * POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * This file contains functions to deal with domain names (dnames). 40238106Sdes * 41238106Sdes * Some of the functions deal with domain names as a wireformat buffer, 42238106Sdes * with a length. 43238106Sdes */ 44238106Sdes 45238106Sdes#ifndef UTIL_DATA_DNAME_H 46238106Sdes#define UTIL_DATA_DNAME_H 47238106Sdes#include "util/storage/lruhash.h" 48238106Sdes 49238106Sdes/** max number of compression ptrs to follow */ 50238106Sdes#define MAX_COMPRESS_PTRS 256 51238106Sdes 52238106Sdes/** 53238106Sdes * Determine length of dname in buffer, no compression ptrs allowed, 54238106Sdes * @param query: the ldns buffer, current position at start of dname. 55238106Sdes * at end, position is at end of the dname. 56238106Sdes * @return: 0 on parse failure, or length including ending 0 of dname. 57238106Sdes */ 58238106Sdessize_t query_dname_len(ldns_buffer* query); 59238106Sdes 60238106Sdes/** 61238106Sdes * Determine if dname in memory is correct. no compression ptrs allowed. 62238106Sdes * @param dname: where dname starts in memory. 63238106Sdes * @param len: dname is not allowed to exceed this length (i.e. of allocation). 64238106Sdes * @return length of dname if dname is ok, 0 on a parse error. 65238106Sdes */ 66238106Sdessize_t dname_valid(uint8_t* dname, size_t len); 67238106Sdes 68238106Sdes/** lowercase query dname */ 69238106Sdesvoid query_dname_tolower(uint8_t* dname); 70238106Sdes 71238106Sdes/** 72238106Sdes * lowercase pkt dname (follows compression pointers) 73238106Sdes * @param pkt: the packet, used to follow compression pointers. Position 74238106Sdes * is unchanged. 75238106Sdes * @param dname: start of dname in packet. 76238106Sdes */ 77238106Sdesvoid pkt_dname_tolower(ldns_buffer* pkt, uint8_t* dname); 78238106Sdes 79238106Sdes/** 80238106Sdes * Compare query dnames (uncompressed storage). The Dnames passed do not 81238106Sdes * have to be lowercased, comparison routine does this. 82238106Sdes * 83238106Sdes * This routine is special, in that the comparison that it does corresponds 84238106Sdes * with the canonical comparison needed when comparing dnames inside rdata 85238106Sdes * for RR types that need canonicalization. That means that the first byte 86238106Sdes * that is smaller (possibly after lowercasing) makes an RR smaller, or the 87238106Sdes * shortest name makes an RR smaller. 88238106Sdes * 89238106Sdes * This routine does not compute the canonical order needed for NSEC 90238106Sdes * processing. 91238106Sdes * 92238106Sdes * Dnames have to be valid format. 93238106Sdes * @param d1: dname to compare 94238106Sdes * @param d2: dname to compare 95238106Sdes * @return: -1, 0, or +1 depending on comparison results. 96238106Sdes * Sort order is first difference found. not the canonical ordering. 97238106Sdes */ 98238106Sdesint query_dname_compare(uint8_t* d1, uint8_t* d2); 99238106Sdes 100238106Sdes/** 101238106Sdes * Determine correct, compressed, dname present in packet. 102238106Sdes * Checks for parse errors. 103238106Sdes * @param pkt: packet to read from (from current start position). 104238106Sdes * @return: 0 on parse error. 105238106Sdes * At exit the position is right after the (compressed) dname. 106238106Sdes * Compression pointers are followed and checked for loops. 107238106Sdes * The uncompressed wireformat length is returned. 108238106Sdes */ 109238106Sdessize_t pkt_dname_len(ldns_buffer* pkt); 110238106Sdes 111238106Sdes/** 112238106Sdes * Compare dnames in packet (compressed). Dnames must be valid. 113238106Sdes * routine performs lowercasing, so the packet casing is preserved. 114238106Sdes * @param pkt: packet, used to resolve compression pointers. 115238106Sdes * @param d1: dname to compare 116238106Sdes * @param d2: dname to compare 117238106Sdes * @return: -1, 0, or +1 depending on comparison results. 118238106Sdes * Sort order is first difference found. not the canonical ordering. 119238106Sdes */ 120238106Sdesint dname_pkt_compare(ldns_buffer* pkt, uint8_t* d1, uint8_t* d2); 121238106Sdes 122238106Sdes/** 123238106Sdes * Hash dname, label by label, lowercasing, into hashvalue. 124238106Sdes * Dname in query format (not compressed). 125238106Sdes * @param dname: dname to hash. 126238106Sdes * @param h: initial hash value. 127238106Sdes * @return: result hash value. 128238106Sdes */ 129238106Sdeshashvalue_t dname_query_hash(uint8_t* dname, hashvalue_t h); 130238106Sdes 131238106Sdes/** 132238106Sdes * Hash dname, label by label, lowercasing, into hashvalue. 133238106Sdes * Dname in pkt format (compressed). 134238106Sdes * @param pkt: packet, for resolving compression pointers. 135238106Sdes * @param dname: dname to hash, pointer to the pkt buffer. 136238106Sdes * Must be valid format. No loops, etc. 137238106Sdes * @param h: initial hash value. 138238106Sdes * @return: result hash value. 139238106Sdes * Result is the same as dname_query_hash, even if compression is used. 140238106Sdes */ 141238106Sdeshashvalue_t dname_pkt_hash(ldns_buffer* pkt, uint8_t* dname, hashvalue_t h); 142238106Sdes 143238106Sdes/** 144238106Sdes * Copy over a valid dname and decompress it. 145238106Sdes * @param pkt: packet to resolve compression pointers. 146238106Sdes * @param to: buffer of size from pkt_len function to hold result. 147238106Sdes * @param dname: pointer into packet where dname starts. 148238106Sdes */ 149238106Sdesvoid dname_pkt_copy(ldns_buffer* pkt, uint8_t* to, uint8_t* dname); 150238106Sdes 151238106Sdes/** 152238106Sdes * Copy over a valid dname to a packet. 153238106Sdes * @param pkt: packet to copy to. 154238106Sdes * @param dname: dname to copy. 155238106Sdes * @return: 0 if not enough space in buffer. 156238106Sdes */ 157238106Sdesint dname_buffer_write(ldns_buffer* pkt, uint8_t* dname); 158238106Sdes 159238106Sdes/** 160238106Sdes * Count the number of labels in an uncompressed dname in memory. 161238106Sdes * @param dname: pointer to uncompressed dname. 162238106Sdes * @return: count of labels, including root label, "com." has 2 labels. 163238106Sdes */ 164238106Sdesint dname_count_labels(uint8_t* dname); 165238106Sdes 166238106Sdes/** 167238106Sdes * Count labels and dname length both, for uncompressed dname in memory. 168238106Sdes * @param dname: pointer to uncompressed dname. 169238106Sdes * @param size: length of dname, including root label. 170238106Sdes * @return: count of labels, including root label, "com." has 2 labels. 171238106Sdes */ 172238106Sdesint dname_count_size_labels(uint8_t* dname, size_t* size); 173238106Sdes 174238106Sdes/** 175238106Sdes * Compare dnames, sorted not canonical, but by label. 176238106Sdes * Such that zone contents follows zone apex. 177238106Sdes * @param d1: first dname. pointer to uncompressed wireformat. 178238106Sdes * @param labs1: number of labels in first dname. 179238106Sdes * @param d2: second dname. pointer to uncompressed wireformat. 180238106Sdes * @param labs2: number of labels in second dname. 181238106Sdes * @param mlabs: number of labels that matched exactly (the shared topdomain). 182238106Sdes * @return: 0 for equal, -1 smaller, or +1 d1 larger than d2. 183238106Sdes */ 184238106Sdesint dname_lab_cmp(uint8_t* d1, int labs1, uint8_t* d2, int labs2, int* mlabs); 185238106Sdes 186238106Sdes/** 187238106Sdes * See if domain name d1 is a strict subdomain of d2. 188238106Sdes * That is a subdomain, but not equal. 189238106Sdes * @param d1: domain name, uncompressed wireformat 190238106Sdes * @param labs1: number of labels in d1, including root label. 191238106Sdes * @param d2: domain name, uncompressed wireformat 192238106Sdes * @param labs2: number of labels in d2, including root label. 193238106Sdes * @return true if d1 is a subdomain of d2, but not equal to d2. 194238106Sdes */ 195238106Sdesint dname_strict_subdomain(uint8_t* d1, int labs1, uint8_t* d2, int labs2); 196238106Sdes 197238106Sdes/** 198238106Sdes * Like dname_strict_subdomain but counts labels 199238106Sdes * @param d1: domain name, uncompressed wireformat 200238106Sdes * @param d2: domain name, uncompressed wireformat 201238106Sdes * @return true if d1 is a subdomain of d2, but not equal to d2. 202238106Sdes */ 203238106Sdesint dname_strict_subdomain_c(uint8_t* d1, uint8_t* d2); 204238106Sdes 205238106Sdes/** 206238106Sdes * Counts labels. Tests is d1 is a subdomain of d2. 207238106Sdes * @param d1: domain name, uncompressed wireformat 208238106Sdes * @param d2: domain name, uncompressed wireformat 209238106Sdes * @return true if d1 is a subdomain of d2. 210238106Sdes */ 211238106Sdesint dname_subdomain_c(uint8_t* d1, uint8_t* d2); 212238106Sdes 213238106Sdes/** 214238106Sdes * Debug helper. Print wireformat dname to output. 215238106Sdes * @param out: like stdout or a file. 216238106Sdes * @param pkt: if not NULL, the packet for resolving compression ptrs. 217238106Sdes * @param dname: pointer to (start of) dname. 218238106Sdes */ 219238106Sdesvoid dname_print(FILE* out, ldns_buffer* pkt, uint8_t* dname); 220238106Sdes 221238106Sdes/** 222238106Sdes * Debug helper. Print dname to given string buffer (string buffer must 223238106Sdes * be at least 255 chars + 1 for the 0, in printable form. 224238106Sdes * This may lose information (? for nonprintable characters, or & if 225238106Sdes * the name is too long, # for a bad label length). 226238106Sdes * @param dname: uncompressed wireformat. 227238106Sdes * @param str: buffer of 255+1 length. 228238106Sdes */ 229238106Sdesvoid dname_str(uint8_t* dname, char* str); 230238106Sdes 231238106Sdes/** 232238106Sdes * Returns true if the uncompressed wireformat dname is the root "." 233238106Sdes * @param dname: the dname to check 234238106Sdes * @return true if ".", false if not. 235238106Sdes */ 236238106Sdesint dname_is_root(uint8_t* dname); 237238106Sdes 238238106Sdes/** 239238106Sdes * Snip off first label from a dname, returning the parent zone. 240238106Sdes * @param dname: from what to strip off. uncompressed wireformat. 241238106Sdes * @param len: length, adjusted to become less. 242238106Sdes * @return stripped off, or "." if input was ".". 243238106Sdes */ 244238106Sdesvoid dname_remove_label(uint8_t** dname, size_t* len); 245238106Sdes 246238106Sdes/** 247238106Sdes * Snip off first N labels from a dname, returning the parent zone. 248238106Sdes * @param dname: from what to strip off. uncompressed wireformat. 249238106Sdes * @param len: length, adjusted to become less. 250238106Sdes * @param n: number of labels to strip off (from the left). 251238106Sdes * if 0, nothing happens. 252238106Sdes * @return stripped off, or "." if input was ".". 253238106Sdes */ 254238106Sdesvoid dname_remove_labels(uint8_t** dname, size_t* len, int n); 255238106Sdes 256238106Sdes/** 257238106Sdes * Count labels for the RRSIG signature label field. 258238106Sdes * Like a normal labelcount, but "*" wildcard and "." root are not counted. 259238106Sdes * @param dname: valid uncompressed wireformat. 260238106Sdes * @return number of labels like in RRSIG; '*' and '.' are not counted. 261238106Sdes */ 262238106Sdesint dname_signame_label_count(uint8_t* dname); 263238106Sdes 264238106Sdes/** 265238106Sdes * Return true if the label is a wildcard, *.example.com. 266238106Sdes * @param dname: valid uncompressed wireformat. 267238106Sdes * @return true if wildcard, or false. 268238106Sdes */ 269238106Sdesint dname_is_wild(uint8_t* dname); 270238106Sdes 271238106Sdes/** 272238106Sdes * Compare dnames, Canonical in rfc4034 sense, but by label. 273238106Sdes * Such that zone contents follows zone apex. 274238106Sdes * 275238106Sdes * @param d1: first dname. pointer to uncompressed wireformat. 276238106Sdes * @param labs1: number of labels in first dname. 277238106Sdes * @param d2: second dname. pointer to uncompressed wireformat. 278238106Sdes * @param labs2: number of labels in second dname. 279238106Sdes * @param mlabs: number of labels that matched exactly (the shared topdomain). 280238106Sdes * @return: 0 for equal, -1 smaller, or +1 d1 larger than d2. 281238106Sdes */ 282238106Sdesint dname_canon_lab_cmp(uint8_t* d1, int labs1, uint8_t* d2, int labs2, 283238106Sdes int* mlabs); 284238106Sdes 285238106Sdes/** 286238106Sdes * Canonical dname compare. Takes care of counting labels. 287238106Sdes * Per rfc 4034 canonical order. 288238106Sdes * 289238106Sdes * @param d1: first dname. pointer to uncompressed wireformat. 290238106Sdes * @param d2: second dname. pointer to uncompressed wireformat. 291238106Sdes * @return: 0 for equal, -1 smaller, or +1 d1 larger than d2. 292238106Sdes */ 293238106Sdesint dname_canonical_compare(uint8_t* d1, uint8_t* d2); 294238106Sdes 295238106Sdes/** 296238106Sdes * Get the shared topdomain between two names. Root "." or longer. 297238106Sdes * @param d1: first dname. pointer to uncompressed wireformat. 298238106Sdes * @param d2: second dname. pointer to uncompressed wireformat. 299238106Sdes * @return pointer to shared topdomain. Ptr to a part of d1. 300238106Sdes */ 301238106Sdesuint8_t* dname_get_shared_topdomain(uint8_t* d1, uint8_t* d2); 302238106Sdes 303238106Sdes#endif /* UTIL_DATA_DNAME_H */ 304