oldmessagerenderer.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright (C) 2009 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 <util/buffer.h>
  16. #include <dns/name.h>
  17. #include <dns/labelsequence.h>
  18. #include <oldmessagerenderer.h>
  19. #include <cctype>
  20. #include <cassert>
  21. #include <set>
  22. using namespace isc::util;
  23. namespace isc {
  24. namespace dns {
  25. namespace { // hide internal-only names from the public namespaces
  26. ///
  27. /// \brief The \c NameCompressNode class represents a pointer to a name
  28. /// rendered in the internal buffer for the \c MessageRendererImpl object.
  29. ///
  30. /// A \c MessageRendererImpl object maintains a set of the \c NameCompressNode
  31. /// objects, and searches the set for the position of the longest match
  32. /// (ancestor) name against each new name to be rendered into the buffer.
  33. struct NameCompressNode {
  34. NameCompressNode(const OldMessageRenderer& renderer,
  35. const OutputBuffer& buffer, const size_t pos,
  36. const size_t len) :
  37. renderer_(renderer), buffer_(buffer), pos_(pos), len_(len) {}
  38. /// The renderer that performs name compression using the node.
  39. /// This is kept in each node to detect the compression mode
  40. /// (case-sensitive or not) in the comparison functor (\c NameCompare).
  41. const OldMessageRenderer& renderer_;
  42. /// The buffer in which the corresponding name is rendered.
  43. const OutputBuffer& buffer_;
  44. /// The position (offset from the beginning) in the buffer where the
  45. /// name starts.
  46. uint16_t pos_;
  47. /// The length of the corresponding name.
  48. uint16_t len_;
  49. };
  50. ///
  51. /// \brief The \c NameCompare class is a functor that gives ordering among
  52. /// \c NameCompressNode objects stored in \c MessageRendererImpl::nodeset_.
  53. ///
  54. /// Its only public method as a functor, \c operator(), gives the ordering
  55. /// between two \c NameCompressNode objects in terms of equivalence, that is,
  56. /// returns whether one is "less than" the other.
  57. /// For our purpose we only need to distinguish two different names, so the
  58. /// ordering is different from the canonical DNS name order used in DNSSEC;
  59. /// basically, it gives the case-insensitive ordering of the two names as their
  60. /// textual representation.
  61. struct NameCompare : public std::binary_function<NameCompressNode,
  62. NameCompressNode,
  63. bool> {
  64. ///
  65. /// Returns true if n1 < n2 as a result of case-insensitive comparison;
  66. /// otherwise return false.
  67. ///
  68. /// The name corresponding to \c n1 or \c n2 may be compressed, in which
  69. /// case we must follow the compression pointer in the associated buffer.
  70. /// The helper private method \c nextPosition() gives the position in the
  71. /// buffer for the next character, taking into account compression.
  72. ///
  73. bool operator()(const NameCompressNode& n1,
  74. const NameCompressNode& n2) const
  75. {
  76. if (n1.len_ < n2.len_) {
  77. return (true);
  78. } else if (n1.len_ > n2.len_) {
  79. return (false);
  80. }
  81. const bool case_sensitive =
  82. (n1.renderer_.getCompressMode() == OldMessageRenderer::CASE_SENSITIVE);
  83. uint16_t pos1 = n1.pos_;
  84. uint16_t pos2 = n2.pos_;
  85. uint16_t l1 = 0;
  86. uint16_t l2 = 0;
  87. for (uint16_t i = 0; i < n1.len_; i++, pos1++, pos2++) {
  88. pos1 = nextPosition(n1.buffer_, pos1, l1);
  89. pos2 = nextPosition(n2.buffer_, pos2, l2);
  90. if (case_sensitive) {
  91. if (n1.buffer_[pos1] < n2.buffer_[pos2]) {
  92. return (true);
  93. } else if (n1.buffer_[pos1] > n2.buffer_[pos2]) {
  94. return (false);
  95. }
  96. } else {
  97. if (tolower(n1.buffer_[pos1]) < tolower(n2.buffer_[pos2])) {
  98. return (true);
  99. } else if (tolower(n1.buffer_[pos1]) >
  100. tolower(n2.buffer_[pos2])) {
  101. return (false);
  102. }
  103. }
  104. }
  105. return (false);
  106. }
  107. private:
  108. uint16_t nextPosition(const OutputBuffer& buffer,
  109. uint16_t pos, uint16_t& llen) const
  110. {
  111. if (llen == 0) {
  112. size_t i = 0;
  113. while ((buffer[pos] & Name::COMPRESS_POINTER_MARK8) ==
  114. Name::COMPRESS_POINTER_MARK8) {
  115. pos = (buffer[pos] & ~Name::COMPRESS_POINTER_MARK8) *
  116. 256 + buffer[pos + 1];
  117. // This loop should stop as long as the buffer has been
  118. // constructed validly and the search/insert argument is based
  119. // on a valid name, which is an assumption for this class.
  120. // But we'll abort if a bug could cause an infinite loop.
  121. i += 2;
  122. assert(i < Name::MAX_WIRE);
  123. }
  124. llen = buffer[pos];
  125. } else {
  126. --llen;
  127. }
  128. return (pos);
  129. }
  130. };
  131. }
  132. ///
  133. /// \brief The \c MessageRendererImpl class is the actual implementation of
  134. /// \c MessageRenderer.
  135. ///
  136. /// The implementation is hidden from applications. We can refer to specific
  137. /// members of this class only within the implementation source file.
  138. ///
  139. struct OldMessageRenderer::MessageRendererImpl {
  140. /// \brief Constructor from an output buffer.
  141. ///
  142. MessageRendererImpl() :
  143. nbuffer_(Name::MAX_WIRE), msglength_limit_(512),
  144. truncated_(false), compress_mode_(OldMessageRenderer::CASE_INSENSITIVE)
  145. {}
  146. /// A local working buffer to convert each given name into wire format.
  147. /// This could be a local variable of the \c writeName() method, but
  148. /// we keep it in the class so that we can reuse it and avoid construction
  149. /// overhead.
  150. OutputBuffer nbuffer_;
  151. /// A set of compression pointers.
  152. std::set<NameCompressNode, NameCompare> nodeset_;
  153. /// The maximum length of rendered data that can fit without
  154. /// truncation.
  155. uint16_t msglength_limit_;
  156. /// A boolean flag that indicates truncation has occurred while rendering
  157. /// the data.
  158. bool truncated_;
  159. /// The name compression mode.
  160. CompressMode compress_mode_;
  161. };
  162. OldMessageRenderer::OldMessageRenderer() :
  163. AbstractMessageRenderer(),
  164. impl_(new MessageRendererImpl)
  165. {}
  166. OldMessageRenderer::~OldMessageRenderer() {
  167. delete impl_;
  168. }
  169. void
  170. OldMessageRenderer::clear() {
  171. AbstractMessageRenderer::clear();
  172. impl_->nbuffer_.clear();
  173. impl_->nodeset_.clear();
  174. impl_->msglength_limit_ = 512;
  175. impl_->truncated_ = false;
  176. impl_->compress_mode_ = CASE_INSENSITIVE;
  177. }
  178. size_t
  179. OldMessageRenderer::getLengthLimit() const {
  180. return (impl_->msglength_limit_);
  181. }
  182. void
  183. OldMessageRenderer::setLengthLimit(const size_t len) {
  184. impl_->msglength_limit_ = len;
  185. }
  186. bool
  187. OldMessageRenderer::isTruncated() const {
  188. return (impl_->truncated_);
  189. }
  190. void
  191. OldMessageRenderer::setTruncated() {
  192. impl_->truncated_ = true;
  193. }
  194. OldMessageRenderer::CompressMode
  195. OldMessageRenderer::getCompressMode() const {
  196. return (impl_->compress_mode_);
  197. }
  198. void
  199. OldMessageRenderer::setCompressMode(const CompressMode mode) {
  200. impl_->compress_mode_ = mode;
  201. }
  202. void
  203. OldMessageRenderer::writeName(const Name& name, const bool compress) {
  204. impl_->nbuffer_.clear();
  205. name.toWire(impl_->nbuffer_);
  206. unsigned int i;
  207. std::set<NameCompressNode, NameCompare>::const_iterator notfound =
  208. impl_->nodeset_.end();
  209. std::set<NameCompressNode, NameCompare>::const_iterator n = notfound;
  210. // Find the longest ancestor name in the rendered set that matches the
  211. // given name.
  212. for (i = 0; i < impl_->nbuffer_.getLength(); i += impl_->nbuffer_[i] + 1) {
  213. // skip the trailing null label
  214. if (impl_->nbuffer_[i] == 0) {
  215. continue;
  216. }
  217. n = impl_->nodeset_.find(NameCompressNode(*this, impl_->nbuffer_, i,
  218. impl_->nbuffer_.getLength() -
  219. i));
  220. if (n != notfound) {
  221. break;
  222. }
  223. }
  224. // Record the current offset before extending the buffer.
  225. const size_t offset = getLength();
  226. // Write uncompress part...
  227. writeData(impl_->nbuffer_.getData(),
  228. compress ? i : impl_->nbuffer_.getLength());
  229. if (compress && n != notfound) {
  230. // ...and compression pointer if available.
  231. uint16_t pointer = (*n).pos_;
  232. pointer |= Name::COMPRESS_POINTER_MARK16;
  233. writeUint16(pointer);
  234. }
  235. // Finally, add to the set the newly rendered name and its ancestors that
  236. // have not been in the set.
  237. for (unsigned int j = 0; j < i; j += impl_->nbuffer_[j] + 1) {
  238. if (impl_->nbuffer_[j] == 0) {
  239. continue;
  240. }
  241. if (offset + j > Name::MAX_COMPRESS_POINTER) {
  242. break;
  243. }
  244. impl_->nodeset_.insert(NameCompressNode(*this, getBuffer(),
  245. offset + j,
  246. impl_->nbuffer_.getLength() -
  247. j));
  248. }
  249. }
  250. void
  251. OldMessageRenderer::writeName(const LabelSequence&, const bool) {
  252. // We shouldn't use this version of writeName (and we internally
  253. // control it, so we simply assert it here)
  254. assert(false);
  255. }
  256. }
  257. }