12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313 |
- #include <algorithm>
- #include <map>
- #include <utility>
- #include <cctype>
- #include <cassert>
- #include <boost/shared_ptr.hpp>
- #include <boost/scoped_ptr.hpp>
- #include <boost/bind.hpp>
- #include <boost/foreach.hpp>
- #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/logger.h>
- #include <datasrc/iterator.h>
- #include <datasrc/data_source.h>
- #include <datasrc/factory.h>
- #include <cc/data.h>
- using namespace std;
- using namespace isc::dns;
- using namespace isc::dns::rdata;
- using namespace isc::data;
- using boost::scoped_ptr;
- namespace isc {
- namespace datasrc {
- namespace {
- typedef map<RRType, ConstRRsetPtr> Domain;
- typedef Domain::value_type DomainPair;
- typedef boost::shared_ptr<Domain> DomainPtr;
- typedef RBTree<Domain> DomainTree;
- typedef RBNode<Domain> DomainNode;
- typedef map<string, ConstRRsetPtr> 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_;
- };
- }
- struct InMemoryZoneFinder::InMemoryZoneFinderImpl {
-
- InMemoryZoneFinderImpl(const RRClass& zone_class, const Name& origin) :
- zone_class_(zone_class), origin_(origin),
- zone_data_(new ZoneData(origin_))
- {}
- static const DomainNode::Flags DOMAINFLAG_WILD = DomainNode::FLAG_USER1;
-
- 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(DOMAINFLAG_WILD);
-
-
-
-
- result = domains.insert(wname, &node);
- assert(result == DomainTree::SUCCESS ||
- result == DomainTree::ALREADYEXISTS);
- }
- }
- }
-
- void contextCheck(const ConstRRsetPtr& rrset,
- const DomainPtr& domain) const {
-
-
- if (rrset->getType() == RRType::CNAME()) {
-
-
-
- if (!domain->empty()) {
- 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 (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<RRset>(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, rrset));
- return (result::SUCCESS);
- }
-
-
- result::Result add(const ConstRRsetPtr& rrset, ZoneData& zone_data) {
-
-
- addValidation(rrset);
-
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ADD_RRSET).
- arg(rrset->getName()).arg(rrset->getType()).arg(origin_);
- 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 (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) {
- switch (add(set, *zone_data)) {
- 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_;
- ConstRRsetPtr 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 ConstRRsetPtr prepareRRset(const Name& name, const ConstRRsetPtr&
- rrset, bool rename)
- {
- if (rename) {
- LOG_DEBUG(logger, DBG_TRACE_DETAILED, DATASRC_MEM_RENAME).
- arg(rrset->getName()).arg(name);
-
- RRsetPtr result(new RRset(name, rrset->getClass(),
- rrset->getType(), rrset->getTTL()));
- for (RdataIteratorPtr i(rrset->getRdataIterator()); !i->isLast();
- i->next()) {
- result->addRdata(i->getCurrent());
- }
- return (result);
- } else {
- return (rrset);
- }
- }
-
-
- FindResult createFindResult(Result code, ConstRRsetPtr rrset,
- bool wild) 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 (FindResult(code, rrset, flags));
- }
-
- FindResult 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 (FindResult(DNAME, prepareRRset(name, state.rrset_,
- false)));
- }
- if (state.zonecut_node_ != NULL) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_DELEG_FOUND).
- arg(state.rrset_->getName());
- return (FindResult(DELEGATION,
- prepareRRset(name, state.rrset_,
- false)));
- }
-
-
-
- if (node_path.getLastComparisonResult().getRelation() ==
- NameComparisonResult::SUPERDOMAIN) {
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_SUPER_STOP).
- arg(node_path.getAbsoluteName()).arg(name);
- return (createFindResult(NXRRSET, ConstRRsetPtr(), false));
- }
-
- if (node->getFlag(DOMAINFLAG_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, ConstRRsetPtr(),
- 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, ConstRRsetPtr(), 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, ConstRRsetPtr(), 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 (FindResult(DELEGATION,
- prepareRRset(name, found->second, rename)));
- }
- }
-
- if (target != NULL && !node->getData()->empty()) {
-
- for (found = node->getData()->begin();
- found != node->getData()->end(); ++found)
- {
- target->push_back(prepareRRset(name, found->second, rename));
- }
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_ANY_SUCCESS).
- arg(name);
- return (createFindResult(SUCCESS, ConstRRsetPtr(), rename));
- }
- 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), 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), rename));
- }
- }
-
- LOG_DEBUG(logger, DBG_TRACE_DATA, DATASRC_MEM_NXRRSET).arg(type).
- arg(name);
- return (createFindResult(NXRRSET, ConstRRsetPtr(), 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_);
- }
- ZoneFinder::FindResult
- InMemoryZoneFinder::find(const Name& name, const RRType& type,
- const FindOptions options)
- {
- return (impl_->find(name, type, NULL, options));
- }
- ZoneFinder::FindResult
- InMemoryZoneFinder::findAll(const Name& name,
- std::vector<ConstRRsetPtr>& target,
- const FindOptions options)
- {
- return (impl_->find(name, RRType::ANY(), &target, options));
- }
- ZoneFinder::FindNSEC3Result
- InMemoryZoneFinder::findNSEC3(const Name&, bool) {
- isc_throw(NotImplemented, "findNSEC3 is not yet implemented for in memory "
- "data source");
- }
- ZoneFinder::FindNSEC3Result
- InMemoryZoneFinder::findNSEC3Tmp(const Name& name, bool recursive) {
- if (!impl_->zone_data_->nsec3_data_) {
- isc_throw(Unexpected, "findNSEC3 is called for non NSEC3 zone");
- }
- if (recursive) {
- isc_throw(Unexpected, "recursive mode isn't expected in tests");
- }
-
-
- string hname_text;
- if (name == Name("example.org")) {
- hname_text = "0P9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
- } else if (name == Name("www.example.org")) {
- hname_text = "2S9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
- } else if (name == Name("xxx.example.org")) {
- hname_text = "Q09MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
- } else if (name == Name("yyy.example.org")) {
- hname_text = "0A9MHAVEQVM6T7VBL5LOP2U3T2RP3TOM";
- } else {
- isc_throw(Unexpected, "unexpected name for NSEC3 test: " << name);
- }
-
- NSEC3Map::const_iterator found =
- impl_->zone_data_->nsec3_data_->map_.lower_bound(hname_text);
- if (found != impl_->zone_data_->nsec3_data_->map_.end() &&
- found->first == hname_text) {
-
- return (FindNSEC3Result(true, 2, found->second, ConstRRsetPtr()));
- } else if (found == impl_->zone_data_->nsec3_data_->map_.end() ||
- found == impl_->zone_data_->nsec3_data_->map_.begin()) {
-
-
- return (FindNSEC3Result(false, 2,
- impl_->zone_data_->nsec3_data_->map_.
- rbegin()->second, ConstRRsetPtr()));
- } else {
-
-
- return (FindNSEC3Result(false, 2, (--found)->second, ConstRRsetPtr()));
- }
-
- isc_throw(Unexpected, "Impossible NSEC3 search result for " << name);
- }
- result::Result
- InMemoryZoneFinder::add(const ConstRRsetPtr& rrset) {
- return (impl_->add(rrset, *impl_->zone_data_));
- }
- void
- InMemoryZoneFinder::load(const string& filename) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEM_LOAD).arg(getOrigin()).
- arg(filename);
-
- scoped_ptr<ZoneData> tmp(new ZoneData(getOrigin()));
- masterLoad(filename.c_str(), getOrigin(), getClass(),
- boost::bind(&InMemoryZoneFinderImpl::addFromLoad, impl_,
- _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");
- }
- namespace {
- void
- addError(ElementPtr errors, const std::string& error) {
- if (errors != ElementPtr() && errors->getType() == Element::list) {
- errors->add(Element::create(error));
- }
- }
- bool
- checkConfigElementString(ConstElementPtr config, const std::string& name,
- ElementPtr errors)
- {
- if (!config->contains(name)) {
- addError(errors,
- "Config for memory backend does not contain a '"
- +name+
- "' value");
- return false;
- } else if (!config->get(name) ||
- config->get(name)->getType() != Element::string) {
- addError(errors, "value of " + name +
- " in memory backend config is not a string");
- return false;
- } else {
- return true;
- }
- }
- bool
- checkZoneConfig(ConstElementPtr config, ElementPtr errors) {
- bool result = true;
- if (!config || config->getType() != Element::map) {
- addError(errors, "Elements in memory backend's zone list must be maps");
- result = false;
- } else {
- if (!checkConfigElementString(config, "origin", errors)) {
- result = false;
- }
- if (!checkConfigElementString(config, "file", errors)) {
- result = false;
- }
-
-
- }
- return result;
- }
- bool
- checkConfig(ConstElementPtr config, ElementPtr errors) {
-
- bool result = true;
- if (!config || config->getType() != Element::map) {
- addError(errors, "Base config for memory backend must be a map");
- result = false;
- } else {
- if (!checkConfigElementString(config, "type", errors)) {
- result = false;
- } else {
- if (config->get("type")->stringValue() != "memory") {
- addError(errors,
- "Config for memory backend is not of type \"memory\"");
- result = false;
- }
- }
- if (!checkConfigElementString(config, "class", errors)) {
- result = false;
- } else {
- try {
- RRClass rrc(config->get("class")->stringValue());
- } catch (const isc::Exception& rrce) {
- addError(errors,
- "Error parsing class config for memory backend: " +
- std::string(rrce.what()));
- result = false;
- }
- }
- if (!config->contains("zones")) {
- addError(errors, "No 'zones' element in memory backend config");
- result = false;
- } else if (!config->get("zones") ||
- config->get("zones")->getType() != Element::list) {
- addError(errors, "'zones' element in memory backend config is not a list");
- result = false;
- } else {
- BOOST_FOREACH(ConstElementPtr zone_config,
- config->get("zones")->listValue()) {
- if (!checkZoneConfig(zone_config, errors)) {
- result = false;
- }
- }
- }
- }
- return (result);
- return true;
- }
- }
- DataSourceClient *
- createInstance(isc::data::ConstElementPtr config, std::string& error) {
- ElementPtr errors(Element::createList());
- if (!checkConfig(config, errors)) {
- error = "Configuration error: " + errors->str();
- return (NULL);
- }
- try {
- return (new InMemoryClient());
- } catch (const std::exception& exc) {
- error = std::string("Error creating memory datasource: ") + exc.what();
- return (NULL);
- } catch (...) {
- error = std::string("Error creating memory datasource, "
- "unknown exception");
- return (NULL);
- }
- }
- void destroyInstance(DataSourceClient* instance) {
- delete instance;
- }
- }
- }
|