domain_codec.c 6.9 KB

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