zone_loader.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/zone_loader.h>
  15. #include <datasrc/master_loader_callbacks.h>
  16. #include <datasrc/client.h>
  17. #include <datasrc/data_source.h>
  18. #include <datasrc/iterator.h>
  19. #include <datasrc/zone.h>
  20. #include <datasrc/logger.h>
  21. #include <datasrc/rrset_collection.h>
  22. #include <dns/rrset.h>
  23. #include <dns/zone_checker.h>
  24. #include <boost/bind.hpp>
  25. using isc::dns::Name;
  26. using isc::dns::ConstRRsetPtr;
  27. using isc::dns::MasterLoader;
  28. namespace isc {
  29. namespace datasrc {
  30. ZoneLoader::ZoneLoader(DataSourceClient& destination, const Name& zone_name,
  31. DataSourceClient& source) :
  32. // Separate the RRsets as that is possibly faster (the data source doesn't
  33. // have to aggregate them) and also because our limit semantics.
  34. iterator_(source.getIterator(zone_name, true)),
  35. updater_(destination.getUpdater(zone_name, true, false)),
  36. complete_(false)
  37. {
  38. // The getIterator should never return NULL. So we check it.
  39. // Or should we throw instead?
  40. assert(iterator_ != ZoneIteratorPtr());
  41. // In case the zone doesn't exist in the destination, throw
  42. if (updater_ == ZoneUpdaterPtr()) {
  43. isc_throw(DataSourceError, "Zone " << zone_name << " not found in "
  44. "destination data source, can't fill it with data");
  45. }
  46. // The dereference of zone_finder is safe, if we can get iterator, we can
  47. // get a finder.
  48. //
  49. // TODO: We probably need a getClass on the data source itself.
  50. if (source.findZone(zone_name).zone_finder->getClass() !=
  51. updater_->getFinder().getClass()) {
  52. isc_throw(isc::InvalidParameter,
  53. "Source and destination class mismatch");
  54. }
  55. }
  56. ZoneLoader::ZoneLoader(DataSourceClient& destination, const Name& zone_name,
  57. const char* filename) :
  58. updater_(destination.getUpdater(zone_name, true, false)),
  59. complete_(false),
  60. loaded_ok_(true)
  61. {
  62. if (updater_ == ZoneUpdaterPtr()) {
  63. isc_throw(DataSourceError, "Zone " << zone_name << " not found in "
  64. "destination data source, can't fill it with data");
  65. } else {
  66. loader_.reset(new
  67. MasterLoader(filename, zone_name,
  68. // TODO: Maybe we should have getClass()
  69. // on the data source?
  70. updater_->getFinder().getClass(),
  71. createMasterLoaderCallbacks(zone_name,
  72. updater_->getFinder().getClass(),
  73. &loaded_ok_),
  74. createMasterLoaderAddCallback(*updater_)));
  75. }
  76. }
  77. namespace {
  78. // Copy up to limit RRsets from source to destination
  79. bool
  80. copyRRsets(const ZoneUpdaterPtr& destination, const ZoneIteratorPtr& source,
  81. size_t limit)
  82. {
  83. size_t loaded = 0;
  84. while (loaded < limit) {
  85. const ConstRRsetPtr rrset(source->getNextRRset());
  86. if (rrset == ConstRRsetPtr()) {
  87. // Done loading, no more RRsets in the input.
  88. return (true);
  89. } else {
  90. destination->addRRset(*rrset);
  91. }
  92. ++loaded;
  93. }
  94. return (false); // Not yet, there may be more
  95. }
  96. void
  97. logWarning(const dns::Name* zone_name, const dns::RRClass* rrclass,
  98. const std::string& reason)
  99. {
  100. LOG_WARN(logger, DATASRC_VALIDATE_WARNING).arg(*zone_name).arg(*rrclass).
  101. arg(reason);
  102. }
  103. void
  104. logError(const dns::Name* zone_name, const dns::RRClass* rrclass,
  105. const std::string& reason)
  106. {
  107. LOG_ERROR(logger, DATASRC_VALIDATE_ERROR).arg(*zone_name).arg(*rrclass).
  108. arg(reason);
  109. }
  110. } // end unnamed namespace
  111. bool
  112. ZoneLoader::loadIncremental(size_t limit) {
  113. if (complete_) {
  114. isc_throw(isc::InvalidOperation,
  115. "Loading has been completed previously");
  116. }
  117. if (iterator_ == ZoneIteratorPtr()) {
  118. assert(loader_.get() != NULL);
  119. try {
  120. complete_ = loader_->loadIncremental(limit);
  121. } catch (const isc::dns::MasterLoaderError& e) {
  122. isc_throw(MasterFileError, e.getMessage().c_str());
  123. }
  124. if (complete_ && !loaded_ok_) {
  125. isc_throw(MasterFileError, "Error while loading master file");
  126. }
  127. } else {
  128. complete_ = copyRRsets(updater_, iterator_, limit);
  129. }
  130. if (complete_) {
  131. // Everything is loaded. Perform some basic sanity checks on the zone.
  132. RRsetCollection collection(updater_);
  133. dns::Name zone_name(updater_->getFinder().getOrigin());
  134. dns::RRClass zone_class(updater_->getFinder().getClass());
  135. dns::ZoneCheckerCallbacks
  136. callbacks(boost::bind(&logError, &zone_name, &zone_class, _1),
  137. boost::bind(&logWarning, &zone_name, &zone_class, _1));
  138. if (!dns::checkZone(zone_name, zone_class, collection, callbacks)) {
  139. // Validation failed.
  140. loaded_ok_ = false;
  141. isc_throw(ZoneContentError, "Errors found when validating zone " <<
  142. zone_name << "/" << zone_class);
  143. }
  144. updater_->commit();
  145. }
  146. return (complete_);
  147. }
  148. } // end namespace datasrc
  149. } // end namespace isc