blob: 45354e727181c365bd51739b1c9027c0370e239f [file] [log] [blame]
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +00001/* 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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
Denis Vlasenko35a064b2008-11-06 00:49:59 +000010#if ENABLE_FEATURE_UDHCP_RFC3397
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000011
12#include "common.h"
13#include "options.h"
14
15#define NS_MAXDNAME 1025 /* max domain name length */
16#define NS_MAXCDNAME 255 /* max compressed domain name length */
17#define NS_MAXLABEL 63 /* max label length */
18#define NS_MAXDNSRCH 6 /* max domains in search path */
19#define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */
20
21
22/* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
23 * returns a newly allocated string containing the space-separated domains,
24 * prefixed with the contents of string pre, or NULL if an error occurs.
25 */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000026char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000027{
Denys Vlasenko7895b912009-07-03 16:59:59 +020028 char *ret = ret; /* for compiler */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000029 char *dst = NULL;
30
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000031 /* We make two passes over the cstr string. First, we compute
32 * how long the resulting string would be. Then we allocate a
33 * new buffer of the required length, and fill it in with the
34 * expanded content. The advantage of this approach is not
35 * having to deal with requiring callers to supply their own
36 * buffer, then having to check if it's sufficiently large, etc.
37 */
Denys Vlasenko7895b912009-07-03 16:59:59 +020038 while (1) {
39 /* note: "return NULL" below are leak-safe since
40 * dst isn't yet allocated */
41 const uint8_t *c;
42 unsigned crtpos, retpos, depth, len;
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000043
44 crtpos = retpos = depth = len = 0;
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000045 while (crtpos < clen) {
46 c = cstr + crtpos;
47
Denys Vlasenko7895b912009-07-03 16:59:59 +020048 if (*c & NS_CMPRSFLGS) {
49 /* pointer */
50 if (crtpos + 2 > clen) /* no offset to jump to? abort */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000051 return NULL;
Denys Vlasenko7895b912009-07-03 16:59:59 +020052 if (retpos == 0) /* toplevel? save return spot */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000053 retpos = crtpos + 2;
54 depth++;
Denys Vlasenko7895b912009-07-03 16:59:59 +020055 crtpos = ((c[0] & 0x3f) << 8) | (c[1] & 0xff); /* jump */
56 } else if (*c) {
57 /* label */
58 if (crtpos + *c + 1 > clen) /* label too long? abort */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000059 return NULL;
60 if (dst)
Denys Vlasenko7895b912009-07-03 16:59:59 +020061 memcpy(dst + len, c + 1, *c);
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000062 len += *c + 1;
63 crtpos += *c + 1;
64 if (dst)
Denys Vlasenko7895b912009-07-03 16:59:59 +020065 dst[len - 1] = '.';
66 } else {
67 /* null: end of current domain name */
68 if (retpos == 0) {
69 /* toplevel? keep going */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000070 crtpos++;
Denys Vlasenko7895b912009-07-03 16:59:59 +020071 } else {
72 /* return to toplevel saved spot */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000073 crtpos = retpos;
74 retpos = depth = 0;
75 }
76 if (dst)
Denys Vlasenko7895b912009-07-03 16:59:59 +020077 dst[len - 1] = ' ';
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000078 }
79
Denys Vlasenko7895b912009-07-03 16:59:59 +020080 if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
81 || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
82 ) {
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000083 return NULL;
Denys Vlasenko7895b912009-07-03 16:59:59 +020084 }
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000085 }
86
Denys Vlasenko7895b912009-07-03 16:59:59 +020087 if (!len) /* expanded string has 0 length? abort */
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +000088 return NULL;
89
Denys Vlasenko7895b912009-07-03 16:59:59 +020090 if (!dst) { /* first pass? */
91 /* allocate dst buffer and copy pre */
92 unsigned plen = strlen(pre);
93 ret = dst = xmalloc(plen + len);
94 memcpy(dst, pre, plen);
95 dst += plen;
96 } else {
97 dst[len - 1] = '\0';
98 break;
99 }
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +0000100 }
101
Denys Vlasenko7895b912009-07-03 16:59:59 +0200102 return ret;
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +0000103}
104
105/* Convert a domain name (src) from human-readable "foo.blah.com" format into
106 * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
107 * NULL if an error occurs.
108 */
109static uint8_t *convert_dname(const char *src)
110{
111 uint8_t c, *res, *lp, *rp;
112 int len;
113
114 res = xmalloc(strlen(src) + 2);
115 rp = lp = res;
116 rp++;
117
118 for (;;) {
119 c = (uint8_t)*src++;
120 if (c == '.' || c == '\0') { /* end of label */
121 len = rp - lp - 1;
122 /* label too long, too short, or two '.'s in a row? abort */
123 if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
124 free(res);
125 return NULL;
126 }
127 *lp = len;
128 lp = rp++;
129 if (c == '\0' || *src == '\0') /* end of dname */
130 break;
131 } else {
132 if (c >= 0x41 && c <= 0x5A) /* uppercase? convert to lower */
133 c += 0x20;
134 *rp++ = c;
135 }
136 }
137
138 *lp = 0;
139 if (rp - res > NS_MAXCDNAME) { /* dname too long? abort */
140 free(res);
141 return NULL;
142 }
143 return res;
144}
145
146/* returns the offset within cstr at which dname can be found, or -1
147 */
148static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
149{
150 const uint8_t *c, *d;
151 int off, inc;
152
153 /* find all labels in cstr */
154 off = 0;
155 while (off < clen) {
156 c = cstr + off;
157
158 if ((*c & NS_CMPRSFLGS) != 0) { /* pointer, skip */
159 off += 2;
160 } else if (*c) { /* label, try matching dname */
161 inc = *c + 1;
162 d = dname;
163 while (*c == *d && memcmp(c + 1, d + 1, *c) == 0) {
164 if (*c == 0) /* match, return offset */
165 return off;
166 d += *c + 1;
167 c += *c + 1;
168 if ((*c & NS_CMPRSFLGS) != 0) /* pointer, jump */
169 c = cstr + (((*c & 0x3f) << 8) | (*(c + 1) & 0xff));
170 }
171 off += inc;
172 } else { /* null, skip */
173 off++;
174 }
175 }
176
177 return -1;
178}
179
180/* computes string to be appended to cstr so that src would be added to
181 * the compression (best case, it's a 2-byte pointer to some offset within
182 * cstr; worst case, it's all of src, converted to rfc3011 format).
183 * The computed string is returned directly; its length is returned via retlen;
184 * NULL and 0, respectively, are returned if an error occurs.
185 */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000186uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +0000187{
188 uint8_t *d, *dname;
189 int off;
190
191 dname = convert_dname(src);
192 if (dname == NULL) {
193 *retlen = 0;
194 return NULL;
195 }
196
197 for (d = dname; *d != 0; d += *d + 1) {
198 off = find_offset(cstr, clen, d);
199 if (off >= 0) { /* found a match, add pointer and terminate string */
200 *d++ = NS_CMPRSFLGS;
201 *d = off;
202 break;
203 }
204 }
205
206 *retlen = d - dname + 1;
207 return dname;
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000208}
Denis Vlasenko4c7e5b62007-02-27 22:39:19 +0000209
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000210#endif /* ENABLE_FEATURE_UDHCP_RFC3397 */