zone_finder.cc 5.3 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 <datasrc/zone_finder.h>
  15. #include <datasrc/exceptions.h>
  16. #include <dns/rrclass.h>
  17. #include <dns/rdata.h>
  18. #include <dns/rrset.h>
  19. #include <dns/rrtype.h>
  20. #include <dns/rdataclass.h>
  21. using namespace std;
  22. using namespace isc::dns;
  23. using namespace isc::dns::rdata;
  24. namespace isc {
  25. namespace datasrc {
  26. namespace {
  27. // Identify zone's SOA and return its MINTTL in the form of RRTTL.
  28. RRTTL
  29. getMinTTL(ZoneFinder& finder, ConstRRsetPtr rrset) {
  30. ConstRRsetPtr soa_rrset;
  31. if (rrset->getType() == RRType::SOA()) {
  32. // Shortcut: if we are looking at SOA itself (which should be the
  33. // case in the expected scenario), we can simply use its RDATA.
  34. soa_rrset = rrset;
  35. } else {
  36. soa_rrset =
  37. finder.findAtOrigin(RRType::SOA(), false,
  38. ZoneFinder::FIND_DEFAULT)->rrset;
  39. }
  40. // In a valid zone there is one and only one SOA RR at the origin.
  41. // Otherwise either zone data or the data source implementation is broken.
  42. if (!soa_rrset || soa_rrset->getRdataCount() != 1) {
  43. isc_throw(DataSourceError, "Zone " << rrset->getName().toText(true)
  44. << "/" << rrset->getClass().toText() << " is broken: "
  45. << (!soa_rrset ? "no SOA" : "empty SOA"));
  46. }
  47. return (RRTTL(dynamic_cast<const generic::SOA&>(
  48. soa_rrset->getRdataIterator()->getCurrent()).
  49. getMinimum()));
  50. }
  51. // Make a fresh copy of given RRset, just replacing RRTTL with the given one.
  52. RRsetPtr
  53. copyRRset(const AbstractRRset& rrset, const RRTTL& ttl) {
  54. RRsetPtr rrset_copy(new RRset(rrset.getName(), rrset.getClass(),
  55. rrset.getType(), ttl));
  56. for (RdataIteratorPtr rit = rrset.getRdataIterator();
  57. !rit->isLast();
  58. rit->next()) {
  59. rrset_copy->addRdata(rit->getCurrent());
  60. }
  61. ConstRRsetPtr rrsig = rrset.getRRsig();
  62. if (rrsig) {
  63. RRsetPtr rrsig_copy(new RRset(rrset.getName(), rrset.getClass(),
  64. RRType::RRSIG(), ttl));
  65. for (RdataIteratorPtr rit = rrsig->getRdataIterator();
  66. !rit->isLast();
  67. rit->next()) {
  68. rrsig_copy->addRdata(rit->getCurrent());
  69. }
  70. rrset_copy->addRRsig(rrsig_copy);
  71. }
  72. return (rrset_copy);
  73. }
  74. }
  75. ZoneFinderContextPtr
  76. ZoneFinder::findAtOrigin(const dns::RRType& type, bool use_minttl,
  77. FindOptions options)
  78. {
  79. ZoneFinderContextPtr context = find(getOrigin(), type, options);
  80. // If we are requested to use the min TTL and the RRset's RR TTL is larger
  81. // than that, we need to make a copy of the RRset, replacing the TTL,
  82. // and return a newly created context copying other parameters.
  83. if (use_minttl && context->rrset) {
  84. const AbstractRRset& rrset = *context->rrset;
  85. const RRTTL min_ttl = getMinTTL(*this, context->rrset);
  86. if (min_ttl < rrset.getTTL()) {
  87. FindResultFlags flags_copy = RESULT_DEFAULT;
  88. if (context->isWildcard()) {
  89. flags_copy = flags_copy | RESULT_WILDCARD;
  90. }
  91. if (context->isNSECSigned()) {
  92. flags_copy = flags_copy | RESULT_NSEC_SIGNED;
  93. } else if (context->isNSEC3Signed()) {
  94. flags_copy = flags_copy | RESULT_NSEC3_SIGNED;
  95. }
  96. return (ZoneFinderContextPtr(
  97. new GenericContext(*this, options,
  98. ResultContext(context->code,
  99. copyRRset(rrset,
  100. min_ttl),
  101. flags_copy))));
  102. }
  103. }
  104. return (context);
  105. }
  106. isc::dns::ConstRRsetPtr
  107. ZoneFinder::stripRRsigs(isc::dns::ConstRRsetPtr rp,
  108. const FindOptions options) {
  109. if (rp) {
  110. isc::dns::ConstRRsetPtr sig_rrset = rp->getRRsig();
  111. if (sig_rrset &&
  112. ((options & ZoneFinder::FIND_DNSSEC) == 0)) {
  113. isc::dns::RRsetPtr result_base
  114. (new isc::dns::RRset(rp->getName(),
  115. rp->getClass(),
  116. rp->getType(),
  117. rp->getTTL()));
  118. for (isc::dns::RdataIteratorPtr i(rp->getRdataIterator());
  119. !i->isLast();
  120. i->next()) {
  121. result_base->addRdata(i->getCurrent());
  122. }
  123. return (result_base);
  124. }
  125. }
  126. return (rp);
  127. }
  128. } // namespace datasrc
  129. } // datasrc isc