2 * util/data/dname.h - domain name handling
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains domain name handling functions.
44 #include "util/data/dname.h"
45 #include "util/data/msgparse.h"
47 #include "util/storage/lookup3.h"
48 #include "sldns/sbuffer.h"
50 /* determine length of a dname in buffer, no compression pointers allowed */
52 query_dname_len(sldns_buffer* query)
57 if(sldns_buffer_remaining(query) < 1)
58 return 0; /* parse error, need label len */
59 labellen = sldns_buffer_read_u8(query);
61 return 0; /* no compression allowed in queries */
63 if(len > LDNS_MAX_DOMAINLEN)
64 return 0; /* too long */
67 if(sldns_buffer_remaining(query) < labellen)
68 return 0; /* parse error, need content */
69 sldns_buffer_skip(query, (ssize_t)labellen);
74 dname_valid(uint8_t* dname, size_t maxlen)
81 return 0; /* no compression ptrs allowed */
83 if(len >= LDNS_MAX_DOMAINLEN)
84 return 0; /* too long */
86 return 0; /* does not fit in memory allocation */
92 return 0; /* does not fit in memory allocation */
96 /** compare uncompressed, noncanonical, registers are hints for speed */
98 query_dname_compare(register uint8_t* d1, register uint8_t* d2)
100 register uint8_t lab1, lab2;
101 log_assert(d1 && d2);
104 while( lab1 != 0 || lab2 != 0 ) {
105 /* compare label length */
106 /* if one dname ends, it has labellength 0 */
112 log_assert(lab1 == lab2 && lab1 != 0);
113 /* compare lowercased labels. */
115 /* compare bytes first for speed */
117 tolower((unsigned char)*d1) != tolower((unsigned char)*d2)) {
118 if(tolower((unsigned char)*d1) < tolower((unsigned char)*d2))
125 /* next pair of labels. */
133 query_dname_tolower(uint8_t* dname)
135 /* the dname is stored uncompressed */
141 *dname = (uint8_t)tolower((unsigned char)*dname);
149 pkt_dname_tolower(sldns_buffer* pkt, uint8_t* dname)
153 if(dname >= sldns_buffer_end(pkt))
157 if(LABEL_IS_PTR(lablen)) {
158 if((size_t)PTR_OFFSET(lablen, *dname)
159 >= sldns_buffer_limit(pkt))
161 dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
163 if(count++ > MAX_COMPRESS_PTRS)
167 if(dname+lablen >= sldns_buffer_end(pkt))
170 *dname = (uint8_t)tolower((unsigned char)*dname);
173 if(dname >= sldns_buffer_end(pkt))
181 pkt_dname_len(sldns_buffer* pkt)
188 /* read dname and determine length */
189 /* check compression pointers, loops, out of bounds */
191 /* read next label */
192 if(sldns_buffer_remaining(pkt) < 1)
194 labellen = sldns_buffer_read_u8(pkt);
195 if(LABEL_IS_PTR(labellen)) {
196 /* compression ptr */
198 if(sldns_buffer_remaining(pkt) < 1)
200 ptr = PTR_OFFSET(labellen, sldns_buffer_read_u8(pkt));
201 if(ptrcount++ > MAX_COMPRESS_PTRS)
202 return 0; /* loop! */
203 if(sldns_buffer_limit(pkt) <= ptr)
204 return 0; /* out of bounds! */
206 endpos = sldns_buffer_position(pkt);
207 sldns_buffer_set_position(pkt, ptr);
211 return 0; /* label too long */
213 if(len > LDNS_MAX_DOMAINLEN)
219 if(sldns_buffer_remaining(pkt) < labellen)
221 sldns_buffer_skip(pkt, (ssize_t)labellen);
225 sldns_buffer_set_position(pkt, endpos);
231 dname_pkt_compare(sldns_buffer* pkt, uint8_t* d1, uint8_t* d2)
234 log_assert(pkt && d1 && d2);
237 while( len1 != 0 || len2 != 0 ) {
239 if(LABEL_IS_PTR(len1)) {
240 d1 = sldns_buffer_at(pkt, PTR_OFFSET(len1, *d1));
244 if(LABEL_IS_PTR(len2)) {
245 d2 = sldns_buffer_at(pkt, PTR_OFFSET(len2, *d2));
249 /* check label length */
250 log_assert(len1 <= LDNS_MAX_LABELLEN);
251 log_assert(len2 <= LDNS_MAX_LABELLEN);
253 if(len1 < len2) return -1;
256 log_assert(len1 == len2 && len1 != 0);
259 if(tolower((unsigned char)*d1) != tolower((unsigned char)*d2)) {
260 if(tolower((unsigned char)*d1) < tolower((unsigned char)*d2))
274 dname_query_hash(uint8_t* dname, hashvalue_type h)
276 uint8_t labuf[LDNS_MAX_LABELLEN+1];
280 /* preserve case of query, make hash label by label */
283 log_assert(lablen <= LDNS_MAX_LABELLEN);
287 labuf[++i] = (uint8_t)tolower((unsigned char)*dname);
290 h = hashlittle(labuf, labuf[0] + 1, h);
298 dname_pkt_hash(sldns_buffer* pkt, uint8_t* dname, hashvalue_type h)
300 uint8_t labuf[LDNS_MAX_LABELLEN+1];
304 /* preserve case of query, make hash label by label */
307 if(LABEL_IS_PTR(lablen)) {
309 dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
313 log_assert(lablen <= LDNS_MAX_LABELLEN);
317 labuf[++i] = (uint8_t)tolower((unsigned char)*dname);
320 h = hashlittle(labuf, labuf[0] + 1, h);
327 void dname_pkt_copy(sldns_buffer* pkt, uint8_t* to, uint8_t* dname)
329 /* copy over the dname and decompress it at the same time */
334 if(LABEL_IS_PTR(lablen)) {
336 dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
340 log_assert(lablen <= LDNS_MAX_LABELLEN);
341 len += (size_t)lablen+1;
342 if(len >= LDNS_MAX_DOMAINLEN) {
343 *to = 0; /* end the result prematurely */
344 log_err("bad dname in dname_pkt_copy");
348 memmove(to, dname, lablen);
357 void dname_print(FILE* out, struct sldns_buffer* pkt, uint8_t* dname)
360 if(!out) out = stdout;
367 if(LABEL_IS_PTR(lablen)) {
370 fputs("??compressionptr??", out);
373 dname = sldns_buffer_at(pkt, PTR_OFFSET(lablen, *dname));
377 if(lablen > LDNS_MAX_LABELLEN) {
378 fputs("??extendedlabel??", out);
382 fputc((int)*dname++, out);
389 dname_count_labels(uint8_t* dname)
404 dname_count_size_labels(uint8_t* dname, size_t* size)
422 * Compare labels in memory, lowercase while comparing.
425 * @param len: number of bytes to compare.
426 * @return: 0, -1, +1 comparison result.
429 memlowercmp(uint8_t* p1, uint8_t* p2, uint8_t len)
432 if(*p1 != *p2 && tolower((unsigned char)*p1) != tolower((unsigned char)*p2)) {
433 if(tolower((unsigned char)*p1) < tolower((unsigned char)*p2))
444 dname_lab_cmp(uint8_t* d1, int labs1, uint8_t* d2, int labs2, int* mlabs)
450 /* first skip so that we compare same label. */
452 while(atlabel > labs2) {
457 log_assert(atlabel == labs2);
458 } else if(labs1 < labs2) {
460 while(atlabel > labs1) {
465 log_assert(atlabel == labs1);
467 lastmlabs = atlabel+1;
468 /* now at same label in d1 and d2, atlabel */
469 /* www.example.com. */
470 /* 4 3 2 1 atlabel number */
471 /* repeat until at root label (which is always the same) */
476 log_assert(len1 != 0 && len2 != 0);
484 /* memlowercmp is inlined here; or just like
485 * if((c=memlowercmp(d1, d2, len1)) != 0) {
487 * lastmlabs = atlabel; } apart from d1++,d2++ */
489 if(*d1 != *d2 && tolower((unsigned char)*d1)
490 != tolower((unsigned char)*d2)) {
491 if(tolower((unsigned char)*d1) <
492 tolower((unsigned char)*d2)) {
503 break; /* out of memlowercmp */
512 /* last difference atlabel number, so number of labels matching,
513 * at the right side, is one less. */
514 *mlabs = lastmlabs-1;
516 /* all labels compared were equal, check if one has more
517 * labels, so that example.com. > com. */
520 else if(labs1 < labs2)
527 dname_lab_startswith(uint8_t* label, char* prefix, char** endptr)
529 size_t plen = strlen(prefix);
530 size_t orig_plen = plen;
531 size_t lablen = (size_t)*label;
536 if(*prefix != tolower((unsigned char)*label)) {
541 if(orig_plen < lablen)
542 *endptr = (char *)label;
544 /* prefix length == label length */
550 dname_buffer_write(sldns_buffer* pkt, uint8_t* dname)
554 if(sldns_buffer_remaining(pkt) < 1)
557 sldns_buffer_write_u8(pkt, lablen);
559 if(sldns_buffer_remaining(pkt) < (size_t)lablen+1)
561 sldns_buffer_write(pkt, dname, lablen);
564 sldns_buffer_write_u8(pkt, lablen);
569 void dname_str(uint8_t* dname, char* str)
574 if(!dname || !*dname) {
581 if(lablen > LDNS_MAX_LABELLEN) {
587 if(len >= LDNS_MAX_DOMAINLEN-1) {
593 if(isalnum((unsigned char)*dname)
594 || *dname == '-' || *dname == '_'
596 *s++ = *(char*)dname++;
609 dname_strict_subdomain(uint8_t* d1, int labs1, uint8_t* d2, int labs2)
612 /* check subdomain: d1: www.example.com. and d2: example.com. */
615 if(dname_lab_cmp(d1, labs1, d2, labs2, &m) > 0) {
616 /* subdomain if all labels match */
623 dname_strict_subdomain_c(uint8_t* d1, uint8_t* d2)
625 return dname_strict_subdomain(d1, dname_count_labels(d1), d2,
626 dname_count_labels(d2));
630 dname_subdomain_c(uint8_t* d1, uint8_t* d2)
633 /* check subdomain: d1: www.example.com. and d2: example.com. */
634 /* or d1: example.com. and d2: example.com. */
635 int labs1 = dname_count_labels(d1);
636 int labs2 = dname_count_labels(d2);
639 if(dname_lab_cmp(d1, labs1, d2, labs2, &m) < 0) {
640 /* must have been example.com , www.example.com - wrong */
641 /* or otherwise different dnames */
648 dname_is_root(uint8_t* dname)
653 log_assert(!LABEL_IS_PTR(len));
658 dname_remove_label(uint8_t** dname, size_t* len)
661 log_assert(dname && *dname && len);
662 lablen = (*dname)[0];
663 log_assert(!LABEL_IS_PTR(lablen));
664 log_assert(*len > lablen);
666 return; /* do not modify root label */
672 dname_remove_labels(uint8_t** dname, size_t* len, int n)
676 dname_remove_label(dname, len);
680 dname_signame_label_count(uint8_t* dname)
686 if(dname[0] == 1 && dname[1] == '*')
699 dname_is_wild(uint8_t* dname)
701 return (dname[0] == 1 && dname[1] == '*');
705 * Compare labels in memory, lowercase while comparing.
706 * Returns canonical order for labels. If all is equal, the
710 * @param len1: length of label 1.
712 * @param len2: length of label 2.
713 * @return: 0, -1, +1 comparison result.
716 memcanoncmp(uint8_t* p1, uint8_t len1, uint8_t* p2, uint8_t len2)
718 uint8_t min = (len1<len2)?len1:len2;
719 int c = memlowercmp(p1, p2, min);
722 /* equal, see who is shortest */
732 dname_canon_lab_cmp(uint8_t* d1, int labs1, uint8_t* d2, int labs2, int* mlabs)
734 /* like dname_lab_cmp, but with different label comparison,
735 * empty character sorts before \000.
736 * So ylyly is before z. */
742 /* first skip so that we compare same label. */
744 while(atlabel > labs2) {
749 log_assert(atlabel == labs2);
750 } else if(labs1 < labs2) {
752 while(atlabel > labs1) {
757 log_assert(atlabel == labs1);
759 lastmlabs = atlabel+1;
760 /* now at same label in d1 and d2, atlabel */
761 /* www.example.com. */
762 /* 4 3 2 1 atlabel number */
763 /* repeat until at root label (which is always the same) */
768 if((c=memcanoncmp(d1, len1, d2, len2)) != 0) {
779 /* last difference atlabel number, so number of labels matching,
780 * at the right side, is one less. */
781 *mlabs = lastmlabs-1;
783 /* all labels compared were equal, check if one has more
784 * labels, so that example.com. > com. */
787 else if(labs1 < labs2)
794 dname_canonical_compare(uint8_t* d1, uint8_t* d2)
797 labs1 = dname_count_labels(d1);
798 labs2 = dname_count_labels(d2);
799 return dname_canon_lab_cmp(d1, labs1, d2, labs2, &m);
802 uint8_t* dname_get_shared_topdomain(uint8_t* d1, uint8_t* d2)
805 size_t len = LDNS_MAX_DOMAINLEN;
806 labs1 = dname_count_labels(d1);
807 labs2 = dname_count_labels(d2);
808 (void)dname_lab_cmp(d1, labs1, d2, labs2, &m);
809 dname_remove_labels(&d1, &len, labs1-m);