]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/ednsextendederror.cc
Merge pull request #14020 from omoerbeek/rec-compiling-rust-dcos
[thirdparty/pdns.git] / pdns / ednsextendederror.cc
CommitLineData
92e3654a
PD
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#include <limits>
23
24#include "ednsextendederror.hh"
25
26static bool getEDNSExtendedErrorOptFromStringView(const std::string_view& option, EDNSExtendedError& eee)
27{
28 if (option.size() < sizeof(uint16_t)) {
29 return false;
30 }
31 eee.infoCode = static_cast<uint8_t>(option.at(0)) * 256 + static_cast<uint8_t>(option.at(1));
32
33 if (option.size() > sizeof(uint16_t)) {
34 eee.extraText = std::string(&option.at(sizeof(uint16_t)), option.size() - sizeof(uint16_t));
35 }
36
37 return true;
38}
39
40bool getEDNSExtendedErrorOptFromString(const string& option, EDNSExtendedError& eee)
41{
42 return getEDNSExtendedErrorOptFromStringView(std::string_view(option), eee);
43}
44
45bool getEDNSExtendedErrorOptFromString(const char* option, unsigned int len, EDNSExtendedError& eee)
46{
47 return getEDNSExtendedErrorOptFromStringView(std::string_view(option, len), eee);
48}
49
50string makeEDNSExtendedErrorOptString(const EDNSExtendedError& eee)
51{
52 if (eee.extraText.size() > static_cast<size_t>(std::numeric_limits<uint16_t>::max() - 2)) {
53 throw std::runtime_error("Trying to create an EDNS Extended Error option with an extra text of size " + std::to_string(eee.extraText.size()));
54 }
55
56 string ret;
57 ret.reserve(sizeof(uint16_t) + eee.extraText.size());
58 ret.resize(sizeof(uint16_t));
59
60 ret[0] = static_cast<char>(static_cast<uint16_t>(eee.infoCode) / 256);
61 ret[1] = static_cast<char>(static_cast<uint16_t>(eee.infoCode) % 256);
62 ret.append(eee.extraText);
63
64 return ret;
65}