]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dnsproxy.hh
dnsdist: Fix DNS over plain HTTP broken by `reloadAllCertificates()`
[thirdparty/pdns.git] / pdns / dnsproxy.hh
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 #pragma once
23 #include <map>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include "dnspacket.hh"
28 #include "lock.hh"
29 #include "iputils.hh"
30
31 #include "namespaces.hh"
32
33 /**
34
35 how will this work.
36
37 This is a thread that just throws packets around. Should handle ~1000 packets/second.
38
39 Consists of a thread receiving packets back from the backend and retransmitting them to the original client.
40
41 Furthermore, it provides a member function that reports the packet to the connection tracker and actually sends it out.
42
43 The sending happens from a source port that is determined by the constructor, but IS random. Furthermore, the ID is XOR-ed with a random value
44 to make sure outside parties can't spoof us.
45
46 To fix: how to remove the stale entries that will surely accumulate
47 */
48
49 class DNSProxy
50 {
51 public:
52 DNSProxy(const string& remote); //!< creates socket
53 ~DNSProxy(); //<! dtor for DNSProxy
54 void go(); //!< launches the actual thread
55 bool completePacket(std::unique_ptr<DNSPacket>& reply, const DNSName& target, const DNSName& aname, uint8_t scopeMask);
56
57 void mainloop(); //!< this is the main loop that receives reply packets and sends them out again
58 private:
59 struct ConntrackEntry
60 {
61 time_t created;
62 boost::optional<ComboAddress> anyLocal;
63 DNSName qname;
64 std::unique_ptr<DNSPacket> complete;
65 DNSName aname;
66 uint8_t anameScopeMask;
67 ComboAddress remote;
68 uint16_t id;
69 uint16_t qtype;
70 int outsock;
71 };
72
73 using map_t = map<int, ConntrackEntry>;
74
75 // Data
76 ComboAddress d_remote;
77 AtomicCounter* d_resanswers;
78 AtomicCounter* d_udpanswers;
79 AtomicCounter* d_resquestions;
80 LockGuarded<map_t> d_conntrack;
81 int d_sock;
82 const uint16_t d_xor;
83
84 static int getID_locked(map_t&);
85 };