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