rdata.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "name.h"
  20. namespace isc {
  21. namespace dns {
  22. class InputBuffer;
  23. class OutputBuffer;
  24. class MessageRenderer;
  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::dns::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::dns::Exception(file, line, what) {}
  46. };
  47. ///
  48. /// \brief A standard DNS module exception that is thrown if ...TBD
  49. ///
  50. class CharStringTooLong : public Exception {
  51. public:
  52. CharStringTooLong(const char* file, size_t line, const char* what) :
  53. isc::dns::Exception(file, line, what) {}
  54. };
  55. class Rdata;
  56. typedef boost::shared_ptr<Rdata> RdataPtr;
  57. /// Abstract RDATA class
  58. class Rdata {
  59. protected:
  60. Rdata() {}
  61. private:
  62. /// Copy constructor is intentionally private. Concrete classes should
  63. /// generally specialize their own versions of copy constructor.
  64. Rdata(const Rdata& source);
  65. void operator=(const Rdata& source);
  66. public:
  67. virtual ~Rdata() {};
  68. ///
  69. /// \name Getter Methods
  70. //
  71. // need generic method for getting n-th field? c.f. ldns
  72. // e.g. string getField(int n);
  73. ///
  74. //@{
  75. // It's not yet clear if we really need to contain the RR type (and/or
  76. // RR class) in RDATA.
  77. //virtual const RRType& getType() const = 0;
  78. //@}
  79. ///
  80. /// \name Converter methods
  81. ///
  82. //@{
  83. virtual std::string toText() const = 0;
  84. virtual void toWire(OutputBuffer& buffer) const = 0;
  85. virtual void toWire(MessageRenderer& renderer) const = 0;
  86. //@}
  87. /// Note about implementation choice: the current implementation relies on
  88. /// dynamic_cast to ensure that the \c other is the same concrete Rdata
  89. /// class as \c this object. Alternatively, we could first convert the
  90. /// data into wire-format and compare the pair as opaque data. This would
  91. /// be more polymorphic, but might involve significant overhead, especially
  92. /// for a large size of RDATA.
  93. virtual int compare(const Rdata& other) const = 0;
  94. // not yet:
  95. // methods specific derived classes: throw an exception by default
  96. //virtual Address& getAddress() = 0;
  97. //virtual Name& getName() = 0;
  98. // polymorphic copy constructor (XXX should revisit it)
  99. //virtual Rdata* copy() const = 0;
  100. };
  101. namespace generic {
  102. class Generic : public Rdata {
  103. public:
  104. explicit Generic(const std::string& rdata_string);
  105. explicit Generic(InputBuffer& buffer, size_t rdata_len);
  106. virtual std::string toText() const;
  107. virtual void toWire(OutputBuffer& buffer) const;
  108. virtual void toWire(MessageRenderer& renderer) const;
  109. ///
  110. /// Note: the comparison is RR type/class agnostic: this method doesn't
  111. /// check whether the two Rdata objects to compare are of the comparable
  112. /// RR type/class. The caller must ensure this condition.
  113. ///
  114. virtual int compare(const Rdata& other) const;
  115. private:
  116. std::vector<uint8_t> data_;
  117. };
  118. class NS : public Rdata {
  119. public:
  120. explicit NS(const std::string& namestr) : nsname_(namestr) {}
  121. explicit NS(InputBuffer& buffer, size_t rdata_len);
  122. NS(const NS& other);
  123. virtual std::string toText() const;
  124. virtual void toWire(OutputBuffer& buffer) const;
  125. virtual void toWire(MessageRenderer& buffer) const;
  126. virtual int compare(const Rdata& other) const;
  127. ///
  128. /// Specialized constructorx
  129. ///
  130. explicit NS(const Name& nsname) : nsname_(nsname) {}
  131. ///
  132. /// Specialized methods
  133. ///
  134. const Name& getNSName() const;
  135. private:
  136. Name nsname_;
  137. };
  138. class SOA : public Rdata {
  139. public:
  140. explicit SOA(const std::string& soastr);
  141. explicit SOA(InputBuffer& buffer, size_t rdata_len);
  142. explicit SOA(const Name& mname, const Name& rname, uint32_t serial,
  143. uint32_t refresh, uint32_t retry, uint32_t expire,
  144. uint32_t minimum);
  145. SOA(const SOA& other);
  146. virtual ~SOA() {}
  147. virtual std::string toText() const;
  148. virtual void toWire(OutputBuffer& buffer) const;
  149. virtual void toWire(MessageRenderer& buffer) const;
  150. virtual int compare(const Rdata& other) const;
  151. private:
  152. /// Note: this is a prototype version; we may reconsider
  153. /// this representation later.
  154. Name mname_;
  155. Name rname_;
  156. /// serial, refresh, retry, expire, minimum, stored in network byte order
  157. uint8_t numdata_[20];
  158. };
  159. class MX : public Rdata {
  160. public:
  161. explicit MX(const std::string& mxstr);
  162. explicit MX(InputBuffer& buffer, size_t rdata_len);
  163. explicit MX(uint16_t preference, const Name& mxname);
  164. MX(const MX& other);
  165. virtual std::string toText() const;
  166. virtual void toWire(OutputBuffer& buffer) const;
  167. virtual void toWire(MessageRenderer& buffer) const;
  168. virtual int compare(const Rdata& other) const;
  169. private:
  170. /// Note: this is a prototype version; we may reconsider
  171. /// this representation later.
  172. uint16_t preference_;
  173. Name mxname_;
  174. };
  175. class TXT : public Rdata {
  176. public:
  177. explicit TXT(const std::string& txtstr);
  178. explicit TXT(InputBuffer& buffer, size_t rdata_len);
  179. TXT(const TXT& other);
  180. virtual std::string toText() const;
  181. virtual void toWire(OutputBuffer& buffer) const;
  182. virtual void toWire(MessageRenderer& buffer) const;
  183. virtual int compare(const Rdata& other) const;
  184. private:
  185. /// Note: this is a prototype version; we may reconsider
  186. /// this representation later.
  187. static const unsigned int MAX_CHARSTRING_LEN = 255;
  188. std::vector<std::vector<uint8_t> > string_list_;
  189. };
  190. } // end of namespace "generic"
  191. namespace in {
  192. class A : public Rdata {
  193. public:
  194. /// \brief Constructor from a textual IPv4 address.
  195. explicit A(const std::string& addrstr);
  196. explicit A(InputBuffer& buffer, size_t rdata_len);
  197. A(const A& ohter);
  198. virtual std::string toText() const;
  199. virtual void toWire(OutputBuffer& buffer) const;
  200. virtual void toWire(MessageRenderer& renderer) const;
  201. virtual int compare(const Rdata& other) const;
  202. //We can use the default destructor.
  203. //virtual ~A() {}
  204. // notyet:
  205. //const struct in_addr& getAddress() const { return (addr_); }
  206. private:
  207. uint32_t addr_; // raw IPv4 address (network byte order)
  208. };
  209. class AAAA : public Rdata {
  210. public:
  211. /// \brief Constructor from a textual IPv6 address.
  212. explicit AAAA(const std::string& addrstr);
  213. explicit AAAA(InputBuffer& buffer, size_t rdata_len);
  214. AAAA(const AAAA& ohter);
  215. virtual std::string toText() const;
  216. virtual void toWire(OutputBuffer& buffer) const;
  217. virtual void toWire(MessageRenderer& renderer) const;
  218. virtual int compare(const Rdata& other) const;
  219. //We can use the default destructor.
  220. //virtual ~AAAA() {}
  221. // notyet:
  222. //const struct in6_addr& getAddress() const { return (addr_); }
  223. private:
  224. uint8_t addr_[16]; // raw IPv6 address (network byte order)
  225. };
  226. } // end of namespace "in"
  227. namespace ch {
  228. class A : public Rdata {
  229. public:
  230. explicit A(const std::string& addrstr);
  231. explicit A(InputBuffer& buffer, size_t rdata_len);
  232. A(const A& ohter);
  233. virtual std::string toText() const;
  234. virtual void toWire(OutputBuffer& buffer) const;
  235. virtual void toWire(MessageRenderer& renderer) const;
  236. virtual int compare(const Rdata& other) const;
  237. private:
  238. };
  239. } // end of namespace "ch"
  240. ///
  241. /// Non class-member functions related to Rdata
  242. ///
  243. /// TBD: document them
  244. RdataPtr createRdata(const RRType& rrtype, const RRClass& rrclass,
  245. const std::string& rdata_string);
  246. RdataPtr createRdata(const RRType& rrtype, const RRClass& rrclass,
  247. InputBuffer& buffer, size_t len);
  248. RdataPtr createRdata(const RRType& rrtype, const RRClass& rrclass,
  249. const Rdata& source);
  250. ///
  251. /// \brief Gives relative ordering of two names in terms of DNSSEC RDATA
  252. /// ordering.
  253. ///
  254. /// This method compares two names as defined in Sections 6.2 and 6.3 of
  255. /// RFC4034: Comparing two names in their canonical form
  256. /// (i.e., converting upper case ASCII characters to lower ones) and
  257. /// as a left-justified unsigned octet sequence. Note that the ordering is
  258. /// different from that for owner names. For example, "a.example" should be
  259. /// sorted before "example" as RDATA, but the ordering is the opposite when
  260. /// compared as owner names.
  261. ///
  262. /// Normally, applications would not need this function directly.
  263. /// This is mostly an internal helper function for \c Rdata related classes
  264. /// to implement their \c compare() method.
  265. /// This function is publicly open, however, for the convenience of
  266. /// external developers who want to implement new or experimental RR types.
  267. ///
  268. /// Additional note about applicability: In fact, BIND9's similar function,
  269. /// \c dns_name_rdatacompare(), is only used in rdata implementations and
  270. /// for testing purposes.
  271. ///
  272. /// \param n1,n2 \c Name class objects to compare.
  273. /// \return -1 if \c n1 would be sorted before \c n2.
  274. /// \return 0 if \c n1 is identical to \c n2 in terms of sorting order.
  275. /// \return 1 if \c n1 would be sorted after \c n2.
  276. ///
  277. int
  278. compareNames(const Name& n1, const Name& n2);
  279. } // end of namespace "rdata"
  280. }
  281. }
  282. #endif // __RDATA_H
  283. // Local Variables:
  284. // mode: c++
  285. // End: