123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #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)
- {}
-
- RRClass zone_class_;
- Name origin_;
- string file_name_;
-
-
- typedef map<RRType, ConstRRsetPtr> Domain;
- typedef Domain::value_type DomainPair;
- typedef boost::shared_ptr<Domain> DomainPtr;
-
- typedef RBTree<Domain> DomainTree;
- typedef RBNode<Domain> DomainNode;
-
- DomainTree domains_;
-
-
- result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
-
- if (!rrset) {
- isc_throw(NullRRset, "The rrset provided is NULL");
- }
- if (rrset->getType() == RRType::CNAME() &&
- rrset->getRdataCount() > 1) {
-
-
-
- isc_throw(AddError, "multiple RRs of singleton type for "
- << rrset->getName());
- }
- Name name(rrset->getName());
- NameComparisonResult compare(origin_.compare(name));
- if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
- compare.getRelation() != NameComparisonResult::EQUAL)
- {
- isc_throw(OutOfZone, "The name " << name <<
- " is not contained in zone " << origin_);
- }
-
- DomainNode* node;
- switch (domains->insert(name, &node)) {
-
- case DomainTree::SUCCESS:
- case DomainTree::ALREADYEXISTS:
- break;
-
- default:
- assert(0);
- }
- assert(node != NULL);
-
- DomainPtr domain;
-
- if (node->isEmpty()) {
- domain.reset(new Domain);
- node->setData(domain);
- } else {
- domain = node->getData();
- }
-
-
-
-
-
-
- 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 (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
-
-
-
-
- if (rrset->getType() == RRType::NS() &&
- rrset->getName() != origin_) {
- node->enableCallback();
- }
- 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),
- options_(options)
- {}
- const DomainNode* zonecut_node_;
- ConstRRsetPtr rrset_;
- const FindOptions options_;
- };
-
-
- static bool zonecutCallback(const DomainNode& node, FindState* state) {
-
-
- if (state->zonecut_node_ != NULL) {
- return (false);
- }
- const Domain::const_iterator found(node.getData()->find(RRType::NS()));
- if (found != node.getData()->end()) {
-
-
-
-
- state->zonecut_node_ = &node;
- state->rrset_ = found->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, zonecutCallback, &state)) {
- case DomainTree::PARTIALMATCH:
- 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);
- assert(!node->isEmpty());
- Domain::const_iterator found;
-
-
- if (node->isCallbackEnabled()) {
- 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));
- }
- }
- }
|