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