/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 | | |
38 | | const std::array<std::string, Protocol::s_numberOfProtocols> Protocol::s_prettyNames = { |
39 | | "Do53 UDP", |
40 | | "Do53 TCP", |
41 | | "DNSCrypt UDP", |
42 | | "DNSCrypt TCP", |
43 | | "DNS over TLS", |
44 | | "DNS over HTTPS"}; |
45 | | |
46 | | Protocol::Protocol(const std::string& s) |
47 | 0 | { |
48 | 0 | const auto& it = std::find(s_names.begin(), s_names.end(), s); |
49 | 0 | if (it == s_names.end()) { |
50 | 0 | throw std::runtime_error("Unknown protocol name: '" + s + "'"); |
51 | 0 | } |
52 | | |
53 | 0 | auto index = std::distance(s_names.begin(), it); |
54 | 0 | d_protocol = static_cast<Protocol::typeenum>(index); |
55 | 0 | } |
56 | | |
57 | | bool Protocol::operator==(Protocol::typeenum type) const |
58 | 0 | { |
59 | 0 | return d_protocol == type; |
60 | 0 | } |
61 | | |
62 | | bool Protocol::operator!=(Protocol::typeenum type) const |
63 | 0 | { |
64 | 0 | return d_protocol != type; |
65 | 0 | } |
66 | | |
67 | | const std::string& Protocol::toString() const |
68 | 0 | { |
69 | 0 | return s_names.at(static_cast<uint8_t>(d_protocol)); |
70 | 0 | } |
71 | | |
72 | | const std::string& Protocol::toPrettyString() const |
73 | 0 | { |
74 | 0 | return s_prettyNames.at(static_cast<uint8_t>(d_protocol)); |
75 | 0 | } |
76 | | |
77 | | bool Protocol::isUDP() const |
78 | 0 | { |
79 | 0 | return d_protocol == DoUDP || d_protocol == DNSCryptUDP; |
80 | 0 | } |
81 | | |
82 | | uint8_t Protocol::toNumber() const |
83 | 0 | { |
84 | 0 | return static_cast<uint8_t>(d_protocol); |
85 | 0 | } |
86 | | } |