]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/ednsoptions.cc
Merge pull request #7909 from qvr/expungebyname-stats
[thirdparty/pdns.git] / pdns / ednsoptions.cc
CommitLineData
12471842
PL
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 */
5c3b5e7f
RG
22#include "dns.hh"
23#include "ednsoptions.hh"
24#include "iputils.hh"
25
26/* extract a specific EDNS0 option from a pointer on the beginning rdLen of the OPT RR */
27int getEDNSOption(char* optRR, const size_t len, uint16_t wantedOption, char ** optionValue, size_t * optionValueSize)
28{
29 assert(optRR != NULL);
30 assert(optionValue != NULL);
31 assert(optionValueSize != NULL);
32 size_t pos = 0;
33 if (len < DNS_RDLENGTH_SIZE)
34 return EINVAL;
35
36 const uint16_t rdLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
37 size_t rdPos = 0;
38 pos += DNS_RDLENGTH_SIZE;
39 if ((pos + rdLen) > len) {
40 return EINVAL;
41 }
42
43 while(len >= (pos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE) &&
44 rdLen >= (rdPos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE)) {
45 const uint16_t optionCode = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
46 pos += EDNS_OPTION_CODE_SIZE;
47 rdPos += EDNS_OPTION_CODE_SIZE;
48 const uint16_t optionLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
49 pos += EDNS_OPTION_LENGTH_SIZE;
50 rdPos += EDNS_OPTION_LENGTH_SIZE;
51 if (optionLen > (rdLen - rdPos) || optionLen > (len - pos))
52 return EINVAL;
53
54 if (optionCode == wantedOption) {
55 *optionValue = optRR + pos - (EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE);
56 *optionValueSize = optionLen + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE;
57 return 0;
58 }
59 else {
60 /* skip this option */
61 pos += optionLen;
62 rdPos += optionLen;
63 }
64 }
65
66 return ENOENT;
67}
68
00b8cadc 69/* extract all EDNS0 options from a pointer on the beginning rdLen of the OPT RR */
29e6303a 70int getEDNSOptions(const char* optRR, const size_t len, EDNSOptionViewMap& options)
00b8cadc
RG
71{
72 assert(optRR != NULL);
73 size_t pos = 0;
74 if (len < DNS_RDLENGTH_SIZE)
75 return EINVAL;
76
77 const uint16_t rdLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
78 size_t rdPos = 0;
79 pos += DNS_RDLENGTH_SIZE;
80 if ((pos + rdLen) > len) {
81 return EINVAL;
82 }
83
84 while(len >= (pos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE) &&
85 rdLen >= (rdPos + EDNS_OPTION_CODE_SIZE + EDNS_OPTION_LENGTH_SIZE)) {
86 const uint16_t optionCode = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
87 pos += EDNS_OPTION_CODE_SIZE;
88 rdPos += EDNS_OPTION_CODE_SIZE;
89 const uint16_t optionLen = (((unsigned char) optRR[pos]) * 256) + ((unsigned char) optRR[pos+1]);
90 pos += EDNS_OPTION_LENGTH_SIZE;
91 rdPos += EDNS_OPTION_LENGTH_SIZE;
92 if (optionLen > (rdLen - rdPos) || optionLen > (len - pos))
93 return EINVAL;
94
29e6303a
RG
95 EDNSOptionViewValue value;
96 value.content = optRR + pos;
97 value.size = optionLen;
98 options[optionCode].values.push_back(std::move(value));
00b8cadc
RG
99
100 /* skip this option */
101 pos += optionLen;
102 rdPos += optionLen;
103 }
104
105 return 0;
106}
107
5c3b5e7f
RG
108void generateEDNSOption(uint16_t optionCode, const std::string& payload, std::string& res)
109{
110 const uint16_t ednsOptionCode = htons(optionCode);
111 const uint16_t payloadLen = htons(payload.length());
112 res.append((const char *) &ednsOptionCode, sizeof ednsOptionCode);
113 res.append((const char *) &payloadLen, sizeof payloadLen);
114 res.append(payload);
115}