memory_datasrc.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <map>
  15. #include <cassert>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/bind.hpp>
  18. #include <dns/name.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrsetlist.h>
  21. #include <dns/masterload.h>
  22. #include <datasrc/memory_datasrc.h>
  23. #include <datasrc/rbtree.h>
  24. using namespace std;
  25. using namespace isc::dns;
  26. namespace isc {
  27. namespace datasrc {
  28. // Private data and hidden methods of MemoryZone
  29. struct MemoryZone::MemoryZoneImpl {
  30. // Constructor
  31. MemoryZoneImpl(const RRClass& zone_class, const Name& origin) :
  32. zone_class_(zone_class), origin_(origin)
  33. {}
  34. // Information about the zone
  35. RRClass zone_class_;
  36. Name origin_;
  37. string file_name_;
  38. // Some type aliases
  39. /*
  40. * Each domain consists of some RRsets. They will be looked up by the
  41. * RRType.
  42. *
  43. * The use of map is questionable with regard to performance - there'll
  44. * be usually only few RRsets in the domain, so the log n benefit isn't
  45. * much and a vector/array might be faster due to its simplicity and
  46. * continuous memory location. But this is unlikely to be a performance
  47. * critical place and map has better interface for the lookups, so we use
  48. * that.
  49. */
  50. typedef map<RRType, ConstRRsetPtr> Domain;
  51. typedef Domain::value_type DomainPair;
  52. typedef boost::shared_ptr<Domain> DomainPtr;
  53. // The tree stores domains
  54. typedef RBTree<Domain> DomainTree;
  55. typedef RBNode<Domain> DomainNode;
  56. // The actual zone data
  57. DomainTree domains_;
  58. /*
  59. * Implementation of longer methods. We put them here, because the
  60. * access is without the impl_-> and it will get inlined anyway.
  61. */
  62. // Implementation of MemoryZone::add
  63. result::Result add(const ConstRRsetPtr& rrset, DomainTree* domains) {
  64. // Sanitize input
  65. if (!rrset) {
  66. isc_throw(NullRRset, "The rrset provided is NULL");
  67. }
  68. if ((rrset->getType() == RRType::CNAME() ||
  69. rrset->getType() == RRType::DNAME()) &&
  70. rrset->getRdataCount() > 1)
  71. {
  72. // XXX: this is not only for CNAME or DNAME. We should generalize
  73. // this code for all other "singleton RR types" (such as SOA) in a
  74. // separate task.
  75. isc_throw(AddError, "multiple RRs of singleton type for "
  76. << rrset->getName());
  77. }
  78. Name name(rrset->getName());
  79. NameComparisonResult compare(origin_.compare(name));
  80. if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
  81. compare.getRelation() != NameComparisonResult::EQUAL)
  82. {
  83. isc_throw(OutOfZone, "The name " << name <<
  84. " is not contained in zone " << origin_);
  85. }
  86. // Get the node
  87. DomainNode* node;
  88. switch (domains->insert(name, &node)) {
  89. // Just check it returns reasonable results
  90. case DomainTree::SUCCESS:
  91. case DomainTree::ALREADYEXISTS:
  92. break;
  93. // Something odd got out
  94. default:
  95. assert(0);
  96. }
  97. assert(node != NULL);
  98. // Now get the domain
  99. DomainPtr domain;
  100. // It didn't exist yet, create it
  101. if (node->isEmpty()) {
  102. domain.reset(new Domain);
  103. node->setData(domain);
  104. } else { // Get existing one
  105. domain = node->getData();
  106. }
  107. // Ensure CNAME and other type of RR don't coexist for the same
  108. // owner name.
  109. // Note: when the check fails and the exception is thrown, it may
  110. // break strong exception guarantee. At the moment we prefer
  111. // code simplicity and don't bother to introduce complicated
  112. // recovery code.
  113. if (rrset->getType() == RRType::CNAME()) {
  114. // XXX: this check will become incorrect when we support DNSSEC
  115. // (depending on how we support DNSSEC). We should revisit it
  116. // at that point.
  117. if (!domain->empty()) {
  118. isc_throw(AddError, "CNAME can't be added with other data for "
  119. << rrset->getName());
  120. }
  121. } else if (domain->find(RRType::CNAME()) != domain->end()) {
  122. isc_throw(AddError, "CNAME and " << rrset->getType() <<
  123. " can't coexist for " << rrset->getName());
  124. }
  125. /*
  126. * Similar with DNAME, but it must not coexist only with NS and only in
  127. * non-apex domains.
  128. */
  129. if (rrset->getName() != origin_ &&
  130. // Adding DNAME, NS already there
  131. ((rrset->getType() == RRType::DNAME() &&
  132. domain->find(RRType::NS()) != domain->end()) ||
  133. // Adding NS, DNAME already there
  134. (rrset->getType() == RRType::NS() &&
  135. domain->find(RRType::DNAME()) != domain->end())))
  136. {
  137. isc_throw(AddError, "DNAME can't coexist with NS in non-apex "
  138. "domain " << rrset->getName());
  139. }
  140. // Try inserting the rrset there
  141. if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
  142. // Ok, we just put it in
  143. // If this RRset creates a zone cut at this node, mark the node
  144. // indicating the need for callback in find().
  145. if (rrset->getType() == RRType::NS() &&
  146. rrset->getName() != origin_) {
  147. node->enableCallback();
  148. // If it is DNAME, we have a callback as well here
  149. } else if (rrset->getType() == RRType::DNAME()) {
  150. node->enableCallback();
  151. }
  152. return (result::SUCCESS);
  153. } else {
  154. // The RRSet of given type was already there
  155. return (result::EXIST);
  156. }
  157. }
  158. /*
  159. * Same as above, but it checks the return value and if it already exists,
  160. * it throws.
  161. */
  162. void addFromLoad(const ConstRRsetPtr& set, DomainTree* domains) {
  163. switch (add(set, domains)) {
  164. case result::EXIST:
  165. isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
  166. set->toText());
  167. case result::SUCCESS:
  168. return;
  169. default:
  170. assert(0);
  171. }
  172. }
  173. // Maintain intermediate data specific to the search context used in
  174. /// \c find().
  175. ///
  176. /// It will be passed to \c zonecutCallback() and record a possible
  177. /// zone cut node and related RRset (normally NS or DNAME).
  178. struct FindState {
  179. FindState(FindOptions options) :
  180. zonecut_node_(NULL),
  181. dname_node_(NULL),
  182. options_(options)
  183. {}
  184. const DomainNode* zonecut_node_;
  185. const DomainNode* dname_node_;
  186. ConstRRsetPtr rrset_;
  187. const FindOptions options_;
  188. };
  189. // A callback called from possible zone cut nodes and nodes with DNAME.
  190. // This will be passed from the \c find() method to \c RBTree::find().
  191. static bool cutCallback(const DomainNode& node, FindState* state) {
  192. // We perform callback check only for the highest zone cut in the
  193. // rare case of nested zone cuts.
  194. if (state->zonecut_node_ != NULL) {
  195. return (false);
  196. }
  197. // We need to look for DNAME first, there's allowed case where
  198. // DNAME and NS coexist in the apex. DNAME is the one to notice,
  199. // the NS is authoritative, not delegation
  200. const Domain::const_iterator foundDNAME(node.getData()->find(
  201. RRType::DNAME()));
  202. if (foundDNAME != node.getData()->end()) {
  203. state->dname_node_ = &node;
  204. state->rrset_ = foundDNAME->second;
  205. // No more processing below the DNAME
  206. return true;
  207. }
  208. // Look for NS
  209. const Domain::const_iterator foundNS(node.getData()->find(
  210. RRType::NS()));
  211. if (foundNS != node.getData()->end()) {
  212. // BIND 9 checks if this node is not the origin. But it cannot
  213. // be the origin because we don't enable the callback at the
  214. // origin node (see MemoryZoneImpl::add()). Or should we do a
  215. // double check for it?
  216. state->zonecut_node_ = &node;
  217. state->rrset_ = foundNS->second;
  218. // Unless glue is allowed the search stops here, so we return
  219. // false; otherwise return true to continue the search.
  220. return ((state->options_ & FIND_GLUE_OK) == 0);
  221. }
  222. // This case should not happen because we enable callback only
  223. // when we add an RR searched for above.
  224. assert(0);
  225. // This is here to avoid warning (therefore compilation error)
  226. // in case assert is turned off. Otherwise we could get "Control
  227. // reached end of non-void function".
  228. return (false);
  229. }
  230. // Implementation of MemoryZone::find
  231. FindResult find(const Name& name, RRType type,
  232. RRsetList* target, const FindOptions options) const
  233. {
  234. // Get the node
  235. DomainNode* node(NULL);
  236. FindState state(options);
  237. switch (domains_.find(name, &node, cutCallback, &state)) {
  238. case DomainTree::PARTIALMATCH:
  239. if (state.dname_node_ != NULL) {
  240. // We were traversing a DNAME node (and wanted to go
  241. // lower below it), so return the DNAME
  242. return (FindResult(DNAME, state.rrset_));
  243. }
  244. if (state.zonecut_node_ != NULL) {
  245. return (FindResult(DELEGATION, state.rrset_));
  246. }
  247. // TODO: we should also cover empty non-terminal cases, which
  248. // will require non trivial code and is deferred for later
  249. // development. For now, we regard any partial match that
  250. // didn't hit a zone cut as "not found".
  251. case DomainTree::NOTFOUND:
  252. return (FindResult(NXDOMAIN, ConstRRsetPtr()));
  253. case DomainTree::EXACTMATCH: // This one is OK, handle it
  254. break;
  255. default:
  256. assert(0);
  257. }
  258. assert(node);
  259. assert(!node->isEmpty());
  260. Domain::const_iterator found;
  261. // If the node callback is enabled, this may be a zone cut. If it
  262. // has a NS RR, we should return a delegation, but not in the apex.
  263. if (node->isCallbackEnabled() && node->getName() != origin_) {
  264. found = node->getData()->find(RRType::NS());
  265. if (found != node->getData()->end()) {
  266. return (FindResult(DELEGATION, found->second));
  267. }
  268. }
  269. // handle type any query
  270. if (target && !node->getData()->empty()) {
  271. // Empty domain will be handled as NXRRSET by normal processing
  272. for (found = node->getData()->begin();
  273. found != node->getData()->end(); found++)
  274. {
  275. target->addRRset(
  276. boost::const_pointer_cast<RRset>(found->second));
  277. }
  278. return (FindResult(SUCCESS, ConstRRsetPtr()));
  279. }
  280. found = node->getData()->find(type);
  281. if (found != node->getData()->end()) {
  282. // Good, it is here
  283. return (FindResult(SUCCESS, found->second));
  284. } else {
  285. // Next, try CNAME.
  286. found = node->getData()->find(RRType::CNAME());
  287. if (found != node->getData()->end()) {
  288. return (FindResult(CNAME, found->second));
  289. }
  290. }
  291. // No exact match or CNAME. Return NXRRSET.
  292. return (FindResult(NXRRSET, ConstRRsetPtr()));
  293. }
  294. };
  295. MemoryZone::MemoryZone(const RRClass& zone_class, const Name& origin) :
  296. impl_(new MemoryZoneImpl(zone_class, origin))
  297. {
  298. }
  299. MemoryZone::~MemoryZone() {
  300. delete impl_;
  301. }
  302. const Name&
  303. MemoryZone::getOrigin() const {
  304. return (impl_->origin_);
  305. }
  306. const RRClass&
  307. MemoryZone::getClass() const {
  308. return (impl_->zone_class_);
  309. }
  310. Zone::FindResult
  311. MemoryZone::find(const Name& name, const RRType& type,
  312. RRsetList* target, const FindOptions options) const
  313. {
  314. return (impl_->find(name, type, target, options));
  315. }
  316. result::Result
  317. MemoryZone::add(const ConstRRsetPtr& rrset) {
  318. return (impl_->add(rrset, &impl_->domains_));
  319. }
  320. void
  321. MemoryZone::load(const string& filename) {
  322. // Load it into a temporary tree
  323. MemoryZoneImpl::DomainTree tmp;
  324. masterLoad(filename.c_str(), getOrigin(), getClass(),
  325. boost::bind(&MemoryZoneImpl::addFromLoad, impl_, _1, &tmp));
  326. // If it went well, put it inside
  327. impl_->file_name_ = filename;
  328. tmp.swap(impl_->domains_);
  329. // And let the old data die with tmp
  330. }
  331. void
  332. MemoryZone::swap(MemoryZone& zone) {
  333. std::swap(impl_, zone.impl_);
  334. }
  335. const string
  336. MemoryZone::getFileName() const {
  337. return (impl_->file_name_);
  338. }
  339. /// Implementation details for \c MemoryDataSrc hidden from the public
  340. /// interface.
  341. ///
  342. /// For now, \c MemoryDataSrc only contains a \c ZoneTable object, which
  343. /// consists of (pointers to) \c MemoryZone objects, we may add more
  344. /// member variables later for new features.
  345. class MemoryDataSrc::MemoryDataSrcImpl {
  346. public:
  347. MemoryDataSrcImpl() : zone_count(0) {}
  348. unsigned int zone_count;
  349. ZoneTable zone_table;
  350. };
  351. MemoryDataSrc::MemoryDataSrc() : impl_(new MemoryDataSrcImpl)
  352. {}
  353. MemoryDataSrc::~MemoryDataSrc() {
  354. delete impl_;
  355. }
  356. unsigned int
  357. MemoryDataSrc::getZoneCount() const {
  358. return (impl_->zone_count);
  359. }
  360. result::Result
  361. MemoryDataSrc::addZone(ZonePtr zone) {
  362. if (!zone) {
  363. isc_throw(InvalidParameter,
  364. "Null pointer is passed to MemoryDataSrc::addZone()");
  365. }
  366. const result::Result result = impl_->zone_table.addZone(zone);
  367. if (result == result::SUCCESS) {
  368. ++impl_->zone_count;
  369. }
  370. return (result);
  371. }
  372. MemoryDataSrc::FindResult
  373. MemoryDataSrc::findZone(const isc::dns::Name& name) const {
  374. return (FindResult(impl_->zone_table.findZone(name).code,
  375. impl_->zone_table.findZone(name).zone));
  376. }
  377. } // end of namespace datasrc
  378. } // end of namespace dns