3
0

domain_codec.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 tarball for details.
  7. */
  8. #if ENABLE_FEATURE_RFC3397
  9. #include "common.h"
  10. #include "options.h"
  11. #define NS_MAXDNAME 1025 /* max domain name length */
  12. #define NS_MAXCDNAME 255 /* max compressed domain name length */
  13. #define NS_MAXLABEL 63 /* max label length */
  14. #define NS_MAXDNSRCH 6 /* max domains in search path */
  15. #define NS_CMPRSFLGS 0xc0 /* name compression pointer flag */
  16. /* expand a RFC1035-compressed list of domain names "cstr", of length "clen";
  17. * returns a newly allocated string containing the space-separated domains,
  18. * prefixed with the contents of string pre, or NULL if an error occurs.
  19. */
  20. char *dname_dec(const uint8_t *cstr, int clen, const char *pre)
  21. {
  22. const uint8_t *c;
  23. int crtpos, retpos, depth, plen = 0, len = 0;
  24. char *dst = NULL;
  25. if (!cstr)
  26. return NULL;
  27. if (pre)
  28. plen = strlen(pre);
  29. /* We make two passes over the cstr string. First, we compute
  30. * how long the resulting string would be. Then we allocate a
  31. * new buffer of the required length, and fill it in with the
  32. * expanded content. The advantage of this approach is not
  33. * having to deal with requiring callers to supply their own
  34. * buffer, then having to check if it's sufficiently large, etc.
  35. */
  36. while (!dst) {
  37. if (len > 0) { /* second pass? allocate dst buffer and copy pre */
  38. dst = xmalloc(len + plen);
  39. memcpy(dst, pre, plen);
  40. }
  41. crtpos = retpos = depth = len = 0;
  42. while (crtpos < clen) {
  43. c = cstr + crtpos;
  44. if ((*c & NS_CMPRSFLGS) != 0) { /* pointer */
  45. if (crtpos + 2 > clen) /* no offset to jump to? abort */
  46. return NULL;
  47. if (retpos == 0) /* toplevel? save return spot */
  48. retpos = crtpos + 2;
  49. depth++;
  50. crtpos = ((*c & 0x3f) << 8) | (*(c + 1) & 0xff); /* jump */
  51. } else if (*c) { /* label */
  52. if (crtpos + *c + 1 > clen) /* label too long? abort */
  53. return NULL;
  54. if (dst)
  55. memcpy(dst + plen + len, c + 1, *c);
  56. len += *c + 1;
  57. crtpos += *c + 1;
  58. if (dst)
  59. *(dst + plen + len - 1) = '.';
  60. } else { /* null: end of current domain name */
  61. if (retpos == 0) { /* toplevel? keep going */
  62. crtpos++;
  63. } else { /* return to toplevel saved spot */
  64. crtpos = retpos;
  65. retpos = depth = 0;
  66. }
  67. if (dst)
  68. *(dst + plen + len - 1) = ' ';
  69. }
  70. if (depth > NS_MAXDNSRCH || /* too many jumps? abort, it's a loop */
  71. len > NS_MAXDNAME * NS_MAXDNSRCH) /* result too long? abort */
  72. return NULL;
  73. }
  74. if (!len) /* expanded string has 0 length? abort */
  75. return NULL;
  76. if (dst)
  77. *(dst + plen + len - 1) = '\0';
  78. }
  79. return dst;
  80. }
  81. /* Convert a domain name (src) from human-readable "foo.blah.com" format into
  82. * RFC1035 encoding "\003foo\004blah\003com\000". Return allocated string, or
  83. * NULL if an error occurs.
  84. */
  85. static uint8_t *convert_dname(const char *src)
  86. {
  87. uint8_t c, *res, *lp, *rp;
  88. int len;
  89. res = xmalloc(strlen(src) + 2);
  90. rp = lp = res;
  91. rp++;
  92. for (;;) {
  93. c = (uint8_t)*src++;
  94. if (c == '.' || c == '\0') { /* end of label */
  95. len = rp - lp - 1;
  96. /* label too long, too short, or two '.'s in a row? abort */
  97. if (len > NS_MAXLABEL || len == 0 || (c == '.' && *src == '.')) {
  98. free(res);
  99. return NULL;
  100. }
  101. *lp = len;
  102. lp = rp++;
  103. if (c == '\0' || *src == '\0') /* end of dname */
  104. break;
  105. } else {
  106. if (c >= 0x41 && c <= 0x5A) /* uppercase? convert to lower */
  107. c += 0x20;
  108. *rp++ = c;
  109. }
  110. }
  111. *lp = 0;
  112. if (rp - res > NS_MAXCDNAME) { /* dname too long? abort */
  113. free(res);
  114. return NULL;
  115. }
  116. return res;
  117. }
  118. /* returns the offset within cstr at which dname can be found, or -1
  119. */
  120. static int find_offset(const uint8_t *cstr, int clen, const uint8_t *dname)
  121. {
  122. const uint8_t *c, *d;
  123. int off, inc;
  124. /* find all labels in cstr */
  125. off = 0;
  126. while (off < clen) {
  127. c = cstr + off;
  128. if ((*c & NS_CMPRSFLGS) != 0) { /* pointer, skip */
  129. off += 2;
  130. } else if (*c) { /* label, try matching dname */
  131. inc = *c + 1;
  132. d = dname;
  133. while (*c == *d && memcmp(c + 1, d + 1, *c) == 0) {
  134. if (*c == 0) /* match, return offset */
  135. return off;
  136. d += *c + 1;
  137. c += *c + 1;
  138. if ((*c & NS_CMPRSFLGS) != 0) /* pointer, jump */
  139. c = cstr + (((*c & 0x3f) << 8) | (*(c + 1) & 0xff));
  140. }
  141. off += inc;
  142. } else { /* null, skip */
  143. off++;
  144. }
  145. }
  146. return -1;
  147. }
  148. /* computes string to be appended to cstr so that src would be added to
  149. * the compression (best case, it's a 2-byte pointer to some offset within
  150. * cstr; worst case, it's all of src, converted to rfc3011 format).
  151. * The computed string is returned directly; its length is returned via retlen;
  152. * NULL and 0, respectively, are returned if an error occurs.
  153. */
  154. uint8_t *dname_enc(const uint8_t *cstr, int clen, const char *src, int *retlen)
  155. {
  156. uint8_t *d, *dname;
  157. int off;
  158. dname = convert_dname(src);
  159. if (dname == NULL) {
  160. *retlen = 0;
  161. return NULL;
  162. }
  163. for (d = dname; *d != 0; d += *d + 1) {
  164. off = find_offset(cstr, clen, d);
  165. if (off >= 0) { /* found a match, add pointer and terminate string */
  166. *d++ = NS_CMPRSFLGS;
  167. *d = off;
  168. break;
  169. }
  170. }
  171. *retlen = d - dname + 1;
  172. return dname;
  173. }
  174. #endif /* ENABLE_FEATURE_RFC3397 */