messagerenderer.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 TBD
  98. bool isTruncated() const;
  99. /// \brief TBD
  100. size_t getLengthLimit() const;
  101. //@}
  102. ///
  103. /// \name Setter Methods
  104. ///
  105. //@{
  106. /// \brief TBD
  107. void setLengthLimit(size_t len);
  108. /// \brief TBD
  109. void setTruncated();
  110. //@}
  111. ///
  112. /// \name Methods for writing data into the internal buffer.
  113. ///
  114. //@{
  115. /// \brief Insert a specified length of gap at the end of the buffer.
  116. ///
  117. /// The caller should not assume any particular value to be inserted.
  118. /// This method is provided as a shortcut to make a hole in the buffer
  119. /// that is to be filled in later, e.g, by \ref writeUint16At().
  120. ///
  121. /// \param len The length of the gap to be inserted in bytes.
  122. void skip(size_t len);
  123. /// \brief TBD
  124. void trim(size_t len);
  125. /// \brief Clear the internal buffer and other internal resources.
  126. ///
  127. /// This method can be used to re-initialize and reuse the renderer
  128. /// without constructing a new one.
  129. void clear();
  130. /// \brief Write an unsigned 8-bit integer into the internal buffer.
  131. ///
  132. /// \param data The 8-bit integer to be written into the internal buffer.
  133. void writeUint8(uint8_t data);
  134. /// \brief Write an unsigned 16-bit integer in host byte order into the
  135. /// internal buffer in network byte order.
  136. ///
  137. /// \param data The 16-bit integer to be written into the buffer.
  138. void writeUint16(uint16_t data);
  139. /// \brief Write an unsigned 16-bit integer in host byte order at the
  140. /// specified position of the internal buffer in network byte order.
  141. ///
  142. /// The buffer must have a sufficient room to store the given data at the
  143. /// given position, that is, <code>pos + 2 < getLength()</code>;
  144. /// otherwise an exception of class \c isc::dns::InvalidBufferPosition will
  145. /// be thrown.
  146. /// Note also that this method never extends the internal buffer.
  147. ///
  148. /// \param data The 16-bit integer to be written into the internal buffer.
  149. /// \param pos The beginning position in the buffer to write the data.
  150. void writeUint16At(uint16_t data, size_t pos);
  151. /// \brief Write an unsigned 32-bit integer in host byte order into the
  152. /// internal buffer in network byte order.
  153. ///
  154. /// \param data The 32-bit integer to be written into the buffer.
  155. void writeUint32(uint32_t data);
  156. /// \brief Copy an arbitrary length of data into the internal buffer
  157. /// of the \c MessageRenderer.
  158. ///
  159. /// No conversion on the copied data is performed.
  160. ///
  161. /// \param data A pointer to the data to be copied into the internal buffer.
  162. /// \param len The length of the data in bytes.
  163. void writeData(const void *data, size_t len);
  164. //@}
  165. ///
  166. /// \name Rendering Methods
  167. ///
  168. //@{
  169. /// \brief Write a \c Name object into the internal buffer in wire format,
  170. /// with or without name compression.
  171. ///
  172. /// If the optional parameter \c compress is \c true, this method tries to
  173. /// compress the \c name if possible, searching the entire message that has
  174. /// been rendered. Otherwise name compression is omitted. Its default
  175. /// value is \c true.
  176. ///
  177. /// Note: even if \c compress is \c true, the position of the \c name (and
  178. /// possibly its ancestor names) in the message is recorded and may be used
  179. /// for compressing subsequent names.
  180. ///
  181. /// \param name A \c Name object to be written.
  182. /// \param compress A boolean indicating whether to enable name compression.
  183. void writeName(const Name& name, bool compress = true);
  184. private:
  185. MessageRendererImpl* impl_;
  186. };
  187. }
  188. }
  189. #endif // __MESSAGERENDERER_H
  190. // Local Variables:
  191. // mode: c++
  192. // End: