domain_codec.c 5.3 KB

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