You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compression.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #include "compression.h"
  24. #include "logger.h"
  25. #include "contrib/mumhash/mum.h"
  26. #define rdns_compression_hash(n) (mum_hash(n.suffix, n.suffix_len, 0xdeadbeef))
  27. #define rdns_compression_equal(n1, n2) ((n1).suffix_len == (n2).suffix_len && \
  28. (memcmp((n1).suffix, (n2).suffix, (n1).suffix_len) == 0))
  29. __KHASH_IMPL(rdns_compression_hash, kh_inline, struct rdns_compression_name, char, 0, rdns_compression_hash,
  30. rdns_compression_equal);
  31. static struct rdns_compression_name *
  32. rdns_can_compress (const char *pos, unsigned int len, khash_t(rdns_compression_hash) *comp)
  33. {
  34. struct rdns_compression_name check;
  35. khiter_t k;
  36. if (comp == NULL) {
  37. return NULL;
  38. }
  39. check.suffix_len = len;
  40. check.suffix = pos;
  41. k = kh_get(rdns_compression_hash, comp, check);
  42. if (k != kh_end(comp)) {
  43. return &kh_key(comp, k);
  44. }
  45. return NULL;
  46. }
  47. static unsigned int
  48. rdns_calculate_label_len (const char *pos, const char *end)
  49. {
  50. const char *p = pos;
  51. unsigned int res = 0;
  52. while (p != end) {
  53. if (*p == '.') {
  54. break;
  55. }
  56. res ++;
  57. p ++;
  58. }
  59. return res;
  60. }
  61. static void
  62. rdns_add_compressed (const char *pos, const char *end,
  63. khash_t(rdns_compression_hash) *comp,
  64. int offset)
  65. {
  66. struct rdns_compression_name new_name;
  67. int r;
  68. if (comp != NULL) {
  69. assert (offset >= 0);
  70. new_name.suffix_len = end - pos;
  71. new_name.suffix = pos;
  72. new_name.offset = offset;
  73. kh_put(rdns_compression_hash, comp, new_name, &r);
  74. }
  75. }
  76. void
  77. rdns_compression_free (khash_t(rdns_compression_hash) *comp)
  78. {
  79. if (comp != NULL) {
  80. kh_destroy(rdns_compression_hash, comp);
  81. }
  82. }
  83. bool
  84. rdns_write_name_compressed (struct rdns_request *req,
  85. const char *name, unsigned int namelen,
  86. khash_t(rdns_compression_hash) **comp)
  87. {
  88. uint8_t *target = req->packet + req->pos;
  89. const char *pos = name, *end = name + namelen;
  90. unsigned int remain = req->packet_len - req->pos - 5, label_len;
  91. struct rdns_resolver *resolver = req->resolver;
  92. uint16_t pointer;
  93. if (comp != NULL && *comp == NULL) {
  94. *comp = kh_init(rdns_compression_hash);
  95. }
  96. while (pos < end && remain > 0) {
  97. if (comp) {
  98. struct rdns_compression_name *test = rdns_can_compress(pos, end - pos, *comp);
  99. if (test != NULL) {
  100. /* Can compress name */
  101. if (remain < 2) {
  102. rdns_info ("no buffer remain for constructing query");
  103. return false;
  104. }
  105. pointer = htons ((uint16_t) test->offset) | DNS_COMPRESSION_BITS;
  106. memcpy(target, &pointer, sizeof(pointer));
  107. req->pos += 2;
  108. return true;
  109. }
  110. }
  111. label_len = rdns_calculate_label_len (pos, end);
  112. if (label_len == 0) {
  113. /* We have empty label it is allowed only if pos == end - 1 */
  114. if (pos == end - 1) {
  115. break;
  116. }
  117. else {
  118. rdns_err ("double dots in the name requested");
  119. return false;
  120. }
  121. }
  122. else if (label_len > DNS_D_MAXLABEL) {
  123. rdns_err ("too large label: %d", (int)label_len);
  124. return false;
  125. }
  126. if (label_len + 1 > remain) {
  127. rdns_info ("no buffer remain for constructing query, strip %d to %d",
  128. (int)label_len, (int)remain);
  129. label_len = remain - 1;
  130. }
  131. if (comp) {
  132. rdns_add_compressed(pos, end, *comp, target - req->packet);
  133. }
  134. /* Write label as is */
  135. *target++ = (uint8_t)label_len;
  136. memcpy (target, pos, label_len);
  137. target += label_len;
  138. pos += label_len + 1;
  139. }
  140. /* Termination label */
  141. *target++ = '\0';
  142. req->pos = target - req->packet;
  143. return true;
  144. }