zone_finder.cc 5.3 KB

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