rdata.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. // $Id$
  15. #ifndef __RDATA_H
  16. #define __RDATA_H 1
  17. #include <stdint.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <exceptions/exceptions.h>
  20. namespace isc {
  21. namespace dns {
  22. class InputBuffer;
  23. class OutputBuffer;
  24. class AbstractMessageRenderer;
  25. class RRType;
  26. class RRClass;
  27. class Name;
  28. namespace rdata {
  29. ///
  30. /// \brief A standard DNS module exception that is thrown if RDATA parser
  31. /// encounters an invalid or inconsistent data length.
  32. ///
  33. class InvalidRdataLength : public Exception {
  34. public:
  35. InvalidRdataLength(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) {}
  37. };
  38. ///
  39. /// \brief A standard DNS module exception that is thrown if RDATA parser
  40. /// fails to recognize a given textual representation.
  41. ///
  42. class InvalidRdataText : public Exception {
  43. public:
  44. InvalidRdataText(const char* file, size_t line, const char* what) :
  45. isc::Exception(file, line, what) {}
  46. };
  47. ///
  48. /// \brief A standard DNS module exception that is thrown if RDATA parser
  49. /// parser encounters a character-string (as defined in RFC1035) exceeding
  50. /// the maximum allowable length (\c MAX_CHARSTRING_LEN).
  51. ///
  52. class CharStringTooLong : public Exception {
  53. public:
  54. CharStringTooLong(const char* file, size_t line, const char* what) :
  55. isc::Exception(file, line, what) {}
  56. };
  57. // Forward declaration to define RdataPtr.
  58. class Rdata;
  59. ///
  60. /// The \c RdataPtr type is a pointer-like type, pointing to an
  61. /// object of some concrete derived class of \c Rdata.
  62. ///
  63. typedef boost::shared_ptr<Rdata> RdataPtr;
  64. typedef boost::shared_ptr<const Rdata> ConstRdataPtr;
  65. /// \brief Possible maximum length of RDATA, which is the maximum unsigned
  66. /// 16 bit value.
  67. const size_t MAX_RDLENGTH = 65535;
  68. /// \brief The maximum allowable length of character-string containing in
  69. /// RDATA as defined in RFC1035, not including the 1-byte length field.
  70. const unsigned int MAX_CHARSTRING_LEN = 255;
  71. /// \brief The \c Rdata class is an abstract base class that provides
  72. /// a set of common interfaces to manipulate concrete RDATA objects.
  73. ///
  74. /// Generally, a separate derived class directly inherited from the base
  75. /// \c Rdata class is defined for each well known RDATA.
  76. /// Each of such classes will define the common logic based on the
  77. /// corresponding protocol standard.
  78. ///
  79. /// Since some types of RRs are class specific and the corresponding RDATA
  80. /// may have different semantics (e.g. type A for class IN and type A for
  81. /// class CH have different representations and semantics), we separate
  82. /// \c Rdata derived classes for such RR types in different namespaces.
  83. /// The namespace of types specific to a class is named the lower-cased class
  84. /// name; for example, RDATA of class IN-specific types are defined in the
  85. /// \c in namespace, and RDATA of class CH-specific types are defined in
  86. /// the \c ch namespace, and so on.
  87. /// The derived classes are named using the RR type name (upper cased) such as
  88. /// \c A or \c AAAA.
  89. /// Thus RDATA of type A RR for class IN and CH are defined as \c in::A and
  90. /// \c ch::A, respectively.
  91. /// Many other RR types are class independent; the derived \c Rdata classes
  92. /// for such RR types are defined in the \c generic namespace. Examples are
  93. /// \c generic::NS and \c generic::SOA.
  94. ///
  95. /// If applications need to refer to these derived classes, it is generally
  96. /// recommended to prepend at least some part of the namespace because the
  97. /// same class name can be used in different namespaces.
  98. /// So, instead of doing
  99. /// \code using namespace isc::dns::rdata::in;
  100. /// A& rdata_type_a; \endcode
  101. /// it is advisable to prepend at least \c in from the namespace:
  102. /// \code using namespace isc::dns::rdata;
  103. /// in::A& rdata_type_a; \endcode
  104. ///
  105. /// In many cases, however, an application doesn't have to care about such
  106. /// derived classes.
  107. /// For instance, to parse an incoming DNS message an application wouldn't
  108. /// have to perform type specific operation unless the application is
  109. /// specifically concerned about a particular type.
  110. /// So, this API generally handles \c Rdata in a polymorphic way through
  111. /// a pointer or reference to this base abstract class.
  112. class Rdata {
  113. ///
  114. /// \name Constructors and Destructor
  115. ///
  116. /// Note: The copy constructor and the assignment operator are intentionally
  117. /// defined as private. Concrete classes should generally specialize their
  118. /// own versions of these methods.
  119. //@{
  120. protected:
  121. /// The default constructor.
  122. ///
  123. /// This is intentionally defined as \c protected as this base class should
  124. /// never be instantiated (except as part of a derived class). In many
  125. /// cases, the derived class wouldn't define a public default constructor
  126. /// either, because an \c Rdata object without concrete data isn't
  127. /// meaningful.
  128. Rdata() {}
  129. private:
  130. Rdata(const Rdata& source);
  131. void operator=(const Rdata& source);
  132. public:
  133. /// The destructor.
  134. virtual ~Rdata() {};
  135. //@}
  136. ///
  137. /// \name Converter methods
  138. ///
  139. //@{
  140. /// \brief Convert an \c Rdata to a string.
  141. ///
  142. /// This method returns a \c std::string object representing the \c Rdata.
  143. ///
  144. /// This is a pure virtual method without the definition; the actual
  145. /// representation is specific to each derived concrete class and
  146. /// should be explicitly defined in the derived class.
  147. ///
  148. /// \return A string representation of \c Rdata.
  149. virtual std::string toText() const = 0;
  150. /// \brief Render the \c Rdata in the wire format into a buffer.
  151. ///
  152. /// This is a pure virtual method without the definition; the actual
  153. /// conversion is specific to each derived concrete class and
  154. /// should be explicitly defined in the derived class.
  155. ///
  156. /// \param buffer An output buffer to store the wire data.
  157. virtual void toWire(OutputBuffer& buffer) const = 0;
  158. /// \brief Render the \c Rdata in the wire format into a
  159. /// \c MessageRenderer object.
  160. ///
  161. /// This is a pure virtual method without the definition; the actual
  162. /// conversion is specific to each derived concrete class and
  163. /// should be explicitly defined in the derived class.
  164. ///
  165. /// \param renderer DNS message rendering context that encapsulates the
  166. /// output buffer in which the \c Rdata is to be stored.
  167. virtual void toWire(AbstractMessageRenderer& renderer) const = 0;
  168. //@}
  169. ///
  170. /// \name Comparison method
  171. ///
  172. //@{
  173. /// \brief Compare two instances of \c Rdata.
  174. ///
  175. /// This method compares \c this and the \c other Rdata objects
  176. /// in terms of the DNSSEC sorting order as defined in RFC4034, and returns
  177. /// the result as an integer.
  178. ///
  179. /// This is a pure virtual method without the definition; the actual
  180. /// comparison logic is specific to each derived concrete class and
  181. /// should be explicitly defined in the derived class.
  182. ///
  183. /// Specific implementations of this method must confirm that \c this
  184. /// and the \c other are objects of the same concrete derived class of
  185. /// \c Rdata. This is normally done by \c dynamic_cast in the
  186. /// implementation. It also means if the assumption isn't met
  187. /// an exception of class \c std::bad_cast will be thrown.
  188. ///
  189. /// Here is an implementation choice: instead of relying on
  190. /// \c dynamic_cast, we could first convert the data into wire-format
  191. /// and compare the pair as opaque data. This would be more polymorphic,
  192. /// but might involve significant overhead, especially for a large size
  193. /// of RDATA.
  194. ///
  195. /// \param other the right-hand operand to compare against.
  196. /// \return < 0 if \c this would be sorted before \c other.
  197. /// \return 0 if \c this is identical to \c other in terms of sorting order.
  198. /// \return > 0 if \c this would be sorted after \c other.
  199. virtual int compare(const Rdata& other) const = 0;
  200. //@}
  201. };
  202. namespace generic {
  203. /// \brief The \c GenericImpl class is the actual implementation of the
  204. /// \c generic::Generic class.
  205. ///
  206. /// The implementation is hidden from applications. This approach requires
  207. /// dynamic memory allocation on construction, copy, or assignment, but
  208. /// we believe it should be acceptable as "unknown" RDATA should be pretty
  209. /// rare.
  210. struct GenericImpl;
  211. /// \brief The \c generic::Generic class represents generic "unknown" RDATA.
  212. ///
  213. /// This class is used as a placeholder for all non well-known type of RDATA.
  214. /// By definition, the stored data is regarded as opaque binary without
  215. /// assuming any structure.
  216. class Generic : public Rdata {
  217. public:
  218. ///
  219. /// \name Constructors, Assignment Operator and Destructor.
  220. ///
  221. //@{
  222. /// \brief Constructor from a string.
  223. ///
  224. /// This method constructs a \c generic::Generic object from a textual
  225. /// representation as defined in RFC3597.
  226. ///
  227. /// If \c rdata_string isn't a valid textual representation of this type
  228. /// of RDATA, an exception of class \c InvalidRdataText or
  229. /// \c InvalidRdataLength will be thrown.
  230. /// If resource allocation to store the data fails, a corresponding standard
  231. /// exception will be thrown.
  232. ///
  233. /// \param rdata_string A string of textual representation of generic
  234. /// RDATA.
  235. explicit Generic(const std::string& rdata_string);
  236. ///
  237. /// \brief Constructor from wire-format data.
  238. ///
  239. /// The \c buffer parameter normally stores a complete DNS message
  240. /// containing the generic RDATA to be constructed.
  241. /// The current read position of the buffer points to the head of the
  242. /// data.
  243. ///
  244. /// This method reads \c rdata_len bytes from the \c buffer, and internally
  245. /// stores the data as an opaque byte sequence.
  246. ///
  247. /// \c rdata_len must not exceed \c MAX_RDLENGTH; otherwise, an exception
  248. /// of class \c InvalidRdataLength will be thrown.
  249. /// If resource allocation to hold the data fails, a corresponding standard
  250. /// exception will be thrown; if the \c buffer doesn't contain \c rdata_len
  251. /// bytes of unread data, an exception of class \c InvalidBufferPosition
  252. /// will be thrown.
  253. ///
  254. /// \param buffer A reference to an \c InputBuffer object storing the
  255. /// \c Rdata to parse.
  256. /// \param rdata_len The length in buffer of the \c Rdata. In bytes.
  257. Generic(InputBuffer& buffer, size_t rdata_len);
  258. ///
  259. /// \brief The destructor.
  260. virtual ~Generic();
  261. ///
  262. /// \brief The copy constructor.
  263. ///
  264. /// If resource allocation to copy the data fails, a corresponding standard
  265. /// exception will be thrown.
  266. ///
  267. /// \param source A reference to a \c generic::Generic object to copy from.
  268. Generic(const Generic& source);
  269. ///
  270. /// \brief The assignment operator.
  271. ///
  272. /// If resource allocation to copy the data fails, a corresponding standard
  273. /// exception will be thrown.
  274. ///
  275. /// \param source A reference to a \c generic::Generic object to copy from.
  276. Generic& operator=(const Generic& source);
  277. //@}
  278. ///
  279. /// \name Converter methods
  280. ///
  281. //@{
  282. /// \brief Convert an \c generic::Generic object to a string.
  283. ///
  284. /// This method converts a generic "unknown" RDATA object into a textual
  285. /// representation of such unknown data as defined in RFC3597.
  286. ///
  287. /// If resource allocation to copy the data fails, a corresponding standard
  288. /// exception will be thrown.
  289. ///
  290. /// \return A string representation of \c generic::Generic.
  291. virtual std::string toText() const;
  292. ///
  293. /// \brief Render the \c generic::Generic in the wire format into a buffer.
  294. ///
  295. /// This will require \c rdata_len bytes of remaining capacity in the
  296. /// \c buffer. If this is not the case and resource allocation for the
  297. /// necessary memory space fails, a corresponding standard exception will
  298. /// be thrown.
  299. ///
  300. /// \param buffer An output buffer to store the wire data.
  301. virtual void toWire(OutputBuffer& buffer) const;
  302. /// \brief Render the \c generic::Generic in the wire format into a
  303. /// \c MessageRenderer object.
  304. ///
  305. /// This will require \c rdata_len bytes of remaining capacity in the
  306. /// \c buffer. If this is not the case and resource allocation for the
  307. /// necessary memory space fails, a corresponding standard exception will
  308. /// be thrown.
  309. ///
  310. /// \param renderer DNS message rendering context that encapsulates the
  311. /// output buffer in which the \c Generic object is to be stored.
  312. virtual void toWire(AbstractMessageRenderer& renderer) const;
  313. //@}
  314. ///
  315. /// \name Comparison method
  316. ///
  317. //@{
  318. /// \brief Compare two instances of \c generic::Generic objects.
  319. ///
  320. /// As defined in RFC4034, this method simply compares the wire-format
  321. /// representations of the two objects as left-justified unsigned octet
  322. /// sequences.
  323. ///
  324. /// The object referenced by \c other must have been instantiated as
  325. /// a c generic::Generic class object; otherwise, an exception of class
  326. /// \c std::bad_cast will be thrown.
  327. /// Note that the comparison is RR type/class agnostic: this method doesn't
  328. /// check whether the two \c Rdata objects to compare are of the comparable
  329. /// RR type/class. For example, \c this object may come from an \c RRset
  330. /// of \c RRType x, and the \c other may come from a different \c RRset
  331. /// of \c RRType y (where x != y). This situation would be considered a
  332. /// bug, but this method cannot detect this type of error.
  333. /// The caller must ensure this condition.
  334. ///
  335. /// \param other the right-hand operand to compare against.
  336. /// \return < 0 if \c this would be sorted before \c other.
  337. /// \return 0 if \c this is identical to \c other in terms of sorting order.
  338. /// \return > 0 if \c this would be sorted after \c other.
  339. virtual int compare(const Rdata& other) const;
  340. //@}
  341. private:
  342. GenericImpl* impl_;
  343. };
  344. ///
  345. /// \brief Insert the name as a string into stream.
  346. ///
  347. /// This method convert the \c rdata into a string and inserts it into the
  348. /// output stream \c os.
  349. ///
  350. /// This function overloads the global \c operator<< to behave as described in
  351. /// \c ostream::operator<< but applied to \c generic::Generic Rdata objects.
  352. ///
  353. /// \param os A \c std::ostream object on which the insertion operation is
  354. /// performed.
  355. /// \param rdata The \c Generic object output by the operation.
  356. /// \return A reference to the same \c std::ostream object referenced by
  357. /// parameter \c os after the insertion operation.
  358. std::ostream& operator<<(std::ostream& os, const Generic& rdata);
  359. } // end of namespace "generic"
  360. //
  361. // Non class-member functions related to Rdata
  362. //
  363. ///
  364. /// \name Parameterized Polymorphic RDATA Factories
  365. ///
  366. /// This set of global functions provide a unified interface to create an
  367. /// \c Rdata object in a parameterized polymorphic way,
  368. /// that is, these functions take a pair of \c RRType and \c RRClass
  369. /// objects and data specific to that pair, and create an object of
  370. /// the corresponding concrete derived class of \c Rdata.
  371. ///
  372. /// These will be useful when parsing/constructing a DNS message or
  373. /// parsing a master file, where information for a specific type of RDATA
  374. /// is given but the resulting object, once created, should better be used
  375. /// in a polymorphic way.
  376. ///
  377. /// For example, if a master file parser encounters an NS RR
  378. /// \verbatim example.com. 3600 IN NS ns.example.com.\endverbatim
  379. /// it stores the text fragments "IN", "NS", and "ns.example.com." in
  380. /// \c std::string objects \c class_txt, \c type_txt, and \c nsname_txt,
  381. /// respectively, then it would create a new \c RdataPtr object as follows:
  382. /// \code RdataPtr rdata = createRdata(RRType(type_txt), RRClass(class_txt),
  383. /// nsname_txt); \endcode
  384. /// On success, \c rdata will point to an object of the \c generic::NS class
  385. /// that internally holds a domain name of "ns.example.com."
  386. ///
  387. /// Internally, these functions uses the corresponding
  388. /// \c RRParamRegistry::createRdata methods of the \c RRParamRegistry.
  389. /// See also the description on these methods for related notes.
  390. //@{
  391. /// \brief Create RDATA of a given pair of RR type and class from a string.
  392. ///
  393. /// This method creates from a string an \c Rdata object of the given pair
  394. /// of RR type and class.
  395. ///
  396. /// \param rrtype An \c RRType object specifying the type/class pair.
  397. /// \param rrclass An \c RRClass object specifying the type/class pair.
  398. /// \param rdata_string A string of textual representation of the \c Rdata.
  399. /// \return An \c RdataPtr object pointing to the created \c Rdata
  400. /// object.
  401. RdataPtr createRdata(const RRType& rrtype, const RRClass& rrclass,
  402. const std::string& rdata_string);
  403. /// \brief Create RDATA of a given pair of RR type and class from
  404. /// wire-format data.
  405. ///
  406. /// This method creates from wire-format binary data an \c Rdata object
  407. /// of the given pair of RR type and class.
  408. ///
  409. /// \c len must not exceed the protocol defined maximum value, \c MAX_RDLENGTH;
  410. /// otherwise, an exception of class \c InvalidRdataLength will be thrown.
  411. ///
  412. /// In some cases, the length of the RDATA is determined without the
  413. /// information of \c len. For example, the RDATA length of an IN/A RR
  414. /// must always be 4. If \c len is not equal to the actual length in such
  415. /// cases, an exception of class InvalidRdataLength will be thrown.
  416. ///
  417. /// \param rrtype An \c RRType object specifying the type/class pair.
  418. /// \param rrclass An \c RRClass object specifying the type/class pair.
  419. /// \param buffer A reference to an \c InputBuffer object storing the
  420. /// \c Rdata to parse.
  421. /// \param len The length in buffer of the \c Rdata. In bytes.
  422. /// \return An \c RdataPtr object pointing to the created \c Rdata
  423. /// object.
  424. RdataPtr createRdata(const RRType& rrtype, const RRClass& rrclass,
  425. InputBuffer& buffer, size_t len);
  426. /// \brief Create RDATA of a given pair of RR type and class, copying
  427. /// of another RDATA of same kind.
  428. ///
  429. /// This method creates an \c Rdata object of the given pair of
  430. /// RR type and class, copying the content of the given \c Rdata,
  431. /// \c source.
  432. ///
  433. /// \param rrtype An \c RRType object specifying the type/class pair.
  434. /// \param rrclass An \c RRClass object specifying the type/class pair.
  435. /// \param source A reference to an \c Rdata object whose content
  436. /// is to be copied to the created \c Rdata object.
  437. /// \return An \c RdataPtr object pointing to the created
  438. /// \c Rdata object.
  439. RdataPtr createRdata(const RRType& rrtype, const RRClass& rrclass,
  440. const Rdata& source);
  441. //@}
  442. ///
  443. /// \brief Gives relative ordering of two names in terms of DNSSEC RDATA
  444. /// ordering.
  445. ///
  446. /// This method compares two names as defined in Sections 6.2 and 6.3 of
  447. /// RFC4034: Comparing two names in their canonical form
  448. /// (i.e., converting upper case ASCII characters to lower ones) and
  449. /// as a left-justified unsigned octet sequence. Note that the ordering is
  450. /// different from that for owner names. For example, "a.example" should be
  451. /// sorted before "example" as RDATA, but the ordering is the opposite when
  452. /// compared as owner names.
  453. ///
  454. /// Normally, applications would not need this function directly.
  455. /// This is mostly an internal helper function for \c Rdata related classes
  456. /// to implement their \c compare() method.
  457. /// This function is publicly open, however, for the convenience of
  458. /// external developers who want to implement new or experimental RR types.
  459. ///
  460. /// This function never throws an exception as long as the given names are
  461. /// valid \c Name objects.
  462. ///
  463. /// Additional note about applicability: In fact, BIND9's similar function,
  464. /// \c dns_name_rdatacompare(), is only used in rdata implementations and
  465. /// for testing purposes.
  466. ///
  467. /// \param n1,n2 \c Name class objects to compare.
  468. /// \return -1 if \c n1 would be sorted before \c n2.
  469. /// \return 0 if \c n1 is identical to \c n2 in terms of sorting order.
  470. /// \return 1 if \c n1 would be sorted after \c n2.
  471. ///
  472. int compareNames(const Name& n1, const Name& n2);
  473. } // end of namespace "rdata"
  474. }
  475. }
  476. #endif // __RDATA_H
  477. // Local Variables:
  478. // mode: c++
  479. // End: