Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu> |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 4 | * |
| 5 | * Loosely based on the isc-dhcpd implementation by dhankins@isc.org |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 9 | #ifdef DNS_COMPR_TESTING |
Denys Vlasenko | 1b7c173 | 2016-03-11 00:26:58 +0100 | [diff] [blame] | 10 | # define _GNU_SOURCE |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 11 | # define FAST_FUNC /* nothing */ |
| 12 | # define xmalloc malloc |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 13 | # define xzalloc(s) calloc(s, 1) |
| 14 | # define xstrdup strdup |
| 15 | # define xrealloc realloc |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 16 | # include <stdlib.h> |
| 17 | # include <stdint.h> |
| 18 | # include <string.h> |
| 19 | # include <stdio.h> |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 20 | # include <ctype.h> |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 21 | #else |
| 22 | # include "common.h" |
| 23 | #endif |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 24 | |
| 25 | #define NS_MAXDNAME 1025 /* max domain name length */ |
| 26 | #define NS_MAXCDNAME 255 /* max compressed domain name length */ |
| 27 | #define NS_MAXLABEL 63 /* max label length */ |
| 28 | #define NS_MAXDNSRCH 6 /* max domains in search path */ |
| 29 | #define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */ |
| 30 | |
| 31 | |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 32 | /* Expand a RFC1035-compressed list of domain names "cstr", of length "clen"; |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 33 | * return a newly allocated string containing the space-separated domains, |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 34 | * prefixed with the contents of string pre, or NULL if an error occurs. |
| 35 | */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 36 | char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre) |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 37 | { |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 38 | char *ret, *end; |
| 39 | unsigned len, crtpos, retpos, depth; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 40 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 41 | crtpos = retpos = depth = 0; |
| 42 | len = strlen(pre); |
| 43 | end = ret = xstrdup(pre); |
| 44 | |
| 45 | /* Scan the string once, allocating new memory as needed */ |
| 46 | while (crtpos < clen) { |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 47 | const uint8_t *c; |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 48 | c = cstr + crtpos; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 49 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 50 | if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { |
| 51 | /* pointer */ |
| 52 | if (crtpos + 2 > clen) /* no offset to jump to? abort */ |
| 53 | goto error; |
| 54 | if (retpos == 0) /* toplevel? save return spot */ |
| 55 | retpos = crtpos + 2; |
| 56 | depth++; |
| 57 | crtpos = ((c[0] << 8) | c[1]) & 0x3fff; /* jump */ |
| 58 | } else if (*c) { |
| 59 | unsigned label_len; |
| 60 | /* label */ |
| 61 | if (crtpos + *c + 1 > clen) /* label too long? abort */ |
| 62 | goto error; |
| 63 | ret = xrealloc(ret, len + *c + 1); |
| 64 | /* \3com ---> "com." */ |
| 65 | end = (char *)mempcpy(ret + len, c + 1, *c); |
| 66 | *end = '.'; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 67 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 68 | label_len = *c + 1; |
| 69 | len += label_len; |
| 70 | crtpos += label_len; |
| 71 | } else { |
| 72 | /* NUL: end of current domain name */ |
| 73 | if (retpos == 0) { |
| 74 | /* toplevel? keep going */ |
| 75 | crtpos++; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 76 | } else { |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 77 | /* return to toplevel saved spot */ |
| 78 | crtpos = retpos; |
| 79 | retpos = depth = 0; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 82 | if (len != 0) { |
| 83 | /* \4host\3com\0\4host and we are at \0: |
| 84 | * \3com was converted to "com.", change dot to space. |
| 85 | */ |
| 86 | ret[len - 1] = ' '; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 87 | } |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 90 | if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */ |
| 91 | || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */ |
| 92 | ) { |
| 93 | goto error; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 94 | } |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 97 | if (ret == end) { /* expanded string is empty? abort */ |
| 98 | error: |
| 99 | free(ret); |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | *end = '\0'; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 104 | return ret; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 107 | /* Convert a domain name (src) from human-readable "foo.BLAH.com" format into |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 108 | * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or |
| 109 | * NULL if an error occurs. |
| 110 | */ |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 111 | static uint8_t *convert_dname(const char *src, int *retlen) |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 112 | { |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 113 | uint8_t *res, *lenptr, *dst; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 114 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 115 | res = xzalloc(strlen(src) + 2); |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 116 | dst = lenptr = res; |
| 117 | dst++; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 118 | |
| 119 | for (;;) { |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 120 | uint8_t c; |
| 121 | int len; |
| 122 | |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 123 | c = (uint8_t)*src++; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 124 | if (c == '.' || c == '\0') { /* end of label */ |
| 125 | len = dst - lenptr - 1; |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 126 | /* label too long, too short, or two '.'s in a row (len will be 0) */ |
| 127 | if (len > NS_MAXLABEL || len == 0) |
| 128 | goto error; |
| 129 | |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 130 | *lenptr = len; |
| 131 | if (c == '\0' || *src == '\0') /* "" or ".": end of src */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 132 | break; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 133 | lenptr = dst++; |
| 134 | continue; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 135 | } |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 136 | *dst++ = tolower(c); |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 139 | *retlen = dst + 1 - res; |
| 140 | if (*retlen > NS_MAXCDNAME) { /* dname too long? abort */ |
| 141 | error: |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 142 | free(res); |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 143 | *retlen = 0; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 146 | |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 147 | return res; |
| 148 | } |
| 149 | |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 150 | #if 0 //UNUSED |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 151 | /* Returns the offset within cstr at which dname can be found, or -1 */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 152 | static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname) |
| 153 | { |
| 154 | const uint8_t *c, *d; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 155 | int off; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 156 | |
| 157 | /* find all labels in cstr */ |
| 158 | off = 0; |
| 159 | while (off < clen) { |
| 160 | c = cstr + off; |
| 161 | |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 162 | if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* pointer, skip */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 163 | off += 2; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 164 | continue; |
| 165 | } |
| 166 | if (*c) { /* label, try matching dname */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 167 | d = dname; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 168 | while (1) { |
| 169 | unsigned len1 = *c + 1; |
| 170 | if (memcmp(c, d, len1) != 0) |
| 171 | break; |
| 172 | if (len1 == 1) /* at terminating NUL - match, return offset */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 173 | return off; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 174 | d += len1; |
| 175 | c += len1; |
Denys Vlasenko | a14a9d7 | 2010-03-24 15:06:23 +0100 | [diff] [blame] | 176 | if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* pointer, jump */ |
| 177 | c = cstr + (((c[0] & 0x3f) << 8) | c[1]); |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 178 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 179 | off += cstr[off] + 1; |
| 180 | continue; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 181 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 182 | /* NUL, skip */ |
| 183 | off++; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return -1; |
| 187 | } |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 188 | #endif |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 189 | |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 190 | uint8_t* FAST_FUNC dname_enc(/*const uint8_t *cstr, int clen,*/ const char *src, int *retlen) |
| 191 | { |
| 192 | #if 0 //UNUSED, was intended for long, repetitive DHCP_DOMAIN_SEARCH options? |
| 193 | uint8_t *d, *dname; |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 194 | /* Computes string to be appended to cstr so that src would be added to |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 195 | * the compression (best case, it's a 2-byte pointer to some offset within |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 196 | * cstr; worst case, it's all of src, converted to <4>host<3>com<0> format). |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 197 | * The computed string is returned directly; its length is returned via retlen; |
| 198 | * NULL and 0, respectively, are returned if an error occurs. |
| 199 | */ |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 200 | dname = convert_dname(src, retlen); |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 201 | if (dname == NULL) { |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
| 204 | |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 205 | d = dname; |
| 206 | while (*d) { |
| 207 | if (cstr) { |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 208 | int off = find_offset(cstr, clen, d); |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 209 | if (off >= 0) { /* found a match, add pointer and return */ |
| 210 | *d++ = NS_CMPRSFLGS | (off >> 8); |
| 211 | *d = off; |
| 212 | break; |
| 213 | } |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 214 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 215 | d += *d + 1; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | *retlen = d - dname + 1; |
| 219 | return dname; |
Denys Vlasenko | 0cad5f9 | 2020-06-09 17:22:06 +0200 | [diff] [blame] | 220 | #endif |
| 221 | return convert_dname(src, retlen); |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 222 | } |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 223 | |
| 224 | #ifdef DNS_COMPR_TESTING |
| 225 | /* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */ |
| 226 | int main(int argc, char **argv) |
| 227 | { |
| 228 | int len; |
| 229 | uint8_t *encoded; |
| 230 | |
Denys Vlasenko | d474ffc | 2016-03-10 11:47:58 +0100 | [diff] [blame] | 231 | uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 }; |
| 232 | printf("NUL:'%s'\n", dname_dec(str, 6, "")); |
| 233 | |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 234 | #define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre)) |
| 235 | printf("'%s'\n", DNAME_DEC("\4host\3com\0", "test1:")); |
| 236 | printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", "")); |
| 237 | printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", "")); |
| 238 | printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", "")); |
| 239 | printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", "")); |
| 240 | |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 241 | #if 0 |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 242 | #define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp)) |
| 243 | encoded = dname_enc(NULL, 0, "test.net", &len); |
| 244 | printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 245 | encoded = DNAME_ENC("\3net\0", "test.net", &len); |
| 246 | printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 247 | encoded = DNAME_ENC("\4test\3net\0", "test.net", &len); |
| 248 | printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
Martin Lewis | 4468c56 | 2020-07-09 14:47:05 -0500 | [diff] [blame] | 249 | #endif |
| 250 | |
| 251 | encoded = dname_enc("test.net", &len); |
| 252 | printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 253 | encoded = dname_enc("test.host.com", &len); |
| 254 | printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 255 | |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | #endif |