| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002-2009 PowerDNS.COM BV |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License version 2 as |
|---|
| 7 | published by the Free Software Foundation; |
|---|
| 8 | |
|---|
| 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | GNU General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU General Public License |
|---|
| 15 | along with this program; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | */ |
|---|
| 18 | #include "packetcache.hh" |
|---|
| 19 | #include "utility.hh" |
|---|
| 20 | #include <errno.h> |
|---|
| 21 | #include "communicator.hh" |
|---|
| 22 | #include <set> |
|---|
| 23 | #include <boost/utility.hpp> |
|---|
| 24 | #include "dnsbackend.hh" |
|---|
| 25 | #include "ueberbackend.hh" |
|---|
| 26 | #include "packethandler.hh" |
|---|
| 27 | #include "resolver.hh" |
|---|
| 28 | #include "logger.hh" |
|---|
| 29 | #include "dns.hh" |
|---|
| 30 | #include "arguments.hh" |
|---|
| 31 | #include "session.hh" |
|---|
| 32 | #include "packetcache.hh" |
|---|
| 33 | #include <boost/foreach.hpp> |
|---|
| 34 | #include <boost/lexical_cast.hpp> |
|---|
| 35 | #include "inflighter.cc" |
|---|
| 36 | |
|---|
| 37 | #include "namespaces.hh" |
|---|
| 38 | |
|---|
| 39 | void CommunicatorClass::addSuckRequest(const string &domain, const string &master, bool priority) |
|---|
| 40 | { |
|---|
| 41 | Lock l(&d_lock); |
|---|
| 42 | |
|---|
| 43 | SuckRequest sr; |
|---|
| 44 | sr.domain = domain; |
|---|
| 45 | sr.master = master; |
|---|
| 46 | |
|---|
| 47 | if(priority) { |
|---|
| 48 | d_suckdomains.push_front(sr); |
|---|
| 49 | // d_havepriosuckrequest=true; |
|---|
| 50 | } |
|---|
| 51 | else |
|---|
| 52 | d_suckdomains.push_back(sr); |
|---|
| 53 | |
|---|
| 54 | d_suck_sem.post(); |
|---|
| 55 | d_any_sem.post(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void CommunicatorClass::suck(const string &domain,const string &remote) |
|---|
| 59 | { |
|---|
| 60 | L<<Logger::Error<<"Initiating transfer of '"<<domain<<"' from remote '"<<remote<<"'"<<endl; |
|---|
| 61 | uint32_t domain_id; |
|---|
| 62 | PacketHandler P; |
|---|
| 63 | |
|---|
| 64 | DomainInfo di; |
|---|
| 65 | di.backend=0; |
|---|
| 66 | bool first=true; |
|---|
| 67 | try { |
|---|
| 68 | Resolver resolver; |
|---|
| 69 | resolver.axfr(remote, domain.c_str()); |
|---|
| 70 | |
|---|
| 71 | UeberBackend *B=dynamic_cast<UeberBackend *>(P.getBackend()); |
|---|
| 72 | |
|---|
| 73 | if(!B->getDomainInfo(domain, di) || !di.backend) { |
|---|
| 74 | L<<Logger::Error<<"Can't determine backend for domain '"<<domain<<"'"<<endl; |
|---|
| 75 | return; |
|---|
| 76 | } |
|---|
| 77 | domain_id=di.id; |
|---|
| 78 | |
|---|
| 79 | Resolver::res_t recs; |
|---|
| 80 | |
|---|
| 81 | while(resolver.axfrChunk(recs)) { |
|---|
| 82 | if(first) { |
|---|
| 83 | L<<Logger::Error<<"AXFR started for '"<<domain<<"', transaction started"<<endl; |
|---|
| 84 | di.backend->startTransaction(domain, domain_id); |
|---|
| 85 | first=false; |
|---|
| 86 | } |
|---|
| 87 | for(Resolver::res_t::iterator i=recs.begin();i!=recs.end();++i) { |
|---|
| 88 | if(!endsOn(i->qname, domain)) { |
|---|
| 89 | L<<Logger::Error<<"Remote "<<remote<<" tried to sneak in out-of-zone data '"<<i->qname<<"' during AXFR of zone '"<<domain<<"', ignoring"<<endl; |
|---|
| 90 | continue; |
|---|
| 91 | } |
|---|
| 92 | i->domain_id=domain_id; |
|---|
| 93 | if(i->qtype.getCode()>=1024) |
|---|
| 94 | throw DBException("Database can't store unknown record type "+lexical_cast<string>(i->qtype.getCode()-1024)); |
|---|
| 95 | |
|---|
| 96 | di.backend->feedRecord(*i); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | di.backend->commitTransaction(); |
|---|
| 100 | di.backend->setFresh(domain_id); |
|---|
| 101 | L<<Logger::Error<<"AXFR done for '"<<domain<<"', zone committed"<<endl; |
|---|
| 102 | } |
|---|
| 103 | catch(DBException &re) { |
|---|
| 104 | L<<Logger::Error<<"Unable to feed record during incoming AXFR of '"+domain+"': "<<re.reason<<endl; |
|---|
| 105 | if(di.backend && !first) { |
|---|
| 106 | L<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
|---|
| 107 | di.backend->abortTransaction(); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | catch(ResolverException &re) { |
|---|
| 111 | L<<Logger::Error<<"Unable to AXFR zone '"+domain+"' from remote '"<<remote<<"': "<<re.reason<<endl; |
|---|
| 112 | if(di.backend && !first) { |
|---|
| 113 | L<<Logger::Error<<"Aborting possible open transaction for domain '"<<domain<<"' AXFR"<<endl; |
|---|
| 114 | di.backend->abortTransaction(); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | struct QueryInfo |
|---|
| 119 | { |
|---|
| 120 | struct timeval query_ttd; |
|---|
| 121 | uint16_t id; |
|---|
| 122 | }; |
|---|
| 123 | |
|---|
| 124 | struct SlaveSenderReceiver |
|---|
| 125 | { |
|---|
| 126 | typedef pair<string, uint16_t> Identifier; |
|---|
| 127 | typedef uint32_t Answer; |
|---|
| 128 | |
|---|
| 129 | map<uint32_t, uint32_t> d_serials; |
|---|
| 130 | |
|---|
| 131 | SlaveSenderReceiver() |
|---|
| 132 | { |
|---|
| 133 | d_resolver.makeUDPSocket(); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | Identifier send(DomainInfo& di) |
|---|
| 137 | { |
|---|
| 138 | random_shuffle(di.masters.begin(), di.masters.end()); |
|---|
| 139 | return make_pair(di.zone, d_resolver.sendResolve(*di.masters.begin(), di.zone.c_str(), QType::SOA)); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | bool receive(Identifier& id, Answer& a) |
|---|
| 143 | { |
|---|
| 144 | if(d_resolver.tryGetSOASerial(&id.first, &a, &id.second)) { |
|---|
| 145 | return 1; |
|---|
| 146 | } |
|---|
| 147 | return 0; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void deliverAnswer(DomainInfo& i, uint32_t serial) |
|---|
| 151 | { |
|---|
| 152 | d_serials[i.id]=serial; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | Resolver d_resolver; |
|---|
| 156 | |
|---|
| 157 | }; |
|---|
| 158 | |
|---|
| 159 | void CommunicatorClass::slaveRefresh(PacketHandler *P) |
|---|
| 160 | { |
|---|
| 161 | UeberBackend *B=dynamic_cast<UeberBackend *>(P->getBackend()); |
|---|
| 162 | vector<DomainInfo> sdomains; |
|---|
| 163 | B->getUnfreshSlaveInfos(&sdomains); |
|---|
| 164 | if(sdomains.empty()) |
|---|
| 165 | { |
|---|
| 166 | if(d_slaveschanged) |
|---|
| 167 | L<<Logger::Warning<<"All slave domains are fresh"<<endl; |
|---|
| 168 | d_slaveschanged=false; |
|---|
| 169 | return; |
|---|
| 170 | } |
|---|
| 171 | else |
|---|
| 172 | L<<Logger::Warning<<sdomains.size()<<" slave domain"<<(sdomains.size()>1 ? "s" : "")<<" need"<< |
|---|
| 173 | (sdomains.size()>1 ? "" : "s")<< |
|---|
| 174 | " checking"<<endl; |
|---|
| 175 | |
|---|
| 176 | SlaveSenderReceiver ssr; |
|---|
| 177 | Inflighter<vector<DomainInfo>, SlaveSenderReceiver> ifl(sdomains, ssr); |
|---|
| 178 | |
|---|
| 179 | ifl.d_maxInFlight = 200; |
|---|
| 180 | |
|---|
| 181 | for(;;) { |
|---|
| 182 | try { |
|---|
| 183 | ifl.run(); |
|---|
| 184 | break; |
|---|
| 185 | } |
|---|
| 186 | catch(exception& e) { |
|---|
| 187 | L<<Logger::Error<<"While checking domain freshness: " << e.what()<<endl; |
|---|
| 188 | } |
|---|
| 189 | catch(AhuException &re) { |
|---|
| 190 | L<<Logger::Error<<"While checking domain freshness: " << re.reason<<endl; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | L<<Logger::Warning<<"Received serial number updates for "<<ssr.d_serials.size()<<" zones"<<endl; |
|---|
| 194 | |
|---|
| 195 | BOOST_FOREACH(DomainInfo& di, sdomains) { |
|---|
| 196 | if(!ssr.d_serials.count(di.id)) |
|---|
| 197 | continue; |
|---|
| 198 | uint32_t theirserial = ssr.d_serials[di.id], ourserial = di.serial; |
|---|
| 199 | |
|---|
| 200 | if(theirserial < ourserial) { |
|---|
| 201 | L<<Logger::Error<<"Domain "<<di.zone<<" more recent than master, our serial " << ourserial << " > their serial "<< theirserial << endl; |
|---|
| 202 | di.backend->setFresh(di.id); |
|---|
| 203 | } |
|---|
| 204 | else if(theirserial == ourserial) { |
|---|
| 205 | L<<Logger::Warning<<"Domain "<< di.zone<<" is fresh"<<endl; |
|---|
| 206 | di.backend->setFresh(di.id); |
|---|
| 207 | } |
|---|
| 208 | else { |
|---|
| 209 | L<<Logger::Warning<<"Domain "<< di.zone<<" is stale, master serial "<<theirserial<<", our serial "<< ourserial <<endl; |
|---|
| 210 | addSuckRequest(di.zone, *di.masters.begin()); |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | |
|---|