rrset_collection.h 4.6 KB

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