rrset_collection.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. RRsetCollection()
  29. {}
  30. /// \brief Constructor.
  31. ///
  32. /// The \c origin and \c rrclass arguments are required for the zone
  33. /// loading, but \c RRsetCollection itself does not do any
  34. /// validation, and the collection of RRsets does not have to form a
  35. /// valid zone. The constructor throws MasterLoaderError if there is
  36. /// an error during loading.
  37. ///
  38. /// \param filename Name of a file containing a collection of RRs in
  39. /// the master file format (which may or may not form a valid zone).
  40. RRsetCollection(const char* filename, const isc::dns::Name& origin,
  41. const isc::dns::RRClass& rrclass);
  42. /// \brief Add an RRset to the collection.
  43. ///
  44. /// Does not do any validation whether \c rrset belongs to a
  45. /// particular zone or not. It throws an \c isc::InvalidParameter
  46. /// exception if an rrset with the same class, type and name already
  47. /// exists.
  48. void addRRset(isc::dns::RRsetPtr rrset);
  49. /// \brief Remove an RRset from the collection.
  50. ///
  51. /// RRset(s) matching the \c name, \c rrclass and \c rrtype are
  52. /// removed from the collection.
  53. void removeRRset(const isc::dns::Name& name,
  54. const isc::dns::RRClass& rrclass,
  55. const isc::dns::RRType& rrtype);
  56. /// \brief Find a matching RRset in the collection.
  57. ///
  58. /// Returns the RRset in the collection that exactly matches the
  59. /// given \c name and \c rrtype. If no matching RRset is found,
  60. /// \c NULL is returned.
  61. ///
  62. /// \param name The name of the RRset to search for.
  63. /// \param rrtype The type of the RRset to search for.
  64. /// \param rrclass The class of the RRset to search for.
  65. /// \returns A pointer to the RRset if found, \c NULL otherwise.
  66. virtual const isc::dns::AbstractRRset* find
  67. (const isc::dns::Name& name, const isc::dns::RRType& rrtype,
  68. const isc::dns::RRClass& rrclass)
  69. const;
  70. isc::dns::RRsetPtr find(const isc::dns::Name& name,
  71. const isc::dns::RRClass& rrclass,
  72. const isc::dns::RRType& rrtype);
  73. isc::dns::ConstRRsetPtr find(const isc::dns::Name& name,
  74. const isc::dns::RRClass& rrclass,
  75. const isc::dns::RRType& rrtype) const;
  76. private:
  77. void addRRset(const isc::dns::Name& name, const isc::dns::RRClass& rrclass,
  78. const isc::dns::RRType& rrtype, const isc::dns::RRTTL& rrttl,
  79. const isc::dns::rdata::RdataPtr& data);
  80. void loaderCallback(const std::string&, size_t, const std::string&);
  81. typedef boost::tuple<isc::dns::RRClass, isc::dns::RRType, isc::dns::Name>
  82. CollectionKey;
  83. typedef std::map<CollectionKey, isc::dns::RRsetPtr> CollectionMap;
  84. CollectionMap rrsets_;
  85. protected:
  86. class DnsIter : public RRsetCollectionBase::Iter {
  87. public:
  88. DnsIter(CollectionMap::iterator& iter) :
  89. iter_(iter)
  90. {}
  91. virtual const isc::dns::AbstractRRset& getValue() {
  92. isc::dns::RRsetPtr& rrset = iter_->second;
  93. return (*rrset);
  94. }
  95. virtual IterPtr getNext() {
  96. CollectionMap::iterator it = iter_;
  97. it++;
  98. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  99. }
  100. virtual bool equals(Iter& other) {
  101. const DnsIter* other_real = dynamic_cast<DnsIter*>(&other);
  102. if (other_real == NULL) {
  103. return (false);
  104. }
  105. return (iter_ == other_real->iter_);
  106. }
  107. private:
  108. CollectionMap::iterator iter_;
  109. };
  110. virtual RRsetCollectionBase::IterPtr getBeginning();
  111. virtual RRsetCollectionBase::IterPtr getEnd();
  112. };
  113. } // end of namespace dns
  114. } // end of namespace isc
  115. #endif // RRSET_COLLECTION_H
  116. // Local Variables:
  117. // mode: c++
  118. // End: