domain_codec.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. # define xzalloc(s) calloc(s, 1)
  14. # define xstrdup strdup
  15. # define xrealloc realloc
  16. # include <stdlib.h>
  17. # include <stdint.h>
  18. # include <string.h>
  19. # include <stdio.h>
  20. # include <ctype.h>
  21. #else
  22. # include "common.h"
  23. #endif
  24. #define NS_MAXDNAME 1025 /* max domain name length */
  25. #define NS_MAXCDNAME 255 /* max compressed domain name length */
  26. #define NS_MAXLABEL 63 /* max label length */
  27. #define NS_MAXDNSRCH 6 /* max domains in search path */
  28. #define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */
  29. /* Expand a RFC1035-compressed list of domain names "cstr", of length "clen";
  30. * return a newly allocated string containing the space-separated domains,
  31. * prefixed with the contents of string pre, or NULL if an error occurs.
  32. */
  33. char* FAST_FUNC dname_dec(const uint8_t *cstr, int clen, const char *pre)
  34. {
  35. char *ret, *end;
  36. unsigned len, crtpos, retpos, depth;
  37. crtpos = retpos = depth = 0;
  38. len = strlen(pre);
  39. end = ret = xstrdup(pre);
  40. /* Scan the string once, allocating new memory as needed */
  41. while (crtpos < clen) {
  42. const uint8_t *c;
  43. c = cstr + crtpos;
  44. if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
  45. /* pointer */
  46. if (crtpos + 2 > clen) /* no offset to jump to? abort */
  47. goto error;
  48. if (retpos == 0) /* toplevel? save return spot */
  49. retpos = crtpos + 2;
  50. depth++;
  51. crtpos = ((c[0] << 8) | c[1]) & 0x3fff; /* jump */
  52. } else if (*c) {
  53. unsigned label_len;
  54. /* label */
  55. if (crtpos + *c + 1 > clen) /* label too long? abort */
  56. goto error;
  57. ret = xrealloc(ret, len + *c + 1);
  58. /* \3com ---> "com." */
  59. end = (char *)mempcpy(ret + len, c + 1, *c);
  60. *end = '.';
  61. label_len = *c + 1;
  62. len += label_len;
  63. crtpos += label_len;
  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 (len != 0) {
  75. /* \4host\3com\0\4host and we are at \0:
  76. * \3com was converted to "com.", change dot to space.
  77. */
  78. ret[len - 1] = ' ';
  79. }
  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. goto error;
  85. }
  86. }
  87. if (ret == end) { /* expanded string is empty? abort */
  88. error:
  89. free(ret);
  90. return NULL;
  91. }
  92. *end = '\0';
  93. return ret;
  94. }
  95. /* Convert a domain name (src) from human-readable "foo.BLAH.com" format into
  96. * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
  97. * NULL if an error occurs.
  98. */
  99. static uint8_t *convert_dname(const char *src, int *retlen)
  100. {
  101. uint8_t *res, *lenptr, *dst;
  102. res = xzalloc(strlen(src) + 2);
  103. dst = lenptr = res;
  104. dst++;
  105. for (;;) {
  106. uint8_t c;
  107. int len;
  108. c = (uint8_t)*src++;
  109. if (c == '.' || c == '\0') { /* end of label */
  110. len = dst - lenptr - 1;
  111. /* label too long, too short, or two '.'s in a row (len will be 0) */
  112. if (len > NS_MAXLABEL || len == 0)
  113. goto error;
  114. *lenptr = len;
  115. if (c == '\0' || *src == '\0') /* "" or ".": end of src */
  116. break;
  117. lenptr = dst++;
  118. continue;
  119. }
  120. *dst++ = tolower(c);
  121. }
  122. *retlen = dst + 1 - res;
  123. if (*retlen > NS_MAXCDNAME) { /* dname too long? abort */
  124. error:
  125. free(res);
  126. *retlen = 0;
  127. return NULL;
  128. }
  129. return res;
  130. }
  131. #if 0 //UNUSED
  132. /* Returns the offset within cstr at which dname can be found, or -1 */
  133. static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
  134. {
  135. const uint8_t *c, *d;
  136. int off;
  137. /* find all labels in cstr */
  138. off = 0;
  139. while (off < clen) {
  140. c = cstr + off;
  141. if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) { /* pointer, skip */
  142. off += 2;
  143. continue;
  144. }
  145. if (*c) { /* label, try matching dname */
  146. d = dname;
  147. while (1) {
  148. unsigned len1 = *c + 1;
  149. if (memcmp(c, d, len1) != 0)
  150. break;
  151. if (len1 == 1) /* at terminating NUL - match, return offset */
  152. return off;
  153. d += len1;
  154. c += len1;
  155. if ((*c & NS_CMPRSFLGS) == NS_CMPRSFLGS) /* pointer, jump */
  156. c = cstr + (((c[0] & 0x3f) << 8) | c[1]);
  157. }
  158. off += cstr[off] + 1;
  159. continue;
  160. }
  161. /* NUL, skip */
  162. off++;
  163. }
  164. return -1;
  165. }
  166. #endif
  167. uint8_t* FAST_FUNC dname_enc(/*const uint8_t *cstr, int clen,*/ const char *src, int *retlen)
  168. {
  169. #if 0 //UNUSED, was intended for long, repetitive DHCP_DOMAIN_SEARCH options?
  170. uint8_t *d, *dname;
  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. dname = convert_dname(src, retlen);
  178. if (dname == NULL) {
  179. return NULL;
  180. }
  181. d = dname;
  182. while (*d) {
  183. if (cstr) {
  184. int off = find_offset(cstr, clen, d);
  185. if (off >= 0) { /* found a match, add pointer and return */
  186. *d++ = NS_CMPRSFLGS | (off >> 8);
  187. *d = off;
  188. break;
  189. }
  190. }
  191. d += *d + 1;
  192. }
  193. *retlen = d - dname + 1;
  194. return dname;
  195. #endif
  196. return convert_dname(src, retlen);
  197. }
  198. #ifdef DNS_COMPR_TESTING
  199. /* gcc -Wall -DDNS_COMPR_TESTING domain_codec.c -o domain_codec && ./domain_codec */
  200. int main(int argc, char **argv)
  201. {
  202. int len;
  203. uint8_t *encoded;
  204. uint8_t str[6] = { 0x00, 0x00, 0x02, 0x65, 0x65, 0x00 };
  205. printf("NUL:'%s'\n", dname_dec(str, 6, ""));
  206. #define DNAME_DEC(encoded,pre) dname_dec((uint8_t*)(encoded), sizeof(encoded), (pre))
  207. printf("'%s'\n", DNAME_DEC("\4host\3com\0", "test1:"));
  208. printf("test2:'%s'\n", DNAME_DEC("\4host\3com\0\4host\3com\0", ""));
  209. printf("test3:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\0", ""));
  210. printf("test4:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5", ""));
  211. printf("test5:'%s'\n", DNAME_DEC("\4host\3com\0\xC0\5\1z\xC0\xA", ""));
  212. #if 0
  213. #define DNAME_ENC(cache,source,lenp) dname_enc((uint8_t*)(cache), sizeof(cache), (source), (lenp))
  214. encoded = dname_enc(NULL, 0, "test.net", &len);
  215. printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  216. encoded = DNAME_ENC("\3net\0", "test.net", &len);
  217. printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  218. encoded = DNAME_ENC("\4test\3net\0", "test.net", &len);
  219. printf("test8:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  220. #endif
  221. encoded = dname_enc("test.net", &len);
  222. printf("test6:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  223. encoded = dname_enc("test.host.com", &len);
  224. printf("test7:'%s' len:%d\n", dname_dec(encoded, len, ""), len);
  225. return 0;
  226. }
  227. #endif