zone_finder_context.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <exceptions/exceptions.h>
  15. #include <dns/rdata.h>
  16. #include <dns/rrset.h>
  17. #include <dns/rrtype.h>
  18. #include <dns/rdataclass.h>
  19. #include <datasrc/zone_finder.h>
  20. #include <boost/foreach.hpp>
  21. #include <vector>
  22. using namespace std;
  23. using namespace isc::dns;
  24. using namespace isc::dns::rdata;
  25. namespace isc {
  26. namespace datasrc {
  27. namespace {
  28. void
  29. getAdditionalAddrs(ZoneFinder& finder, const Name& name,
  30. const vector<RRType>& requested_types,
  31. vector<ConstRRsetPtr>& result_rrsets,
  32. ZoneFinder::FindOptions options)
  33. {
  34. // Ignore out-of-zone names
  35. const NameComparisonResult cmp = finder.getOrigin().compare(name);
  36. if ((cmp.getRelation() != NameComparisonResult::SUPERDOMAIN) &&
  37. (cmp.getRelation() != NameComparisonResult::EQUAL)) {
  38. return;
  39. }
  40. BOOST_FOREACH(RRType rrtype, requested_types) {
  41. ConstZoneFinderContextPtr ctx = finder.find(name, rrtype, options);
  42. if (ctx->code == ZoneFinder::SUCCESS) {
  43. result_rrsets.push_back(ctx->rrset);
  44. }
  45. }
  46. }
  47. void
  48. getAdditionalForRRset(ZoneFinder& finder, const AbstractRRset& rrset,
  49. const vector<RRType>& requested_types,
  50. vector<ConstRRsetPtr>& result,
  51. ZoneFinder::FindOptions orig_options)
  52. {
  53. RdataIteratorPtr rdata_iterator(rrset.getRdataIterator());
  54. ZoneFinder::FindOptions options = ZoneFinder::FIND_DEFAULT;
  55. if ((orig_options & ZoneFinder::FIND_DNSSEC) != 0) {
  56. options = options | ZoneFinder::FIND_DNSSEC;
  57. }
  58. for (; !rdata_iterator->isLast(); rdata_iterator->next()) {
  59. const Rdata& rdata(rdata_iterator->getCurrent());
  60. if (rrset.getType() == RRType::NS()) {
  61. // Need to perform the search in the "GLUE OK" mode.
  62. const generic::NS& ns = dynamic_cast<const generic::NS&>(rdata);
  63. getAdditionalAddrs(finder, ns.getNSName(), requested_types,
  64. result, options | ZoneFinder::FIND_GLUE_OK);
  65. } else if (rrset.getType() == RRType::MX()) {
  66. const generic::MX& mx = dynamic_cast<const generic::MX&>(rdata);
  67. getAdditionalAddrs(finder, mx.getMXName(), requested_types,
  68. result, options);
  69. }
  70. }
  71. }
  72. }
  73. void
  74. ZoneFinder::Context::getAdditionalImpl(const vector<RRType>& requested_types,
  75. vector<ConstRRsetPtr>& result)
  76. {
  77. // If rrset is non NULL, it should have been SUCCESS/DELEGATION; otherwise
  78. // we should have responded to type ANY query.
  79. ZoneFinder* finder = getFinder();
  80. if (finder == NULL) {
  81. // This is a bug of the derived class implementation.
  82. isc_throw(isc::Unexpected, "NULL ZoneFinder in finder Context");
  83. }
  84. if (rrset) {
  85. getAdditionalForRRset(*finder, *rrset, requested_types, result,
  86. options_);
  87. return;
  88. }
  89. const vector<ConstRRsetPtr>* all_sets = getAllRRsets();
  90. if (all_sets == NULL) { // bug of the derived class implementation.
  91. isc_throw(isc::Unexpected, "All RRsets is NULL in finder Context");
  92. }
  93. BOOST_FOREACH(ConstRRsetPtr rrset_in_set, *getAllRRsets()) {
  94. getAdditionalForRRset(*finder, *rrset_in_set, requested_types, result,
  95. options_);
  96. }
  97. }
  98. } // namespace datasrc
  99. } // datasrc isc