messagerenderer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #ifndef __MESSAGERENDERER_H
  15. #define __MESSAGERENDERER_H 1
  16. #include <util/buffer.h>
  17. namespace isc {
  18. namespace dns {
  19. // forward declarations
  20. class Name;
  21. /// \brief The \c AbstractMessageRenderer class is an abstract base class
  22. /// that provides common interfaces for rendering a DNS message into a buffer
  23. /// in wire format.
  24. ///
  25. /// A specific derived class of \c AbstractMessageRenderer (we call it
  26. /// a renderer class hereafter) is simply responsible for name compression at
  27. /// least in the current design. A renderer class object (conceptually)
  28. /// manages the positions of names rendered in some sort of buffer and uses
  29. /// that information to render subsequent names with compression.
  30. ///
  31. /// A renderer class is mainly intended to be used as a helper for a more
  32. /// comprehensive \c Message class internally; normal applications won't have
  33. /// to care about details of this class.
  34. ///
  35. /// Once a renderer class object is constructed with a buffer, it is
  36. /// generally expected that all rendering operations are performed via that
  37. /// object. If the application modifies the buffer in
  38. /// parallel with the renderer, the result will be undefined.
  39. ///
  40. /// Note to developers: we introduced a separate class for name compression
  41. /// because previous benchmark with BIND9 showed compression affects overall
  42. /// response performance very much. By having a separate class dedicated for
  43. /// this purpose, we'll be able to change the internal implementation of name
  44. /// compression in the future without affecting other part of the API and
  45. /// implementation.
  46. ///
  47. /// In addition, by introducing a class hierarchy from
  48. /// \c AbstractMessageRenderer, we allow an application to use a customized
  49. /// renderer class for specific purposes. For example, a high performance
  50. /// DNS server may want to use an optimized renderer class assuming some
  51. /// specific underlying data representation.
  52. ///
  53. /// \note Some functions (like writeUint8) are not virtual. It is because
  54. /// it is hard to imagine any version of message renderer that would
  55. /// do anything else than just putting the data into a buffer, so we
  56. /// provide a default implementation and having them virtual would only
  57. /// hurt the performance with no real gain. If it would happen a different
  58. /// implementation is really needed, we can make them virtual in future.
  59. /// The only one that is virtual is writeName and it's because this
  60. /// function is much more complicated, therefore there's a lot of space
  61. /// for different implementations or behaviours.
  62. class AbstractMessageRenderer {
  63. public:
  64. /// \brief Compression mode constants.
  65. ///
  66. /// The \c CompressMode enum type represents the name compression mode
  67. /// for renderer classes.
  68. /// \c CASE_INSENSITIVE means compress names in case-insensitive manner;
  69. /// \c CASE_SENSITIVE means compress names in case-sensitive manner.
  70. /// By default, a renderer compresses names in case-insensitive
  71. /// manner.
  72. /// Compression mode can be dynamically modified by the
  73. /// \c setCompressMode() method.
  74. /// The mode can be changed even in the middle of rendering, although this
  75. /// is not an intended usage. In this case the names already compressed
  76. /// are intact; only names being compressed after the mode change are
  77. /// affected by the change.
  78. /// If a renderer class object is reinitialized by the \c clear()
  79. /// method, the compression mode will be reset to the default, which is
  80. /// \c CASE_INSENSITIVE
  81. ///
  82. /// One specific case where case-sensitive compression is required is
  83. /// AXFR as described in draft-ietf-dnsext-axfr-clarify. A primary
  84. /// authoritative DNS server implementation using this API would specify
  85. /// \c CASE_SENSITIVE before rendering outgoing AXFR messages.
  86. ///
  87. enum CompressMode {
  88. CASE_INSENSITIVE, //!< Compress names case-insensitive manner (default)
  89. CASE_SENSITIVE //!< Compress names case-sensitive manner
  90. };
  91. protected:
  92. ///
  93. /// \name Constructors and Destructor
  94. //@{
  95. /// \brief The default constructor.
  96. ///
  97. /// This is intentionally defined as \c protected as this base class should
  98. /// never be instantiated (except as part of a derived class).
  99. /// \param buffer The buffer where the data should be rendered into.
  100. ///
  101. /// We are now doing this:
  102. /// \todo We might want to revisit this API at some point and remove the
  103. /// buffer parameter. In that case it would create it's own buffer and
  104. /// a function to extract the data would be available instead. It seems
  105. /// like a cleaner design, but it's left undone until we would actually
  106. /// benefit from the change.
  107. AbstractMessageRenderer();
  108. public:
  109. /// \brief The destructor.
  110. virtual ~AbstractMessageRenderer() {}
  111. //@}
  112. protected:
  113. /// \brief Return the output buffer we render into.
  114. const isc::util::OutputBuffer& getBuffer() const { return (*buffer_); }
  115. isc::util::OutputBuffer& getBuffer() { return (*buffer_); }
  116. private:
  117. /// \short Local (default) buffer to store data.
  118. isc::util::OutputBuffer local_buffer_;
  119. /// \short Buffer to store data
  120. ///
  121. /// It was decided that there's no need to have this in every subclass,
  122. /// at least not now, and this reduces code size and gives compiler a better
  123. /// chance to optimise.
  124. isc::util::OutputBuffer* buffer_;
  125. public:
  126. ///
  127. /// \name Getter Methods
  128. ///
  129. //@{
  130. /// \brief Return a pointer to the head of the data stored in the internal
  131. /// buffer.
  132. ///
  133. /// This method works exactly same as the same method of the \c OutputBuffer
  134. /// class; all notes for \c OutputBuffer apply.
  135. const void* getData() const {
  136. return (buffer_->getData());
  137. }
  138. /// \brief Return the length of data written in the internal buffer.
  139. size_t getLength() const {
  140. return (buffer_->getLength());
  141. }
  142. /// \brief Return whether truncation has occurred while rendering.
  143. ///
  144. /// Once the return value of this method is \c true, it doesn't make sense
  145. /// to try rendering more data, although this class itself doesn't reject
  146. /// the attempt.
  147. ///
  148. /// This method never throws an exception.
  149. ///
  150. /// \return true if truncation has occurred; otherwise \c false.
  151. virtual bool isTruncated() const = 0;
  152. /// \brief Return the maximum length of rendered data that can fit in the
  153. /// corresponding DNS message without truncation.
  154. ///
  155. /// This method never throws an exception.
  156. ///
  157. /// \return The maximum length in bytes.
  158. virtual size_t getLengthLimit() const = 0;
  159. /// \brief Return the compression mode of the renderer class object.
  160. ///
  161. /// This method never throws an exception.
  162. ///
  163. /// \return The current compression mode.
  164. virtual CompressMode getCompressMode() const = 0;
  165. //@}
  166. ///
  167. /// \name Setter Methods
  168. ///
  169. //@{
  170. /// TBD
  171. void setBuffer(isc::util::OutputBuffer* buffer);
  172. /// \brief Mark the renderer to indicate truncation has occurred while
  173. /// rendering.
  174. ///
  175. /// This method never throws an exception.
  176. virtual void setTruncated() = 0;
  177. /// \brief Set the maximum length of rendered data that can fit in the
  178. /// corresponding DNS message without truncation.
  179. ///
  180. /// This method never throws an exception.
  181. ///
  182. /// \param len The maximum length in bytes.
  183. virtual void setLengthLimit(size_t len) = 0;
  184. /// \brief Set the compression mode of the renderer class object.
  185. ///
  186. /// This method never throws an exception.
  187. ///
  188. /// \param mode A \c CompressMode value representing the compression mode.
  189. virtual void setCompressMode(CompressMode mode) = 0;
  190. //@}
  191. ///
  192. /// \name Methods for writing data into the internal buffer.
  193. ///
  194. //@{
  195. /// \brief Insert a specified length of gap at the end of the buffer.
  196. ///
  197. /// The caller should not assume any particular value to be inserted.
  198. /// This method is provided as a shortcut to make a hole in the buffer
  199. /// that is to be filled in later, e.g, by \ref writeUint16At().
  200. ///
  201. /// \param len The length of the gap to be inserted in bytes.
  202. void skip(size_t len) {
  203. buffer_->skip(len);
  204. }
  205. /// \brief Trim the specified length of data from the end of the internal
  206. /// buffer.
  207. ///
  208. /// This method is provided for such cases as DNS message truncation.
  209. ///
  210. /// The specified length must not exceed the current data size of the
  211. /// buffer; otherwise an exception of class \c isc::OutOfRange will
  212. /// be thrown.
  213. ///
  214. /// \param len The length of data that should be trimmed.
  215. void trim(size_t len) {
  216. buffer_->trim(len);
  217. }
  218. /// \brief Clear the internal buffer and other internal resources.
  219. ///
  220. /// This method can be used to re-initialize and reuse the renderer
  221. /// without constructing a new one.
  222. virtual void clear();
  223. /// \brief Write an unsigned 8-bit integer into the internal buffer.
  224. ///
  225. /// \param data The 8-bit integer to be written into the internal buffer.
  226. void writeUint8(const uint8_t data) {
  227. buffer_->writeUint8(data);
  228. }
  229. /// \brief Write an unsigned 16-bit integer in host byte order into the
  230. /// internal buffer in network byte order.
  231. ///
  232. /// \param data The 16-bit integer to be written into the buffer.
  233. void writeUint16(uint16_t data) {
  234. buffer_->writeUint16(data);
  235. }
  236. /// \brief Write an unsigned 16-bit integer in host byte order at the
  237. /// specified position of the internal buffer in network byte order.
  238. ///
  239. /// The buffer must have a sufficient room to store the given data at the
  240. /// given position, that is, <code>pos + 2 < getLength()</code>;
  241. /// otherwise an exception of class \c isc::dns::InvalidBufferPosition will
  242. /// be thrown.
  243. /// Note also that this method never extends the internal buffer.
  244. ///
  245. /// \param data The 16-bit integer to be written into the internal buffer.
  246. /// \param pos The beginning position in the buffer to write the data.
  247. void writeUint16At(uint16_t data, size_t pos) {
  248. buffer_->writeUint16At(data, pos);
  249. }
  250. /// \brief Write an unsigned 32-bit integer in host byte order into the
  251. /// internal buffer in network byte order.
  252. ///
  253. /// \param data The 32-bit integer to be written into the buffer.
  254. void writeUint32(uint32_t data) {
  255. buffer_->writeUint32(data);
  256. }
  257. /// \brief Copy an arbitrary length of data into the internal buffer
  258. /// of the renderer object.
  259. ///
  260. /// No conversion on the copied data is performed.
  261. ///
  262. /// \param data A pointer to the data to be copied into the internal buffer.
  263. /// \param len The length of the data in bytes.
  264. void writeData(const void *data, size_t len) {
  265. buffer_->writeData(data, len);
  266. }
  267. /// \brief Write a \c Name object into the internal buffer in wire format,
  268. /// with or without name compression.
  269. ///
  270. /// If the optional parameter \c compress is \c true, this method tries to
  271. /// compress the \c name if possible, searching the entire message that has
  272. /// been rendered. Otherwise name compression is omitted. Its default
  273. /// value is \c true.
  274. ///
  275. /// Note: even if \c compress is \c true, the position of the \c name (and
  276. /// possibly its ancestor names) in the message is recorded and may be used
  277. /// for compressing subsequent names.
  278. ///
  279. /// \param name A \c Name object to be written.
  280. /// \param compress A boolean indicating whether to enable name compression.
  281. virtual void writeName(const Name& name, bool compress = true) = 0;
  282. //@}
  283. };
  284. /// The \c MessageRenderer is a concrete derived class of
  285. /// \c AbstractMessageRenderer as a general purpose implementation of the
  286. /// renderer interfaces.
  287. ///
  288. /// A \c MessageRenderer object is constructed with a \c OutputBuffer
  289. /// object, which is the buffer into which the rendered %data will be written.
  290. /// Normally the buffer is expected to be empty on construction, but it doesn't
  291. /// have to be so; the renderer object will start rendering from the
  292. /// end of the buffer at the time of construction. However, if the
  293. /// pre-existing portion of the buffer contains DNS names, these names won't
  294. /// be considered for name compression.
  295. class MessageRenderer : public AbstractMessageRenderer {
  296. public:
  297. using AbstractMessageRenderer::CASE_INSENSITIVE;
  298. using AbstractMessageRenderer::CASE_SENSITIVE;
  299. /// \brief Constructor from an output buffer.
  300. MessageRenderer();
  301. virtual ~MessageRenderer();
  302. virtual bool isTruncated() const;
  303. virtual size_t getLengthLimit() const;
  304. virtual CompressMode getCompressMode() const;
  305. virtual void setTruncated();
  306. virtual void setLengthLimit(size_t len);
  307. virtual void setCompressMode(CompressMode mode);
  308. virtual void clear();
  309. virtual void writeName(const Name& name, bool compress = true);
  310. private:
  311. struct MessageRendererImpl;
  312. MessageRendererImpl* impl_;
  313. };
  314. }
  315. }
  316. #endif // __MESSAGERENDERER_H
  317. // Local Variables:
  318. // mode: c++
  319. // End: