zone_data_loader.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright (C) 2012 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 <datasrc/master_loader_callbacks.h>
  15. #include <datasrc/memory/zone_data_loader.h>
  16. #include <datasrc/memory/zone_data_updater.h>
  17. #include <datasrc/memory/logger.h>
  18. #include <datasrc/memory/segment_object_holder.h>
  19. #include <datasrc/memory/util_internal.h>
  20. #include <datasrc/memory/rrset_collection.h>
  21. #include <dns/master_loader.h>
  22. #include <dns/rrcollator.h>
  23. #include <dns/rdataclass.h>
  24. #include <dns/rrset.h>
  25. #include <dns/zone_checker.h>
  26. #include <boost/foreach.hpp>
  27. #include <boost/bind.hpp>
  28. #include <boost/noncopyable.hpp>
  29. #include <map>
  30. using namespace isc::dns;
  31. using namespace isc::dns::rdata;
  32. namespace isc {
  33. namespace datasrc {
  34. namespace memory {
  35. using detail::SegmentObjectHolder;
  36. using detail::getCoveredType;
  37. namespace { // unnamed namespace
  38. // A functor type used for loading.
  39. typedef boost::function<void(isc::dns::ConstRRsetPtr)> LoadCallback;
  40. // A helper internal class for \c loadZoneData(). make it non-copyable
  41. // to avoid accidental copy.
  42. //
  43. // The current internal implementation no longer expects that both a
  44. // normal (non RRSIG) RRset and (when signed) its RRSIG are added at
  45. // once, but we do that here anyway to avoid merging RdataSets every
  46. // single time which can be inefficient.
  47. //
  48. // We hold all RRsets of the same owner name in node_rrsets_ and
  49. // node_rrsigsets_, and add the matching pairs of RRsets to the zone
  50. // when we see a new owner name. We do this to limit the size of
  51. // NodeRRsets below. However, RRsets can occur in any order.
  52. //
  53. // The caller is responsible for adding the RRsets of the last group
  54. // in the input sequence by explicitly calling flushNodeRRsets() at the
  55. // end. It's cleaner and more robust if we let the destructor of this class
  56. // do it, but since we cannot guarantee the adding operation is exception free,
  57. // we don't choose that option to maintain the common expectation for
  58. // destructors.
  59. class ZoneDataLoader : boost::noncopyable {
  60. public:
  61. ZoneDataLoader(util::MemorySegment& mem_sgmt,
  62. const isc::dns::RRClass rrclass,
  63. const isc::dns::Name& zone_name, ZoneData& zone_data) :
  64. updater_(mem_sgmt, rrclass, zone_name, zone_data)
  65. {}
  66. void addFromLoad(const isc::dns::ConstRRsetPtr& rrset);
  67. void flushNodeRRsets();
  68. private:
  69. typedef std::map<isc::dns::RRType, isc::dns::ConstRRsetPtr> NodeRRsets;
  70. typedef NodeRRsets::value_type NodeRRsetsVal;
  71. // A helper to identify the covered type of an RRSIG.
  72. const isc::dns::Name& getCurrentName() const;
  73. private:
  74. NodeRRsets node_rrsets_;
  75. NodeRRsets node_rrsigsets_;
  76. std::vector<isc::dns::ConstRRsetPtr> non_consecutive_rrsets_;
  77. ZoneDataUpdater updater_;
  78. };
  79. void
  80. ZoneDataLoader::addFromLoad(const ConstRRsetPtr& rrset) {
  81. // If we see a new name, flush the temporary holders, adding the
  82. // pairs of RRsets and RRSIGs of the previous name to the zone.
  83. if ((!node_rrsets_.empty() || !node_rrsigsets_.empty() ||
  84. !non_consecutive_rrsets_.empty()) &&
  85. (getCurrentName() != rrset->getName())) {
  86. flushNodeRRsets();
  87. }
  88. // Store this RRset until it can be added to the zone. If an rrtype
  89. // that's already been seen is found, queue it in a different vector
  90. // to be merged later.
  91. const bool is_rrsig = rrset->getType() == RRType::RRSIG();
  92. NodeRRsets& node_rrsets = is_rrsig ? node_rrsigsets_ : node_rrsets_;
  93. const RRType& rrtype = is_rrsig ? getCoveredType(rrset) : rrset->getType();
  94. if (!node_rrsets.insert(NodeRRsetsVal(rrtype, rrset)).second) {
  95. non_consecutive_rrsets_.insert(non_consecutive_rrsets_.begin(), rrset);
  96. }
  97. if (rrset->getRRsig()) {
  98. addFromLoad(rrset->getRRsig());
  99. }
  100. }
  101. void
  102. ZoneDataLoader::flushNodeRRsets() {
  103. BOOST_FOREACH(NodeRRsetsVal val, node_rrsets_) {
  104. // Identify the corresponding RRSIG for the RRset, if any. If
  105. // found add both the RRset and its RRSIG at once.
  106. ConstRRsetPtr sig_rrset;
  107. NodeRRsets::iterator sig_it = node_rrsigsets_.find(val.first);
  108. if (sig_it != node_rrsigsets_.end()) {
  109. sig_rrset = sig_it->second;
  110. node_rrsigsets_.erase(sig_it);
  111. }
  112. updater_.add(val.second, sig_rrset);
  113. }
  114. // Normally rrsigsets map should be empty at this point, but it's still
  115. // possible that an RRSIG that don't has covered RRset is added; they
  116. // still remain in the map. We add them to the zone separately.
  117. BOOST_FOREACH(NodeRRsetsVal val, node_rrsigsets_) {
  118. updater_.add(ConstRRsetPtr(), val.second);
  119. }
  120. // Add any non-consecutive rrsets too.
  121. BOOST_FOREACH(ConstRRsetPtr rrset, non_consecutive_rrsets_) {
  122. if (rrset->getType() == RRType::RRSIG()) {
  123. updater_.add(ConstRRsetPtr(), rrset);
  124. } else {
  125. updater_.add(rrset, ConstRRsetPtr());
  126. }
  127. }
  128. node_rrsets_.clear();
  129. node_rrsigsets_.clear();
  130. non_consecutive_rrsets_.clear();
  131. }
  132. const Name&
  133. ZoneDataLoader::getCurrentName() const {
  134. if (!node_rrsets_.empty()) {
  135. return (node_rrsets_.begin()->second->getName());
  136. }
  137. assert(!node_rrsigsets_.empty());
  138. return (node_rrsigsets_.begin()->second->getName());
  139. }
  140. void
  141. logWarning(const dns::Name* zone_name, const dns::RRClass* rrclass,
  142. const std::string& reason)
  143. {
  144. LOG_WARN(logger, DATASRC_MEMORY_CHECK_WARNING).arg(*zone_name).
  145. arg(*rrclass).arg(reason);
  146. }
  147. void
  148. logError(const dns::Name* zone_name, const dns::RRClass* rrclass,
  149. const std::string& reason)
  150. {
  151. LOG_ERROR(logger, DATASRC_MEMORY_CHECK_ERROR).arg(*zone_name).arg(*rrclass).
  152. arg(reason);
  153. }
  154. ZoneData*
  155. loadZoneDataInternal(util::MemorySegment& mem_sgmt,
  156. const isc::dns::RRClass& rrclass,
  157. const Name& zone_name,
  158. boost::function<void(LoadCallback)> rrset_installer)
  159. {
  160. SegmentObjectHolder<ZoneData, RRClass> holder(
  161. mem_sgmt, ZoneData::create(mem_sgmt, zone_name), rrclass);
  162. ZoneDataLoader loader(mem_sgmt, rrclass, zone_name, *holder.get());
  163. rrset_installer(boost::bind(&ZoneDataLoader::addFromLoad, &loader, _1));
  164. // Add any last RRsets that were left
  165. loader.flushNodeRRsets();
  166. const ZoneNode* origin_node = holder.get()->getOriginNode();
  167. const RdataSet* rdataset = origin_node->getData();
  168. // If the zone is NSEC3-signed, check if it has NSEC3PARAM
  169. if (holder.get()->isNSEC3Signed()) {
  170. if (RdataSet::find(rdataset, RRType::NSEC3PARAM()) == NULL) {
  171. LOG_WARN(logger, DATASRC_MEMORY_MEM_NO_NSEC3PARAM).
  172. arg(zone_name).arg(rrclass);
  173. }
  174. }
  175. RRsetCollection collection(*(holder.get()), rrclass);
  176. const dns::ZoneCheckerCallbacks
  177. callbacks(boost::bind(&logError, &zone_name, &rrclass, _1),
  178. boost::bind(&logWarning, &zone_name, &rrclass, _1));
  179. if (!dns::checkZone(zone_name, rrclass, collection, callbacks)) {
  180. isc_throw(ZoneValidationError,
  181. "Errors found when validating zone: "
  182. << zone_name << "/" << rrclass);
  183. }
  184. return (holder.release());
  185. }
  186. // A wrapper for dns::MasterLoader used by loadZoneData() below. Essentially
  187. // it converts the two callback types. Note the mostly redundant wrapper of
  188. // boost::bind. It converts function<void(ConstRRsetPtr)> to
  189. // function<void(RRsetPtr)> (MasterLoader expects the latter). SunStudio
  190. // doesn't seem to do this conversion if we just pass 'callback'.
  191. void
  192. masterLoaderWrapper(const char* const filename, const Name& origin,
  193. const RRClass& zone_class, LoadCallback callback)
  194. {
  195. bool load_ok = false; // (we don't use it)
  196. dns::RRCollator collator(boost::bind(callback, _1));
  197. try {
  198. dns::MasterLoader(filename, origin, zone_class,
  199. createMasterLoaderCallbacks(origin, zone_class,
  200. &load_ok),
  201. collator.getCallback()).load();
  202. collator.flush();
  203. } catch (const dns::MasterLoaderError& e) {
  204. isc_throw(ZoneLoaderException, e.what());
  205. }
  206. }
  207. // The installer called from the iterator version of loadZoneData().
  208. void
  209. generateRRsetFromIterator(ZoneIterator* iterator, LoadCallback callback) {
  210. ConstRRsetPtr rrset;
  211. while ((rrset = iterator->getNextRRset()) != NULL) {
  212. callback(rrset);
  213. }
  214. }
  215. } // end of unnamed namespace
  216. ZoneData*
  217. loadZoneData(util::MemorySegment& mem_sgmt,
  218. const isc::dns::RRClass& rrclass,
  219. const isc::dns::Name& zone_name,
  220. const std::string& zone_file)
  221. {
  222. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_LOAD_FROM_FILE).
  223. arg(zone_name).arg(rrclass).arg(zone_file);
  224. return (loadZoneDataInternal(mem_sgmt, rrclass, zone_name,
  225. boost::bind(masterLoaderWrapper,
  226. zone_file.c_str(),
  227. zone_name, rrclass,
  228. _1)));
  229. }
  230. ZoneData*
  231. loadZoneData(util::MemorySegment& mem_sgmt,
  232. const isc::dns::RRClass& rrclass,
  233. const isc::dns::Name& zone_name,
  234. ZoneIterator& iterator)
  235. {
  236. LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_MEMORY_MEM_LOAD_FROM_DATASRC).
  237. arg(zone_name).arg(rrclass);
  238. return (loadZoneDataInternal(mem_sgmt, rrclass, zone_name,
  239. boost::bind(generateRRsetFromIterator,
  240. &iterator, _1)));
  241. }
  242. } // namespace memory
  243. } // namespace datasrc
  244. } // namespace isc