messagerenderer.cc 10 KB

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