123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- #include <map>
- #include <cassert>
- #include <boost/shared_ptr.hpp>
- #include <boost/bind.hpp>
- #include <dns/name.h>
- #include <dns/rrclass.h>
- #include <dns/rrsetlist.h>
- #include <dns/masterload.h>
- #include <datasrc/memory_datasrc.h>
- #include <datasrc/rbtree.h>
- using namespace std;
- using namespace isc::dns;
- namespace isc {
- namespace datasrc {
- struct MemoryZone::MemoryZoneImpl {
-
- MemoryZoneImpl(const RRClass& zone_class, const Name& origin) :
- zone_class_(zone_class), origin_(origin), origin_data_(NULL)
- {
-
- domains_.insert(origin, &origin_data_);
- DomainPtr origin_domain(new Domain);
- origin_data_->setData(origin_domain);
- }
-
-
- typedef map<RRType, ConstRRsetPtr> Domain;
- typedef Domain::value_type DomainPair;
- typedef boost::shared_ptr<Domain> DomainPtr;
-
- typedef RBTree<Domain, true> DomainTree;
- typedef RBNode<Domain> DomainNode;
- static const DomainNode::Flags DOMAINFLAG_WILD = DomainNode::FLAG_USER1;
-
- RRClass zone_class_;
- Name origin_;
- DomainNode* origin_data_;
- string file_name_;
-
- DomainTree domains_;
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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()) {
- DomainNode* node;
- DomainTree::Result result;
-
-
-
-
- result = domains.insert(wname.split(1), &node);
- assert(result == DomainTree::SUCCESS ||
- result == DomainTree::ALREADYEXISTS);
-
-
- result = domains.insert(wname, &node);
- assert(result == DomainTree::SUCCESS ||
- result == DomainTree::ALREADYEXISTS);
- node->setFlag(DOMAINFLAG_WILD);
- }
- }
- }
-
- void contextCheck(const ConstRRsetPtr& rrset,
- const DomainPtr& domain) const {
-
-
- if (rrset->getType() == RRType::CNAME()) {
-
-
-
- if (!domain->empty()) {
- isc_throw(AddError, "CNAME can't be added with other data for "
- << rrset->getName());
- }
- } else if (domain->find(RRType::CNAME()) != domain->end()) {
- 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())))
- {
- 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->getType() == RRType::CNAME() ||
- rrset->getType() == RRType::DNAME()) &&
- rrset->getRdataCount() > 1)
- {
-
-
-
- isc_throw(AddError, "multiple RRs of singleton type for "
- << rrset->getName());
- }
- NameComparisonResult compare(origin_.compare(rrset->getName()));
- if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
- compare.getRelation() != NameComparisonResult::EQUAL)
- {
- isc_throw(OutOfZone, "The name " << rrset->getName() <<
- " is not contained in zone " << origin_);
- }
-
-
-
-
-
-
-
-
- if (rrset->getName().isWildcard()) {
- if (rrset->getType() == RRType::NS()) {
- isc_throw(AddError, "Invalid NS owner name (wildcard): " <<
- rrset->getName());
- }
- if (rrset->getType() == RRType::DNAME()) {
- isc_throw(AddError, "Invalid DNAME owner name (wildcard): " <<
- rrset->getName());
- }
- }
- }
-
-
- result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
-
- addValidation(rrset);
-
-
-
-
- addWildcards(*domains, rrset->getName());
-
- DomainNode* node;
- DomainTree::Result result = 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);
- }
- return (result::SUCCESS);
- } else {
-
- return (result::EXIST);
- }
- }
-
- void addFromLoad(const ConstRRsetPtr& set, DomainTree* domains) {
- switch (add(set, domains)) {
- case result::EXIST:
- 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()) {
- 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);
- }
-
-
-
-
-
-
- state->zonecut_node_ = &node;
- state->rrset_ = foundNS->second;
-
-
- return ((state->options_ & FIND_GLUE_OK) == 0);
- }
-
-
- assert(0);
-
-
-
- return (false);
- }
-
- FindResult find(const Name& name, RRType type,
- RRsetList* target, const FindOptions options) const
- {
-
- DomainNode* node(NULL);
- FindState state(options);
- switch (domains_.find(name, &node, cutCallback, &state)) {
- case DomainTree::PARTIALMATCH:
-
- if (state.dname_node_ != NULL) {
-
-
- return (FindResult(DNAME, state.rrset_));
- }
- if (state.zonecut_node_ != NULL) {
- return (FindResult(DELEGATION, state.rrset_));
- }
-
-
-
-
- case DomainTree::NOTFOUND:
- return (FindResult(NXDOMAIN, ConstRRsetPtr()));
- case DomainTree::EXACTMATCH:
- break;
- default:
- assert(0);
- }
- assert(node);
-
-
- if (node->isEmpty()) {
- return (FindResult(NXRRSET, ConstRRsetPtr()));
- }
- Domain::const_iterator found;
-
-
- if (node->getFlag(DomainNode::FLAG_CALLBACK) && node != origin_data_) {
- found = node->getData()->find(RRType::NS());
- if (found != node->getData()->end()) {
- return (FindResult(DELEGATION, found->second));
- }
- }
-
- if (target != NULL && !node->getData()->empty()) {
-
- for (found = node->getData()->begin();
- found != node->getData()->end(); found++)
- {
- target->addRRset(
- boost::const_pointer_cast<RRset>(found->second));
- }
- return (FindResult(SUCCESS, ConstRRsetPtr()));
- }
- found = node->getData()->find(type);
- if (found != node->getData()->end()) {
-
- return (FindResult(SUCCESS, found->second));
- } else {
-
- found = node->getData()->find(RRType::CNAME());
- if (found != node->getData()->end()) {
- return (FindResult(CNAME, found->second));
- }
- }
-
- return (FindResult(NXRRSET, ConstRRsetPtr()));
- }
- };
- MemoryZone::MemoryZone(const RRClass& zone_class, const Name& origin) :
- impl_(new MemoryZoneImpl(zone_class, origin))
- {
- }
- MemoryZone::~MemoryZone() {
- delete impl_;
- }
- const Name&
- MemoryZone::getOrigin() const {
- return (impl_->origin_);
- }
- const RRClass&
- MemoryZone::getClass() const {
- return (impl_->zone_class_);
- }
- Zone::FindResult
- MemoryZone::find(const Name& name, const RRType& type,
- RRsetList* target, const FindOptions options) const
- {
- return (impl_->find(name, type, target, options));
- }
- result::Result
- MemoryZone::add(const ConstRRsetPtr& rrset) {
- return (impl_->add(rrset, &impl_->domains_));
- }
- void
- MemoryZone::load(const string& filename) {
-
- MemoryZoneImpl::DomainTree tmp;
- masterLoad(filename.c_str(), getOrigin(), getClass(),
- boost::bind(&MemoryZoneImpl::addFromLoad, impl_, _1, &tmp));
-
- impl_->file_name_ = filename;
- tmp.swap(impl_->domains_);
-
- }
- void
- MemoryZone::swap(MemoryZone& zone) {
- std::swap(impl_, zone.impl_);
- }
- const string
- MemoryZone::getFileName() const {
- return (impl_->file_name_);
- }
- class MemoryDataSrc::MemoryDataSrcImpl {
- public:
- MemoryDataSrcImpl() : zone_count(0) {}
- unsigned int zone_count;
- ZoneTable zone_table;
- };
- MemoryDataSrc::MemoryDataSrc() : impl_(new MemoryDataSrcImpl)
- {}
- MemoryDataSrc::~MemoryDataSrc() {
- delete impl_;
- }
- unsigned int
- MemoryDataSrc::getZoneCount() const {
- return (impl_->zone_count);
- }
- result::Result
- MemoryDataSrc::addZone(ZonePtr zone) {
- if (!zone) {
- isc_throw(InvalidParameter,
- "Null pointer is passed to MemoryDataSrc::addZone()");
- }
- const result::Result result = impl_->zone_table.addZone(zone);
- if (result == result::SUCCESS) {
- ++impl_->zone_count;
- }
- return (result);
- }
- MemoryDataSrc::FindResult
- MemoryDataSrc::findZone(const isc::dns::Name& name) const {
- return (FindResult(impl_->zone_table.findZone(name).code,
- impl_->zone_table.findZone(name).zone));
- }
- }
- }
|