messagerenderer.cc 9.6 KB

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