ns_name_pack.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Compression of DNS domain names.
  2. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (c) 1996,1999 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <arpa/nameser.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <shlib-compat.h>
  21. /* Thinking in noninternationalized USASCII (per the DNS spec),
  22. convert this character to lower case if it's upper case. */
  23. static int
  24. mklower (int ch)
  25. {
  26. if (ch >= 'A' && ch <= 'Z')
  27. return ch - 'A' + 'a';
  28. return ch;
  29. }
  30. /* Search for the counted-label name in an array of compressed names.
  31. Returns the offset from MSG if found, or -1.
  32. DNPTRS is the pointer to the first name on the list, not the
  33. pointer to the start of the message. */
  34. static int
  35. dn_find (const unsigned char *domain, const unsigned char *msg,
  36. const unsigned char **dnptrs,
  37. const unsigned char **lastdnptr)
  38. {
  39. const unsigned char *dn, *cp, *sp;
  40. const unsigned char **cpp;
  41. unsigned int n;
  42. for (cpp = dnptrs; cpp < lastdnptr; cpp++)
  43. {
  44. sp = *cpp;
  45. /* Terminate search on: root label, compression pointer, unusable
  46. offset. */
  47. while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 && (sp - msg) < 0x4000)
  48. {
  49. dn = domain;
  50. cp = sp;
  51. while ((n = *cp++) != 0)
  52. {
  53. /* Check for indirection. */
  54. switch (n & NS_CMPRSFLGS)
  55. {
  56. case 0: /* Normal case, n == len. */
  57. if (n != *dn++)
  58. goto next;
  59. for (; n > 0; n--)
  60. if (mklower (*dn++) != mklower (*cp++))
  61. goto next;
  62. /* Is next root for both? */
  63. if (*dn == '\0' && *cp == '\0')
  64. return sp - msg;
  65. if (*dn)
  66. continue;
  67. goto next;
  68. case NS_CMPRSFLGS: /* Indirection. */
  69. cp = msg + (((n & 0x3f) << 8) | *cp);
  70. break;
  71. default: /* Illegal type. */
  72. __set_errno (EMSGSIZE);
  73. return -1;
  74. }
  75. }
  76. next: ;
  77. sp += *sp + 1;
  78. }
  79. }
  80. __set_errno (ENOENT);
  81. return -1;
  82. }
  83. /* Packs domain name SRC into DST. Returns size of the compressed
  84. name, or -1.
  85. DNPTRS is an array of pointers to previous compressed names.
  86. DNPTRS[0] is a pointer to the beginning of the message. The array
  87. ends with NULL. LASTDNPTR is a pointer to the end of the array
  88. pointed to by 'dnptrs'.
  89. The list of pointers in DNPTRS is updated for labels inserted into
  90. the message as we compress the name. If DNPTRS is NULL, we don't
  91. try to compress names. If LASTDNPTR is NULL, we don't update the
  92. list. */
  93. int
  94. ___ns_name_pack (const unsigned char *src, unsigned char *dst, int dstsiz,
  95. const unsigned char **dnptrs, const unsigned char **lastdnptr)
  96. {
  97. unsigned char *dstp;
  98. const unsigned char **cpp, **lpp, *eob, *msg;
  99. const unsigned char *srcp;
  100. int n, l, first = 1;
  101. srcp = src;
  102. dstp = dst;
  103. eob = dstp + dstsiz;
  104. lpp = cpp = NULL;
  105. if (dnptrs != NULL)
  106. {
  107. if ((msg = *dnptrs++) != NULL)
  108. {
  109. for (cpp = dnptrs; *cpp != NULL; cpp++)
  110. ;
  111. lpp = cpp; /* End of list to search. */
  112. }
  113. }
  114. else
  115. msg = NULL;
  116. /* Make sure the domain we are about to add is legal. */
  117. l = 0;
  118. do
  119. {
  120. n = *srcp;
  121. if (n >= 64)
  122. {
  123. __set_errno (EMSGSIZE);
  124. return -1;
  125. }
  126. l += n + 1;
  127. if (l > MAXCDNAME)
  128. {
  129. __set_errno (EMSGSIZE);
  130. return -1;
  131. }
  132. srcp += n + 1;
  133. }
  134. while (n != 0);
  135. /* from here on we need to reset compression pointer array on error */
  136. srcp = src;
  137. do
  138. {
  139. /* Look to see if we can use pointers. */
  140. n = *srcp;
  141. if (n != 0 && msg != NULL)
  142. {
  143. l = dn_find (srcp, msg, dnptrs, lpp);
  144. if (l >= 0)
  145. {
  146. if (eob - dstp <= 1)
  147. goto cleanup;
  148. *dstp++ = (l >> 8) | NS_CMPRSFLGS;
  149. *dstp++ = l % 256;
  150. return dstp - dst;
  151. }
  152. /* Not found, save it. */
  153. if (lastdnptr != NULL && cpp < lastdnptr - 1
  154. && (dstp - msg) < 0x4000 && first)
  155. {
  156. *cpp++ = dstp;
  157. *cpp = NULL;
  158. first = 0;
  159. }
  160. }
  161. /* Copy label to buffer. */
  162. if (n >= 64)
  163. /* Should not happen. */
  164. goto cleanup;
  165. if (n + 1 > eob - dstp)
  166. goto cleanup;
  167. memcpy (dstp, srcp, n + 1);
  168. srcp += n + 1;
  169. dstp += n + 1;
  170. }
  171. while (n != 0);
  172. if (dstp > eob)
  173. {
  174. cleanup:
  175. if (msg != NULL)
  176. *lpp = NULL;
  177. __set_errno (EMSGSIZE);
  178. return -1;
  179. }
  180. return dstp - dst;
  181. }
  182. versioned_symbol (libc, ___ns_name_pack, ns_name_pack, GLIBC_2_34);
  183. versioned_symbol (libc, ___ns_name_pack, __ns_name_pack, GLIBC_PRIVATE);
  184. libc_hidden_ver (___ns_name_pack, __ns_name_pack)
  185. #if OTHER_SHLIB_COMPAT (libresolv, GLIBC_2_9, GLIBC_2_34)
  186. compat_symbol (libresolv, ___ns_name_pack, ns_name_pack, GLIBC_2_9);
  187. #endif