messagerenderer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /// \todo We might want to revisit this API at some point and remove the
  101. /// buffer parameter. In that case it would create it's own buffer and
  102. /// a function to extract the data would be available instead. It seems
  103. /// like a cleaner design, but it's left undone until we would actually
  104. /// benefit from the change.
  105. AbstractMessageRenderer(isc::util::OutputBuffer& buffer) :
  106. buffer_(buffer)
  107. {}
  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 Buffer to store data
  118. ///
  119. /// It was decided that there's no need to have this in every subclass,
  120. /// at least not now, and this reduces code size and gives compiler a better
  121. /// chance to optimise.
  122. isc::util::OutputBuffer& buffer_;
  123. public:
  124. ///
  125. /// \name Getter Methods
  126. ///
  127. //@{
  128. /// \brief Return a pointer to the head of the data stored in the internal
  129. /// buffer.
  130. ///
  131. /// This method works exactly same as the same method of the \c OutputBuffer
  132. /// class; all notes for \c OutputBuffer apply.
  133. const void* getData() const {
  134. return (buffer_.getData());
  135. }
  136. /// \brief Return the length of data written in the internal buffer.
  137. size_t getLength() const {
  138. return (buffer_.getLength());
  139. }
  140. /// \brief Return whether truncation has occurred while rendering.
  141. ///
  142. /// Once the return value of this method is \c true, it doesn't make sense
  143. /// to try rendering more data, although this class itself doesn't reject
  144. /// the attempt.
  145. ///
  146. /// This method never throws an exception.
  147. ///
  148. /// \return true if truncation has occurred; otherwise \c false.
  149. virtual bool isTruncated() const = 0;
  150. /// \brief Return the maximum length of rendered data that can fit in the
  151. /// corresponding DNS message without truncation.
  152. ///
  153. /// This method never throws an exception.
  154. ///
  155. /// \return The maximum length in bytes.
  156. virtual size_t getLengthLimit() const = 0;
  157. /// \brief Return the compression mode of the renderer class object.
  158. ///
  159. /// This method never throws an exception.
  160. ///
  161. /// \return The current compression mode.
  162. virtual CompressMode getCompressMode() const = 0;
  163. //@}
  164. ///
  165. /// \name Setter Methods
  166. ///
  167. //@{
  168. /// \brief Mark the renderer to indicate truncation has occurred while
  169. /// rendering.
  170. ///
  171. /// This method never throws an exception.
  172. virtual void setTruncated() = 0;
  173. /// \brief Set the maximum length of rendered data that can fit in the
  174. /// corresponding DNS message without truncation.
  175. ///
  176. /// This method never throws an exception.
  177. ///
  178. /// \param len The maximum length in bytes.
  179. virtual void setLengthLimit(size_t len) = 0;
  180. /// \brief Set the compression mode of the renderer class object.
  181. ///
  182. /// This method never throws an exception.
  183. ///
  184. /// \param mode A \c CompressMode value representing the compression mode.
  185. virtual void setCompressMode(CompressMode mode) = 0;
  186. //@}
  187. ///
  188. /// \name Methods for writing data into the internal buffer.
  189. ///
  190. //@{
  191. /// \brief Insert a specified length of gap at the end of the buffer.
  192. ///
  193. /// The caller should not assume any particular value to be inserted.
  194. /// This method is provided as a shortcut to make a hole in the buffer
  195. /// that is to be filled in later, e.g, by \ref writeUint16At().
  196. ///
  197. /// \param len The length of the gap to be inserted in bytes.
  198. void skip(size_t len) {
  199. buffer_.skip(len);
  200. }
  201. /// \brief Trim the specified length of data from the end of the internal
  202. /// buffer.
  203. ///
  204. /// This method is provided for such cases as DNS message truncation.
  205. ///
  206. /// The specified length must not exceed the current data size of the
  207. /// buffer; otherwise an exception of class \c isc::OutOfRange will
  208. /// be thrown.
  209. ///
  210. /// \param len The length of data that should be trimmed.
  211. void trim(size_t len) {
  212. buffer_.trim(len);
  213. }
  214. /// \brief Clear the internal buffer and other internal resources.
  215. ///
  216. /// This method can be used to re-initialize and reuse the renderer
  217. /// without constructing a new one.
  218. virtual void clear();
  219. /// \brief Write an unsigned 8-bit integer into the internal buffer.
  220. ///
  221. /// \param data The 8-bit integer to be written into the internal buffer.
  222. void writeUint8(const uint8_t data) {
  223. buffer_.writeUint8(data);
  224. }
  225. /// \brief Write an unsigned 16-bit integer in host byte order into the
  226. /// internal buffer in network byte order.
  227. ///
  228. /// \param data The 16-bit integer to be written into the buffer.
  229. void writeUint16(uint16_t data) {
  230. buffer_.writeUint16(data);
  231. }
  232. /// \brief Write an unsigned 16-bit integer in host byte order at the
  233. /// specified position of the internal buffer in network byte order.
  234. ///
  235. /// The buffer must have a sufficient room to store the given data at the
  236. /// given position, that is, <code>pos + 2 < getLength()</code>;
  237. /// otherwise an exception of class \c isc::dns::InvalidBufferPosition will
  238. /// be thrown.
  239. /// Note also that this method never extends the internal buffer.
  240. ///
  241. /// \param data The 16-bit integer to be written into the internal buffer.
  242. /// \param pos The beginning position in the buffer to write the data.
  243. void writeUint16At(uint16_t data, size_t pos) {
  244. buffer_.writeUint16At(data, pos);
  245. }
  246. /// \brief Write an unsigned 32-bit integer in host byte order into the
  247. /// internal buffer in network byte order.
  248. ///
  249. /// \param data The 32-bit integer to be written into the buffer.
  250. void writeUint32(uint32_t data) {
  251. buffer_.writeUint32(data);
  252. }
  253. /// \brief Copy an arbitrary length of data into the internal buffer
  254. /// of the renderer object.
  255. ///
  256. /// No conversion on the copied data is performed.
  257. ///
  258. /// \param data A pointer to the data to be copied into the internal buffer.
  259. /// \param len The length of the data in bytes.
  260. void writeData(const void *data, size_t len) {
  261. buffer_.writeData(data, len);
  262. }
  263. /// \brief Write a \c Name object into the internal buffer in wire format,
  264. /// with or without name compression.
  265. ///
  266. /// If the optional parameter \c compress is \c true, this method tries to
  267. /// compress the \c name if possible, searching the entire message that has
  268. /// been rendered. Otherwise name compression is omitted. Its default
  269. /// value is \c true.
  270. ///
  271. /// Note: even if \c compress is \c true, the position of the \c name (and
  272. /// possibly its ancestor names) in the message is recorded and may be used
  273. /// for compressing subsequent names.
  274. ///
  275. /// \param name A \c Name object to be written.
  276. /// \param compress A boolean indicating whether to enable name compression.
  277. virtual void writeName(const Name& name, bool compress = true) = 0;
  278. //@}
  279. };
  280. /// The \c MessageRenderer is a concrete derived class of
  281. /// \c AbstractMessageRenderer as a general purpose implementation of the
  282. /// renderer interfaces.
  283. ///
  284. /// A \c MessageRenderer object is constructed with a \c OutputBuffer
  285. /// object, which is the buffer into which the rendered %data will be written.
  286. /// Normally the buffer is expected to be empty on construction, but it doesn't
  287. /// have to be so; the renderer object will start rendering from the
  288. /// end of the buffer at the time of construction. However, if the
  289. /// pre-existing portion of the buffer contains DNS names, these names won't
  290. /// be considered for name compression.
  291. class MessageRenderer : public AbstractMessageRenderer {
  292. public:
  293. using AbstractMessageRenderer::CASE_INSENSITIVE;
  294. using AbstractMessageRenderer::CASE_SENSITIVE;
  295. /// \brief Constructor from an output buffer.
  296. ///
  297. /// \param buffer An \c OutputBuffer object to which wire format data is
  298. /// written.
  299. MessageRenderer(isc::util::OutputBuffer& buffer);
  300. virtual ~MessageRenderer();
  301. virtual bool isTruncated() const;
  302. virtual size_t getLengthLimit() const;
  303. virtual CompressMode getCompressMode() const;
  304. virtual void setTruncated();
  305. virtual void setLengthLimit(size_t len);
  306. virtual void setCompressMode(CompressMode mode);
  307. virtual void clear();
  308. virtual void writeName(const Name& name, bool compress = true);
  309. private:
  310. struct MessageRendererImpl;
  311. MessageRendererImpl* impl_;
  312. };
  313. }
  314. }
  315. #endif // __MESSAGERENDERER_H
  316. // Local Variables:
  317. // mode: c++
  318. // End: