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