rdatafields.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright (C) 2010 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 __RDATAFIELDS_H
  15. #define __RDATAFIELDS_H 1
  16. #include <stdint.h>
  17. #include <cstddef>
  18. namespace isc {
  19. namespace dns {
  20. class OutputBuffer;
  21. class AbstractMessageRenderer;
  22. namespace rdata {
  23. class Rdata;
  24. /// A low-level, RR type-independent representation of DNS RDATA.
  25. ///
  26. /// <b>Purpose of the Class</b>
  27. ///
  28. /// This class intends to help "serialization" of the content of RDATA
  29. /// in a space-efficient manner. Specific derived classes of \c Rdata
  30. /// focus on the convenience of accessing RDATA fields for RR type-specific
  31. /// protocol operations, and can be inefficient in terms of space.
  32. /// For example, a DNS character string may be internally represented as a
  33. /// \c std::string object with all of the overhead of the richer class.
  34. /// If an application needs to maintain a very large number of RRs and it
  35. /// does not have to perform RR specific operation so often, it may make more
  36. /// sense to store the data in memory in a lower-level but space efficient
  37. /// form.
  38. ///
  39. /// Another purpose of this class is to improve rendering performance for
  40. /// RDATA. If the only requirement were space efficiency, it would be just
  41. /// sufficient to convert the \c RDATA into a binary sequence in the wire
  42. /// format. However, to render the data in a DNS message, we'd have to
  43. /// re-construct a corresponding \c Rdata object in the case where name
  44. /// compression is necessary. This is not desirable, and this class is
  45. /// provided to avoid such unnecessary overhead.
  46. ///
  47. /// <b>Data Format</b>
  48. ///
  49. /// To meet these goals, this class helps convert an \c Rdata object into
  50. /// two pieces of information: Wire-format representation of the \c Rdata
  51. /// and associated meta information for efficient rendering.
  52. ///
  53. /// Specifically, it maintains the wire-format data as a sequence of typed
  54. /// fields. The types are:
  55. /// - Compressible name: a domain name as an RDATA field that can be compressed
  56. /// - Incompressible name: a domain name as an RDATA field that cannot be
  57. /// compressed
  58. /// - Other data: any other fields of RDATA, which should be treated as opaque
  59. ///
  60. /// (See also the description of \c RdataFields::Type)
  61. /// Whether a name can or cannot be compressed is determined according to
  62. /// RFC3597.
  63. ///
  64. /// A "other data" field may not always correspond to a single RDATA field.
  65. /// A \c RdataFields field (of other data) is just a contiguous region of the
  66. /// wire-format data that does not involve name compression.
  67. /// For example, the SOA RDATA begins with two "compressible" names followed
  68. /// by 5 32-bit fields.
  69. /// In \c RdataFields the last 5 fields would be considered a single 20-byte
  70. /// field.
  71. ///
  72. /// Each \c RdataFields field is identified by the \c FieldSpec structure,
  73. /// which provides the type and length of the field.
  74. /// An \c RdataFields object internally maintains a sequence of \c FieldSpec
  75. /// objects in a form of plain C-style array, which can be referenced via
  76. /// a pointer returned by the \c getFieldSpecData() method.
  77. /// The \c \c FieldSpec for a specific field can also be retrieved by the
  78. /// \c getFieldSpec() method.
  79. ///
  80. /// The following diagram shows the internal memory representation of
  81. /// an SOA RDATA in the form of \c RdataFields object and how an application
  82. /// can get access to the memory region.
  83. /** \verbatim
  84. accessible via |0 getDataLength() bytes
  85. getData()----------> <MNAME><RNAME><Rest of the data>
  86. <---------- 3 * sizeof(FieldSpec) bytes ------------->
  87. getFieldSpecData()-> { compressible name { compressible name { other data
  88. len: MNAME-len } len: RNAME-len } len: 20 }
  89. \endverbatim
  90. */
  91. /// where MNAME and RNAME are wire format representations of the MNAME and
  92. /// RNAME fields of the SOA RDATA, respectively, and "Rest of the data"
  93. /// encodes the remaining 20 bytes of the RDATA in network byte order.
  94. ///
  95. /// <b>Usage of the Class</b>
  96. ///
  97. /// One major and common use case of the \c RdataFields class is to convert
  98. /// a \c Rdata object (possibly given from a DNS message or some configuration
  99. /// source such as a zone file) in the serialized format and store a copy of
  100. /// the data somewhere in memory. The following code sample implements this
  101. /// scenario:
  102. /// \code // assume "rdata" is a reference type to Rdata
  103. /// const RdataFields fields(rdata);
  104. /// const unsigned int nfields = fields.getFieldCount();
  105. /// memcpy(some_place, fields.getFieldSpecData(), nfields*sizeof(FieldSpec));
  106. /// const size_t data_length = fields.getDataLength();
  107. /// memcpy(other_place, fields.getData(), data_length);
  108. /// // (nfields and data_length should be stored somewhere, too)
  109. /// \endcode
  110. ///
  111. /// Another typical usage is to render the stored data in the wire format
  112. /// as efficiently as possible. The following code is an example of such
  113. /// usage:
  114. /// \code // assume "renderer" is of type MessageRenderer
  115. /// // retrieve data_length and nfields from the storage
  116. /// RdataFields(static_cast<const FieldSpec*>(some_place),
  117. /// nfields, other_place, data_length).toWire(renderer);
  118. /// \endcode
  119. ///
  120. /// <b>Notes to Users</b>
  121. ///
  122. /// The main purposes of this class is to help efficient operation
  123. /// for some (limited classes of) performance sensitive application.
  124. /// For this reason the interface and implementation rely on relatively
  125. /// lower-level, riskier primitives such as passing around bear pointers.
  126. ///
  127. /// It is therefore discouraged to use this class for general purpose
  128. /// applications that do not need to maximize performance in terms of either
  129. /// memory footprint or rendering speed.
  130. /// All functionality provided by this class can be achieved via higher level
  131. /// interfaces such as the \c Rdata class variants.
  132. /// Normal applications should use those interfaces.
  133. ///
  134. /// The data format is public information so that an application can examine
  135. /// and use selected parts of data. For example, an application may want to
  136. /// encode domain names in RDATA in a different way while storing the other
  137. /// data in a separate place.
  138. /// However, at this moment the format is still in flux, and it may not
  139. /// be compatible with future versions (see below).
  140. ///
  141. /// <b>Development Notes</b>
  142. ///
  143. /// We should conduct benchmark tests to measure rendering performance.
  144. ///
  145. /// The current implementation needs to re-construct name objects from
  146. /// compressible and incompressible name fields as wire-format data.
  147. /// This is not efficient, and we'll probably want to improve this in a
  148. /// future version. One possibility is to store offset information as well
  149. /// as the name data (at the cost of increasing memory footprint), and
  150. /// to use the pair of data for faster rendering.
  151. class RdataFields {
  152. public:
  153. /// Types of \c RdataFields fields.
  154. ///
  155. /// \c COMPRESSIBLE_NAME and \c INCOMPRESSIBLE_NAME represent a domain
  156. /// name used as a field of an RDATA that can and cannot be compressed
  157. /// per RFC3597.
  158. /// \c DATA means all other types of fields.
  159. enum Type {
  160. DATA, ///< Plain data.
  161. COMPRESSIBLE_NAME, ///< A domain name subject to name compression.
  162. INCOMPRESSIBLE_NAME ///< A domain name that shouldn't be compressed.
  163. };
  164. /// Structure that specifies a single \c RdataFields field.
  165. ///
  166. /// This is a straightforward pair of the type and length of a single
  167. /// \c RdataFields field.
  168. ///
  169. /// In some cases an application may want to do deeper inspection of
  170. /// some \c RdataFields field(s). For example, an application may want
  171. /// to construct a \c Name object for each domain name field of an RDATA
  172. /// and use it for some special purpose.
  173. /// The \c FieldSpec structure provides necessary parameters to get access
  174. /// to a specific \c RdataFields field.
  175. ///
  176. /// The following code snippet implements the above example scenario:
  177. /// \code // assume "fields" is of type RdataFields
  178. /// size_t offset = 0;
  179. /// for (int i = 0; i < fields.getFieldCount(); ++i) {
  180. /// const FieldSpec spec = fields.getFieldSpec(i);
  181. /// if (spec.type == RdataFields::COMPRESSIBLE_NAME ||
  182. /// spec.type == RdataFields::INCOMPRESSIBLE_NAME) {
  183. /// InputBuffer ibuffer(fields.getData() + offset, spec.len);
  184. /// Name name(ibuffer);
  185. /// // do something with name
  186. /// }
  187. /// offset += spec.len;
  188. /// } \endcode
  189. ///
  190. /// Note that the offset is not included in \c FieldSpec.
  191. /// This is because such deeper inspection would be a relatively rare
  192. /// operation while it is desirable to keep this structure as small as
  193. /// possible for the purpose of space efficiency.
  194. /// Also, if and when an application wants to look into a specific field,
  195. /// it would be quite likely that the application iterates over all fields
  196. /// and does something special for selected fields like the above example.
  197. /// In that case the application can easily and efficiently identify the
  198. /// necessary offset, again, as shown in the above code example.
  199. struct FieldSpec {
  200. FieldSpec(Type type_param, uint16_t len_param) :
  201. type(type_param), len(len_param)
  202. {}
  203. Type type; ///< The type of the field.
  204. uint16_t len; ///< The length of the field in bytes.
  205. };
  206. ///
  207. /// \name Constructors and Destructor.
  208. ///
  209. /// \b Note:
  210. /// The copy constructor and the assignment operator are intentionally
  211. /// defined as private, making this class non copyable.
  212. //@{
  213. private:
  214. RdataFields(const RdataFields& source);
  215. RdataFields& operator=(const RdataFields& source);
  216. public:
  217. /// Constructor from Rdata.
  218. ///
  219. /// This constructor converts the data of a given \c Rdata object into
  220. /// an \c RdataFields object so that the resulting data can be stored
  221. /// in memory in a space-efficient way.
  222. ///
  223. /// It makes a local copy of the original data and dynamically allocates
  224. /// necessary memory, so is not very efficient.
  225. /// The basic idea is to perform the expensive conversion once and keep
  226. /// using the result as long as possible to improve overall performance
  227. /// in a longer term.
  228. ///
  229. /// If the internal resource allocation fails, a corresponding standard
  230. /// exception will be thrown.
  231. /// The current implementation of this constructor internally calls
  232. /// the <code>Rdata::toWire(AbstractMessageRenderer&) const</code> method
  233. /// for the conversion.
  234. /// If that method throws an exception it will be propagated to the caller
  235. /// of this constructor.
  236. ///
  237. /// \param rdata The RDATA for which the \c RdataFields to be constructed.
  238. RdataFields(const Rdata& rdata);
  239. /// Constructor from field parameters.
  240. ///
  241. /// The intended usage of this version of constructor is to form a
  242. /// structured representation of \c RDATA encoded by the other
  243. /// constructor so that the resulting object can be used for subsequent
  244. /// operations such as rendering in the wire format.
  245. /// This version is intended to be efficient by not making any copy
  246. /// of variable length data or expensive data inspection.
  247. ///
  248. /// This constructor is basically exception free, except against bogus
  249. /// input parameters.
  250. /// Specifically, the parameters must meet the following conditions;
  251. /// otherwise an exception of class \c InvalidParameter will be thrown.
  252. /// - \c fields can be \c NULL if and only if \c nfields is 0
  253. /// - \c data can be \c NULL if and only if \c data_length is 0
  254. /// - the sum of the lengths of \c fields entries must be equal to
  255. /// \c data_length
  256. ///
  257. /// This constructor assumes that the memory region pointed by \c data (if
  258. /// non \c NULL) is encoded as a sequence of valid \c RdataFields fields,
  259. /// and does not perform deep inspection on each field.
  260. /// In particular, for fields of type \c COMPRESSIBLE_NAME or
  261. /// \c INCOMPRESSIBLE_NAME, this constructor assumes the corresponding
  262. /// memory region is a valid representation of domain name.
  263. /// Otherwise, a subsequent method call such as
  264. /// <code>toWire(AbstractMessageRenderer&) const</code>
  265. /// may trigger an unexpected exception.
  266. ///
  267. /// It is the caller's responsibility to ensure this assumption.
  268. /// In general, this constructor is expected to be used for serialized data
  269. /// generated by the other constructor from a valid \c Rdata.
  270. /// The result is not guaranteed if the data is generated in any other
  271. /// ways.
  272. ///
  273. /// The resulting \c RdataFields object does not maintain a copy of
  274. /// \c fields or \c data. It is the caller's responsibility to ensure
  275. /// the memory regions pointed to by these parameters are valid and intact
  276. /// as long as the \c RdataFields object is used.
  277. ///
  278. /// \param fields An array of \c FieldSpec entries. This can be \c NULL.
  279. /// \param nfields The number of entries of \c fields.
  280. /// \param data A pointer to memory region for the entire RDATA. This can
  281. /// be NULL.
  282. /// \param data_length The length of \c data in bytes.
  283. RdataFields(const FieldSpec* fields, const unsigned int nfields,
  284. const uint8_t* data, const size_t data_length);
  285. /// The destructor.
  286. ~RdataFields();
  287. //@}
  288. ///
  289. /// \name Getter Methods
  290. ///
  291. //@{
  292. /// \brief Return the length of the entire RDATA encoded in the
  293. /// \c RdataFields in bytes.
  294. ///
  295. /// This method never throws an exception.
  296. unsigned int getDataLength() const { return (data_length_); }
  297. /// \brief Return a pointer to the RDATA encoded in the \c RdataFields.
  298. ///
  299. /// This method never throws an exception.
  300. const uint8_t* getData() const { return (data_); }
  301. /// \brief Return the number of fields encoded in the RdataFields.
  302. ///
  303. /// This method never throws an exception.
  304. unsigned int getFieldCount() const { return (nfields_); }
  305. /// \brief Return a pointer to a sequence of \c FieldSpec for the
  306. /// \c RdataFields.
  307. ///
  308. /// This method never throws an exception.
  309. const void* getFieldSpecData() const { return (fields_); }
  310. /// \brief Return the specification of the field identified by the given
  311. /// index.
  312. ///
  313. /// \c field_id is the field index, which must be in the range of
  314. /// <code>[0, getFieldCount())</code>. 0 means the first field, and
  315. /// <code>getFieldCount()-1</code> means the last.
  316. ///
  317. /// If the given index is not in the valid range, an exception of class
  318. /// \c OutOfRange will be thrown.
  319. /// This method never throws an exception otherwise.
  320. ///
  321. /// \param field_id The index of an \c RdataFields field to be returned.
  322. /// \return A \c FieldSpec structure that contains the information of
  323. /// the \c field_id-th field.
  324. FieldSpec getFieldSpec(const unsigned int field_id) const;
  325. //@}
  326. ///
  327. /// \name Converter Methods
  328. ///
  329. //@{
  330. /// \brief Render the RdataFields in the wire format with name compression.
  331. ///
  332. /// This method may require resource allocation in \c renderer.
  333. /// If it fails, a corresponding standard exception will be thrown.
  334. /// It should not throw any other exception as long as the \c RdataFields
  335. /// object was constructed from valid parameters (see the description of
  336. /// constructors). The result is not guaranteed if it's constructed in
  337. /// any other ways.
  338. ///
  339. /// \param renderer DNS message rendering context that encapsulates the
  340. /// output buffer and name compression information.
  341. void toWire(AbstractMessageRenderer& renderer) const;
  342. /// \brief Render the RdataFields in the wire format without name
  343. /// compression.
  344. ///
  345. /// This method may require resource allocation in \c buffer.
  346. /// If it fails, a corresponding standard exception will be thrown.
  347. ///
  348. /// \param buffer An output buffer to store the wire data.
  349. void toWire(OutputBuffer& buffer) const;
  350. //@}
  351. private:
  352. const FieldSpec* fields_;
  353. unsigned int nfields_;
  354. const uint8_t* data_;
  355. size_t data_length_;
  356. // hide further details within the implementation
  357. struct RdataFieldsDetail;
  358. RdataFieldsDetail* detail_;
  359. };
  360. }
  361. }
  362. }
  363. #endif // __RDATAFIELDS_H
  364. // Local Variables:
  365. // mode: c++
  366. // End: