rrttl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 RRTTL_H
  15. #define RRTTL_H 1
  16. #include <exceptions/exceptions.h>
  17. #include <boost/optional.hpp>
  18. #include <stdint.h>
  19. namespace isc {
  20. namespace util {
  21. class InputBuffer;
  22. class OutputBuffer;
  23. }
  24. namespace dns {
  25. // forward declarations
  26. class AbstractMessageRenderer;
  27. class RRTTL; // forward declaration to define MaybeRRTTL
  28. /// \brief A shortcut for a compound type to represent RRTTL-or-not.
  29. ///
  30. /// A value of this type can be interpreted in a boolean context, whose
  31. /// value is \c true if and only if it contains a valid RRTTL object.
  32. /// And, if it contains a valid RRTTL object, its value is accessible
  33. /// using \c operator*, just like a bare pointer to \c RRTTL.
  34. typedef boost::optional<RRTTL> MaybeRRTTL;
  35. ///
  36. /// \brief A standard DNS module exception that is thrown if an RRTTL object
  37. /// is being constructed from an unrecognized string.
  38. ///
  39. class InvalidRRTTL : public Exception {
  40. public:
  41. InvalidRRTTL(const char* file, size_t line, const char* what) :
  42. isc::Exception(file, line, what) {}
  43. };
  44. ///
  45. /// \brief A standard DNS module exception that is thrown if an RRTTL object
  46. /// is being constructed from a incomplete (too short) wire-format data.
  47. ///
  48. class IncompleteRRTTL : public Exception {
  49. public:
  50. IncompleteRRTTL(const char* file, size_t line, const char* what) :
  51. isc::Exception(file, line, what) {}
  52. };
  53. ///
  54. /// The \c RRTTL class encapsulates TTLs used in DNS resource records.
  55. ///
  56. /// This is a straightforward class; an \c RRTTL object simply maintains a
  57. /// 32-bit unsigned integer corresponding to the TTL value. The main purpose
  58. /// of this class is to provide convenient interfaces to convert a textual
  59. /// representation into the integer TTL value and vice versa, and to handle
  60. /// wire-format representations.
  61. class RRTTL {
  62. public:
  63. ///
  64. /// \name Constructors, Factory and Destructor
  65. ///
  66. /// Note: We use the default copy constructor and the default copy
  67. /// assignment operator intentionally.
  68. //@{
  69. /// Constructor from an integer TTL value.
  70. ///
  71. /// This constructor never throws an exception.
  72. ///
  73. /// \param ttlval An 32-bit integer of the RRTTL.
  74. explicit RRTTL(uint32_t ttlval) : ttlval_(ttlval) {}
  75. /// Constructor from a string.
  76. ///
  77. /// It accepts either a decimal number, specifying number of seconds. Or,
  78. /// it can be given a sequence of numbers and units, like "2H" (meaning
  79. /// two hours), "1W3D" (one week and 3 days). The allowed units are W
  80. /// (week), D (day), H (hour), M (minute) and S (second). They can be also
  81. /// specified in lower-case. No further restrictions are checked (so they
  82. /// can be specified in arbitrary order and even things like "1D1D" can
  83. /// be used to specify two days).
  84. ///
  85. /// \param ttlstr A string representation of the \c RRTTL.
  86. ///
  87. /// \throw InvalidRRTTL in case the string is not recognized as valid
  88. /// TTL representation.
  89. explicit RRTTL(const std::string& ttlstr);
  90. /// Constructor from wire-format data.
  91. ///
  92. /// The \c buffer parameter normally stores a complete DNS message
  93. /// containing the RRTTL to be constructed. The current read position of
  94. /// the buffer points to the head of the type.
  95. ///
  96. /// If the given data does not large enough to contain a 16-bit integer,
  97. /// an exception of class \c IncompleteRRTTL will be thrown.
  98. ///
  99. /// \param buffer A buffer storing the wire format data.
  100. explicit RRTTL(isc::util::InputBuffer& buffer);
  101. /// A separate factory of RRTTL from text.
  102. ///
  103. /// This static method is similar to the constructor that takes a string
  104. /// object, but works as a factory and reports parsing failure in the
  105. /// form of the return value. Normally the constructor version should
  106. /// suffice, but in some cases the caller may have to expect mixture of
  107. /// valid and invalid input, and may want to minimize the overhead of
  108. /// possible exception handling. This version is provided for such
  109. /// purpose.
  110. ///
  111. /// If the given text represents a valid RRTTL, it returns a \c MaybeRRTTL
  112. /// object that stores a corresponding \c RRTTL object, which is
  113. /// accessible via \c operator*(). In this case the returned object will
  114. /// be interpreted as \c true in a boolean context. If the given text
  115. /// does not represent a valid RRTTL, it returns a \c MaybeRRTTL object
  116. /// which is interpreted as \c false in a boolean context.
  117. ///
  118. /// One main purpose of this function is to minimize the overhead
  119. /// when the given text does not represent a valid RR TTL. For this
  120. /// reason this function intentionally omits the capability of delivering
  121. /// a detailed reason for the parse failure, such as in the \c want()
  122. /// string when exception is thrown from the constructor (it will
  123. /// internally require a creation of string object, which is relatively
  124. /// expensive). If such detailed information is necessary, the constructor
  125. /// version should be used to catch the resulting exception.
  126. ///
  127. /// This function never throws the \c InvalidRRTTL exception.
  128. ///
  129. /// \param ttlstr A string representation of the \c RRTTL.
  130. /// \return An MaybeRRTTL object either storing an RRTTL object for
  131. /// the given text or a \c false value.
  132. static MaybeRRTTL createFromText(const std::string& ttlstr);
  133. ///
  134. //@}
  135. ///
  136. /// \name Converter methods
  137. ///
  138. //@{
  139. /// \brief Convert the \c RRTTL to a string.
  140. ///
  141. /// This version of implementation simply converts the TTL value into the
  142. /// numeric textual representation. We may introduce more human-readable
  143. /// format depending on the context in future versions.
  144. ///
  145. /// If resource allocation in rendering process fails, a corresponding
  146. /// standard exception will be thrown.
  147. ///
  148. /// \return A string representation of the \c RRTTL.
  149. const std::string toText() const;
  150. /// \brief Render the \c RRTTL in the wire format.
  151. ///
  152. /// This method renders the TTL value in network byte order via \c renderer,
  153. /// which encapsulates output buffer and other rendering contexts.
  154. ///
  155. /// If resource allocation in rendering process fails, a corresponding
  156. /// standard exception will be thrown.
  157. ///
  158. /// \param renderer DNS message rendering context that encapsulates the
  159. /// output buffer in which the RRTTL is to be stored.
  160. void toWire(AbstractMessageRenderer& renderer) const;
  161. /// \brief Render the \c RRTTL in the wire format.
  162. ///
  163. /// This method renders the TTL value in network byte order into the
  164. /// \c buffer.
  165. ///
  166. /// If resource allocation in rendering process fails, a corresponding
  167. /// standard exception will be thrown.
  168. ///
  169. /// \param buffer An output buffer to store the wire data.
  170. void toWire(isc::util::OutputBuffer& buffer) const;
  171. //@}
  172. ///
  173. /// \name Getter Methods
  174. ///
  175. //@{
  176. /// \brief Returns the TTL value as a 32-bit unsigned integer.
  177. ///
  178. /// This method never throws an exception.
  179. ///
  180. /// \return An 32-bit integer corresponding to the RRTTL.
  181. uint32_t getValue() const { return (ttlval_); }
  182. //@}
  183. ///
  184. /// \name Comparison methods
  185. ///
  186. /// Comparison between two \c RRTTL objects is performed in a
  187. /// straightforward way, that is, comparing the corresponding TTL values
  188. /// (which is the result of the \c getValue() method) as 32-bit unsigned
  189. /// integers.
  190. //@{
  191. /// \brief Return true iff two RRTTLs are equal.
  192. ///
  193. /// This method never throws an exception.
  194. ///
  195. /// \param other the \c RRTTL object to compare against.
  196. bool equals(const RRTTL& other) const
  197. { return (ttlval_ == other.ttlval_); }
  198. /// \brief Same as \c equals().
  199. bool operator==(const RRTTL& other) const
  200. { return (ttlval_ == other.ttlval_); }
  201. /// \brief Return true iff two RRTTLs are not equal.
  202. ///
  203. /// This method never throws an exception.
  204. ///
  205. /// \param other the \c RRTTL object to compare against.
  206. bool nequals(const RRTTL& other) const
  207. { return (ttlval_ != other.ttlval_); }
  208. /// \brief Same as \c nequals().
  209. bool operator!=(const RRTTL& other) const
  210. { return (ttlval_ != other.ttlval_); }
  211. /// \brief Less-than or equal comparison for RRTTL against \c other.
  212. ///
  213. /// This method never throws an exception.
  214. ///
  215. /// \param other the \c RRTTL object to compare against.
  216. /// \return true if \c this RRTTL is less than or equal to the \c other;
  217. /// otherwise false.
  218. bool leq(const RRTTL& other) const
  219. { return (ttlval_ <= other.ttlval_); }
  220. /// Same as \c leq()
  221. bool operator<=(const RRTTL& other) const
  222. { return (ttlval_ <= other.ttlval_); }
  223. /// \brief Greater-than or equal comparison for RRTTL against \c other.
  224. ///
  225. /// This method never throws an exception.
  226. ///
  227. /// \param other the \c RRTTL object to compare against.
  228. /// \return true if \c this RRTTL is greater than or equal to the \c other;
  229. /// otherwise false.
  230. bool geq(const RRTTL& other) const
  231. { return (ttlval_ >= other.ttlval_); }
  232. /// Same as \c geq()
  233. bool operator>=(const RRTTL& other) const
  234. { return (ttlval_ >= other.ttlval_); }
  235. /// \brief Less-than comparison for RRTTL against \c other.
  236. ///
  237. /// This method never throws an exception.
  238. ///
  239. /// \param other the \c RRTTL object to compare against.
  240. /// \return true if \c this RRTTL is less than the \c other;
  241. /// otherwise false.
  242. bool lthan(const RRTTL& other) const
  243. { return (ttlval_ < other.ttlval_); }
  244. /// Same as \c lthan()
  245. bool operator<(const RRTTL& other) const
  246. { return (ttlval_ < other.ttlval_); }
  247. /// \brief Greater-than comparison for RRTTL against \c other.
  248. ///
  249. /// This method never throws an exception.
  250. ///
  251. /// \param other the \c RRTTL object to compare against.
  252. /// \return true if \c this RRTTL is greater than the \c other;
  253. /// otherwise false.
  254. bool gthan(const RRTTL& other) const
  255. { return (ttlval_ > other.ttlval_); }
  256. /// Same as \c gthan()
  257. bool operator>(const RRTTL& other) const
  258. { return (ttlval_ > other.ttlval_); }
  259. //@}
  260. ///
  261. /// \name Protocol constants
  262. ///
  263. //@{
  264. /// \brief The TTL of the max allowable value, per RFC2181 Section 8.
  265. ///
  266. /// The max value is the largest unsigned 31 bit integer, 2^31-1.
  267. ///
  268. /// \note At the moment an RRTTL object can have a value larger than
  269. /// this limit. We may revisit it in a future version.
  270. static const RRTTL& MAX_TTL() {
  271. static const RRTTL max_ttl(0x7fffffff);
  272. return (max_ttl);
  273. }
  274. //@}
  275. private:
  276. uint32_t ttlval_;
  277. };
  278. ///
  279. /// \brief Insert the \c RRTTL as a string into stream.
  280. ///
  281. /// This method convert the \c rrttl into a string and inserts it into the
  282. /// output stream \c os.
  283. ///
  284. /// This function overloads the global operator<< to behave as described in
  285. /// ostream::operator<< but applied to \c RRTTL objects.
  286. ///
  287. /// \param os A \c std::ostream object on which the insertion operation is
  288. /// performed.
  289. /// \param rrttl The \c RRTTL object output by the operation.
  290. /// \return A reference to the same \c std::ostream object referenced by
  291. /// parameter \c os after the insertion operation.
  292. std::ostream&
  293. operator<<(std::ostream& os, const RRTTL& rrttl);
  294. }
  295. }
  296. #endif // RRTTL_H
  297. // Local Variables:
  298. // mode: c++
  299. // End: