rrset_collection.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
  49. const RRClass& rrclass)
  50. {
  51. MasterLoaderCallbacks callbacks
  52. (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
  53. boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
  54. MasterLoader loader(filename, origin, rrclass, callbacks,
  55. boost::bind(&RRsetCollection::addRRset,
  56. this, _1, _2, _3, _4, _5),
  57. MasterLoader::DEFAULT);
  58. loader.load();
  59. }
  60. RRsetCollection::RRsetCollection(std::istream& input_stream, const Name& origin,
  61. const RRClass& rrclass)
  62. {
  63. MasterLoaderCallbacks callbacks
  64. (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
  65. boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
  66. MasterLoader loader(input_stream, origin, rrclass, callbacks,
  67. boost::bind(&RRsetCollection::addRRset,
  68. this, _1, _2, _3, _4, _5),
  69. MasterLoader::DEFAULT);
  70. loader.load();
  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. void
  104. RRsetCollection::removeRRset(const Name& name, const RRClass& rrclass,
  105. const RRType& rrtype)
  106. {
  107. const CollectionKey key(rrclass, rrtype, name);
  108. rrsets_.erase(key);
  109. }
  110. RRsetCollectionBase::IterPtr
  111. RRsetCollection::getBeginning() {
  112. CollectionMap::iterator it = rrsets_.begin();
  113. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  114. }
  115. RRsetCollectionBase::IterPtr
  116. RRsetCollection::getEnd() {
  117. CollectionMap::iterator it = rrsets_.end();
  118. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  119. }
  120. } // end of namespace dns
  121. } // end of namespace isc