123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629 |
- #include <exceptions/exceptions.h>
- #include <dns/name.h>
- #include <dns/nsec3hash.h>
- #include <dns/rdataclass.h>
- #include <dns/rrclass.h>
- #include <dns/rrsetlist.h>
- #include <dns/masterload.h>
- #include <datasrc/memory_datasrc.h>
- #include <datasrc/rbtree.h>
- #include <datasrc/rbnode_rrset.h>
- #include <datasrc/logger.h>
- #include <datasrc/iterator.h>
- #include <datasrc/data_source.h>
- #include <datasrc/factory.h>
- #include <boost/shared_ptr.hpp>
- #include <boost/scoped_ptr.hpp>
- #include <boost/bind.hpp>
- #include <boost/foreach.hpp>
- #include <algorithm>
- #include <map>
- #include <utility>
- #include <cctype>
- #include <cassert>
- using namespace std;
- using namespace isc::dns;
- using namespace isc::dns::rdata;
- using boost::scoped_ptr;
- namespace isc {
- namespace datasrc {
- using namespace internal;
- namespace {
- typedef boost::shared_ptr<internal::RBNodeRRset> RBNodeRRsetPtr;
- typedef boost::shared_ptr<const internal::RBNodeRRset> ConstRBNodeRRsetPtr;
- typedef map<RRType, ConstRBNodeRRsetPtr> Domain;
- typedef Domain::value_type DomainPair;
- typedef boost::shared_ptr<Domain> DomainPtr;
- typedef RBTree<Domain> DomainTree;
- typedef RBNode<Domain> DomainNode;
- namespace domain_flag {
- const DomainNode::Flags WILD = DomainNode::FLAG_USER1;
- const DomainNode::Flags GLUE = DomainNode::FLAG_USER2;
- };
- typedef map<string, ConstRBNodeRRsetPtr> NSEC3Map;
- typedef NSEC3Map::value_type NSEC3Pair;
- struct ZoneData {
- ZoneData(const Name& origin) : domains_(true), origin_data_(NULL) {
-
- domains_.insert(origin, &origin_data_);
- DomainPtr origin_domain(new Domain);
- origin_data_->setData(origin_domain);
- }
-
- DomainTree domains_;
-
- DomainNode* origin_data_;
-
- struct NSEC3Data {
- NSEC3Data(const generic::NSEC3PARAM& nsec3param) :
- hash_(NSEC3Hash::create(nsec3param))
- {}
- NSEC3Data(const generic::NSEC3& nsec3) :
- hash_(NSEC3Hash::create(nsec3))
- {}
- NSEC3Map map_;
- const scoped_ptr<NSEC3Hash> hash_;
- };
- scoped_ptr<NSEC3Data> nsec3_data_;
- };
- }
- namespace internal {
- struct AdditionalNodeInfo {
- AdditionalNodeInfo(DomainNode* node) : node_(node) {}
- DomainNode* node_;
- };
- struct RBNodeRRsetImpl {
- public:
- RBNodeRRsetImpl(const ConstRRsetPtr& rrset) : rrset_(rrset)
- {}
- ConstRRsetPtr rrset_;
- scoped_ptr<vector<AdditionalNodeInfo> > additionals_;
- };
- RBNodeRRset::RBNodeRRset(const ConstRRsetPtr& rrset) :
- impl_(new RBNodeRRsetImpl(rrset))
- {
- }
- RBNodeRRset::~RBNodeRRset() {
- delete impl_;
- }
- unsigned int
- RBNodeRRset::getRdataCount() const {
- return (impl_->rrset_->getRdataCount());
- }
- const Name&
- RBNodeRRset::getName() const {
- return (impl_->rrset_->getName());
- }
- const RRClass&
- RBNodeRRset::getClass() const {
- return (impl_->rrset_->getClass());
- }
- const RRType&
- RBNodeRRset::getType() const {
- return (impl_->rrset_->getType());
- }
- const RRTTL&
- RBNodeRRset::getTTL() const {
- return (impl_->rrset_->getTTL());
- }
- void
- RBNodeRRset::setName(const Name&) {
- isc_throw(isc::NotImplemented, "RBNodeRRset::setName() not supported");
- }
- void
- RBNodeRRset::setTTL(const RRTTL&) {
- isc_throw(isc::NotImplemented, "RBNodeRRset::setTTL() not supported");
- }
- string
- RBNodeRRset::toText() const {
- return (impl_->rrset_->toText());
- }
- unsigned int
- RBNodeRRset::toWire(AbstractMessageRenderer& renderer) const {
- return (impl_->rrset_->toWire(renderer));
- }
- unsigned int
- RBNodeRRset::toWire(isc::util::OutputBuffer& buffer) const {
- return (impl_->rrset_->toWire(buffer));
- }
- void
- RBNodeRRset::addRdata(ConstRdataPtr) {
- isc_throw(isc::NotImplemented, "RBNodeRRset::addRdata() not supported");
- }
- void
- RBNodeRRset::addRdata(const Rdata&) {
- isc_throw(isc::NotImplemented, "RBNodeRRset::addRdata() not supported");
- }
- RdataIteratorPtr
- RBNodeRRset::getRdataIterator() const {
- return (impl_->rrset_->getRdataIterator());
- }
- RRsetPtr
- RBNodeRRset::getRRsig() const {
- return (impl_->rrset_->getRRsig());
- }
- void
- RBNodeRRset::addRRsig(const ConstRdataPtr& rdata) {
- AbstractRRset* p = const_cast<AbstractRRset*>(impl_->rrset_.get());
- p->addRRsig(rdata);
- }
- void
- RBNodeRRset::addRRsig(const RdataPtr& rdata) {
- AbstractRRset* p = const_cast<AbstractRRset*>(impl_->rrset_.get());
- p->addRRsig(rdata);
- }
- void
- RBNodeRRset::addRRsig(const AbstractRRset& sigs) {
- AbstractRRset* p = const_cast<AbstractRRset*>(impl_->rrset_.get());
- p->addRRsig(sigs);
- }
- void
- RBNodeRRset::addRRsig(const ConstRRsetPtr& sigs) {
- AbstractRRset* p = const_cast<AbstractRRset*>(impl_->rrset_.get());
- p->addRRsig(sigs);
- }
- void
- RBNodeRRset::addRRsig(const RRsetPtr& sigs) {
- AbstractRRset* p = const_cast<AbstractRRset*>(impl_->rrset_.get());
- p->addRRsig(sigs);
- }
- void
- RBNodeRRset::removeRRsig() {
- AbstractRRset* p = const_cast<AbstractRRset*>(impl_->rrset_.get());
- p->removeRRsig();
- }
- ConstRRsetPtr
- RBNodeRRset::getUnderlyingRRset() const {
- return (impl_->rrset_);
- }
- void
- RBNodeRRset::addAdditionalNode(const AdditionalNodeInfo& additional) {
-
- if (!impl_->additionals_) {
- impl_->additionals_.reset(new vector<AdditionalNodeInfo>);
- }
- impl_->additionals_->push_back(additional);
- }
- const vector<AdditionalNodeInfo>*
- RBNodeRRset::getAdditionalNodes() const {
- return (impl_->additionals_.get());
- }
- void
- RBNodeRRset::copyAdditionalNodes(RBNodeRRset& dst) const {
- if (impl_->additionals_) {
- dst.impl_->additionals_.reset(
- new vector<AdditionalNodeInfo>(impl_->additionals_->begin(),
- impl_->additionals_->end()));
- }
- }
- }
- namespace {
- struct RBNodeResultContext {
-
-
-
-
-
- RBNodeResultContext(ZoneFinder::Result code_param,
- ConstRBNodeRRsetPtr rrset_param,
- ZoneFinder::FindResultFlags flags_param,
- const DomainNode* node) :
- code(code_param), rrset(rrset_param), flags(flags_param),
- found_node(node)
- {}
- const ZoneFinder::Result code;
- const ConstRBNodeRRsetPtr rrset;
- const ZoneFinder::FindResultFlags flags;
- const DomainNode* const found_node;
- };
- }
- class InMemoryZoneFinder::Context : public ZoneFinder::Context {
- public:
-
-
-
-
-
-
- Context(ZoneFinder& finder, ZoneFinder::FindOptions options,
- const RBNodeResultContext& result) :
- ZoneFinder::Context(finder, options,
- ResultContext(result.code, result.rrset,
- result.flags)),
- rrset_(result.rrset), found_node_(result.found_node)
- {}
- protected:
- virtual void getAdditionalImpl(const vector<RRType>& requested_types,
- vector<ConstRRsetPtr>& result)
- {
- if (!rrset_) {
-
-
- if (found_node_ == NULL || found_node_->isEmpty()) {
- isc_throw(isc::Unexpected,
- "Invalid call to in-memory getAdditional: caller's "
- "bug or broken zone");
- }
- BOOST_FOREACH(const DomainPair& dom_it, *found_node_->getData()) {
- getAdditionalForRRset(*dom_it.second, requested_types,
- result);
- }
- } else {
- getAdditionalForRRset(*rrset_, requested_types, result);
- }
- }
- private:
-
-
-
-
- static void getAdditionalForRRset(const RBNodeRRset& rrset,
- const vector<RRType>& requested_types,
- vector<ConstRRsetPtr>& result)
- {
- const vector<AdditionalNodeInfo>* additionals_ =
- rrset.getAdditionalNodes();
- if (additionals_ == NULL) {
- return;
- }
- const bool glue_ok = (rrset.getType() == RRType::NS());
- BOOST_FOREACH(const AdditionalNodeInfo& additional, *additionals_) {
- assert(additional.node_ != NULL);
- if (additional.node_->isEmpty()) {
- continue;
- }
- if (!glue_ok && additional.node_->getFlag(domain_flag::GLUE)) {
- continue;
- }
- BOOST_FOREACH(const RRType& rrtype, requested_types) {
- Domain::const_iterator found =
- additional.node_->getData()->find(rrtype);
- if (found != additional.node_->getData()->end()) {
-
- result.push_back(found->second);
- }
- }
- }
- }
- const ConstRBNodeRRsetPtr rrset_;
- const DomainNode* const found_node_;
- };
- struct InMemoryZoneFinder::InMemoryZoneFinderImpl {
-
- InMemoryZoneFinderImpl(const RRClass& zone_class, const Name& origin) :
- zone_class_(zone_class), origin_(origin),
- zone_data_(new ZoneData(origin_))
- {}
-
- RRClass zone_class_;
- Name origin_;
- string file_name_;
-
- scoped_ptr<ZoneData> zone_data_;
-
-
-
-
-
-
-
-
-
-
-
-
-
- void addWildcards(DomainTree& domains, const Name& name) {
- Name wname(name);
- const unsigned int labels(wname.getLabelCount());
- const unsigned int origin_labels(origin_.getLabelCount());
- for (unsigned int l = labels;
- l > origin_labels;
- --l, wname = wname.split(1)) {
- if (wname.isWildcard()) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_WILDCARD).
- arg(name);
-
-
- DomainNode* node;
- DomainTree::Result result(domains.insert(wname.split(1),
- &node));
- assert(result == DomainTree::SUCCESS ||
- result == DomainTree::ALREADYEXISTS);
- node->setFlag(domain_flag::WILD);
-
-
-
-
- result = domains.insert(wname, &node);
- assert(result == DomainTree::SUCCESS ||
- result == DomainTree::ALREADYEXISTS);
- }
- }
- }
-
-
- static bool isNotNSEC(const DomainPair& element) {
- return (element.second->getType() != RRType::NSEC());
- }
-
- void contextCheck(const AbstractRRset& rrset, const Domain& domain) const {
-
-
-
- if (rrset.getType() == RRType::CNAME()) {
- if (find_if(domain.begin(), domain.end(), isNotNSEC)
- != domain.end()) {
- LOG_ERROR(logger, DATASRC_MEM_CNAME_TO_NONEMPTY).
- arg(rrset.getName());
- isc_throw(AddError, "CNAME can't be added with other data for "
- << rrset.getName());
- }
- } else if (rrset.getType() != RRType::NSEC() &&
- domain.find(RRType::CNAME()) != domain.end()) {
- LOG_ERROR(logger, DATASRC_MEM_CNAME_COEXIST).arg(rrset.getName());
- isc_throw(AddError, "CNAME and " << rrset.getType() <<
- " can't coexist for " << rrset.getName());
- }
-
- if (rrset.getName() != origin_ &&
-
- ((rrset.getType() == RRType::DNAME() &&
- domain.find(RRType::NS()) != domain.end()) ||
-
- (rrset.getType() == RRType::NS() &&
- domain.find(RRType::DNAME()) != domain.end())))
- {
- LOG_ERROR(logger, DATASRC_MEM_DNAME_NS).arg(rrset.getName());
- isc_throw(AddError, "DNAME can't coexist with NS in non-apex "
- "domain " << rrset.getName());
- }
- }
-
-
-
- void addValidation(const ConstRRsetPtr rrset) {
- if (!rrset) {
- isc_throw(NullRRset, "The rrset provided is NULL");
- }
- if (rrset->getRdataCount() == 0) {
- isc_throw(AddError, "The rrset provided is empty: " <<
- rrset->getName() << "/" << rrset->getType());
- }
-
-
- if ((rrset->getType() == RRType::CNAME() ||
- rrset->getType() == RRType::DNAME()) &&
- rrset->getRdataCount() > 1)
- {
-
-
-
- LOG_ERROR(logger, DATASRC_MEM_SINGLETON).arg(rrset->getName()).
- arg(rrset->getType());
- isc_throw(AddError, "multiple RRs of singleton type for "
- << rrset->getName());
- }
-
-
- if ((rrset->getType() == RRType::NSEC3() ||
- rrset->getType() == RRType::NSEC3PARAM()) &&
- rrset->getRdataCount() > 1) {
- isc_throw(AddError, "Multiple NSEC3/NSEC3PARAM RDATA is given for "
- << rrset->getName() << " which isn't supported");
- }
- NameComparisonResult compare(origin_.compare(rrset->getName()));
- if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
- compare.getRelation() != NameComparisonResult::EQUAL)
- {
- LOG_ERROR(logger, DATASRC_MEM_OUT_OF_ZONE).arg(rrset->getName()).
- arg(origin_);
- isc_throw(OutOfZone, "The name " << rrset->getName() <<
- " is not contained in zone " << origin_);
- }
-
-
-
-
-
-
-
-
- if (rrset->getName().isWildcard()) {
- if (rrset->getType() == RRType::NS()) {
- LOG_ERROR(logger, DATASRC_MEM_WILDCARD_NS).
- arg(rrset->getName());
- isc_throw(AddError, "Invalid NS owner name (wildcard): " <<
- rrset->getName());
- }
- if (rrset->getType() == RRType::DNAME()) {
- LOG_ERROR(logger, DATASRC_MEM_WILDCARD_DNAME).
- arg(rrset->getName());
- isc_throw(AddError, "Invalid DNAME owner name (wildcard): " <<
- rrset->getName());
- }
- }
-
-
-
-
-
- if (rrset->getType() == RRType::NSEC3() &&
- (rrset->getName().isWildcard() ||
- rrset->getName().getLabelCount() !=
- origin_.getLabelCount() + 1)) {
- LOG_ERROR(logger, DATASRC_BAD_NSEC3_NAME).
- arg(rrset->getName());
- isc_throw(AddError, "Invalid NSEC3 owner name: " <<
- rrset->getName());
- }
- }
- result::Result addRRsig(const ConstRRsetPtr sig_rrset, ZoneData& zone_data)
- {
-
-
- RdataIteratorPtr rit = sig_rrset->getRdataIterator();
- const RRType covered = dynamic_cast<const generic::RRSIG&>(
- rit->getCurrent()).typeCovered();
- for (rit->next(); !rit->isLast(); rit->next()) {
- if (dynamic_cast<const generic::RRSIG&>(
- rit->getCurrent()).typeCovered() != covered) {
- isc_throw(AddError, "RRSIG contains mixed covered types: "
- << sig_rrset->toText());
- }
- }
-
-
- ConstRRsetPtr covered_rrset;
- if (covered != RRType::NSEC3()) {
- DomainNode* node = NULL;
- if (zone_data.domains_.find(sig_rrset->getName(), &node) !=
- DomainTree::EXACTMATCH || node == NULL || !node->getData()) {
- isc_throw(AddError,
- "RRSIG is being added, but no RR to be covered: "
- << sig_rrset->getName());
- }
- const Domain::const_iterator it = node->getData()->find(covered);
- if (it != node->getData()->end()) {
- covered_rrset = it->second;
- }
- } else {
-
-
- if (zone_data.nsec3_data_) {
-
-
-
-
-
-
- string fst_label =
- sig_rrset->getName().split(0, 1).toText(true);
- transform(fst_label.begin(), fst_label.end(),
- fst_label.begin(), ::toupper);
- NSEC3Map::const_iterator found =
- zone_data.nsec3_data_->map_.find(fst_label);
- if (found != zone_data.nsec3_data_->map_.end()) {
- covered_rrset = found->second;
- assert(covered_rrset->getType() == covered);
- }
- }
- }
- if (!covered_rrset) {
- isc_throw(AddError, "RRSIG is being added, but no RR of "
- "covered type found: " << sig_rrset->toText());
- }
-
-
- if (covered_rrset->getRRsig()) {
- isc_throw(AddError,
- "RRSIG is being added to override an existing one: "
- << sig_rrset->toText());
- }
-
-
-
-
-
-
-
-
-
- boost::const_pointer_cast<AbstractRRset>(covered_rrset)->addRRsig(sig_rrset);
- return (result::SUCCESS);
- }
- result::Result addNSEC3(const ConstRRsetPtr rrset, ZoneData& zone_data) {
-
- const generic::NSEC3& nsec3_rdata =
- dynamic_cast<const generic::NSEC3&>(
- rrset->getRdataIterator()->getCurrent());
-
-
- if (!zone_data.nsec3_data_) {
- zone_data.nsec3_data_.reset(new ZoneData::NSEC3Data(nsec3_rdata));
- } else if (!zone_data.nsec3_data_->hash_->match(nsec3_rdata)) {
- isc_throw(AddError, "NSEC3 with inconsistent parameters: " <<
- rrset->toText());
- }
- string fst_label = rrset->getName().split(0, 1).toText(true);
- transform(fst_label.begin(), fst_label.end(), fst_label.begin(),
- ::toupper);
-
-
- if (zone_data.nsec3_data_->map_.find(fst_label) !=
- zone_data.nsec3_data_->map_.end()) {
- return (result::EXIST);
- }
- zone_data.nsec3_data_->map_.insert(
- NSEC3Pair(fst_label, ConstRBNodeRRsetPtr(new RBNodeRRset(rrset))));
- return (result::SUCCESS);
- }
-
-
- result::Result add(const ConstRRsetPtr& rawrrset, ZoneData& zone_data,
- vector<RBNodeRRset*>* need_additionals)
- {
-
-
- addValidation(rawrrset);
-
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_RRSET).
- arg(rawrrset->getName()).arg(rawrrset->getType()).arg(origin_);
-
-
-
- RBNodeRRsetPtr rrset(new RBNodeRRset(rawrrset));
- if (rrset->getType() == RRType::NSEC3()) {
- return (addNSEC3(rrset, zone_data));
- }
-
-
- if (rrset->getType() == RRType::RRSIG()) {
- return (addRRsig(rrset, zone_data));
- }
-
-
-
-
- addWildcards(zone_data.domains_, rrset->getName());
-
- DomainNode* node;
- DomainTree::Result result = zone_data.domains_.insert(rrset->getName(),
- &node);
-
- assert((result == DomainTree::SUCCESS ||
- result == DomainTree::ALREADYEXISTS) && node!= NULL);
-
- DomainPtr domain;
-
- if (node->isEmpty()) {
- domain.reset(new Domain);
- node->setData(domain);
- } else {
- domain = node->getData();
- }
-
-
-
-
-
- contextCheck(*rrset, *domain);
-
- if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
-
-
-
- if (rrset->getType() == RRType::NS() &&
- rrset->getName() != origin_) {
- node->setFlag(DomainNode::FLAG_CALLBACK);
-
- } else if (rrset->getType() == RRType::DNAME()) {
- node->setFlag(DomainNode::FLAG_CALLBACK);
- }
- if (need_additionals != NULL &&
- (rrset->getType() == RRType::NS() ||
- rrset->getType() == RRType::MX())) {
- need_additionals->push_back(rrset.get());
- }
-
-
- if (rrset->getType() == RRType::NSEC3PARAM() &&
- rrset->getName() == origin_) {
-
- const generic::NSEC3PARAM& param =
- dynamic_cast<const generic::NSEC3PARAM&>(
- rrset->getRdataIterator()->getCurrent());
- if (!zone_data.nsec3_data_) {
- zone_data.nsec3_data_.reset(
- new ZoneData::NSEC3Data(param));
- } else if (!zone_data.nsec3_data_->hash_->match(param)) {
- isc_throw(AddError, "NSEC3PARAM with inconsistent "
- "parameters: " << rrset->toText());
- }
- }
- return (result::SUCCESS);
- } else {
-
- return (result::EXIST);
- }
- }
-
- void addFromLoad(const ConstRRsetPtr& set, ZoneData* zone_data,
- vector<RBNodeRRset*>* need_additionals)
- {
- switch (add(set, *zone_data, need_additionals)) {
- case result::EXIST:
- LOG_ERROR(logger, DATASRC_MEM_DUP_RRSET).
- arg(set->getName()).arg(set->getType());
- isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
- set->toText());
- case result::SUCCESS:
- return;
- default:
- assert(0);
- }
- }
-
-
-
-
-
- struct FindState {
- FindState(FindOptions options) :
- zonecut_node_(NULL),
- dname_node_(NULL),
- options_(options)
- {}
- const DomainNode* zonecut_node_;
- const DomainNode* dname_node_;
- ConstRBNodeRRsetPtr rrset_;
- const FindOptions options_;
- };
-
-
- static bool cutCallback(const DomainNode& node, FindState* state) {
-
-
-
-
- const Domain::const_iterator foundDNAME(node.getData()->find(
- RRType::DNAME()));
- if (foundDNAME != node.getData()->end()) {
- LOG_DEBUG(logger, DBG_TRACE_DETAILED,
- DATASRC_MEM_DNAME_ENCOUNTERED);
- state->dname_node_ = &node;
- state->rrset_ = foundDNAME->second;
-
-
-
-
-
-
- return (true);
- }
-
- const Domain::const_iterator foundNS(node.getData()->find(
- RRType::NS()));
- if (foundNS != node.getData()->end()) {
-
-
- if (state->zonecut_node_ != NULL) {
- return (false);
- }
- LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_NS_ENCOUNTERED);
-
-
-
-
-
-
- state->zonecut_node_ = &node;
- state->rrset_ = foundNS->second;
-
-
- return ((state->options_ & FIND_GLUE_OK) == 0);
- }
-
-
- assert(0);
-
-
-
- return (false);
- }
-
- static ConstRBNodeRRsetPtr prepareRRset(const Name& name,
- const ConstRBNodeRRsetPtr& rrset,
- bool rename, FindOptions options)
- {
- if (rename) {
- LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_RENAME).
- arg(rrset->getName()).arg(name);
- RRsetPtr result_base(new RRset(name, rrset->getClass(),
- rrset->getType(),
- rrset->getTTL()));
- for (RdataIteratorPtr i(rrset->getRdataIterator()); !i->isLast();
- i->next()) {
- result_base->addRdata(i->getCurrent());
- }
- if ((options & FIND_DNSSEC) != 0) {
- ConstRRsetPtr sig_rrset = rrset->getRRsig();
- if (sig_rrset) {
- RRsetPtr result_sig(new RRset(name, sig_rrset->getClass(),
- RRType::RRSIG(),
- sig_rrset->getTTL()));
- for (RdataIteratorPtr i(sig_rrset->getRdataIterator());
- !i->isLast();
- i->next())
- {
- result_sig->addRdata(i->getCurrent());
- }
- result_base->addRRsig(result_sig);
- }
- }
- RBNodeRRsetPtr result(new RBNodeRRset(result_base));
- rrset->copyAdditionalNodes(*result);
- return (result);
- } else {
- return (rrset);
- }
- }
-
-
-
-
-
-
-
- RBNodeResultContext createFindResult(Result code,
- ConstRBNodeRRsetPtr rrset,
- bool wild = false,
- const DomainNode* node = NULL) const
- {
- FindResultFlags flags = RESULT_DEFAULT;
- if (wild) {
- flags = flags | RESULT_WILDCARD;
- }
- if ((code == NXRRSET || code == NXDOMAIN || wild) &&
- zone_data_->nsec3_data_) {
- flags = flags | RESULT_NSEC3_SIGNED;
- }
- return (RBNodeResultContext(code, rrset, flags, node));
- }
-
- RBNodeResultContext find(const Name& name, RRType type,
- std::vector<ConstRRsetPtr>* target,
- const FindOptions options) const
- {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FIND).arg(name).
- arg(type);
-
- DomainNode* node(NULL);
- FindState state(options);
- RBTreeNodeChain<Domain> node_path;
- bool rename(false);
- switch (zone_data_->domains_.find(name, &node, node_path, cutCallback,
- &state)) {
- case DomainTree::PARTIALMATCH:
-
- if (state.dname_node_ != NULL) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DNAME_FOUND).
- arg(state.rrset_->getName());
-
-
- return (createFindResult(DNAME,
- prepareRRset(name, state.rrset_,
- false, options)));
- }
- if (state.zonecut_node_ != NULL) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DELEG_FOUND).
- arg(state.rrset_->getName());
- return (createFindResult(DELEGATION,
- prepareRRset(name, state.rrset_,
- false, options)));
- }
-
-
-
- if (node_path.getLastComparisonResult().getRelation() ==
- NameComparisonResult::SUPERDOMAIN) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUPER_STOP).
- arg(name);
- return (createFindResult(NXRRSET, ConstRBNodeRRsetPtr()));
- }
-
- if (node->getFlag(domain_flag::WILD)) {
-
- if (node_path.getLastComparisonResult().getRelation() ==
- NameComparisonResult::COMMONANCESTOR && node_path.
- getLastComparisonResult().getCommonLabels() > 1) {
- LOG_DEBUG(logger, DBG_TRACE_DATA,
- DATASRC_MEM_WILDCARD_CANCEL).arg(name);
- return (createFindResult(NXDOMAIN,
- ConstRBNodeRRsetPtr(),
- false));
- }
- const Name wildcard(Name("*").concatenate(
- node_path.getAbsoluteName()));
- DomainTree::Result result =
- zone_data_->domains_.find(wildcard, &node);
-
- assert(result == DomainTree::EXACTMATCH);
-
- rename = true;
- break;
- }
-
- case DomainTree::NOTFOUND:
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NOT_FOUND).
- arg(name);
- return (createFindResult(NXDOMAIN, ConstRBNodeRRsetPtr(),
- false));
- case DomainTree::EXACTMATCH:
- break;
- default:
- assert(0);
- }
- assert(node != NULL);
-
-
- if (node->isEmpty()) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DOMAIN_EMPTY).
- arg(name);
- return (createFindResult(NXRRSET, ConstRBNodeRRsetPtr(), rename));
- }
- Domain::const_iterator found;
-
-
-
-
- if (node->getFlag(DomainNode::FLAG_CALLBACK) &&
- node != zone_data_->origin_data_ && type != RRType::DS()) {
- found = node->getData()->find(RRType::NS());
- if (found != node->getData()->end()) {
- LOG_DEBUG(logger, DBG_TRACE_DATA,
- DATASRC_MEM_EXACT_DELEGATION).arg(name);
- return (createFindResult(DELEGATION,
- prepareRRset(name, found->second,
- rename, options)));
- }
- }
-
- if (target != NULL && !node->getData()->empty()) {
-
- for (found = node->getData()->begin();
- found != node->getData()->end(); ++found)
- {
- target->push_back(prepareRRset(name, found->second, rename,
- options));
- }
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ANY_SUCCESS).
- arg(name);
- return (createFindResult(SUCCESS, ConstRBNodeRRsetPtr(), rename,
- node));
- }
- found = node->getData()->find(type);
- if (found != node->getData()->end()) {
-
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUCCESS).arg(name).
- arg(type);
- return (createFindResult(SUCCESS, prepareRRset(name,
- found->second,
- rename, options),
- rename));
- } else {
-
- found = node->getData()->find(RRType::CNAME());
- if (found != node->getData()->end()) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_CNAME).arg(name);
- return (createFindResult(CNAME,
- prepareRRset(name, found->second,
- rename, options),
- rename));
- }
- }
-
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NXRRSET).arg(type).
- arg(name);
- return (createFindResult(NXRRSET, ConstRBNodeRRsetPtr(), rename));
- }
- };
- InMemoryZoneFinder::InMemoryZoneFinder(const RRClass& zone_class, const Name& origin) :
- impl_(new InMemoryZoneFinderImpl(zone_class, origin))
- {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_CREATE).arg(origin).
- arg(zone_class);
- }
- InMemoryZoneFinder::~InMemoryZoneFinder() {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_DESTROY).arg(getOrigin()).
- arg(getClass());
- delete impl_;
- }
- Name
- InMemoryZoneFinder::getOrigin() const {
- return (impl_->origin_);
- }
- RRClass
- InMemoryZoneFinder::getClass() const {
- return (impl_->zone_class_);
- }
- ZoneFinderContextPtr
- InMemoryZoneFinder::find(const Name& name, const RRType& type,
- const FindOptions options)
- {
- return (ZoneFinderContextPtr(
- new Context(*this, options, impl_->find(name, type, NULL,
- options))));
- }
- ZoneFinderContextPtr
- InMemoryZoneFinder::findAll(const Name& name,
- std::vector<ConstRRsetPtr>& target,
- const FindOptions options)
- {
- return (ZoneFinderContextPtr(
- new Context(*this, options, impl_->find(name, RRType::ANY(),
- &target, options))));
- }
- ZoneFinder::FindNSEC3Result
- InMemoryZoneFinder::findNSEC3(const Name& name, bool recursive) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FINDNSEC3).arg(name).
- arg(recursive ? "recursive" : "non-recursive");
- if (!impl_->zone_data_->nsec3_data_) {
- isc_throw(DataSourceError,
- "findNSEC3 attempt for non NSEC3 signed zone: " <<
- impl_->origin_ << "/" << impl_->zone_class_);
- }
- const NSEC3Map& map = impl_->zone_data_->nsec3_data_->map_;
- if (map.empty()) {
- isc_throw(DataSourceError,
- "findNSEC3 attempt but zone has no NSEC3 RR: " <<
- impl_->origin_ << "/" << impl_->zone_class_);
- }
- const NameComparisonResult cmp_result = name.compare(impl_->origin_);
- if (cmp_result.getRelation() != NameComparisonResult::EQUAL &&
- cmp_result.getRelation() != NameComparisonResult::SUBDOMAIN) {
- isc_throw(InvalidParameter, "findNSEC3 attempt for out-of-zone name: "
- << name << ", zone: " << impl_->origin_ << "/"
- << impl_->zone_class_);
- }
-
- const NSEC3Hash& nsec3hash = *impl_->zone_data_->nsec3_data_->hash_;
- const unsigned int olabels = impl_->origin_.getLabelCount();
- const unsigned int qlabels = name.getLabelCount();
- ConstRBNodeRRsetPtr covering_proof;
-
-
-
- for (unsigned int labels = qlabels; labels >= olabels; --labels) {
- const string hlabel = nsec3hash.calculate(
- labels == qlabels ? name : name.split(qlabels - labels, labels));
- NSEC3Map::const_iterator found = map.lower_bound(hlabel);
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_FINDNSEC3_TRYHASH).
- arg(name).arg(labels).arg(hlabel);
-
-
-
- if (found == map.end() || found->first != hlabel) {
-
-
-
-
- if (found == map.end() || found == map.begin()) {
- covering_proof = map.rbegin()->second;
- } else {
-
-
-
- covering_proof = (--found)->second;
- }
- if (!recursive) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC,
- DATASRC_MEM_FINDNSEC3_COVER).
- arg(name).arg(*covering_proof);
- return (FindNSEC3Result(false, labels, covering_proof,
- ConstRRsetPtr()));
- }
- } else {
- LOG_DEBUG(logger, DBG_TRACE_BASIC,
- DATASRC_MEM_FINDNSEC3_MATCH).arg(name).arg(labels).
- arg(*found->second);
- return (FindNSEC3Result(true, labels, found->second,
- covering_proof));
- }
- }
- isc_throw(DataSourceError, "recursive findNSEC3 mode didn't stop, likely "
- "a broken NSEC3 zone: " << impl_->origin_ << "/"
- << impl_->zone_class_);
- }
- result::Result
- InMemoryZoneFinder::add(const ConstRRsetPtr& rrset) {
- return (impl_->add(rrset, *impl_->zone_data_, NULL));
- }
- namespace {
- const Name
- getAdditionalName(RRType rrtype, const rdata::Rdata& rdata) {
- if (rrtype == RRType::NS()) {
- const generic::NS& ns = dynamic_cast<const generic::NS&>(rdata);
- return (ns.getNSName());
- } else {
-
- assert(rrtype == RRType::MX());
- const generic::MX& mx = dynamic_cast<const generic::MX&>(rdata);
- return (mx.getMXName());
- }
- }
- bool
- checkZoneCut(const DomainNode& node, pair<bool, bool>* arg) {
-
-
- if (arg->first) {
- return (false);
- }
-
-
- if (node.getData()->find(RRType::DNAME()) != node.getData()->end()) {
- return (true);
- } else if (node.getData()->find(RRType::NS()) != node.getData()->end()) {
- arg->first = true;
- arg->second = true;
- return (false);
- }
- return (false);
- }
- void
- addAdditional(RBNodeRRset* rrset, ZoneData* zone_data) {
- RdataIteratorPtr rdata_iterator = rrset->getRdataIterator();
- for (; !rdata_iterator->isLast(); rdata_iterator->next()) {
-
-
-
-
-
-
- RBTreeNodeChain<Domain> node_path;
- DomainNode* node = NULL;
-
-
-
- pair<bool, bool> callback_arg(false, false);
- const DomainTree::Result result =
- zone_data->domains_.find(
- getAdditionalName(rrset->getType(),
- rdata_iterator->getCurrent()),
- &node, node_path, checkZoneCut, &callback_arg);
- if (result == DomainTree::EXACTMATCH) {
- assert(node != NULL);
- if (callback_arg.second ||
- (node->getFlag(DomainNode::FLAG_CALLBACK) &&
- node->getData()->find(RRType::NS()) !=
- node->getData()->end())) {
-
- node->setFlag(domain_flag::GLUE);
- }
-
-
-
- rrset->addAdditionalNode(node);
- }
- }
- }
- }
- void
- InMemoryZoneFinder::load(const string& filename) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_LOAD).arg(getOrigin()).
- arg(filename);
-
-
-
- vector<RBNodeRRset*> need_additionals;
- scoped_ptr<ZoneData> tmp(new ZoneData(getOrigin()));
- masterLoad(filename.c_str(), getOrigin(), getClass(),
- boost::bind(&InMemoryZoneFinderImpl::addFromLoad, impl_,
- _1, tmp.get(), &need_additionals));
-
-
- for_each(need_additionals.begin(), need_additionals.end(),
- boost::bind(addAdditional, _1, tmp.get()));
-
- if (tmp->nsec3_data_) {
-
-
-
- assert(tmp->origin_data_ != NULL && !tmp->origin_data_->isEmpty());
- if (tmp->origin_data_->getData()->find(RRType::NSEC3PARAM()) ==
- tmp->origin_data_->getData()->end()) {
- LOG_WARN(logger, DATASRC_MEM_NO_NSEC3PARAM).
- arg(getOrigin()).arg(getClass());
- }
- }
-
- impl_->file_name_ = filename;
- tmp.swap(impl_->zone_data_);
-
- }
- void
- InMemoryZoneFinder::swap(InMemoryZoneFinder& zone_finder) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_SWAP).arg(getOrigin()).
- arg(zone_finder.getOrigin());
- std::swap(impl_, zone_finder.impl_);
- }
- const string
- InMemoryZoneFinder::getFileName() const {
- return (impl_->file_name_);
- }
- isc::dns::Name
- InMemoryZoneFinder::findPreviousName(const isc::dns::Name&) const {
- isc_throw(NotImplemented, "InMemory data source doesn't support DNSSEC "
- "yet, can't find previous name");
- }
- class InMemoryClient::InMemoryClientImpl {
- public:
- InMemoryClientImpl() : zone_count(0) {}
- unsigned int zone_count;
- ZoneTable zone_table;
- };
- InMemoryClient::InMemoryClient() : impl_(new InMemoryClientImpl)
- {}
- InMemoryClient::~InMemoryClient() {
- delete impl_;
- }
- unsigned int
- InMemoryClient::getZoneCount() const {
- return (impl_->zone_count);
- }
- result::Result
- InMemoryClient::addZone(ZoneFinderPtr zone_finder) {
- if (!zone_finder) {
- isc_throw(InvalidParameter,
- "Null pointer is passed to InMemoryClient::addZone()");
- }
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_ADD_ZONE).
- arg(zone_finder->getOrigin()).arg(zone_finder->getClass().toText());
- const result::Result result = impl_->zone_table.addZone(zone_finder);
- if (result == result::SUCCESS) {
- ++impl_->zone_count;
- }
- return (result);
- }
- InMemoryClient::FindResult
- InMemoryClient::findZone(const isc::dns::Name& name) const {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_FIND_ZONE).arg(name);
- ZoneTable::FindResult result(impl_->zone_table.findZone(name));
- return (FindResult(result.code, result.zone));
- }
- namespace {
- class MemoryIterator : public ZoneIterator {
- private:
- RBTreeNodeChain<Domain> chain_;
- Domain::const_iterator dom_iterator_;
- const DomainTree& tree_;
- const DomainNode* node_;
-
- RdataIteratorPtr rdata_iterator_;
- bool separate_rrs_;
- bool ready_;
- public:
- MemoryIterator(const DomainTree& tree, const Name& origin, bool separate_rrs) :
- tree_(tree),
- separate_rrs_(separate_rrs),
- ready_(true)
- {
-
-
- DomainTree::Result result(tree_.find<void*>(origin, &node_, chain_,
- NULL, NULL));
-
- if (result != DomainTree::EXACTMATCH) {
- isc_throw(Unexpected,
- "In-memory zone corrupted, missing origin node");
- }
-
- if (node_ != NULL && node_->getData() != DomainPtr()) {
- dom_iterator_ = node_->getData()->begin();
- if (separate_rrs_ && dom_iterator_ != node_->getData()->end()) {
- rdata_iterator_ = dom_iterator_->second->getRdataIterator();
- }
- }
- }
- virtual ConstRRsetPtr getNextRRset() {
- if (!ready_) {
- isc_throw(Unexpected, "Iterating past the zone end");
- }
-
- while (node_ != NULL && (node_->getData() == DomainPtr() ||
- dom_iterator_ == node_->getData()->end())) {
- node_ = tree_.nextNode(chain_);
-
-
- if (node_ != NULL && node_->getData() != NULL) {
- dom_iterator_ = node_->getData()->begin();
-
- if (separate_rrs_) {
- rdata_iterator_ = dom_iterator_->second->getRdataIterator();
- }
- }
- }
- if (node_ == NULL) {
-
- ready_ = false;
- return (ConstRRsetPtr());
- }
- if (separate_rrs_) {
-
-
- RRsetPtr result(new RRset(dom_iterator_->second->getName(),
- dom_iterator_->second->getClass(),
- dom_iterator_->second->getType(),
- dom_iterator_->second->getTTL()));
- result->addRdata(rdata_iterator_->getCurrent());
- rdata_iterator_->next();
- if (rdata_iterator_->isLast()) {
-
- ++dom_iterator_;
-
-
- if (dom_iterator_ != node_->getData()->end()) {
- rdata_iterator_ = dom_iterator_->second->getRdataIterator();
- }
- }
- return (result);
- } else {
-
- ConstRRsetPtr result(dom_iterator_->second);
-
- ++dom_iterator_;
- return (result);
- }
- }
- virtual ConstRRsetPtr getSOA() const {
- isc_throw(NotImplemented, "Not imelemented");
- }
- };
- }
- ZoneIteratorPtr
- InMemoryClient::getIterator(const Name& name, bool separate_rrs) const {
- ZoneTable::FindResult result(impl_->zone_table.findZone(name));
- if (result.code != result::SUCCESS) {
- isc_throw(DataSourceError, "No such zone: " + name.toText());
- }
- const InMemoryZoneFinder*
- zone(dynamic_cast<const InMemoryZoneFinder*>(result.zone.get()));
- if (zone == NULL) {
-
- isc_throw(Unexpected, "The zone at " + name.toText() +
- " is not InMemoryZoneFinder");
- }
- return (ZoneIteratorPtr(new MemoryIterator(
- zone->impl_->zone_data_->domains_, name,
- separate_rrs)));
- }
- ZoneUpdaterPtr
- InMemoryClient::getUpdater(const isc::dns::Name&, bool, bool) const {
- isc_throw(isc::NotImplemented, "Update attempt on in memory data source");
- }
- pair<ZoneJournalReader::Result, ZoneJournalReaderPtr>
- InMemoryClient::getJournalReader(const isc::dns::Name&, uint32_t,
- uint32_t) const
- {
- isc_throw(isc::NotImplemented, "Journaling isn't supported for "
- "in memory data source");
- }
- }
- }
|