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.
 
 
 
 
 
 

149 lines
5.3 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #pragma once
  23. #include "ednsoptions.hh"
  24. #include "misc.hh"
  25. #include "iputils.hh"
  26. class PacketCache : public boost::noncopyable
  27. {
  28. public:
  29. static uint32_t canHashPacket(const std::string& packet, uint16_t* ecsBegin, uint16_t* ecsEnd)
  30. {
  31. uint32_t ret = 0;
  32. ret = burtle(reinterpret_cast<const unsigned char*>(packet.c_str()) + 2, sizeof(dnsheader) - 2, ret); // rest of dnsheader, skip id
  33. size_t packetSize = packet.size();
  34. size_t pos = sizeof(dnsheader);
  35. const char* end = packet.c_str() + packetSize;
  36. const char* p = packet.c_str() + pos;
  37. for(; p < end && *p; ++p, ++pos) { // XXX if you embed a 0 in your qname we'll stop lowercasing there
  38. const unsigned char l = dns_tolower(*p); // label lengths can safely be lower cased
  39. ret=burtle(&l, 1, ret);
  40. } // XXX the embedded 0 in the qname will break the subnet stripping
  41. const struct dnsheader* dh = reinterpret_cast<const struct dnsheader*>(packet.c_str());
  42. const char* skipBegin = p;
  43. const char* skipEnd = p;
  44. if (ecsBegin != nullptr && ecsEnd != nullptr) {
  45. *ecsBegin = 0;
  46. *ecsEnd = 0;
  47. }
  48. /* we need at least 1 (final empty label) + 2 (QTYPE) + 2 (QCLASS)
  49. + OPT root label (1), type (2), class (2) and ttl (4)
  50. + the OPT RR rdlen (2)
  51. = 16
  52. */
  53. if(ntohs(dh->arcount)==1 && (pos+16) < packetSize) {
  54. char* optionBegin = nullptr;
  55. size_t optionLen = 0;
  56. /* skip the final empty label (1), the qtype (2), qclass (2) */
  57. /* root label (1), type (2), class (2) and ttl (4) */
  58. int res = getEDNSOption(const_cast<char*>(reinterpret_cast<const char*>(p)) + 14, end - (p + 14), EDNSOptionCode::ECS, &optionBegin, &optionLen);
  59. if (res == 0) {
  60. skipBegin = optionBegin;
  61. skipEnd = optionBegin + optionLen;
  62. if (ecsBegin != nullptr && ecsEnd != nullptr) {
  63. *ecsBegin = optionBegin - packet.c_str();
  64. *ecsEnd = *ecsBegin + optionLen;
  65. }
  66. }
  67. }
  68. if (skipBegin > p) {
  69. ret = burtle(reinterpret_cast<const unsigned char*>(p), skipBegin-p, ret);
  70. }
  71. if (skipEnd < end) {
  72. ret = burtle(reinterpret_cast<const unsigned char*>(skipEnd), end-skipEnd, ret);
  73. }
  74. return ret;
  75. }
  76. static uint32_t canHashPacket(const std::string& packet)
  77. {
  78. uint32_t ret = 0;
  79. ret = burtle(reinterpret_cast<const unsigned char*>(packet.c_str()) + 2, sizeof(dnsheader) - 2, ret); // rest of dnsheader, skip id
  80. size_t packetSize = packet.size();
  81. size_t pos = sizeof(dnsheader);
  82. const char* end = packet.c_str() + packetSize;
  83. const char* p = packet.c_str() + pos;
  84. for(; p < end && *p; ++p) { // XXX if you embed a 0 in your qname we'll stop lowercasing there
  85. const unsigned char l = dns_tolower(*p); // label lengths can safely be lower cased
  86. ret=burtle(&l, 1, ret);
  87. } // XXX the embedded 0 in the qname will break the subnet stripping
  88. if (p < end) {
  89. ret = burtle(reinterpret_cast<const unsigned char*>(p), end-p, ret);
  90. }
  91. return ret;
  92. }
  93. static bool queryHeaderMatches(const std::string& cachedQuery, const std::string& query)
  94. {
  95. if (cachedQuery.size() != query.size()) {
  96. return false;
  97. }
  98. return (cachedQuery.compare(/* skip the ID */ 2, sizeof(dnsheader) - 2, query, 2, sizeof(dnsheader) - 2) == 0);
  99. }
  100. static bool queryMatches(const std::string& cachedQuery, const std::string& query, const DNSName& qname)
  101. {
  102. if (!queryHeaderMatches(cachedQuery, query)) {
  103. return false;
  104. }
  105. size_t pos = sizeof(dnsheader) + qname.wirelength();
  106. return (cachedQuery.compare(pos, cachedQuery.size() - pos, query, pos, query.size() - pos) == 0);
  107. }
  108. static bool queryMatches(const std::string& cachedQuery, const std::string& query, const DNSName& qname, uint16_t ecsBegin, uint16_t ecsEnd)
  109. {
  110. if (!queryHeaderMatches(cachedQuery, query)) {
  111. return false;
  112. }
  113. size_t pos = sizeof(dnsheader) + qname.wirelength();
  114. if (ecsBegin != 0 && ecsBegin >= pos && ecsEnd > ecsBegin) {
  115. if (cachedQuery.compare(pos, ecsBegin - pos, query, pos, ecsBegin - pos) != 0) {
  116. return false;
  117. }
  118. if (cachedQuery.compare(ecsEnd, cachedQuery.size() - ecsEnd, query, ecsEnd, query.size() - ecsEnd) != 0) {
  119. return false;
  120. }
  121. }
  122. else {
  123. if (cachedQuery.compare(pos, cachedQuery.size() - pos, query, pos, query.size() - pos) != 0) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. };