rrset_collection.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include <dns/rrset_collection.h>
  15. #include <dns/master_loader_callbacks.h>
  16. #include <dns/master_loader.h>
  17. #include <exceptions/exceptions.h>
  18. #include <boost/bind.hpp>
  19. using namespace isc;
  20. namespace isc {
  21. namespace dns {
  22. void
  23. RRsetCollection::loaderCallback(const std::string&, size_t, const std::string&)
  24. {
  25. // We just ignore callbacks for errors and warnings.
  26. }
  27. void
  28. RRsetCollection::addRRset(const Name& name, const RRClass& rrclass,
  29. const RRType& rrtype, const RRTTL& rrttl,
  30. const rdata::RdataPtr& data)
  31. {
  32. RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
  33. rrset->addRdata(data);
  34. addRRset(rrset);
  35. }
  36. void
  37. RRsetCollection::addRRset(RRsetPtr rrset) {
  38. const CollectionKey key(rrset->getClass(), rrset->getType(),
  39. rrset->getName());
  40. CollectionMap::const_iterator it = rrsets_.find(key);
  41. if (it != rrsets_.end()) {
  42. isc_throw(InvalidParameter,
  43. "RRset for " << rrset->getName() << "/" << rrset->getClass()
  44. << " with type " << rrset->getType() << " already exists");
  45. }
  46. rrsets_.insert(std::pair<CollectionKey, RRsetPtr>(key, rrset));
  47. }
  48. template<typename T>
  49. void
  50. RRsetCollection::constructHelper(T source, const isc::dns::Name& origin,
  51. const isc::dns::RRClass& rrclass)
  52. {
  53. MasterLoaderCallbacks callbacks
  54. (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
  55. boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
  56. MasterLoader loader(source, origin, rrclass, callbacks,
  57. boost::bind(&RRsetCollection::addRRset,
  58. this, _1, _2, _3, _4, _5),
  59. MasterLoader::DEFAULT);
  60. loader.load();
  61. }
  62. RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
  63. const RRClass& rrclass)
  64. {
  65. constructHelper<const char*>(filename, origin, rrclass);
  66. }
  67. RRsetCollection::RRsetCollection(std::istream& input_stream, const Name& origin,
  68. const RRClass& rrclass)
  69. {
  70. constructHelper<std::istream&>(input_stream, origin, rrclass);
  71. }
  72. const AbstractRRset*
  73. RRsetCollection::find(const Name& name, const RRType& rrtype,
  74. const RRClass& rrclass) const {
  75. const CollectionKey key(rrclass, rrtype, name);
  76. CollectionMap::const_iterator it = rrsets_.find(key);
  77. if (it != rrsets_.end()) {
  78. return (&(*it->second));
  79. }
  80. return (NULL);
  81. }
  82. RRsetPtr
  83. RRsetCollection::find(const Name& name, const RRClass& rrclass,
  84. const RRType& rrtype) {
  85. const CollectionKey key(rrclass, rrtype, name);
  86. CollectionMap::iterator it = rrsets_.find(key);
  87. if (it != rrsets_.end()) {
  88. return (it->second);
  89. }
  90. return (RRsetPtr());
  91. }
  92. ConstRRsetPtr
  93. RRsetCollection::find(const Name& name, const RRClass& rrclass,
  94. const RRType& rrtype) const
  95. {
  96. const CollectionKey key(rrclass, rrtype, name);
  97. CollectionMap::const_iterator it = rrsets_.find(key);
  98. if (it != rrsets_.end()) {
  99. return (it->second);
  100. }
  101. return (ConstRRsetPtr());
  102. }
  103. bool
  104. RRsetCollection::removeRRset(const Name& name, const RRClass& rrclass,
  105. const RRType& rrtype)
  106. {
  107. const CollectionKey key(rrclass, rrtype, name);
  108. CollectionMap::iterator it = rrsets_.find(key);
  109. if (it == rrsets_.end()) {
  110. return (false);
  111. }
  112. rrsets_.erase(it);
  113. return (true);
  114. }
  115. RRsetCollectionBase::IterPtr
  116. RRsetCollection::getBeginning() {
  117. CollectionMap::iterator it = rrsets_.begin();
  118. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  119. }
  120. RRsetCollectionBase::IterPtr
  121. RRsetCollection::getEnd() {
  122. CollectionMap::iterator it = rrsets_.end();
  123. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  124. }
  125. } // end of namespace dns
  126. } // end of namespace isc