messagerenderer.h 7.9 KB

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