nsec_47.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. uint8_t bitmap[8 * 1024]; // 64k bits
  50. vector<uint8_t> typebits;
  51. iss >> nextname;
  52. if (iss.bad() || iss.fail()) {
  53. isc_throw(InvalidRdataText, "Invalid NSEC name");
  54. }
  55. memset(bitmap, 0, sizeof(bitmap));
  56. do {
  57. string type;
  58. iss >> type;
  59. try {
  60. const int code = RRType(type).getCode();
  61. bitmap[code / 8] |= (0x80 >> (code % 8));
  62. } catch (...) {
  63. isc_throw(InvalidRdataText, "Invalid RRtype in NSEC");
  64. }
  65. } while (!iss.eof());
  66. for (int window = 0; window < 256; window++) {
  67. int octet;
  68. for (octet = 31; octet >= 0; octet--) {
  69. if (bitmap[window * 32 + octet] != 0) {
  70. break;
  71. }
  72. }
  73. if (octet < 0)
  74. continue;
  75. typebits.push_back(window);
  76. typebits.push_back(octet + 1);
  77. for (int i = 0; i <= octet; i++) {
  78. typebits.push_back(bitmap[window * 32 + i]);
  79. }
  80. }
  81. impl_ = new NSECImpl(Name(nextname), typebits);
  82. }
  83. NSEC::NSEC(InputBuffer& buffer, size_t rdata_len) {
  84. const size_t pos = buffer.getPosition();
  85. const Name nextname(buffer);
  86. // rdata_len must be sufficiently large to hold non empty bitmap.
  87. if (rdata_len <= buffer.getPosition() - pos) {
  88. isc_throw(DNSMessageFORMERR,
  89. "NSEC RDATA from wire too short: " << rdata_len << "bytes");
  90. }
  91. rdata_len -= (buffer.getPosition() - pos);
  92. vector<uint8_t> typebits(rdata_len);
  93. buffer.readData(&typebits[0], rdata_len);
  94. checkRRTypeBitmaps("NSEC", typebits);
  95. impl_ = new NSECImpl(nextname, typebits);
  96. }
  97. NSEC::NSEC(const NSEC& source) :
  98. Rdata(), impl_(new NSECImpl(*source.impl_))
  99. {}
  100. NSEC&
  101. NSEC::operator=(const NSEC& source) {
  102. if (impl_ == source.impl_) {
  103. return (*this);
  104. }
  105. NSECImpl* newimpl = new NSECImpl(*source.impl_);
  106. delete impl_;
  107. impl_ = newimpl;
  108. return (*this);
  109. }
  110. NSEC::~NSEC() {
  111. delete impl_;
  112. }
  113. string
  114. NSEC::toText() const {
  115. ostringstream s;
  116. int len = 0;
  117. s << impl_->nextname_;
  118. // In the following loop we use string::at() rather than operator[].
  119. // Since the index calculation is a bit complicated, it will be safer
  120. // and easier to find a bug (if any). Note that this conversion method
  121. // is generally not expected to be very efficient, so the slight overhead
  122. // of at() should be acceptable.
  123. for (int i = 0; i < impl_->typebits_.size(); i += len) {
  124. assert(i + 2 <= impl_->typebits_.size());
  125. const int block = impl_->typebits_.at(i);
  126. len = impl_->typebits_.at(i + 1);
  127. assert(len > 0 && len <= 32);
  128. i += 2;
  129. for (int j = 0; j < len; j++) {
  130. if (impl_->typebits_.at(i + j) == 0) {
  131. continue;
  132. }
  133. for (int k = 0; k < 8; k++) {
  134. if ((impl_->typebits_.at(i + j) & (0x80 >> k)) == 0) {
  135. continue;
  136. }
  137. const int t = block * 256 + j * 8 + k;
  138. s << " " << RRType(t);
  139. }
  140. }
  141. }
  142. return (s.str());
  143. }
  144. void
  145. NSEC::toWire(OutputBuffer& buffer) const {
  146. impl_->nextname_.toWire(buffer);
  147. buffer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  148. }
  149. void
  150. NSEC::toWire(MessageRenderer& renderer) const {
  151. impl_->nextname_.toWire(renderer);
  152. renderer.writeData(&impl_->typebits_[0], impl_->typebits_.size());
  153. }
  154. int
  155. NSEC::compare(const Rdata& other) const {
  156. const NSEC& other_nsec = dynamic_cast<const NSEC&>(other);
  157. int cmp = compareNames(impl_->nextname_, other_nsec.impl_->nextname_);
  158. if (cmp != 0) {
  159. return (cmp);
  160. }
  161. const size_t this_len = impl_->typebits_.size();
  162. const size_t other_len = other_nsec.impl_->typebits_.size();
  163. const size_t cmplen = min(this_len, other_len);
  164. cmp = memcmp(&impl_->typebits_[0], &other_nsec.impl_->typebits_[0],
  165. cmplen);
  166. if (cmp != 0) {
  167. return (cmp);
  168. } else {
  169. return ((this_len == other_len) ? 0 : (this_len < other_len) ? -1 : 1);
  170. }
  171. }
  172. // END_RDATA_NAMESPACE
  173. // END_ISC_NAMESPACE