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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. static struct rdns_compression_entry *
  26. rdns_can_compress (const char *pos, struct rdns_compression_entry *comp)
  27. {
  28. struct rdns_compression_entry *res;
  29. HASH_FIND_STR (comp, pos, res);
  30. return res;
  31. }
  32. static unsigned int
  33. rdns_calculate_label_len (const char *pos, const char *end)
  34. {
  35. const char *p = pos;
  36. unsigned int res = 0;
  37. while (p != end) {
  38. if (*p == '.') {
  39. break;
  40. }
  41. res ++;
  42. p ++;
  43. }
  44. return res;
  45. }
  46. static void
  47. rdns_add_compressed (const char *pos, const char *end,
  48. struct rdns_compression_entry **comp, int offset)
  49. {
  50. struct rdns_compression_entry *new;
  51. assert (offset >= 0);
  52. new = malloc (sizeof (*new));
  53. if (new != NULL) {
  54. new->label = pos;
  55. new->offset = offset;
  56. HASH_ADD_KEYPTR (hh, *comp, pos, (end - pos), new);
  57. }
  58. }
  59. void
  60. rnds_compression_free (struct rdns_compression_entry *comp)
  61. {
  62. struct rdns_compression_entry *cur, *tmp;
  63. if (comp) {
  64. free (comp->hh.tbl->buckets);
  65. free (comp->hh.tbl);
  66. HASH_ITER (hh, comp, cur, tmp) {
  67. free (cur);
  68. }
  69. }
  70. }
  71. bool
  72. rdns_write_name_compressed (struct rdns_request *req,
  73. const char *name, unsigned int namelen,
  74. struct rdns_compression_entry **comp)
  75. {
  76. uint8_t *target = req->packet + req->pos;
  77. const char *pos = name, *end = name + namelen;
  78. unsigned int remain = req->packet_len - req->pos - 5, label_len;
  79. struct rdns_compression_entry *head = NULL, *test;
  80. struct rdns_resolver *resolver = req->resolver;
  81. uint16_t pointer;
  82. if (comp != NULL) {
  83. head = *comp;
  84. }
  85. while (pos < end && remain > 0) {
  86. if (head != NULL) {
  87. test = rdns_can_compress (pos, head);
  88. if (test != NULL) {
  89. if (remain < 2) {
  90. rdns_info ("no buffer remain for constructing query");
  91. return false;
  92. }
  93. pointer = htons ((uint16_t)test->offset) | DNS_COMPRESSION_BITS;
  94. memcpy (target, &pointer, sizeof (pointer));
  95. req->pos += 2;
  96. return true;
  97. }
  98. }
  99. label_len = rdns_calculate_label_len (pos, end);
  100. if (label_len == 0) {
  101. /* We have empty label it is allowed only if pos == end - 1 */
  102. if (pos == end - 1) {
  103. break;
  104. }
  105. else {
  106. rdns_err ("double dots in the name requested");
  107. return false;
  108. }
  109. }
  110. else if (label_len > DNS_D_MAXLABEL) {
  111. rdns_err ("too large label: %d", (int)label_len);
  112. return false;
  113. }
  114. if (label_len + 1 > remain) {
  115. rdns_info ("no buffer remain for constructing query, strip %d to %d",
  116. (int)label_len, (int)remain);
  117. label_len = remain - 1;
  118. }
  119. if (comp != NULL) {
  120. rdns_add_compressed (pos, end, comp, target - req->packet);
  121. }
  122. /* Write label as is */
  123. *target++ = (uint8_t)label_len;
  124. memcpy (target, pos, label_len);
  125. target += label_len;
  126. pos += label_len + 1;
  127. }
  128. /* Termination label */
  129. *target++ = '\0';
  130. req->pos = target - req->packet;
  131. return true;
  132. }