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.
 
 
 
 
 
 

143 lines
3.8 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. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "dns.hh"
  26. #include "misc.hh"
  27. #include <stdexcept>
  28. #include <iostream>
  29. #include <boost/algorithm/string.hpp>
  30. #include <boost/assign/list_of.hpp>
  31. #include "dnsparser.hh"
  32. std::vector<std::string> RCode::rcodes_s = boost::assign::list_of
  33. ("No Error")
  34. ("Form Error")
  35. ("Server Failure")
  36. ("Non-Existent domain")
  37. ("Not Implemented")
  38. ("Query Refused")
  39. ("Name Exists when it should not")
  40. ("RR Set Exists when it should not")
  41. ("RR Set that should exist does not")
  42. ("Server Not Authoritative for zone / Not Authorized")
  43. ("Name not contained in zone")
  44. ("Err#11")
  45. ("Err#12")
  46. ("Err#13")
  47. ("Err#14")
  48. ("Err#15") // Last non-extended RCode
  49. ("Bad OPT Version / TSIG Signature Failure")
  50. ("Key not recognized")
  51. ("Signature out of time window")
  52. ("Bad TKEY Mode")
  53. ("Duplicate key name")
  54. ("Algorithm not supported")
  55. ("Bad Truncation")
  56. ("Bad/missing Server Cookie")
  57. ;
  58. std::string RCode::to_s(uint8_t rcode) {
  59. if (rcode > 0xF)
  60. return std::string("ErrOutOfRange");
  61. return ERCode::to_s(rcode);
  62. }
  63. std::string ERCode::to_s(uint8_t rcode) {
  64. if (rcode > RCode::rcodes_s.size()-1)
  65. return std::string("Err#")+std::to_string(rcode);
  66. return RCode::rcodes_s[rcode];
  67. }
  68. std::string Opcode::to_s(uint8_t opcode) {
  69. static const std::vector<std::string> s_opcodes = { "Query", "IQuery", "Status", "3", "Notify", "Update" };
  70. if (opcode >= s_opcodes.size()) {
  71. return std::to_string(opcode);
  72. }
  73. return s_opcodes.at(opcode);
  74. }
  75. class BoundsCheckingPointer
  76. {
  77. public:
  78. explicit BoundsCheckingPointer(const char* a, size_t length)
  79. : d_ptr(a), d_length(length)
  80. {}
  81. explicit BoundsCheckingPointer(const std::string& str)
  82. : d_ptr(str.c_str()), d_length(str.size())
  83. {}
  84. char operator[](size_t offset) const
  85. {
  86. if(offset < d_length)
  87. return d_ptr[offset];
  88. throw runtime_error("out of bounds: "+std::to_string(offset)+" >= " + std::to_string(d_length));
  89. }
  90. private:
  91. const char* d_ptr;
  92. const size_t d_length;
  93. };
  94. // goal is to hash based purely on the question name, and turn error into 'default'
  95. uint32_t hashQuestion(const char* packet, uint16_t len, uint32_t init)
  96. {
  97. if(len < 12)
  98. return init;
  99. uint32_t ret=init;
  100. const unsigned char* end = (const unsigned char*)packet+len;
  101. const unsigned char* pos = (const unsigned char*)packet+12;
  102. unsigned char labellen;
  103. while((labellen=*pos++) && pos < end) {
  104. if(pos + labellen + 1 > end) // include length field in hash
  105. return 0;
  106. ret=burtleCI(pos, labellen+1, ret);
  107. pos += labellen;
  108. }
  109. return ret;
  110. }
  111. string& attodot(string &str)
  112. {
  113. if(str.find_first_of("@")==string::npos)
  114. return str;
  115. for (unsigned int i = 0; i < str.length(); i++)
  116. {
  117. if (str[i] == '@') {
  118. str[i] = '.';
  119. break;
  120. } else if (str[i] == '.') {
  121. str.insert(i++, "\\");
  122. }
  123. }
  124. return str;
  125. }