rrset_collection.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <boost/bind.hpp>
  18. namespace isc {
  19. namespace dns {
  20. void
  21. RRsetCollection::loaderCallback(const std::string&, size_t, const std::string&)
  22. {
  23. // We just ignore callbacks for errors and warnings.
  24. }
  25. void
  26. RRsetCollection::addRRset(const Name& name, const RRClass& rrclass,
  27. const RRType& rrtype, const RRTTL& rrttl,
  28. const rdata::RdataPtr& data)
  29. {
  30. RRsetPtr rrset(new BasicRRset(name, rrclass, rrtype, rrttl));
  31. rrset->addRdata(data);
  32. addRRset(rrset);
  33. }
  34. void
  35. RRsetCollection::addRRset(RRsetPtr rrset) {
  36. const CollectionKey key(rrset->getClass(), rrset->getType(),
  37. rrset->getName());
  38. rrsets_.insert(std::pair<CollectionKey, RRsetPtr>(key, rrset));
  39. }
  40. RRsetCollection::RRsetCollection(const char* filename, const Name& origin,
  41. const RRClass& rrclass)
  42. {
  43. MasterLoaderCallbacks callbacks
  44. (boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3),
  45. boost::bind(&RRsetCollection::loaderCallback, this, _1, _2, _3));
  46. MasterLoader loader(filename, origin, rrclass, callbacks,
  47. boost::bind(&RRsetCollection::addRRset,
  48. this, _1, _2, _3, _4, _5),
  49. MasterLoader::DEFAULT);
  50. loader.load();
  51. }
  52. const AbstractRRset*
  53. RRsetCollection::find(const Name& name, const RRType& rrtype,
  54. const RRClass& rrclass) const {
  55. const CollectionKey key(rrclass, rrtype, name);
  56. CollectionMap::const_iterator it = rrsets_.find(key);
  57. if (it != rrsets_.end()) {
  58. return (&(*it->second));
  59. }
  60. return (NULL);
  61. }
  62. RRsetPtr
  63. RRsetCollection::find(const Name& name, const RRClass& rrclass,
  64. const RRType& rrtype) {
  65. const CollectionKey key(rrclass, rrtype, name);
  66. CollectionMap::iterator it = rrsets_.find(key);
  67. if (it != rrsets_.end()) {
  68. return (it->second);
  69. }
  70. return (RRsetPtr());
  71. }
  72. ConstRRsetPtr
  73. RRsetCollection::find(const Name& name, const RRClass& rrclass,
  74. const RRType& rrtype) const
  75. {
  76. const CollectionKey key(rrclass, rrtype, name);
  77. CollectionMap::const_iterator it = rrsets_.find(key);
  78. if (it != rrsets_.end()) {
  79. return (it->second);
  80. }
  81. return (ConstRRsetPtr());
  82. }
  83. void
  84. RRsetCollection::removeRRset(const Name& name, const RRClass& rrclass,
  85. const RRType& rrtype)
  86. {
  87. const CollectionKey key(rrclass, rrtype, name);
  88. rrsets_.erase(key);
  89. }
  90. RRsetCollectionBase::IterPtr
  91. RRsetCollection::getBeginning() {
  92. CollectionMap::iterator it = rrsets_.begin();
  93. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  94. }
  95. RRsetCollectionBase::IterPtr
  96. RRsetCollection::getEnd() {
  97. CollectionMap::iterator it = rrsets_.end();
  98. return (RRsetCollectionBase::IterPtr(new DnsIter(it)));
  99. }
  100. } // end of namespace dns
  101. } // end of namespace isc