3
0

domain_codec.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #ifdef DNS_COMPR_TESTING
  10. # define _GNU_SOURCE
  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
  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. /* Expand a RFC1035-compressed list of domain names "cstr", of length "clen";
  26. * returns a newly allocated string containing the space-separated domains,
  27. * prefixed with the contents of string pre, or NULL if an error occurs.
  28. */
  29. char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
  30. {
  31. char *ret = ret; /* for compiler */
  32. char *dst = NULL;
  33. /* We make two passes over the cstr string. First, we compute
  34. * how long the resulting string would be. Then we allocate a
  35. * new buffer of the required length, and fill it in with the
  36. * expanded content. The advantage of this approach is not
  37. * having to deal with requiring callers to supply their own
  38. * buffer, then having to check if it's sufficiently large, etc.
  39. */
  40. while (1) {
  41. /* note: "return NULL" below are leak-safe since
  42. * dst isn't allocated yet */
  43. const uint8_t *c;
  44. unsigned crtpos, retpos, depth, len;
  45. crtpos = retpos = depth = len = 0;
  46. while (crtpos < clen) {
  47. c = cstr + crtpos;
  48. if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  49. /* pointer */
  50. if (crtpos + 2 > clen) /* no offset to jump to? abort */
  51. return NULL;
  52. if (retpos == 0) /* toplevel? save return spot */
  53. retpos = crtpos + 2;
  54. depth++;
  55. crtpos = ((c[0] & 0x3f) << 8) | c[1]; /* jump */
  56. } else if (*c) {
  57. /* label */
  58. if (crtpos + *c + 1 > clen) /* label too long? abort */
  59. return NULL;
  60. if (dst)
  61. /* \3com ---> "com." */
  62. ((char*)mempcpy(dst + len, c + 1, *c))[0] = '.';
  63. len += *c + 1;
  64. crtpos += *c + 1;
  65. } else {
  66. /* NUL: end of current domain name */
  67. if (retpos == 0) {
  68. /* toplevel? keep going */
  69. crtpos++;
  70. } else {
  71. /* return to toplevel saved spot */
  72. crtpos = retpos;
  73. retpos = depth = 0;
  74. }
  75. if (dst && len != 0)
  76. /* \4host\3com\0\4host and we are at \0:
  77. * \3com was converted to "com.", change dot to space.
  78. */
  79. dst[len - 1] = ' ';
  80. }
  81. if (depth > NS_MAXDNSRCH /* too many jumps? abort, it's a loop */
  82. || len > NS_MAXDNAME * NS_MAXDNSRCH /* result too long? abort */
  83. ) {
  84. return NULL;
  85. }
  86. }
  87. if (!len) /* expanded string has 0 length? abort */
  88. return NULL;
  89. if (!dst) { /* first pass? */
  90. /* allocate dst buffer and copy pre */
  91. unsigned plen = strlen(pre);
  92. ret = xmalloc(plen + len);
  93. dst = stpcpy(ret, pre);
  94. } else {
  95. dst[len - 1] = '\0';
  96. break;
  97. }
  98. }
  99. return ret;
  100. }
  101. /* Convert a domain name (src) from human-readable "foo.blah.com" format into
  102. * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
  103. * NULL if an error occurs.
  104. */
  105. static uint8_t *convert_dname(const char *src)
  106. {
  107. uint8_t c, *res, *lenptr, *dst;
  108. int len;
  109. res = xmalloc(strlen(src) + 2);
  110. dst = lenptr = res;
  111. dst++;
  112. for (;;) {
  113. c = (uint8_t)*src++;
  114. if (c == '.' || c == '\0') { /* end of label */
  115. len = dst - lenptr - 1;
  116. /* label too long, too short, or two '.'s in a row? abort */
  117. if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
  118. free(res);
  119. return NULL;
  120. }
  121. *lenptr = len;
  122. if (c == '\0' || *src == '\0') /* "" or ".": end of src */
  123. break;
  124. lenptr = dst++;
  125. continue;
  126. }
  127. if (c >= 'A' && c <= 'Z') /* uppercase? convert to lower */
  128. c += ('a' - 'A');
  129. *dst++ = c;
  130. }
  131. if (dst - res >= NS_MAXCDNAME) { /* dname too long? abort */
  132. free(res);
  133. return NULL;
  134. }
  135. *dst = 0;
  136. return res;
  137. }
  138. /* Returns the offset within cstr at which dname can be found, or -1 */
  139. static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
  140. {
  141. const uint8_t *c, *d;
  142. int off;
  143. /* find all labels in cstr */
  144. off = 0;
  145. while (off < clen) {
  146. c = cstr + off;
  147. if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* pointer, skip */
  148. off += 2;
  149. continue;
  150. }
  151. if (*c) { /* label, try matching dname */
  152. d = dname;
  153. while (1) {
  154. unsigned len1 = *c + 1;
  155. if (memcmp(c, d, len1) != 0)
  156. break;
  157. if (len1 == 1) /* at terminating NUL - match, return offset */
  158. return off;
  159. d += len1;
  160. c += len1;
  161. if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* pointer, jump */
  162. c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
  163. }
  164. off += cstr[off] + 1;
  165. continue;
  166. }
  167. /* NUL, skip */
  168. off++;
  169. }
  170. return -1;
  171. }
  172. /* Computes string to be appended to cstr so that src would be added to
  173. * the compression (best case, it's a 2-byte pointer to some offset within
  174. * cstr; worst case, it's all of src, converted to <4>host<3>com<0> format).
  175. * The computed string is returned directly; its length is returned via retlen;
  176. * NULL and 0, respectively, are returned if an error occurs.
  177. */
  178. uint8_t* FAST_FUNC dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
  179. {
  180. uint8_t *d, *dname;
  181. int off;
  182. dname = convert_dname(src);
  183. if (dname == NULL) {
  184. *retlen = 0;
  185. return NULL;
  186. }
  187. d = dname;
  188. while (*d) {
  189. if (cstr) {
  190. off = find_offset(cstr, clen, d);
  191. if (off >= 0) { /* found a match, add pointer and return */
  192. *d++ = NS_CMPRSFLGS | (off >> 8);
  193. *d = off;
  194. break;
  195. }
  196. }
  197. d += *d + 1;
  198. }
  199. *retlen = d - dname + 1;
  200. return dname;
  201. }
  202. #ifdef DNS_COMPR_TESTING
  203. /* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */
  204. int main(int argc, char **argv)
  205. {
  206. int len;
  207. uint8_t *encoded;
  208. uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 };
  209. printf("NUL:'%s'\n", dname_dec(str, 6, ""));
  210. #define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre))
  211. printf("'%s'\n", DNAME_DEC("\4host\3com\0", "test1:"));
  212. printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", ""));
  213. printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", ""));
  214. printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", ""));
  215. printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", ""));
  216. #define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
  217. encoded = dname_enc(NULL, 0, "test.net", &len);
  218. printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  219. encoded = DNAME_ENC("\3net\0", "test.net", &len);
  220. printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  221. encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
  222. printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  223. return 0;
  224. }
  225. #endif