master_loader.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef MASTER_LOADER_H
  15. #define MASTER_LOADER_H
  16. #include <dns/master_loader_callbacks.h>
  17. #include <boost/noncopyable.hpp>
  18. namespace isc {
  19. namespace dns {
  20. class Name;
  21. class RRClass;
  22. /// \brief Error while loading by MasterLoader without specifying the
  23. /// MANY_ERRORS option.
  24. class MasterLoaderError : public isc::Exception {
  25. public:
  26. MasterLoaderError(const char* file, size_t line, const char* what) :
  27. Exception(file, line, what)
  28. {}
  29. };
  30. /// \brief A class able to load DNS master files
  31. ///
  32. /// This class is able to produce a stream of RRs from a master file.
  33. /// It is able to load all of the master file at once, or by blocks
  34. /// incrementally.
  35. ///
  36. /// It reports the loaded RRs and encountered errors by callbacks.
  37. class MasterLoader : boost::noncopyable {
  38. public:
  39. /// \brief Options how the parsing should work.
  40. enum Options {
  41. DEFAULT = 0, ///< Nothing special.
  42. MANY_ERRORS = 1 ///< Lenient mode (see documentation of MasterLoader
  43. /// constructor).
  44. };
  45. /// \brief Constructor
  46. ///
  47. /// This creates a master loader and provides it with all
  48. /// relevant information.
  49. ///
  50. /// Except for the exceptions listed below, the constructor doesn't
  51. /// throw. Most errors (like non-existent master file) are reported
  52. /// by the callbacks during load() or loadIncremental().
  53. ///
  54. /// \param master_file Path to the file to load.
  55. /// \param zone_origin The origin of zone to be expected inside
  56. /// the master file. Currently unused, but it is expected to
  57. /// be used for some validation.
  58. /// \param zone_class The class of zone to be expected inside the
  59. /// master file.
  60. /// \param callbacks The callbacks by which it should report problems.
  61. /// Usually, the callback carries a filename and line number of the
  62. /// input where the problem happens. There's a special case of empty
  63. /// filename and zero line in case the opening of the top-level master
  64. /// file fails.
  65. /// \param add_callback The callback which would be called with each
  66. /// loaded RR.
  67. /// \param options Options for the parsing, which is bitwise-or of
  68. /// the Options values or DEFAULT. If the MANY_ERRORS option is
  69. /// included, the parser tries to continue past errors. If it
  70. /// is not included, it stops at first encountered error.
  71. /// \throw std::bad_alloc when there's not enough memory.
  72. /// \throw isc::InvalidParameter if add_callback is empty.
  73. MasterLoader(const char* master_file,
  74. const Name& zone_origin,
  75. const RRClass& zone_class,
  76. const MasterLoaderCallbacks& callbacks,
  77. const AddRRCallback& add_callback,
  78. Options options = DEFAULT);
  79. /// \brief Constructor from a stream
  80. ///
  81. /// This is a constructor very similar to the previous one. The only
  82. /// difference is it doesn't take a filename, but an input stream
  83. /// to read the data from. It is expected to be mostly used in tests,
  84. /// but it is public as it may possibly be useful for other currently
  85. /// unknown purposes.
  86. MasterLoader(std::istream& input,
  87. const Name& zone_origin,
  88. const RRClass& zone_class,
  89. const MasterLoaderCallbacks& callbacks,
  90. const AddRRCallback& add_callback,
  91. Options options = DEFAULT);
  92. /// \brief Destructor
  93. ~MasterLoader();
  94. /// \brief Load some RRs
  95. ///
  96. /// This method loads at most count_limit RRs and reports them. In case
  97. /// an error (either fatal or without MANY_ERRORS) or end of file is
  98. /// encountered, they may be less.
  99. ///
  100. /// \param count_limit Upper limit on the number of RRs loaded.
  101. /// \return In case it stops because of the count limit, it returns false.
  102. /// It returns true if the loading is done.
  103. /// \throw isc::InvalidOperation when called after loading was done
  104. /// already.
  105. /// \throw MasterLoaderError when there's an error in the input master
  106. /// file and the MANY_ERRORS is not specified. It never throws this
  107. /// in case MANY_ERRORS is specified.
  108. bool loadIncremental(size_t count_limit);
  109. /// \brief Load everything
  110. ///
  111. /// This simply calls loadIncremental until the loading is done.
  112. /// \throw isc::InvalidOperation when called after loading was done
  113. /// already.
  114. /// \throw MasterLoaderError when there's an error in the input master
  115. /// file and the MANY_ERRORS is not specified. It never throws this
  116. /// in case MANY_ERRORS is specified.
  117. void load() {
  118. while (!loadIncremental(1000)) { // 1000 = arbitrary largish number
  119. // Body intentionally left blank
  120. }
  121. }
  122. private:
  123. class MasterLoaderImpl;
  124. MasterLoaderImpl* impl_;
  125. };
  126. } // end namespace dns
  127. } // end namespace isc
  128. #endif // MASTER_LOADER_H