2007-02-28 04:09:19 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2017-10-05 18:10:24 +05:30
|
|
|
/*
|
|
|
|
* RFC1035 domain compression routines (C) 2007 Gabriel Somlo <somlo at cmu.edu>
|
2007-02-28 04:09:19 +05:30
|
|
|
*
|
|
|
|
* Loosely based on the isc-dhcpd implementation by dhankins@isc.org
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2007-02-28 04:09:19 +05:30
|
|
|
*/
|
2010-03-24 21:13:16 +05:30
|
|
|
#ifdef DNS_COMPR_TESTING
|
2016-03-11 04:56:58 +05:30
|
|
|
# define _GNU_SOURCE
|
2010-03-24 21:13:16 +05:30
|
|
|
# define FAST_FUNC /* nothing */
|
|
|
|
# define xmalloc malloc
|
2020-07-10 01:17:05 +05:30
|
|
|
# define xzalloc(s) calloc(s, 1)
|
|
|
|
# define xstrdup strdup
|
|
|
|
# define xrealloc realloc
|
2010-03-24 21:13:16 +05:30
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stdint.h>
|
|
|
|
# include <string.h>
|
|
|
|
# include <stdio.h>
|
2020-07-10 01:17:05 +05:30
|
|
|
# include <ctype.h>
|
2010-03-24 21:13:16 +05:30
|
|
|
#else
|
|
|
|
# include "common.h"
|
|
|
|
#endif
|
2007-02-28 04:09:19 +05:30
|
|
|
|
|
|
|
#define NS_MAXDNAME 1025 /* max domain name length */
|
|
|
|
#define NS_MAXCDNAME 255 /* max compressed domain name length */
|
|
|
|
#define NS_MAXLABEL 63 /* max label length */
|
|
|
|
#define NS_MAXDNSRCH 6 /* max domains in search path */
|
|
|
|
#define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */
|
|
|
|
|
|
|
|
|
2010-03-26 14:39:34 +05:30
|
|
|
/* Expand a RFC1035-compressed list of domain names "cstr", of length "clen";
|
2020-07-10 01:17:05 +05:30
|
|
|
* return a newly allocated string containing the space-separated domains,
|
2007-02-28 04:09:19 +05:30
|
|
|
* prefixed with the contents of string pre, or NULL if an error occurs.
|
|
|
|
*/
|
2008-09-26 15:04:59 +05:30
|
|
|
char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
|
2007-02-28 04:09:19 +05:30
|
|
|
{
|
2020-07-10 01:17:05 +05:30
|
|
|
char *ret, *end;
|
|
|
|
unsigned len, crtpos, retpos, depth;
|
2007-02-28 04:09:19 +05:30
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
crtpos = retpos = depth = 0;
|
|
|
|
len = strlen(pre);
|
|
|
|
end = ret = xstrdup(pre);
|
|
|
|
|
|
|
|
/* Scan the string once, allocating new memory as needed */
|
|
|
|
while (crtpos < clen) {
|
2009-07-03 20:29:59 +05:30
|
|
|
const uint8_t *c;
|
2020-07-10 01:17:05 +05:30
|
|
|
c = cstr + crtpos;
|
2007-02-28 04:09:19 +05:30
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
|
|
|
|
/* pointer */
|
|
|
|
if (crtpos + 2 > clen) /* no offset to jump to? abort */
|
|
|
|
goto error;
|
|
|
|
if (retpos == 0) /* toplevel? save return spot */
|
|
|
|
retpos = crtpos + 2;
|
|
|
|
depth++;
|
|
|
|
crtpos = ((c[0] << 8) | c[1]) & 0x3fff; /* jump */
|
|
|
|
} else if (*c) {
|
|
|
|
unsigned label_len;
|
|
|
|
/* label */
|
|
|
|
if (crtpos + *c + 1 > clen) /* label too long? abort */
|
|
|
|
goto error;
|
|
|
|
ret = xrealloc(ret, len + *c + 1);
|
|
|
|
/* \3com ---> "com." */
|
|
|
|
end = (char *)mempcpy(ret + len, c + 1, *c);
|
|
|
|
*end = '.';
|
2007-02-28 04:09:19 +05:30
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
label_len = *c + 1;
|
|
|
|
len += label_len;
|
|
|
|
crtpos += label_len;
|
|
|
|
} else {
|
|
|
|
/* NUL: end of current domain name */
|
|
|
|
if (retpos == 0) {
|
|
|
|
/* toplevel? keep going */
|
|
|
|
crtpos++;
|
2009-07-03 20:29:59 +05:30
|
|
|
} else {
|
2020-07-10 01:17:05 +05:30
|
|
|
/* return to toplevel saved spot */
|
|
|
|
crtpos = retpos;
|
|
|
|
retpos = depth = 0;
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
if (len != 0) {
|
|
|
|
/* \4host\3com\0\4host and we are at \0:
|
|
|
|
* \3com was converted to "com.", change dot to space.
|
|
|
|
*/
|
|
|
|
ret[len - 1] = ' ';
|
2009-07-03 20:29:59 +05:30
|
|
|
}
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
|
|
|
|
|| len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
|
|
|
|
) {
|
|
|
|
goto error;
|
2009-07-03 20:29:59 +05:30
|
|
|
}
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
if (ret == end) { /* expanded string is empty? abort */
|
|
|
|
error:
|
|
|
|
free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*end = '\0';
|
2009-07-03 20:29:59 +05:30
|
|
|
return ret;
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
2020-06-09 20:52:06 +05:30
|
|
|
/* Convert a domain name (src) from human-readable "foo.BLAH.com" format into
|
2007-02-28 04:09:19 +05:30
|
|
|
* RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
|
|
|
|
* NULL if an error occurs.
|
|
|
|
*/
|
2020-06-09 20:52:06 +05:30
|
|
|
static uint8_t *convert_dname(const char *src, int *retlen)
|
2007-02-28 04:09:19 +05:30
|
|
|
{
|
2020-07-10 01:17:05 +05:30
|
|
|
uint8_t *res, *lenptr, *dst;
|
2007-02-28 04:09:19 +05:30
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
res = xzalloc(strlen(src) + 2);
|
2010-03-24 20:41:32 +05:30
|
|
|
dst = lenptr = res;
|
|
|
|
dst++;
|
2007-02-28 04:09:19 +05:30
|
|
|
|
|
|
|
for (;;) {
|
2020-07-10 01:17:05 +05:30
|
|
|
uint8_t c;
|
|
|
|
int len;
|
|
|
|
|
2007-02-28 04:09:19 +05:30
|
|
|
c = (uint8_t)*src++;
|
2010-03-24 20:41:32 +05:30
|
|
|
if (c == '.' || c == '\0') { /* end of label */
|
|
|
|
len = dst - lenptr - 1;
|
2020-07-10 01:17:05 +05:30
|
|
|
/* label too long, too short, or two '.'s in a row (len will be 0) */
|
|
|
|
if (len > NS_MAXLABEL || len == 0)
|
|
|
|
goto error;
|
|
|
|
|
2010-03-24 20:41:32 +05:30
|
|
|
*lenptr = len;
|
|
|
|
if (c == '\0' || *src == '\0') /* "" or ".": end of src */
|
2007-02-28 04:09:19 +05:30
|
|
|
break;
|
2010-03-24 20:41:32 +05:30
|
|
|
lenptr = dst++;
|
|
|
|
continue;
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
2020-07-10 01:17:05 +05:30
|
|
|
*dst++ = tolower(c);
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
*retlen = dst + 1 - res;
|
|
|
|
if (*retlen > NS_MAXCDNAME) { /* dname too long? abort */
|
|
|
|
error:
|
2007-02-28 04:09:19 +05:30
|
|
|
free(res);
|
2020-06-09 20:52:06 +05:30
|
|
|
*retlen = 0;
|
2007-02-28 04:09:19 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
2010-03-24 20:41:32 +05:30
|
|
|
|
2007-02-28 04:09:19 +05:30
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-06-09 20:52:06 +05:30
|
|
|
#if 0 //UNUSED
|
2010-03-26 14:39:34 +05:30
|
|
|
/* Returns the offset within cstr at which dname can be found, or -1 */
|
2007-02-28 04:09:19 +05:30
|
|
|
static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
|
|
|
|
{
|
|
|
|
const uint8_t *c, *d;
|
2010-03-24 20:41:32 +05:30
|
|
|
int off;
|
2007-02-28 04:09:19 +05:30
|
|
|
|
|
|
|
/* find all labels in cstr */
|
|
|
|
off = 0;
|
|
|
|
while (off < clen) {
|
|
|
|
c = cstr + off;
|
|
|
|
|
2010-03-24 20:41:32 +05:30
|
|
|
if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* pointer, skip */
|
2007-02-28 04:09:19 +05:30
|
|
|
off += 2;
|
2010-03-24 20:41:32 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*c) { /* label, try matching dname */
|
2007-02-28 04:09:19 +05:30
|
|
|
d = dname;
|
2010-03-24 20:41:32 +05:30
|
|
|
while (1) {
|
|
|
|
unsigned len1 = *c + 1;
|
|
|
|
if (memcmp(c, d, len1) != 0)
|
|
|
|
break;
|
|
|
|
if (len1 == 1) /* at terminating NUL - match, return offset */
|
2007-02-28 04:09:19 +05:30
|
|
|
return off;
|
2010-03-24 20:41:32 +05:30
|
|
|
d += len1;
|
|
|
|
c += len1;
|
2010-03-24 19:36:23 +05:30
|
|
|
if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* pointer, jump */
|
|
|
|
c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
2010-03-24 20:41:32 +05:30
|
|
|
off += cstr[off] + 1;
|
|
|
|
continue;
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
2010-03-24 20:41:32 +05:30
|
|
|
/* NUL, skip */
|
|
|
|
off++;
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2020-06-09 20:52:06 +05:30
|
|
|
#endif
|
2007-02-28 04:09:19 +05:30
|
|
|
|
2020-06-09 20:52:06 +05:30
|
|
|
uint8_t* FAST_FUNC dname_enc(/*const uint8_t *cstr, int clen,*/ const char *src, int *retlen)
|
|
|
|
{
|
|
|
|
#if 0 //UNUSED, was intended for long, repetitive DHCP_DOMAIN_SEARCH options?
|
|
|
|
uint8_t *d, *dname;
|
2010-03-26 14:39:34 +05:30
|
|
|
/* Computes string to be appended to cstr so that src would be added to
|
2007-02-28 04:09:19 +05:30
|
|
|
* the compression (best case, it's a 2-byte pointer to some offset within
|
2010-03-24 20:41:32 +05:30
|
|
|
* cstr; worst case, it's all of src, converted to <4>host<3>com<0> format).
|
2007-02-28 04:09:19 +05:30
|
|
|
* The computed string is returned directly; its length is returned via retlen;
|
|
|
|
* NULL and 0, respectively, are returned if an error occurs.
|
|
|
|
*/
|
2020-06-09 20:52:06 +05:30
|
|
|
dname = convert_dname(src, retlen);
|
2007-02-28 04:09:19 +05:30
|
|
|
if (dname == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-03-24 20:41:32 +05:30
|
|
|
d = dname;
|
|
|
|
while (*d) {
|
|
|
|
if (cstr) {
|
2020-06-09 20:52:06 +05:30
|
|
|
int off = find_offset(cstr, clen, d);
|
2010-03-24 20:41:32 +05:30
|
|
|
if (off >= 0) { /* found a match, add pointer and return */
|
|
|
|
*d++ = NS_CMPRSFLGS | (off >> 8);
|
|
|
|
*d = off;
|
|
|
|
break;
|
|
|
|
}
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
2010-03-24 20:41:32 +05:30
|
|
|
d += *d + 1;
|
2007-02-28 04:09:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
*retlen = d - dname + 1;
|
|
|
|
return dname;
|
2020-06-09 20:52:06 +05:30
|
|
|
#endif
|
|
|
|
return convert_dname(src, retlen);
|
2007-03-07 15:05:43 +05:30
|
|
|
}
|
2010-03-24 21:13:16 +05:30
|
|
|
|
|
|
|
#ifdef DNS_COMPR_TESTING
|
|
|
|
/* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
uint8_t *encoded;
|
|
|
|
|
2016-03-10 16:17:58 +05:30
|
|
|
uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 };
|
|
|
|
printf("NUL:'%s'\n", dname_dec(str, 6, ""));
|
|
|
|
|
2010-03-24 21:13:16 +05:30
|
|
|
#define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre))
|
|
|
|
printf("'%s'\n", DNAME_DEC("\4host\3com\0", "test1:"));
|
|
|
|
printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", ""));
|
|
|
|
printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", ""));
|
|
|
|
printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", ""));
|
|
|
|
printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", ""));
|
|
|
|
|
2020-07-10 01:17:05 +05:30
|
|
|
#if 0
|
2010-03-24 21:13:16 +05:30
|
|
|
#define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
|
|
|
|
encoded = dname_enc(NULL, 0, "test.net", &len);
|
|
|
|
printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
|
|
|
encoded = DNAME_ENC("\3net\0", "test.net", &len);
|
|
|
|
printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
|
|
|
encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
|
|
|
|
printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
2020-07-10 01:17:05 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
encoded = dname_enc("test.net", &len);
|
|
|
|
printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
|
|
|
encoded = dname_enc("test.host.com", &len);
|
|
|
|
printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
|
|
|
|
|
2010-03-24 21:13:16 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|