1 /* vi: set sw=4 ts=4: */
3 /* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
5 * Loosely based on the isc-dhcpd implementation by dhankins@isc.org
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #define NS_MAXDNAME 1025 /* max domain name length */
13 #define NS_MAXCDNAME 255 /* max compressed domain name length */
14 #define NS_MAXLABEL 63 /* max label length */
15 #define NS_MAXDNSRCH 6 /* max domains in search path */
16 #define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */
19 /* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
20 * returns a newly allocated string containing the space-separated domains,
21 * prefixed with the contents of string pre, or NULL if an error occurs.
23 char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
25 char *ret = ret; /* for compiler */
28 /* We make two passes over the cstr string. First, we compute
29 * how long the resulting string would be. Then we allocate a
30 * new buffer of the required length, and fill it in with the
31 * expanded content. The advantage of this approach is not
32 * having to deal with requiring callers to supply their own
33 * buffer, then having to check if it's sufficiently large, etc.
36 /* note: "return NULL" below are leak-safe since
37 * dst isn't yet allocated */
39 unsigned crtpos, retpos, depth, len;
41 crtpos = retpos = depth = len = 0;
42 while (crtpos < clen) {
45 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
47 if (crtpos + 2 > clen) /* no offset to jump to? abort */
49 if (retpos == 0) /* toplevel? save return spot */
52 crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */
55 if (crtpos + *c + 1 > clen) /* label too long? abort */
58 memcpy(dst + len, c + 1, *c);
64 /* NUL: end of current domain name */
66 /* toplevel? keep going */
69 /* return to toplevel saved spot */
77 if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
78 || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
84 if (!len) /* expanded string has 0 length? abort */
87 if (!dst) { /* first pass? */
88 /* allocate dst buffer and copy pre */
89 unsigned plen = strlen(pre);
90 ret = dst = xmalloc(plen + len);
91 memcpy(dst, pre, plen);
102 /* Convert a domain name (src) from human-readable "foo.blah.com" format into
103 * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
104 * NULL if an error occurs.
106 static uint8_t *convert_dname(const char *src)
108 uint8_t c, *res, *lenptr, *dst;
111 res = xmalloc(strlen(src) + 2);
117 if (c == '.' || c == '\0') { /* end of label */
118 len = dst - lenptr - 1;
119 /* label too long, too short, or two '.'s in a row? abort */
120 if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
125 if (c == '\0' || *src == '\0') /* "" or ".": end of src */
130 if (c >= 'A' && c <= 'Z') /* uppercase? convert to lower */
135 if (dst - res >= NS_MAXCDNAME) { /* dname too long? abort */
144 /* returns the offset within cstr at which dname can be found, or -1
146 static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
148 const uint8_t *c, *d;
151 /* find all labels in cstr */
156 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* pointer, skip */
160 if (*c) { /* label, try matching dname */
163 unsigned len1 = *c + 1;
164 if (memcmp(c, d, len1) != 0)
166 if (len1 == 1) /* at terminating NUL - match, return offset */
170 if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* pointer, jump */
171 c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
173 off += cstr[off] + 1;
183 /* computes string to be appended to cstr so that src would be added to
184 * the compression (best case, it's a 2-byte pointer to some offset within
185 * cstr; worst case, it's all of src, converted to <4>host<3>com<0> format).
186 * The computed string is returned directly; its length is returned via retlen;
187 * NULL and 0, respectively, are returned if an error occurs.
189 uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
194 dname = convert_dname(src);
203 off = find_offset(cstr, clen, d);
204 if (off >= 0) { /* found a match, add pointer and return */
205 *d++ = NS_CMPRSFLGS | (off >> 8);
213 *retlen = d - dname + 1;