nsec_47.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <iostream>
  15. #include <string>
  16. #include <sstream>
  17. #include <vector>
  18. #include <util/encode/base64.h>
  19. #include <util/buffer.h>
  20. #include <dns/exceptions.h>
  21. #include <dns/messagerenderer.h>
  22. #include <dns/name.h>
  23. #include <dns/rrtype.h>
  24. #include <dns/rrttl.h>
  25. #include <dns/rdata.h>
  26. #include <dns/rdataclass.h>
  27. #include <dns/rdata/generic/detail/nsec_bitmap.h>
  28. #include <stdio.h>
  29. #include <time.h>
  30. using namespace std;
  31. using namespace isc::util;
  32. using namespace isc::util::encode;
  33. using namespace isc::dns::rdata::generic::detail::nsec;
  34. // BEGIN_ISC_NAMESPACE
  35. // BEGIN_RDATA_NAMESPACE
  36. struct NSECImpl {
  37. // straightforward representation of NSEC RDATA fields
  38. NSECImpl(const Name& next, vector<uint8_t> typebits) :
  39. nextname_(next), typebits_(typebits)
  40. {}
  41. Name nextname_;
  42. vector<uint8_t> typebits_;
  43. };
  44. NSEC::NSEC(const string& nsec_str) :
  45. impl_(NULL)
  46. {
  47. istringstream iss(nsec_str);
  48. string nextname;
  49. iss >> nextname;
  50. if (iss.bad() || iss.fail()) {
  51. isc_throw(InvalidRdataText, "Invalid NSEC name");
  52. }
  53. if (iss.eof()) {
  54. isc_throw(InvalidRdataText, "NSEC bitmap is missing");
  55. }
  56. vector<uint8_t> typebits;
  57. buildBitmapsFromText("NSEC", iss, typebits);
  58. impl_ = new NSECImpl(Name(nextname), typebits);
  59. }
  60. NSEC::NSEC(InputBuffer& buffer, size_t rdata_len) {
  61. const size_t pos = buffer.getPosition();
  62. const Name nextname(buffer);
  63. // rdata_len must be sufficiently large to hold non empty bitmap.
  64. if (rdata_len <= buffer.getPosition() - pos) {
  65. isc_throw(DNSMessageFORMERR,
  66. "NSEC RDATA from wire too short: " << rdata_len << "bytes");
  67. }
  68. rdata_len -= (buffer.getPosition() - pos);
  69. vector<uint8_t> typebits(rdata_len);
  70. buffer.readData(&typebits[0], rdata_len);
  71. checkRRTypeBitmaps("NSEC", typebits);
  72. impl_ = new NSECImpl(nextname, typebits);
  73. }
  74. NSEC::NSEC(const NSEC& source) :
  75. Rdata(), impl_(new NSECImpl(*source.impl_))
  76. {}
  77. NSEC&
  78. NSEC::operator=(const NSEC& source) {
  79. if (impl_ == source.impl_) {
  80. return (*this);
  81. }
  82. NSECImpl* newimpl = new NSECImpl(*source.impl_);
  83. delete impl_;
  84. impl_ = newimpl;
  85. return (*this);
  86. }
  87. NSEC::~NSEC() {
  88. delete impl_;
  89. }
  90. string
  91. NSEC::toText() const {
  92. ostringstream s;
  93. s << impl_->nextname_;
  94. bitmapsToText(impl_->typebits_, s);
  95. return (s.str());
  96. }
  97. void
  98. NSEC::toWire(OutputBuffer& buffer) const {
  99. impl_->nextname_.toWire(buffer);
  100. buffer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  101. }
  102. void
  103. NSEC::toWire(AbstractMessageRenderer& renderer) const {
  104. impl_->nextname_.toWire(renderer);
  105. renderer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  106. }
  107. const Name&
  108. NSEC::getNextName() const {
  109. return (impl_->nextname_);
  110. }
  111. int
  112. NSEC::compare(const Rdata& other) const {
  113. const NSEC& other_nsec = dynamic_cast<const NSEC&>(other);
  114. int cmp = compareNames(impl_->nextname_, other_nsec.impl_->nextname_);
  115. if (cmp != 0) {
  116. return (cmp);
  117. }
  118. const size_t this_len = impl_->typebits_.size();
  119. const size_t other_len = other_nsec.impl_->typebits_.size();
  120. const size_t cmplen = min(this_len, other_len);
  121. cmp = memcmp(&impl_->typebits_[0], &other_nsec.impl_->typebits_[0],
  122. cmplen);
  123. if (cmp != 0) {
  124. return (cmp);
  125. } else {
  126. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  127. }
  128. }
  129. // END_RDATA_NAMESPACE
  130. // END_ISC_NAMESPACE