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