File: dns.cc

package info (click to toggle)
pdns-recursor 5.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,116 kB
  • sloc: cpp: 109,650; javascript: 20,651; python: 5,657; sh: 5,094; makefile: 780; ansic: 582; xml: 37
file content (186 lines) | stat: -rw-r--r-- 4,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "dns.hh"
#include "misc.hh"
#include "views.hh"
#include <stdexcept>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/assign/list_of.hpp>
#include "dnsparser.hh"

const std::array<std::string, 24> RCode::rcodes_s = {
  "No Error",
  "Form Error",
  "Server Failure",
  "Non-Existent domain",
  "Not Implemented",
  "Query Refused",
  "Name Exists when it should not",
  "RR Set Exists when it should not",
  "RR Set that should exist does not",
  "Server Not Authoritative for zone / Not Authorized",
  "Name not contained in zone",
  "Err#11",
  "Err#12",
  "Err#13",
  "Err#14",
  "Err#15",  // Last non-extended RCode
  "Bad OPT Version / TSIG Signature Failure",
  "Key not recognized",
  "Signature out of time window",
  "Bad TKEY Mode",
  "Duplicate key name",
  "Algorithm not supported",
  "Bad Truncation",
  "Bad/missing Server Cookie"
};

static const std::array<std::string, 24> rcodes_short_s =  {
  "noerror",
  "formerr",
  "servfail",
  "nxdomain",
  "notimp",
  "refused",
  "yxdomain",
  "yxrrset",
  "nxrrset",
  "notauth",
  "notzone",
  "rcode11",
  "rcode12",
  "rcode13",
  "rcode14",
  "rcode15",
  "badvers",
  "badkey",
  "badtime",
  "badmode",
  "badname",
  "badalg",
  "badtrunc",
  "badcookie",
};

std::string RCode::to_s(uint8_t rcode) {
  if (rcode > 0xF) {
    return "ErrOutOfRange";
  }
  return ERCode::to_s(rcode);
}

std::string RCode::to_short_s(uint8_t rcode) {
  if (rcode > 0xF) {
    return "ErrOutOfRange";
  }
  return ERCode::to_short_s(rcode);
}

std::optional<uint8_t> RCode::from_short(const std::string_view& rcode_string)
{
  const auto* position = std::find(rcodes_short_s.begin(), rcodes_short_s.end(), rcode_string);
  if (position == rcodes_short_s.end()) {
    return std::nullopt;
  }
  auto code = std::distance(rcodes_short_s.begin(), position);
  if (code > 0xF) {
    return std::nullopt;
  }
  return code;
}

std::string ERCode::to_s(uint16_t rcode) {
  if (rcode >= RCode::rcodes_s.size()) {
    return std::string("Err#") + std::to_string(rcode);
  }
  return RCode::rcodes_s.at(rcode);
}

std::string ERCode::to_short_s(uint16_t rcode) {
  if (rcode >= rcodes_short_s.size()) {
    return "rcode" + std::to_string(rcode);
  }
  return rcodes_short_s.at(rcode);
}

std::optional<uint16_t> ERCode::from_short(const std::string_view& ercode_string)
{
  const auto* position = std::find(rcodes_short_s.begin(), rcodes_short_s.end(), ercode_string);
  if (position == rcodes_short_s.end()) {
    return std::nullopt;
  }
  return std::distance(rcodes_short_s.begin(), position);
}

std::string Opcode::to_s(uint8_t opcode) {
  static const std::array<std::string, 6> s_opcodes = { "Query", "IQuery", "Status", "3", "Notify", "Update" };

  if (opcode >= s_opcodes.size()) {
    return std::to_string(opcode);
  }

  return s_opcodes.at(opcode);
}

// goal is to hash based purely on the question name, and turn error into 'default'
uint32_t hashQuestion(const uint8_t* packet, uint16_t packet_len, uint32_t init, bool& wasOK)
{
  if (packet_len < sizeof(dnsheader)) {
    wasOK = false;
    return init;
  }
  // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
  pdns::views::UnsignedCharView name(packet + sizeof(dnsheader), packet_len - sizeof(dnsheader));
  pdns::views::UnsignedCharView::size_type len = 0;

  while (len < name.length()) {
    uint8_t labellen = name[len++];
    if (labellen == 0) {
      wasOK = true;
      // len is name.length() at max as it was < before the increment
      return burtleCI(name.data(), len, init);
    }
    len += labellen;
  }
  // We've encountered a label that is too long
  wasOK = false;
  return init;
}

static const std::array<std::string, 4> placeNames = {
  "QUESTION",
  "ANSWER",
  "AUTHORITY",
  "ADDITIONAL"
};

std::string DNSResourceRecord::placeString(uint8_t place)
{
  if (place >= placeNames.size()) {
    return "?";
  }
  return placeNames.at(place);
}