]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdist-xpf.cc
Merge pull request #7945 from pieterlexis/syncres-CNAME-cache-cleanup
[thirdparty/pdns.git] / pdns / dnsdist-xpf.cc
CommitLineData
53c57da7
RG
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 "dnsdist-xpf.hh"
24
25#include "dnsparser.hh"
26#include "xpf.hh"
27
28bool addXPF(DNSQuestion& dq, uint16_t optionCode, bool preserveTrailingData)
29{
30 std::string payload = generateXPFPayload(dq.tcp, *dq.remote, *dq.local);
31 uint8_t root = '\0';
32 dnsrecordheader drh;
33 drh.d_type = htons(optionCode);
34 drh.d_class = htons(QClass::IN);
35 drh.d_ttl = 0;
36 drh.d_clen = htons(payload.size());
37 size_t recordHeaderLen = sizeof(root) + sizeof(drh);
38
39 size_t available = dq.size - dq.len;
40
41 if ((payload.size() + recordHeaderLen) > available) {
42 return false;
43 }
44
45 size_t xpfSize = sizeof(root) + sizeof(drh) + payload.size();
46 uint32_t realPacketLen = getDNSPacketLength(reinterpret_cast<const char*>(dq.dh), dq.len);
47 if (realPacketLen < dq.len && preserveTrailingData) {
48 size_t toMove = dq.len - realPacketLen;
49 memmove(reinterpret_cast<char*>(dq.dh) + realPacketLen + xpfSize, reinterpret_cast<const char*>(dq.dh) + realPacketLen, toMove);
50 dq.len += xpfSize;
51 }
52 else {
53 dq.len = realPacketLen + xpfSize;
54 }
55
56 size_t pos = realPacketLen;
57 memcpy(reinterpret_cast<char*>(dq.dh) + pos, &root, sizeof(root));
58 pos += sizeof(root);
59 memcpy(reinterpret_cast<char*>(dq.dh) + pos, &drh, sizeof(drh));
60 pos += sizeof(drh);
61 memcpy(reinterpret_cast<char*>(dq.dh) + pos, payload.data(), payload.size());
62 pos += payload.size();
63
64 dq.dh->arcount = htons(ntohs(dq.dh->arcount) + 1);
65
66 return true;
67}