]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursordist/rec-system-resolve.hh
Make resolve check interval and self-resolve check settable
[thirdparty/pdns.git] / pdns / recursordist / rec-system-resolve.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
23 #pragma once
24
25 #include "config.h"
26
27 #include <condition_variable>
28 #include <functional>
29 #include <thread>
30
31 #include "namespaces.hh"
32 #include "iputils.hh"
33 #include "lock.hh"
34
35 /************************************************************************************************
36 The pdns::RecResolve class implements a facility to use the system configured resolver. At the moment
37 of writing, this can only be used to configure forwarding by name instead of IP.
38 ************************************************************************************************/
39
40 /************************************************************************************************
41 DESIGN CONSIDERATIONS
42
43 - all names looked up with lookupAndRegister() will be entered into a table.
44
45 - the names in the table will ber periodically checked by a refresh thread. Set the period (before
46 starting to use the system resolver) by calling pdns::RecResolve::setInstanceParameters().
47
48 - if *a* name resolves to a different result than stored, we will call the callback. Currently this is
49 used to call the equivalent of rec_control reload-zones
50
51 - A manual rec_control reload-zones will *also* flush the existing table before doing the reload, so
52 we force a re-resolve all names. See
53 rec_channel_rec.cc:reloadZoneConfigurationWithSysResolveReset()
54
55 **************************************************************************************************/
56
57 /************************************************************************************************
58 PRACTICAL CONSIDERATIONS/IMPLEMENTATION LIMITS
59
60 - Currently the facility is *only* used by the forwarding code
61
62 - We resolve with AI_ADDRCONFIG, the address families enabled will depend on the network config
63 of the machine
64
65 - We pick the first address that getaddrinfo() produced. Currently no handling of multiple addresses
66 and/or multiple address families.
67
68 - There is a check to detect *some* cases of self-resolve. This is done by resolving
69 id.server/CH/TXT and comparing the result to the system-id set. Both false positives and false
70 negatives can occur.
71
72 **************************************************************************************************/
73 namespace pdns
74 {
75 class RecResolve
76 {
77 public:
78 // Should be called before any getInstance() call is done
79 static void setInstanceParameters(std::string serverID, time_t ttl, time_t interval, bool selfResolveCheck, const std::function<void()>& callback);
80 // Get "the" instance of the system resolver.
81 static RecResolve& getInstance();
82
83 RecResolve(time_t ttl, time_t interval, bool selfResolveCheck, const std::function<void()>& callback = nullptr);
84 ~RecResolve();
85 // Lookup a name and register it in the names to be checked if not already there
86 ComboAddress lookupAndRegister(const std::string& name, time_t now);
87 // Lookup a name which must be already registered
88 ComboAddress lookup(const std::string& name);
89
90 // When an instance is created, it will run a refresh thread, stop it with this method
91 void stopRefresher();
92 // And restart it again
93 void startRefresher();
94 // Wipe one or all names
95 void wipe(const std::string& name = "");
96
97 private:
98 bool refresh(time_t now);
99 struct AddressData
100 {
101 ComboAddress d_address;
102 time_t d_ttd{0};
103 };
104 struct ResolveData
105 {
106 std::map<std::string, AddressData> d_map;
107 };
108 LockGuarded<ResolveData> d_data;
109 const time_t d_ttl;
110
111 // This private class implements the refresher thread
112 class Refresher
113 {
114 public:
115 Refresher(time_t interval, const std::function<void()>& callback, bool selfResolveCheck, pdns::RecResolve& res);
116 Refresher(const Refresher&) = delete;
117 Refresher(Refresher&&) = delete;
118 Refresher& operator=(const Refresher&) = delete;
119 Refresher& operator=(Refresher&&) = delete;
120 ~Refresher();
121
122 void start();
123 void finish();
124 void trigger();
125
126 private:
127 void refreshLoop();
128
129 pdns::RecResolve& d_resolver;
130 std::function<void()> d_callback;
131 const time_t d_interval;
132 std::thread d_thread;
133 std::mutex mutex;
134 std::condition_variable condVar;
135 std::atomic<bool> wakeup{false};
136 std::atomic<bool> stop{false};
137 const bool d_selfResolveCheck;
138 };
139
140 Refresher d_refresher;
141
142 static std::string s_serverID;
143 static std::function<void()> s_callback;
144 static time_t s_ttl;
145 static time_t s_interval;
146 static bool s_selfResolveCheck;
147 };
148
149 }