2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
27 #include <condition_variable>
31 #include "namespaces.hh"
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 ************************************************************************************************/
40 /************************************************************************************************
43 - all names looked up with lookupAndRegister() will be entered into a table.
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().
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
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()
55 **************************************************************************************************/
57 /************************************************************************************************
58 PRACTICAL CONSIDERATIONS/IMPLEMENTATION LIMITS
60 - Currently the facility is *only* used by the forwarding code
62 - We resolve with AI_ADDRCONFIG, the address families enabled will depend on the network config
65 - We pick the first address that getaddrinfo() produced. Currently no handling of multiple addresses
66 and/or multiple address families.
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
72 **************************************************************************************************/
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();
83 RecResolve(time_t ttl, time_t interval, bool selfResolveCheck, const std::function<void()>& callback = nullptr);
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);
90 // When an instance is created, it will run a refresh thread, stop it with this method
92 // And restart it again
93 void startRefresher();
94 // Wipe one or all names
95 void wipe(const std::string& name = "");
98 bool refresh(time_t now);
101 ComboAddress d_address;
106 std::map<std::string, AddressData> d_map;
108 LockGuarded<ResolveData> d_data;
111 // This private class implements the refresher thread
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;
129 pdns::RecResolve& d_resolver;
130 std::function<void()> d_callback;
131 const time_t d_interval;
132 std::thread d_thread;
134 std::condition_variable condVar;
135 std::atomic<bool> wakeup{false};
136 std::atomic<bool> stop{false};
137 const bool d_selfResolveCheck;
140 Refresher d_refresher;
142 static std::string s_serverID;
143 static std::function<void()> s_callback;
145 static time_t s_interval;
146 static bool s_selfResolveCheck;