rrset_collection.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 RRSET_COLLECTION_H
  15. #define RRSET_COLLECTION_H 1
  16. #include <dns/rrset_collection_base.h>
  17. #include <dns/rrclass.h>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <boost/tuple/tuple_comparison.hpp>
  20. #include <map>
  21. namespace isc {
  22. namespace dns {
  23. /// \brief libdns++ implementation of RRsetCollectionBase using an STL
  24. /// container.
  25. class RRsetCollection : public RRsetCollectionBase {
  26. public:
  27. /// \brief Constructor.
  28. ///
  29. /// This constructor creates an empty collection without any data in
  30. /// it. RRsets can be added to the collection with the \c addRRset()
  31. /// method.
  32. RRsetCollection() {}
  33. /// \brief Constructor.
  34. ///
  35. /// The \c origin and \c rrclass arguments are required for the zone
  36. /// loading, but \c RRsetCollection itself does not do any
  37. /// validation, and the collection of RRsets does not have to form a
  38. /// valid zone.
  39. ///
  40. /// \throws MasterLoaderError if there is an error during loading.
  41. /// \param filename Name of a file containing a collection of RRs in
  42. /// the master file format (which may or may not form a valid zone).
  43. /// \param origin The zone origin.
  44. /// \param rrclass The zone class.
  45. RRsetCollection(const char* filename, const isc::dns::Name& origin,
  46. const isc::dns::RRClass& rrclass);
  47. /// \brief Constructor.
  48. ///
  49. /// This constructor is similar to the previous one, but instead of
  50. /// taking a filename to load a zone from, it takes an input
  51. /// stream.
  52. ///
  53. /// \throws MasterLoaderError if there is an error during loading.
  54. /// \param input_stream The input stream to load from.
  55. /// \param origin The zone origin.
  56. /// \param rrclass The zone class.
  57. RRsetCollection(std::istream& input_stream, const isc::dns::Name& origin,
  58. const isc::dns::RRClass& rrclass);
  59. /// \brief Destructor
  60. virtual ~RRsetCollection() {}
  61. /// \brief Add an RRset to the collection.
  62. ///
  63. /// Does not do any validation whether \c rrset belongs to a
  64. /// particular zone or not. A reference to \c rrset is taken in an
  65. /// internally managed \c shared_ptr, so even if the caller's
  66. /// \c RRsetPtr is destroyed, the RRset it wrapped is still alive
  67. /// and managed by the \c RRsetCollection. It throws an
  68. /// \c isc::InvalidParameter exception if an rrset with the same
  69. /// class, type and name already exists.
  70. ///
  71. /// Callers must not modify the RRset after adding it to the
  72. /// collection, as the rrset is indexed internally by the
  73. /// collection.
  74. void addRRset(isc::dns::RRsetPtr rrset);
  75. /// \brief Remove an RRset from the collection.
  76. ///
  77. /// RRset(s) matching the \c name, \c rrclass and \c rrtype are
  78. /// removed from the collection.
  79. ///
  80. /// \return \c true if a matching RRset was deleted, \c false if no
  81. /// such RRset exists.
  82. bool removeRRset(const isc::dns::Name& name,
  83. const isc::dns::RRClass& rrclass,
  84. const isc::dns::RRType& rrtype);
  85. /// \brief Find a matching RRset in the collection.
  86. ///
  87. /// Returns the RRset in the collection that exactly matches the
  88. /// given \c name, \c rrclass and \c rrtype. If no matching RRset
  89. /// is found, \c NULL is returned.
  90. ///
  91. /// \param name The name of the RRset to search for.
  92. /// \param rrclass The class of the RRset to search for.
  93. /// \param rrtype The type of the RRset to search for.
  94. /// \return The RRset if found, \c NULL otherwise.
  95. virtual isc::dns::ConstRRsetPtr find(const isc::dns::Name& name,
  96. const isc::dns::RRClass& rrclass,
  97. const isc::dns::RRType& rrtype) const;
  98. /// \brief Find a matching RRset in the collection (non-const
  99. /// variant).
  100. ///
  101. /// See above for a description of the method and arguments.
  102. isc::dns::RRsetPtr find(const isc::dns::Name& name,
  103. const isc::dns::RRClass& rrclass,
  104. const isc::dns::RRType& rrtype);
  105. private:
  106. template<typename T>
  107. void constructHelper(T source, const isc::dns::Name& origin,
  108. const isc::dns::RRClass& rrclass);
  109. void loaderCallback(const std::string&, size_t, const std::string&);
  110. typedef boost::tuple<isc::dns::RRClass, isc::dns::RRType, isc::dns::Name>
  111. CollectionKey;
  112. typedef std::map<CollectionKey, isc::dns::RRsetPtr> CollectionMap;
  113. CollectionMap rrsets_;
  114. protected:
  115. class DnsIter : public RRsetCollectionBase::Iter {
  116. public:
  117. DnsIter(CollectionMap::iterator& iter) :
  118. iter_(iter)
  119. {}
  120. virtual const isc::dns::AbstractRRset& getValue() {
  121. isc::dns::RRsetPtr& rrset = iter_->second;
  122. return (*rrset);
  123. }
  124. virtual IterPtr getNext() {
  125. CollectionMap::iterator it = iter_;
  126. ++it;
  127. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  128. }
  129. virtual bool equals(Iter& other) {
  130. const DnsIter* other_real = dynamic_cast<DnsIter*>(&other);
  131. if (other_real == NULL) {
  132. return (false);
  133. }
  134. return (iter_ == other_real->iter_);
  135. }
  136. private:
  137. CollectionMap::iterator iter_;
  138. };
  139. virtual RRsetCollectionBase::IterPtr getBeginning();
  140. virtual RRsetCollectionBase::IterPtr getEnd();
  141. };
  142. } // end of namespace dns
  143. } // end of namespace isc
  144. #endif // RRSET_COLLECTION_H
  145. // Local Variables:
  146. // mode: c++
  147. // End: