messagerenderer.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // $Id$
  15. #ifndef __MESSAGERENDERER_H
  16. #define __MESSAGERENDERER_H 1
  17. namespace isc {
  18. namespace dns {
  19. // forward declarations
  20. class OutputBuffer;
  21. class Name;
  22. class MessageRendererImpl;
  23. ///
  24. /// \brief The \c MessageRenderer class encapsulates implementation details
  25. /// of rendering a DNS message into a buffer in wire format.
  26. ///
  27. /// In effect, it's simply responsible for name compression at least in the
  28. /// current implementation. A \c MessageRenderer class object manages the
  29. /// positions of names rendered in a buffer and uses that information to render
  30. /// subsequent names with compression.
  31. ///
  32. /// This class is mainly intended to be used as a helper for a more
  33. /// comprehensive \c Message class internally; normal applications won't have
  34. /// to care about this class.
  35. ///
  36. /// A \c MessageRenderer class object is constructed with a \c OutputBuffer
  37. /// object, which is the buffer into which the rendered data will be written.
  38. /// Normally the buffer is expected to be empty on construction, but it doesn't
  39. /// have to be so; the \c MessageRenderer object will start rendering from the
  40. /// end of the buffer at the time of construction. However, if the
  41. /// pre-existing portion of the buffer contains DNS names, these names won't
  42. /// be considered for name compression.
  43. ///
  44. /// Once a \c MessageRenderer object is constructed with a buffer, it is
  45. /// generally expected that all rendering operations are performed via the
  46. /// \c MessageRenderer object. If the application modifies the buffer in
  47. /// parallel with the \c MessageRenderer, the result will be undefined.
  48. ///
  49. /// Note to developers: we introduced a separate class for name compression
  50. /// because previous benchmark with BIND9 showed compression affects overall
  51. /// response performance very much. By having a separate class dedicated for
  52. /// this purpose, we'll be able to change the internal implementation of name
  53. /// compression in the future without affecting other part of the API and
  54. /// implementation. For the same reason, we adopt the "pimpl" idiom in the
  55. /// class definition (i.e., using a pointer to a \c MessageRendererImpl class,
  56. /// which is defined with the class implementation, not in the header file):
  57. /// we may want to modify the compression implementation without modifying the
  58. /// header file thereby requesting rebuild the package.
  59. ///
  60. /// Furthermore, we may eventually want to allow other developers to develop
  61. /// and use their own compression implementation. Should such a case become
  62. /// realistic, we may want to make the \c MessageRendererImpl class an abstract
  63. /// base class and let concrete derived classes have their own implementations.
  64. /// At the moment we don't the strong need for it, so we rather avoid over
  65. /// abstraction and keep the definition simpler.
  66. class MessageRenderer {
  67. public:
  68. ///
  69. /// \name Constructors and Destructor
  70. //@{
  71. /// \brief Constructor from an output buffer.
  72. ///
  73. /// \param buffer An \c OutputBuffer object to which wire format data is
  74. /// written.
  75. MessageRenderer(OutputBuffer& buffer);
  76. /// \brief The destructor.
  77. ///
  78. /// The destructor does nothing on the given \c buffer on construction;
  79. /// in fact, it is expected that the user will use the resulting buffer
  80. /// for some post rendering purposes (e.g., send the data to the network).
  81. /// It's user's responsibility to do any necessary cleanup for the
  82. /// \c buffer.
  83. ~MessageRenderer();
  84. //@}
  85. ///
  86. /// \name Getter Methods
  87. ///
  88. //@{
  89. /// \brief Return a pointer to the head of the data stored in the internal
  90. /// buffer.
  91. ///
  92. /// This method works exactly same as the same method of the \c OutputBuffer
  93. /// class; all notes for \c OutputBuffer apply.
  94. const void* getData() const;
  95. /// \brief Return the length of data written in the internal buffer.
  96. size_t getLength() const;
  97. /// \brief Return whether truncation has occurred while rendering.
  98. ///
  99. /// Once the return value of this method is \c true, it doesn't make sense
  100. /// to try rendering more data, although this class itself doesn't reject
  101. /// the attempt.
  102. ///
  103. /// This method never throws an exception.
  104. ///
  105. /// \return true if truncation has occurred; otherwise \c false.
  106. bool isTruncated() const;
  107. /// \brief Return the maximum length of rendered data that can fit in the
  108. /// corresponding DNS message without truncation.
  109. ///
  110. /// This method never throws an exception.
  111. ///
  112. /// \return The maximum length in bytes.
  113. size_t getLengthLimit() const;
  114. //@}
  115. ///
  116. /// \name Setter Methods
  117. ///
  118. //@{
  119. /// \brief Mark the renderer to indicate truncation has occurred while
  120. /// rendering.
  121. ///
  122. /// This method never throws an exception.
  123. void setTruncated();
  124. /// \brief Set the maximum length of rendered data that can fit in the
  125. /// corresponding DNS message without truncation.
  126. ///
  127. /// This method never throws an exception.
  128. ///
  129. /// \param len The maximum length in bytes.
  130. void setLengthLimit(size_t len);
  131. //@}
  132. ///
  133. /// \name Methods for writing data into the internal buffer.
  134. ///
  135. //@{
  136. /// \brief Insert a specified length of gap at the end of the buffer.
  137. ///
  138. /// The caller should not assume any particular value to be inserted.
  139. /// This method is provided as a shortcut to make a hole in the buffer
  140. /// that is to be filled in later, e.g, by \ref writeUint16At().
  141. ///
  142. /// \param len The length of the gap to be inserted in bytes.
  143. void skip(size_t len);
  144. /// \brief Trim the specified length of data from the end of the internal
  145. /// buffer.
  146. ///
  147. /// This method is provided for such cases as DNS message truncation.
  148. ///
  149. /// The specified length must not exceed the current data size of the
  150. /// buffer; otherwise an exception of class \c isc::OutOfRange will
  151. /// be thrown.
  152. ///
  153. /// \param len The length of data that should be trimmed.
  154. void trim(size_t len);
  155. /// \brief Clear the internal buffer and other internal resources.
  156. ///
  157. /// This method can be used to re-initialize and reuse the renderer
  158. /// without constructing a new one.
  159. void clear();
  160. /// \brief Write an unsigned 8-bit integer into the internal buffer.
  161. ///
  162. /// \param data The 8-bit integer to be written into the internal buffer.
  163. void writeUint8(uint8_t data);
  164. /// \brief Write an unsigned 16-bit integer in host byte order into the
  165. /// internal buffer in network byte order.
  166. ///
  167. /// \param data The 16-bit integer to be written into the buffer.
  168. void writeUint16(uint16_t data);
  169. /// \brief Write an unsigned 16-bit integer in host byte order at the
  170. /// specified position of the internal buffer in network byte order.
  171. ///
  172. /// The buffer must have a sufficient room to store the given data at the
  173. /// given position, that is, <code>pos + 2 < getLength()</code>;
  174. /// otherwise an exception of class \c isc::dns::InvalidBufferPosition will
  175. /// be thrown.
  176. /// Note also that this method never extends the internal buffer.
  177. ///
  178. /// \param data The 16-bit integer to be written into the internal buffer.
  179. /// \param pos The beginning position in the buffer to write the data.
  180. void writeUint16At(uint16_t data, size_t pos);
  181. /// \brief Write an unsigned 32-bit integer in host byte order into the
  182. /// internal buffer in network byte order.
  183. ///
  184. /// \param data The 32-bit integer to be written into the buffer.
  185. void writeUint32(uint32_t data);
  186. /// \brief Copy an arbitrary length of data into the internal buffer
  187. /// of the \c MessageRenderer.
  188. ///
  189. /// No conversion on the copied data is performed.
  190. ///
  191. /// \param data A pointer to the data to be copied into the internal buffer.
  192. /// \param len The length of the data in bytes.
  193. void writeData(const void *data, size_t len);
  194. //@}
  195. ///
  196. /// \name Rendering Methods
  197. ///
  198. //@{
  199. /// \brief Write a \c Name object into the internal buffer in wire format,
  200. /// with or without name compression.
  201. ///
  202. /// If the optional parameter \c compress is \c true, this method tries to
  203. /// compress the \c name if possible, searching the entire message that has
  204. /// been rendered. Otherwise name compression is omitted. Its default
  205. /// value is \c true.
  206. ///
  207. /// Note: even if \c compress is \c true, the position of the \c name (and
  208. /// possibly its ancestor names) in the message is recorded and may be used
  209. /// for compressing subsequent names.
  210. ///
  211. /// \param name A \c Name object to be written.
  212. /// \param compress A boolean indicating whether to enable name compression.
  213. void writeName(const Name& name, bool compress = true);
  214. private:
  215. MessageRendererImpl* impl_;
  216. };
  217. }
  218. }
  219. #endif // __MESSAGERENDERER_H
  220. // Local Variables:
  221. // mode: c++
  222. // End: