Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | |
| 3 | /* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu> |
| 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 |
| 13 | # include <stdlib.h> |
| 14 | # include <stdint.h> |
| 15 | # include <string.h> |
| 16 | # include <stdio.h> |
| 17 | #else |
| 18 | # include "common.h" |
| 19 | #endif |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 20 | |
| 21 | #define NS_MAXDNAME 1025 /* max domain name length */ |
| 22 | #define NS_MAXCDNAME 255 /* max compressed domain name length */ |
| 23 | #define NS_MAXLABEL 63 /* max label length */ |
| 24 | #define NS_MAXDNSRCH 6 /* max domains in search path */ |
| 25 | #define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */ |
| 26 | |
| 27 | |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 28 | /* Expand a RFC1035-compressed list of domain names "cstr", of length "clen"; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 29 | * returns a newly allocated string containing the space-separated domains, |
| 30 | * prefixed with the contents of string pre, or NULL if an error occurs. |
| 31 | */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 32 | 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] | 33 | { |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 34 | char *ret = ret; /* for compiler */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 35 | char *dst = NULL; |
| 36 | |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 37 | /* We make two passes over the cstr string. First, we compute |
| 38 | * how long the resulting string would be. Then we allocate a |
| 39 | * new buffer of the required length, and fill it in with the |
| 40 | * expanded content. The advantage of this approach is not |
| 41 | * having to deal with requiring callers to supply their own |
| 42 | * buffer, then having to check if it's sufficiently large, etc. |
| 43 | */ |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 44 | while (1) { |
| 45 | /* note: "return NULL" below are leak-safe since |
Denys Vlasenko | 5bec08c | 2016-02-26 14:56:18 +0100 | [diff] [blame] | 46 | * dst isn't allocated yet */ |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 47 | const uint8_t *c; |
| 48 | unsigned crtpos, retpos, depth, len; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 49 | |
| 50 | crtpos = retpos = depth = len = 0; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 51 | while (crtpos < clen) { |
| 52 | c = cstr + crtpos; |
| 53 | |
Denys Vlasenko | a14a9d7 | 2010-03-24 15:06:23 +0100 | [diff] [blame] | 54 | if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 55 | /* pointer */ |
| 56 | if (crtpos + 2 > clen) /* no offset to jump to? abort */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 57 | return NULL; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 58 | if (retpos == 0) /* toplevel? save return spot */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 59 | retpos = crtpos + 2; |
| 60 | depth++; |
Denys Vlasenko | a14a9d7 | 2010-03-24 15:06:23 +0100 | [diff] [blame] | 61 | crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */ |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 62 | } else if (*c) { |
| 63 | /* label */ |
| 64 | if (crtpos + *c + 1 > clen) /* label too long? abort */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 65 | return NULL; |
| 66 | if (dst) |
Denys Vlasenko | d474ffc | 2016-03-10 11:47:58 +0100 | [diff] [blame] | 67 | /* \3com ---> "com." */ |
| 68 | ((char*)mempcpy(dst + len, c + 1, *c))[0] = '.'; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 69 | len += *c + 1; |
| 70 | crtpos += *c + 1; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 71 | } else { |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 72 | /* NUL: end of current domain name */ |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 73 | if (retpos == 0) { |
| 74 | /* toplevel? keep going */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 75 | crtpos++; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 76 | } else { |
| 77 | /* return to toplevel saved spot */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 78 | crtpos = retpos; |
| 79 | retpos = depth = 0; |
| 80 | } |
Denys Vlasenko | d474ffc | 2016-03-10 11:47:58 +0100 | [diff] [blame] | 81 | if (dst && len != 0) |
| 82 | /* \4host\3com\0\4host and we are at \0: |
| 83 | * \3com was converted to "com.", change dot to space. |
| 84 | */ |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 85 | dst[len - 1] = ' '; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 88 | if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */ |
| 89 | || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */ |
| 90 | ) { |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 91 | return NULL; |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 92 | } |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 95 | if (!len) /* expanded string has 0 length? abort */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 98 | if (!dst) { /* first pass? */ |
| 99 | /* allocate dst buffer and copy pre */ |
| 100 | unsigned plen = strlen(pre); |
Denys Vlasenko | 5bec08c | 2016-02-26 14:56:18 +0100 | [diff] [blame] | 101 | ret = xmalloc(plen + len); |
| 102 | dst = stpcpy(ret, pre); |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 103 | } else { |
| 104 | dst[len - 1] = '\0'; |
| 105 | break; |
| 106 | } |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denys Vlasenko | 7895b91 | 2009-07-03 16:59:59 +0200 | [diff] [blame] | 109 | return ret; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* Convert a domain name (src) from human-readable "foo.blah.com" format into |
| 113 | * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or |
| 114 | * NULL if an error occurs. |
| 115 | */ |
| 116 | static uint8_t *convert_dname(const char *src) |
| 117 | { |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 118 | uint8_t c, *res, *lenptr, *dst; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 119 | int len; |
| 120 | |
| 121 | res = xmalloc(strlen(src) + 2); |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 122 | dst = lenptr = res; |
| 123 | dst++; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 124 | |
| 125 | for (;;) { |
| 126 | c = (uint8_t)*src++; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 127 | if (c == '.' || c == '\0') { /* end of label */ |
| 128 | len = dst - lenptr - 1; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 129 | /* label too long, too short, or two '.'s in a row? abort */ |
| 130 | if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) { |
| 131 | free(res); |
| 132 | return NULL; |
| 133 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 134 | *lenptr = len; |
| 135 | if (c == '\0' || *src == '\0') /* "" or ".": end of src */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 136 | break; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 137 | lenptr = dst++; |
| 138 | continue; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 139 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 140 | if (c >= 'A' && c <= 'Z') /* uppercase? convert to lower */ |
| 141 | c += ('a' - 'A'); |
| 142 | *dst++ = c; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 145 | if (dst - res >= NS_MAXCDNAME) { /* dname too long? abort */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 146 | free(res); |
| 147 | return NULL; |
| 148 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 149 | |
| 150 | *dst = 0; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 151 | return res; |
| 152 | } |
| 153 | |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 154 | /* 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] | 155 | static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname) |
| 156 | { |
| 157 | const uint8_t *c, *d; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 158 | int off; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 159 | |
| 160 | /* find all labels in cstr */ |
| 161 | off = 0; |
| 162 | while (off < clen) { |
| 163 | c = cstr + off; |
| 164 | |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 165 | if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* pointer, skip */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 166 | off += 2; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 167 | continue; |
| 168 | } |
| 169 | if (*c) { /* label, try matching dname */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 170 | d = dname; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 171 | while (1) { |
| 172 | unsigned len1 = *c + 1; |
| 173 | if (memcmp(c, d, len1) != 0) |
| 174 | break; |
| 175 | if (len1 == 1) /* at terminating NUL - match, return offset */ |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 176 | return off; |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 177 | d += len1; |
| 178 | c += len1; |
Denys Vlasenko | a14a9d7 | 2010-03-24 15:06:23 +0100 | [diff] [blame] | 179 | if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* pointer, jump */ |
| 180 | c = cstr + (((c[0] & 0x3f) << 8) | c[1]); |
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 | off += cstr[off] + 1; |
| 183 | continue; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 184 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 185 | /* NUL, skip */ |
| 186 | off++; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | return -1; |
| 190 | } |
| 191 | |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 192 | /* 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] | 193 | * 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] | 194 | * 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] | 195 | * The computed string is returned directly; its length is returned via retlen; |
| 196 | * NULL and 0, respectively, are returned if an error occurs. |
| 197 | */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 198 | uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen) |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 199 | { |
| 200 | uint8_t *d, *dname; |
| 201 | int off; |
| 202 | |
| 203 | dname = convert_dname(src); |
| 204 | if (dname == NULL) { |
| 205 | *retlen = 0; |
| 206 | return NULL; |
| 207 | } |
| 208 | |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 209 | d = dname; |
| 210 | while (*d) { |
| 211 | if (cstr) { |
| 212 | off = find_offset(cstr, clen, d); |
| 213 | if (off >= 0) { /* found a match, add pointer and return */ |
| 214 | *d++ = NS_CMPRSFLGS | (off >> 8); |
| 215 | *d = off; |
| 216 | break; |
| 217 | } |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 218 | } |
Denys Vlasenko | 702f7d6 | 2010-03-24 16:11:32 +0100 | [diff] [blame] | 219 | d += *d + 1; |
Denis Vlasenko | 4c7e5b6 | 2007-02-27 22:39:19 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | *retlen = d - dname + 1; |
| 223 | return dname; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 224 | } |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 225 | |
| 226 | #ifdef DNS_COMPR_TESTING |
| 227 | /* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */ |
| 228 | int main(int argc, char **argv) |
| 229 | { |
| 230 | int len; |
| 231 | uint8_t *encoded; |
| 232 | |
Denys Vlasenko | d474ffc | 2016-03-10 11:47:58 +0100 | [diff] [blame] | 233 | uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 }; |
| 234 | printf("NUL:'%s'\n", dname_dec(str, 6, "")); |
| 235 | |
Denys Vlasenko | 3d9c69f | 2010-03-24 16:43:16 +0100 | [diff] [blame] | 236 | #define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre)) |
| 237 | printf("'%s'\n", DNAME_DEC("\4host\3com\0", "test1:")); |
| 238 | printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", "")); |
| 239 | printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", "")); |
| 240 | printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", "")); |
| 241 | printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", "")); |
| 242 | |
| 243 | #define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp)) |
| 244 | encoded = dname_enc(NULL, 0, "test.net", &len); |
| 245 | printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 246 | encoded = DNAME_ENC("\3net\0", "test.net", &len); |
| 247 | printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 248 | encoded = DNAME_ENC("\4test\3net\0", "test.net", &len); |
| 249 | printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len); |
| 250 | return 0; |
| 251 | } |
| 252 | #endif |