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.
25 #include <semaphore.h>
29 #include <boost/multi_index_container.hpp>
30 #include <boost/multi_index/identity.hpp>
31 #include <boost/multi_index/sequenced_index.hpp>
32 using namespace boost::multi_index;
39 #include "packethandler.hh"
41 #include "namespaces.hh"
42 #include "dns_random.hh"
48 bool operator<(const SuckRequest& b) const
50 return tie(domain, master) < tie(b.domain, b.master);
56 typedef multi_index_container<
60 ordered_unique<tag<IDTag>, identity<SuckRequest> >
63 typedef UniQueue::index<IDTag>::type domains_by_name_t;
65 class NotificationQueue
68 void add(const DNSName &domain, const string &ip)
70 const ComboAddress caIp(ip);
72 NotificationRequest nr;
74 nr.ip = caIp.toStringWithPort();
76 nr.id = dns_random_uint16();
79 d_nqueue.push_back(nr);
82 bool removeIf(const string &remote, uint16_t id, const DNSName &domain)
84 ServiceTuple stRemote, stQueued;
85 parseService(remote, stRemote);
87 for(d_nqueue_t::iterator i=d_nqueue.begin(); i!=d_nqueue.end(); ++i) {
88 parseService(i->ip, stQueued);
89 if(i->id==id && stQueued.host == stRemote.host && i->domain==domain) {
97 bool getOne(DNSName &domain, string &ip, uint16_t *id, bool &purged)
99 for(d_nqueue_t::iterator i=d_nqueue.begin();i!=d_nqueue.end();++i)
100 if(i->next <= time(0)) {
103 i->next=time(0)+1+(1<<i->attempts);
119 time_t early=std::numeric_limits<time_t>::max() - 1;
120 for(d_nqueue_t::const_iterator i=d_nqueue.begin();i!=d_nqueue.end();++i)
121 early=min(early,i->next);
122 return early-time(0);
128 struct NotificationRequest
137 typedef std::list<NotificationRequest> d_nqueue_t;
144 /** this class contains a thread that communicates with other nameserver and does housekeeping.
145 Initially, it is notified only of zones that need to be pulled in because they have been updated. */
147 class CommunicatorClass
153 d_masterschanged=d_slaveschanged=true;
156 d_preventSelfNotification = false;
158 time_t doNotifications(PacketHandler *P);
162 void drillHole(const DNSName &domain, const string &ip);
163 bool justNotified(const DNSName &domain, const string &ip);
164 void addSuckRequest(const DNSName &domain, const ComboAddress& master);
165 void addSlaveCheckRequest(const DomainInfo& di, const ComboAddress& remote);
166 void addTrySuperMasterRequest(const DNSPacket& p);
167 void notify(const DNSName &domain, const string &ip);
169 void retrievalLoopThread();
170 void sendNotification(int sock, const DNSName &domain, const ComboAddress& remote, uint16_t id, UeberBackend* B);
171 bool notifyDomain(const DNSName &domain, UeberBackend* B);
173 void loadArgsIntoSet(const char *listname, set<string> &listset);
174 void makeNotifySockets();
175 void queueNotifyDomain(const DomainInfo& di, UeberBackend* B);
176 int d_nsock4, d_nsock6;
177 map<pair<DNSName,string>,time_t>d_holes;
178 std::mutex d_holelock;
179 void suck(const DNSName &domain, const ComboAddress& remote);
180 void ixfrSuck(const DNSName &domain, const TSIGTriplet& tt, const ComboAddress& laddr, const ComboAddress& remote, std::unique_ptr<AuthLua4>& pdl,
181 ZoneStatus& zs, vector<DNSRecord>* axfr);
183 void slaveRefresh(PacketHandler *P);
184 void masterUpdateCheck(PacketHandler *P);
187 UniQueue d_suckdomains;
188 set<DNSName> d_inprogress;
190 Semaphore d_suck_sem;
192 time_t d_tickinterval;
193 set<DomainInfo> d_tocheck;
195 bool operator()(const DNSPacket& a, const DNSPacket& b) const {
196 return a.qdomain < b.qdomain;
200 std::set<DNSPacket, cmp> d_potentialsupermasters;
202 set<string> d_alsoNotify;
203 NotificationQueue d_nq;
204 NetmaskGroup d_onlyNotify;
205 bool d_masterschanged, d_slaveschanged;
206 bool d_preventSelfNotification;
208 // Used to keep some state on domains that failed their freshness checks.
209 // uint64_t == counter of the number of failures (increased by 1 every consecutive slave-cycle-interval that the domain fails)
210 // time_t == wait at least until this time before attempting a new check
211 map<DNSName, pair<uint64_t, time_t> > d_failedSlaveRefresh;
213 struct RemoveSentinel
215 explicit RemoveSentinel(const DNSName& dn, CommunicatorClass* cc) : d_dn(dn), d_cc(cc)
221 std::lock_guard<std::mutex> l(d_cc->d_lock);
222 d_cc->d_inprogress.erase(d_dn);
228 CommunicatorClass* d_cc;
233 // class that one day might be more than a function to help you get IP addresses for a nameserver
237 vector<string> lookup(const DNSName &name, UeberBackend *b)
239 vector<string> addresses;
241 this->resolve_name(&addresses, name);
244 b->lookup(QType(QType::ANY),name,-1);
247 if(rr.dr.d_type == QType::A || rr.dr.d_type==QType::AAAA)
248 addresses.push_back(rr.dr.d_content->getZoneRepresentation()); // SOL if you have a CNAME for an NS
254 void resolve_name(vector<string>* addresses, const DNSName& name)
256 struct addrinfo* res;
257 struct addrinfo hints;
258 memset(&hints, 0, sizeof(hints));
259 hints.ai_socktype = SOCK_DGRAM; // otherwise we get everything in triplicate (!)
260 for(int n = 0; n < 2; ++n) {
261 hints.ai_family = n ? AF_INET : AF_INET6;
263 remote.sin4.sin_family = AF_INET6;
264 if(!getaddrinfo(name.toString().c_str(), 0, &hints, &res)) {
265 struct addrinfo* address = res;
267 if (address->ai_addrlen <= sizeof(remote)) {
268 remote.setSockaddr(address->ai_addr, address->ai_addrlen);
269 addresses->push_back(remote.toString());
271 } while((address = address->ai_next));