Coverage Report

Created: 2025-06-13 06:28

/src/pdns/pdns/dnsdistdist/dnsdist-protocols.cc
Line
Count
Source (jump to first uncovered line)
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
23
#include <algorithm>
24
#include <stdexcept>
25
26
#include "dnsdist-protocols.hh"
27
28
namespace dnsdist
29
{
30
const std::array<std::string, Protocol::s_numberOfProtocols> Protocol::s_names = {
31
  "DoUDP",
32
  "DoTCP",
33
  "DNSCryptUDP",
34
  "DNSCryptTCP",
35
  "DoT",
36
  "DoH",
37
  "DoQ",
38
  "DoH3"};
39
40
const std::array<std::string, Protocol::s_numberOfProtocols> Protocol::s_prettyNames = {
41
  "Do53 UDP",
42
  "Do53 TCP",
43
  "DNSCrypt UDP",
44
  "DNSCrypt TCP",
45
  "DNS over TLS",
46
  "DNS over HTTPS",
47
  "DNS over QUIC",
48
  "DNS over HTTP/3"};
49
50
Protocol::Protocol(const std::string& protocol)
51
0
{
52
0
  const auto& namesIt = std::find(s_names.begin(), s_names.end(), protocol);
53
0
  if (namesIt == s_names.end()) {
54
0
    throw std::runtime_error("Unknown protocol name: '" + protocol + "'");
55
0
  }
56
57
0
  auto index = std::distance(s_names.begin(), namesIt);
58
0
  d_protocol = static_cast<Protocol::typeenum>(index);
59
0
}
60
61
bool Protocol::operator==(Protocol::typeenum type) const
62
0
{
63
0
  return d_protocol == type;
64
0
}
65
66
bool Protocol::operator!=(Protocol::typeenum type) const
67
0
{
68
0
  return d_protocol != type;
69
0
}
70
71
const std::string& Protocol::toString() const
72
0
{
73
0
  return s_names.at(static_cast<uint8_t>(d_protocol));
74
0
}
75
76
const std::string& Protocol::toPrettyString() const
77
0
{
78
0
  return s_prettyNames.at(static_cast<uint8_t>(d_protocol));
79
0
}
80
81
bool Protocol::isUDP() const
82
0
{
83
0
  return d_protocol == DoUDP || d_protocol == DNSCryptUDP;
84
0
}
85
86
bool Protocol::isEncrypted() const
87
0
{
88
0
  return d_protocol != DoUDP && d_protocol != DoTCP;
89
0
}
90
91
uint8_t Protocol::toNumber() const
92
0
{
93
0
  return static_cast<uint8_t>(d_protocol);
94
0
}
95
}