]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dns.cc
rec: Don't account chained queries more than once
[thirdparty/pdns.git] / pdns / dns.cc
CommitLineData
12471842
PL
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 */
870a0fe4
AT
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
3ea54bf0 25#include "dns.hh"
3d40879b 26#include "misc.hh"
3ea54bf0 27#include <stdexcept>
3d40879b
BH
28#include <iostream>
29#include <boost/algorithm/string.hpp>
6ab9bec7 30#include <boost/assign/list_of.hpp>
ad5855b5 31#include "dnsparser.hh"
6ab9bec7
AT
32
33std::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")
4fc3ef66 43 ("Server Not Authoritative for zone / Not Authorized")
6ab9bec7 44 ("Name not contained in zone")
b47a9990
AT
45 ("Err#11")
46 ("Err#12")
47 ("Err#13")
48 ("Err#14")
49 ("Err#15")
4fc3ef66 50 ("Bad OPT Version / TSIG Signature Failure")
6ab9bec7
AT
51 ("Key not recognized")
52 ("Signature out of time window")
53 ("Bad TKEY Mode")
54 ("Duplicate key name")
4fc3ef66
AT
55 ("Algorithm not supported")
56 ("Bad Truncation")
57;
6ab9bec7
AT
58
59std::string RCode::to_s(unsigned short rcode) {
4fc3ef66 60 if (rcode > RCode::rcodes_s.size()-1 )
335da0ba 61 return std::string("Err#")+std::to_string(rcode);
6ab9bec7
AT
62 return RCode::rcodes_s[rcode];
63}
3d40879b 64
3d40879b
BH
65class BoundsCheckingPointer
66{
67public:
a683e8bd 68 explicit BoundsCheckingPointer(const char* a, size_t length)
3d40879b
BH
69 : d_ptr(a), d_length(length)
70 {}
71
72 explicit BoundsCheckingPointer(const std::string& str)
73 : d_ptr(str.c_str()), d_length(str.size())
74 {}
75
76
a683e8bd 77 char operator[](size_t offset) const
3d40879b
BH
78 {
79 if(offset < d_length)
80 return d_ptr[offset];
335da0ba 81 throw runtime_error("out of bounds: "+std::to_string(offset)+" >= " + std::to_string(d_length));
3d40879b
BH
82 }
83private:
84 const char* d_ptr;
a683e8bd 85 const size_t d_length;
3d40879b
BH
86};
87
3d40879b 88
fb8dcbe9 89// goal is to hash based purely on the question name, and turn error into 'default'
06ea9015 90uint32_t hashQuestion(const char* packet, uint16_t len, uint32_t init)
91{
92 if(len < 12)
93 return init;
94
95 uint32_t ret=init;
96 const unsigned char* end = (const unsigned char*)packet+len;
97 const unsigned char* pos = (const unsigned char*)packet+12;
98
99 unsigned char labellen;
100 while((labellen=*pos++) && pos < end) {
fb8dcbe9 101 if(pos + labellen + 1 > end) // include length field in hash
06ea9015 102 return 0;
6200227b 103 ret=burtleCI(pos, labellen+1, ret);
06ea9015 104 pos += labellen;
105 }
106 return ret;
107}
3ea54bf0 108
213cc78f
BH
109
110string& attodot(string &str)
111{
112 if(str.find_first_of("@")==string::npos)
113 return str;
114
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}