rrset_collection.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. The constructor throws MasterLoaderError if there is
  39. /// an error during loading.
  40. ///
  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. The constructor throws MasterLoaderError if there is an
  52. /// error during loading.
  53. ///
  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. void removeRRset(const isc::dns::Name& name,
  80. const isc::dns::RRClass& rrclass,
  81. const isc::dns::RRType& rrtype);
  82. /// \brief Find a matching RRset in the collection.
  83. ///
  84. /// Returns the RRset in the collection that exactly matches the
  85. /// given \c name and \c rrtype. If no matching RRset is found,
  86. /// \c NULL is returned.
  87. ///
  88. /// \param name The name of the RRset to search for.
  89. /// \param rrtype The type of the RRset to search for.
  90. /// \param rrclass The class of the RRset to search for.
  91. /// \returns A pointer to the RRset if found, \c NULL otherwise.
  92. virtual const isc::dns::AbstractRRset* find
  93. (const isc::dns::Name& name, const isc::dns::RRType& rrtype,
  94. const isc::dns::RRClass& rrclass)
  95. const;
  96. isc::dns::RRsetPtr find(const isc::dns::Name& name,
  97. const isc::dns::RRClass& rrclass,
  98. const isc::dns::RRType& rrtype);
  99. isc::dns::ConstRRsetPtr find(const isc::dns::Name& name,
  100. const isc::dns::RRClass& rrclass,
  101. const isc::dns::RRType& rrtype) const;
  102. private:
  103. void addRRset(const isc::dns::Name& name, const isc::dns::RRClass& rrclass,
  104. const isc::dns::RRType& rrtype, const isc::dns::RRTTL& rrttl,
  105. const isc::dns::rdata::RdataPtr& data);
  106. void loaderCallback(const std::string&, size_t, const std::string&);
  107. typedef boost::tuple<isc::dns::RRClass, isc::dns::RRType, isc::dns::Name>
  108. CollectionKey;
  109. typedef std::map<CollectionKey, isc::dns::RRsetPtr> CollectionMap;
  110. CollectionMap rrsets_;
  111. protected:
  112. class DnsIter : public RRsetCollectionBase::Iter {
  113. public:
  114. DnsIter(CollectionMap::iterator& iter) :
  115. iter_(iter)
  116. {}
  117. virtual const isc::dns::AbstractRRset& getValue() {
  118. isc::dns::RRsetPtr& rrset = iter_->second;
  119. return (*rrset);
  120. }
  121. virtual IterPtr getNext() {
  122. CollectionMap::iterator it = iter_;
  123. it++;
  124. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  125. }
  126. virtual bool equals(Iter& other) {
  127. const DnsIter* other_real = dynamic_cast<DnsIter*>(&other);
  128. if (other_real == NULL) {
  129. return (false);
  130. }
  131. return (iter_ == other_real->iter_);
  132. }
  133. private:
  134. CollectionMap::iterator iter_;
  135. };
  136. virtual RRsetCollectionBase::IterPtr getBeginning();
  137. virtual RRsetCollectionBase::IterPtr getEnd();
  138. };
  139. } // end of namespace dns
  140. } // end of namespace isc
  141. #endif // RRSET_COLLECTION_H
  142. // Local Variables:
  143. // mode: c++
  144. // End: