zonetable.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2010 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/name.h>
  15. #include <dns/rrclass.h>
  16. #include <datasrc/zonetable.h>
  17. #include <datasrc/rbtree.h>
  18. using namespace std;
  19. using namespace isc::dns;
  20. namespace isc {
  21. namespace datasrc {
  22. struct MemoryZone::MemoryZoneImpl {
  23. MemoryZoneImpl(const RRClass& zone_class, const Name& origin) :
  24. zone_class_(zone_class), origin_(origin)
  25. {}
  26. RRClass zone_class_;
  27. Name origin_;
  28. };
  29. MemoryZone::MemoryZone(const RRClass& zone_class, const Name& origin) :
  30. impl_(new MemoryZoneImpl(zone_class, origin))
  31. {
  32. }
  33. MemoryZone::~MemoryZone() {
  34. delete impl_;
  35. }
  36. const Name&
  37. MemoryZone::getOrigin() const {
  38. return (impl_->origin_);
  39. }
  40. const RRClass&
  41. MemoryZone::getClass() const {
  42. return (impl_->zone_class_);
  43. }
  44. Zone::FindResult
  45. MemoryZone::find(const Name&, const RRType&) const {
  46. // This is a tentative implementation that always returns NXDOMAIN.
  47. return (FindResult(NXDOMAIN, RRsetPtr()));
  48. }
  49. /// \short Private data and implementation of ZoneTable
  50. struct ZoneTable::ZoneTableImpl {
  51. // Type aliases to make it shorter
  52. typedef RBTree<Zone> ZoneTree;
  53. typedef RBNode<Zone> ZoneNode;
  54. // The actual storage
  55. ZoneTree zones_;
  56. /*
  57. * The implementation methods are here and just wrap-called in the
  58. * ZoneTable. We have variables locally (without impl_->), have
  59. * type aliases, etc. And they will get inlined anyway.
  60. */
  61. // Implementation of ZoneTable::addZone
  62. result::Result addZone(ZonePtr zone) {
  63. // Sanity check
  64. if (!zone) {
  65. isc_throw(InvalidParameter,
  66. "Null pointer is passed to ZoneTable::addZone()");
  67. }
  68. // Get the node where we put the zone
  69. ZoneNode* node(NULL);
  70. switch (zones_.insert(zone->getOrigin(), &node)) {
  71. // This is OK
  72. case ZoneTree::SUCCEED:
  73. case ZoneTree::ALREADYEXIST:
  74. break;
  75. // Can Not Happen
  76. default:
  77. assert(0);
  78. }
  79. // Can Not Happen
  80. assert(node);
  81. // Is it empty? We either just created it or it might be nonterminal
  82. if (node->isEmpty()) {
  83. node->setData(zone);
  84. return (result::SUCCESS);
  85. } else { // There's something there already
  86. return (result::EXIST);
  87. }
  88. }
  89. // Implementation of ZoneTable::findZone
  90. ZoneTable::FindResult findZone(const Name& name) const {
  91. ZoneNode *node(NULL);
  92. result::Result my_result;
  93. // Translate the return codes
  94. switch (zones_.find(name, &node)) {
  95. case ZoneTree::EXACTMATCH:
  96. my_result = result::SUCCESS;
  97. break;
  98. case ZoneTree::PARTIALMATCH:
  99. my_result = result::PARTIALMATCH;
  100. break;
  101. // We have no data there, so translate the pointer to NULL as well
  102. case ZoneTree::NOTFOUND:
  103. return (FindResult(result::NOTFOUND, ConstZonePtr()));
  104. // Can Not Happen
  105. default:
  106. assert(0);
  107. }
  108. // Can Not Happen (remember, NOTFOUND is handled)
  109. assert(node);
  110. return (FindResult(my_result, node->getData()));
  111. }
  112. };
  113. ZoneTable::ZoneTable() : impl_(new ZoneTableImpl)
  114. {}
  115. ZoneTable::~ZoneTable() {
  116. delete impl_;
  117. }
  118. result::Result
  119. ZoneTable::addZone(ZonePtr zone) {
  120. return (impl_->addZone(zone));
  121. }
  122. result::Result
  123. ZoneTable::removeZone(const Name&) {
  124. // TODO Implement
  125. assert(0);
  126. }
  127. ZoneTable::FindResult
  128. ZoneTable::findZone(const Name& name) const {
  129. return (impl_->findZone(name));
  130. }
  131. } // end of namespace datasrc
  132. } // end of namespace isc