memory_datasrc.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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->getRdataCount() > 1) {
  70. // XXX: this is not only for CNAME. We should generalize this
  71. // code for all other "singleton RR types" (such as SOA) in a
  72. // separate task.
  73. isc_throw(AddError, "multiple RRs of singleton type for "
  74. << rrset->getName());
  75. }
  76. Name name(rrset->getName());
  77. NameComparisonResult compare(origin_.compare(name));
  78. if (compare.getRelation() != NameComparisonResult::SUPERDOMAIN &&
  79. compare.getRelation() != NameComparisonResult::EQUAL)
  80. {
  81. isc_throw(OutOfZone, "The name " << name <<
  82. " is not contained in zone " << origin_);
  83. }
  84. // Get the node
  85. DomainNode* node;
  86. switch (domains->insert(name, &node)) {
  87. // Just check it returns reasonable results
  88. case DomainTree::SUCCESS:
  89. case DomainTree::ALREADYEXISTS:
  90. break;
  91. // Something odd got out
  92. default:
  93. assert(0);
  94. }
  95. assert(node != NULL);
  96. // Now get the domain
  97. DomainPtr domain;
  98. // It didn't exist yet, create it
  99. if (node->isEmpty()) {
  100. domain.reset(new Domain);
  101. node->setData(domain);
  102. } else { // Get existing one
  103. domain = node->getData();
  104. }
  105. // Ensure CNAME and other type of RR don't coexist for the same
  106. // owner name.
  107. // Note: when the check fails and the exception is thrown, it may
  108. // break strong exception guarantee. At the moment we prefer
  109. // code simplicity and don't bother to introduce complicated
  110. // recovery code.
  111. if (rrset->getType() == RRType::CNAME()) {
  112. // XXX: this check will become incorrect when we support DNSSEC
  113. // (depending on how we support DNSSEC). We should revisit it
  114. // at that point.
  115. if (!domain->empty()) {
  116. isc_throw(AddError, "CNAME can't be added with other data for "
  117. << rrset->getName());
  118. }
  119. } else if (domain->find(RRType::CNAME()) != domain->end()) {
  120. isc_throw(AddError, "CNAME and " << rrset->getType() <<
  121. " can't coexist for " << rrset->getName());
  122. }
  123. // Try inserting the rrset there
  124. if (domain->insert(DomainPair(rrset->getType(), rrset)).second) {
  125. // Ok, we just put it in
  126. // If this RRset creates a zone cut at this node, mark the node
  127. // indicating the need for callback in find().
  128. // TBD: handle DNAME, too
  129. if (rrset->getType() == RRType::NS() &&
  130. rrset->getName() != origin_) {
  131. node->enableCallback();
  132. }
  133. return (result::SUCCESS);
  134. } else {
  135. // The RRSet of given type was already there
  136. return (result::EXIST);
  137. }
  138. }
  139. /*
  140. * Same as above, but it checks the return value and if it already exists,
  141. * it throws.
  142. */
  143. void addFromLoad(const ConstRRsetPtr& set, DomainTree* domains) {
  144. switch (add(set, domains)) {
  145. case result::EXIST:
  146. isc_throw(dns::MasterLoadError, "Duplicate rrset: " <<
  147. set->toText());
  148. case result::SUCCESS:
  149. return;
  150. default:
  151. assert(0);
  152. }
  153. }
  154. // Maintain intermediate data specific to the search context used in
  155. /// \c find().
  156. ///
  157. /// It will be passed to \c zonecutCallback() and record a possible
  158. /// zone cut node and related RRset (normally NS or DNAME).
  159. struct FindState {
  160. FindState(FindOptions options) : zonecut_node_(NULL),
  161. options_(options)
  162. {}
  163. const DomainNode* zonecut_node_;
  164. ConstRRsetPtr rrset_;
  165. const FindOptions options_;
  166. };
  167. // A callback called from possible zone cut nodes. This will be passed
  168. // from the \c find() method to \c RBTree::find().
  169. static bool zonecutCallback(const DomainNode& node, FindState* state) {
  170. // We perform callback check only for the highest zone cut in the
  171. // rare case of nested zone cuts.
  172. if (state->zonecut_node_ != NULL) {
  173. return (false);
  174. }
  175. const Domain::const_iterator found(node.getData()->find(RRType::NS()));
  176. if (found != node.getData()->end()) {
  177. // BIND 9 checks if this node is not the origin. But it cannot
  178. // be the origin because we don't enable the callback at the
  179. // origin node (see MemoryZoneImpl::add()). Or should we do a
  180. // double check for it?
  181. state->zonecut_node_ = &node;
  182. state->rrset_ = found->second;
  183. // Unless glue is allowed the search stops here, so we return
  184. // false; otherwise return true to continue the search.
  185. return ((state->options_ & FIND_GLUE_OK) == 0);
  186. }
  187. // This case should not happen because we enable callback only
  188. // when we add an RR searched for above.
  189. assert(0);
  190. // This is here to avoid warning (therefore compilation error)
  191. // in case assert is turned off. Otherwise we could get "Control
  192. // reached end of non-void function".
  193. return (false);
  194. }
  195. // Implementation of MemoryZone::find
  196. FindResult find(const Name& name, RRType type,
  197. RRsetList* target, const FindOptions options) const
  198. {
  199. // Get the node
  200. DomainNode* node(NULL);
  201. FindState state(options);
  202. switch (domains_.find(name, &node, zonecutCallback, &state)) {
  203. case DomainTree::PARTIALMATCH:
  204. if (state.zonecut_node_ != NULL) {
  205. return (FindResult(DELEGATION, state.rrset_));
  206. }
  207. // TODO: we should also cover empty non-terminal cases, which
  208. // will require non trivial code and is deferred for later
  209. // development. For now, we regard any partial match that
  210. // didn't hit a zone cut as "not found".
  211. case DomainTree::NOTFOUND:
  212. return (FindResult(NXDOMAIN, ConstRRsetPtr()));
  213. case DomainTree::EXACTMATCH: // This one is OK, handle it
  214. break;
  215. default:
  216. assert(0);
  217. }
  218. assert(node);
  219. assert(!node->isEmpty());
  220. Domain::const_iterator found;
  221. // If the node callback is enabled, this may be a zone cut. If it
  222. // has a NS RR, we should return a delegation.
  223. if (node->isCallbackEnabled()) {
  224. found = node->getData()->find(RRType::NS());
  225. if (found != node->getData()->end()) {
  226. return (FindResult(DELEGATION, found->second));
  227. }
  228. }
  229. // handle type any query
  230. if (target != NULL && !node->getData()->empty()) {
  231. // Empty domain will be handled as NXRRSET by normal processing
  232. for (found = node->getData()->begin();
  233. found != node->getData()->end(); found++)
  234. {
  235. target->addRRset(
  236. boost::const_pointer_cast<RRset>(found->second));
  237. }
  238. return (FindResult(SUCCESS, ConstRRsetPtr()));
  239. }
  240. found = node->getData()->find(type);
  241. if (found != node->getData()->end()) {
  242. // Good, it is here
  243. return (FindResult(SUCCESS, found->second));
  244. } else {
  245. // Next, try CNAME.
  246. found = node->getData()->find(RRType::CNAME());
  247. if (found != node->getData()->end()) {
  248. return (FindResult(CNAME, found->second));
  249. }
  250. }
  251. // No exact match or CNAME. Return NXRRSET.
  252. return (FindResult(NXRRSET, ConstRRsetPtr()));
  253. }
  254. };
  255. MemoryZone::MemoryZone(const RRClass& zone_class, const Name& origin) :
  256. impl_(new MemoryZoneImpl(zone_class, origin))
  257. {
  258. }
  259. MemoryZone::~MemoryZone() {
  260. delete impl_;
  261. }
  262. const Name&
  263. MemoryZone::getOrigin() const {
  264. return (impl_->origin_);
  265. }
  266. const RRClass&
  267. MemoryZone::getClass() const {
  268. return (impl_->zone_class_);
  269. }
  270. Zone::FindResult
  271. MemoryZone::find(const Name& name, const RRType& type,
  272. RRsetList* target, const FindOptions options) const
  273. {
  274. return (impl_->find(name, type, target, options));
  275. }
  276. result::Result
  277. MemoryZone::add(const ConstRRsetPtr& rrset) {
  278. return (impl_->add(rrset, &impl_->domains_));
  279. }
  280. void
  281. MemoryZone::load(const string& filename) {
  282. // Load it into a temporary tree
  283. MemoryZoneImpl::DomainTree tmp;
  284. masterLoad(filename.c_str(), getOrigin(), getClass(),
  285. boost::bind(&MemoryZoneImpl::addFromLoad, impl_, _1, &tmp));
  286. // If it went well, put it inside
  287. impl_->file_name_ = filename;
  288. tmp.swap(impl_->domains_);
  289. // And let the old data die with tmp
  290. }
  291. void
  292. MemoryZone::swap(MemoryZone& zone) {
  293. std::swap(impl_, zone.impl_);
  294. }
  295. const string
  296. MemoryZone::getFileName() const {
  297. return (impl_->file_name_);
  298. }
  299. /// Implementation details for \c MemoryDataSrc hidden from the public
  300. /// interface.
  301. ///
  302. /// For now, \c MemoryDataSrc only contains a \c ZoneTable object, which
  303. /// consists of (pointers to) \c MemoryZone objects, we may add more
  304. /// member variables later for new features.
  305. class MemoryDataSrc::MemoryDataSrcImpl {
  306. public:
  307. MemoryDataSrcImpl() : zone_count(0) {}
  308. unsigned int zone_count;
  309. ZoneTable zone_table;
  310. };
  311. MemoryDataSrc::MemoryDataSrc() : impl_(new MemoryDataSrcImpl)
  312. {}
  313. MemoryDataSrc::~MemoryDataSrc() {
  314. delete impl_;
  315. }
  316. unsigned int
  317. MemoryDataSrc::getZoneCount() const {
  318. return (impl_->zone_count);
  319. }
  320. result::Result
  321. MemoryDataSrc::addZone(ZonePtr zone) {
  322. if (!zone) {
  323. isc_throw(InvalidParameter,
  324. "Null pointer is passed to MemoryDataSrc::addZone()");
  325. }
  326. const result::Result result = impl_->zone_table.addZone(zone);
  327. if (result == result::SUCCESS) {
  328. ++impl_->zone_count;
  329. }
  330. return (result);
  331. }
  332. MemoryDataSrc::FindResult
  333. MemoryDataSrc::findZone(const isc::dns::Name& name) const {
  334. return (FindResult(impl_->zone_table.findZone(name).code,
  335. impl_->zone_table.findZone(name).zone));
  336. }
  337. } // end of namespace datasrc
  338. } // end of namespace dns