]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/communicator.hh
Merge pull request #4602 from pieterlexis/DNSSEC-forwards-NSEC3-optout
[thirdparty/pdns.git] / pdns / communicator.hh
CommitLineData
12c86877 1/*
12471842
PL
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 */
12c86877
BH
22#ifndef PDNS_COMMUNICATOR_HH
23#define PDNS_COMMUNICATOR_HH
24
25#include <pthread.h>
26#include <string>
27#include <semaphore.h>
28#include <queue>
29#include <list>
f1a8bee5 30#include <limits>
dbcb3066
BH
31#include <boost/multi_index_container.hpp>
32#include <boost/multi_index/identity.hpp>
33#include <boost/multi_index/sequenced_index.hpp>
3e7dcee6 34#include <boost/scoped_ptr.hpp>
dbcb3066 35using namespace boost::multi_index;
1258abe0 36
76473b92
KM
37#include <unistd.h>
38#include <fcntl.h>
39#include <netdb.h>
1258abe0 40
12c86877
BH
41#include "lock.hh"
42#include "packethandler.hh"
43
10f4eea8 44#include "namespaces.hh"
12c86877
BH
45
46struct SuckRequest
47{
5fca2e23 48 DNSName domain;
12c86877 49 string master;
dbcb3066
BH
50 bool operator<(const SuckRequest& b) const
51 {
52 return tie(domain, master) < tie(b.domain, b.master);
53 }
12c86877
BH
54};
55
dbcb3066
BH
56struct IDTag{};
57
58typedef multi_index_container<
59 SuckRequest,
60 indexed_by<
61 sequenced<>,
62 ordered_unique<tag<IDTag>, identity<SuckRequest> >
63 >
64> UniQueue;
a71bee29 65typedef UniQueue::index<IDTag>::type domains_by_name_t;
dbcb3066 66
12c86877
BH
67class NotificationQueue
68{
69public:
5fca2e23 70 void add(const DNSName &domain, const string &ip)
12c86877 71 {
3c8a7112
KM
72 const ComboAddress caIp(ip);
73
1258abe0
BH
74 NotificationRequest nr;
75 nr.domain = domain;
3c8a7112 76 nr.ip = caIp.toStringWithPort();
1258abe0
BH
77 nr.attempts = 0;
78 nr.id = Utility::random()%0xffff;
79 nr.next = time(0);
12c86877
BH
80
81 d_nqueue.push_back(nr);
82 }
3c8a7112 83
5fca2e23 84 bool removeIf(const string &remote, uint16_t id, const DNSName &domain)
12c86877 85 {
3c8a7112
KM
86 ServiceTuple stRemote, stQueued;
87 parseService(remote, stRemote);
88
a71bee29 89 for(d_nqueue_t::iterator i=d_nqueue.begin(); i!=d_nqueue.end(); ++i) {
3c8a7112
KM
90 parseService(i->ip, stQueued);
91 if(i->id==id && stQueued.host == stRemote.host && i->domain==domain) {
4957a608
BH
92 d_nqueue.erase(i);
93 return true;
12c86877
BH
94 }
95 }
96 return false;
97 }
98
5fca2e23 99 bool getOne(DNSName &domain, string &ip, uint16_t *id, bool &purged)
12c86877
BH
100 {
101 for(d_nqueue_t::iterator i=d_nqueue.begin();i!=d_nqueue.end();++i)
102 if(i->next <= time(0)) {
4957a608
BH
103 i->attempts++;
104 purged=false;
105 i->next=time(0)+1+(1<<i->attempts);
106 domain=i->domain;
107 ip=i->ip;
108 *id=i->id;
109 purged=false;
110 if(i->attempts>4) {
111 purged=true;
112 d_nqueue.erase(i);
113 }
114 return true;
12c86877
BH
115 }
116 return false;
117 }
3c8a7112 118
12c86877
BH
119 time_t earliest()
120 {
10f4eea8 121 time_t early=std::numeric_limits<time_t>::max() - 1;
12c86877
BH
122 for(d_nqueue_t::const_iterator i=d_nqueue.begin();i!=d_nqueue.end();++i)
123 early=min(early,i->next);
124 return early-time(0);
125 }
3c8a7112 126
2d00c43d 127 void dump();
3c8a7112 128
12c86877
BH
129private:
130 struct NotificationRequest
131 {
5fca2e23 132 DNSName domain;
12c86877 133 string ip;
1c514700 134 time_t next;
12c86877 135 int attempts;
092f210a 136 uint16_t id;
12c86877
BH
137 };
138
a71bee29 139 typedef std::list<NotificationRequest> d_nqueue_t;
12c86877
BH
140 d_nqueue_t d_nqueue;
141
142};
143
3e7dcee6 144struct ZoneStatus;
145
12c86877
BH
146/** this class contains a thread that communicates with other nameserver and does housekeeping.
147 Initially, it is notified only of zones that need to be pulled in because they have been updated. */
148
149class CommunicatorClass
150{
151public:
152 CommunicatorClass()
153 {
154 pthread_mutex_init(&d_lock,0);
155 pthread_mutex_init(&d_holelock,0);
dbcb3066 156
12c86877 157 d_tickinterval=60;
bd11bd1d 158 d_masterschanged=d_slaveschanged=true;
50d471ea
AT
159 d_nsock4 = -1;
160 d_nsock6 = -1;
161 d_havepriosuckrequest = false;
162 d_preventSelfNotification = false;
12c86877 163 }
88c0425a 164 time_t doNotifications();
dbcb3066
BH
165 void go();
166
167
5fca2e23
PD
168 void drillHole(const DNSName &domain, const string &ip);
169 bool justNotified(const DNSName &domain, const string &ip);
d3ee36f2 170 void addSuckRequest(const DNSName &domain, const string &master);
7f3d870e 171 void addSlaveCheckRequest(const DomainInfo& di, const ComboAddress& remote);
7108e055 172 void addTrySuperMasterRequest(DNSPacket *p);
5fca2e23 173 void notify(const DNSName &domain, const string &ip);
12c86877 174 void mainloop();
dbcb3066 175 void retrievalLoopThread();
5fca2e23 176 void sendNotification(int sock, const DNSName &domain, const ComboAddress& remote, uint16_t id);
a71bee29 177
12c86877
BH
178 static void *launchhelper(void *p)
179 {
180 static_cast<CommunicatorClass *>(p)->mainloop();
181 return 0;
182 }
dbcb3066
BH
183 static void *retrieveLaunchhelper(void *p)
184 {
185 static_cast<CommunicatorClass *>(p)->retrievalLoopThread();
186 return 0;
187 }
5fca2e23 188 bool notifyDomain(const DNSName &domain);
12c86877 189private:
0c01dd7c 190 void makeNotifySockets();
5fca2e23 191 void queueNotifyDomain(const DNSName &domain, UeberBackend *B);
0c01dd7c 192 int d_nsock4, d_nsock6;
675fa24c 193 map<pair<DNSName,string>,time_t>d_holes;
12c86877 194 pthread_mutex_t d_holelock;
dbcb3066 195 void launchRetrievalThreads();
d3ee36f2 196 void suck(const DNSName &domain, const string &remote);
3e7dcee6 197 void ixfrSuck(const DNSName &domain, const TSIGTriplet& tt, const ComboAddress& laddr, const ComboAddress& remote, boost::scoped_ptr<AuthLua>& pdl,
198 ZoneStatus& zs, vector<DNSRecord>* axfr);
cd189f24 199
12c86877
BH
200 void slaveRefresh(PacketHandler *P);
201 void masterUpdateCheck(PacketHandler *P);
202 pthread_mutex_t d_lock;
dbcb3066
BH
203
204 UniQueue d_suckdomains;
3e7dcee6 205 set<DNSName> d_inprogress;
dbcb3066 206
12c86877
BH
207 Semaphore d_suck_sem;
208 Semaphore d_any_sem;
88c0425a 209 time_t d_tickinterval;
7f3d870e 210 set<DomainInfo> d_tocheck;
7108e055 211 vector<DNSPacket> d_potentialsupermasters;
24d3239e 212 set<string> d_alsoNotify;
cf58746e
PL
213 NotificationQueue d_nq;
214 NetmaskGroup d_onlyNotify;
215 bool d_havepriosuckrequest;
216 bool d_masterschanged, d_slaveschanged;
217 bool d_preventSelfNotification;
3e7dcee6 218
219 struct RemoveSentinel
220 {
221 explicit RemoveSentinel(const DNSName& dn, CommunicatorClass* cc) : d_dn(dn), d_cc(cc)
222 {}
223
224 ~RemoveSentinel()
225 {
226 Lock l(&d_cc->d_lock);
227 d_cc->d_inprogress.erase(d_dn);
228 }
229 DNSName d_dn;
230 CommunicatorClass* d_cc;
231};
232
12c86877
BH
233};
234
ef03cc09 235// class that one day might be more than a function to help you get IP addresses for a nameserver
236class FindNS
237{
238public:
239 vector<string> lookup(const DNSName &name, DNSBackend *b)
240 {
241 vector<string> addresses;
242
243 this->resolve_name(&addresses, name);
244
2a642697
PD
245 if(b) {
246 b->lookup(QType(QType::ANY),name);
247 DNSResourceRecord rr;
248 while(b->get(rr))
249 if(rr.qtype.getCode() == QType::A || rr.qtype.getCode()==QType::AAAA)
250 addresses.push_back(rr.content); // SOL if you have a CNAME for an NS
251 }
ef03cc09 252 return addresses;
253 }
254
255 vector<string> lookup(const DNSName &name, UeberBackend *b)
256 {
257 vector<string> addresses;
258
259 this->resolve_name(&addresses, name);
260
2a642697
PD
261 if(b) {
262 b->lookup(QType(QType::ANY),name);
263 DNSResourceRecord rr;
264 while(b->get(rr))
265 if(rr.qtype.getCode() == QType::A || rr.qtype.getCode()==QType::AAAA)
266 addresses.push_back(rr.content); // SOL if you have a CNAME for an NS
267 }
ef03cc09 268 return addresses;
269 }
270
271private:
272 void resolve_name(vector<string>* addresses, const DNSName& name)
273 {
274 struct addrinfo* res;
275 struct addrinfo hints;
276 memset(&hints, 0, sizeof(hints));
277
278 for(int n = 0; n < 2; ++n) {
279 hints.ai_family = n ? AF_INET : AF_INET6;
280 ComboAddress remote;
281 remote.sin4.sin_family = AF_INET6;
282 if(!getaddrinfo(name.toString().c_str(), 0, &hints, &res)) {
283 struct addrinfo* address = res;
284 do {
a683e8bd
RG
285 if (address->ai_addrlen <= sizeof(remote)) {
286 memcpy(&remote, address->ai_addr, address->ai_addrlen);
287 addresses->push_back(remote.toString());
288 }
ef03cc09 289 } while((address = address->ai_next));
290 freeaddrinfo(res);
291 }
292 }
293 }
294};
295
296
12c86877 297#endif