rrttl.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <stdint.h>
  17. #include <exceptions/exceptions.h>
  18. namespace isc {
  19. namespace util {
  20. class InputBuffer;
  21. class OutputBuffer;
  22. }
  23. namespace dns {
  24. // forward declarations
  25. class AbstractMessageRenderer;
  26. ///
  27. /// \brief A standard DNS module exception that is thrown if an RRTTL object
  28. /// is being constructed from an unrecognized string.
  29. ///
  30. class InvalidRRTTL : public Exception {
  31. public:
  32. InvalidRRTTL(const char* file, size_t line, const char* what) :
  33. isc::Exception(file, line, what) {}
  34. };
  35. ///
  36. /// \brief A standard DNS module exception that is thrown if an RRTTL object
  37. /// is being constructed from a incomplete (too short) wire-format data.
  38. ///
  39. class IncompleteRRTTL : public Exception {
  40. public:
  41. IncompleteRRTTL(const char* file, size_t line, const char* what) :
  42. isc::Exception(file, line, what) {}
  43. };
  44. ///
  45. /// The \c RRTTL class encapsulates TTLs used in DNS resource records.
  46. ///
  47. /// This is a straightforward class; an \c RRTTL object simply maintains a
  48. /// 32-bit unsigned integer corresponding to the TTL value. The main purpose
  49. /// of this class is to provide convenient interfaces to convert a textual
  50. /// representation into the integer TTL value and vice versa, and to handle
  51. /// wire-format representations.
  52. class RRTTL {
  53. public:
  54. ///
  55. /// \name Constructors and Destructor
  56. ///
  57. /// Note: We use the default copy constructor and the default copy
  58. /// assignment operator intentionally.
  59. //@{
  60. /// Constructor from an integer TTL value.
  61. ///
  62. /// This constructor never throws an exception.
  63. ///
  64. /// \param ttlval An 32-bit integer of the RRTTL.
  65. explicit RRTTL(uint32_t ttlval) : ttlval_(ttlval) {}
  66. /// Constructor from a string.
  67. ///
  68. /// This version of the implementation only accepts decimal TTL values in
  69. /// seconds.
  70. /// In a near future version, we'll extend it so that we can accept more
  71. /// convenient ones such as "2H" or "1D".
  72. ///
  73. /// If the given string is not recognized as a valid representation of
  74. /// an RR TTL, an exception of class \c InvalidRRTTL will be thrown.
  75. ///
  76. /// \param ttlstr A string representation of the \c RRTTL
  77. explicit RRTTL(const std::string& ttlstr);
  78. /// Constructor from wire-format data.
  79. ///
  80. /// The \c buffer parameter normally stores a complete DNS message
  81. /// containing the RRTTL to be constructed. The current read position of
  82. /// the buffer points to the head of the type.
  83. ///
  84. /// If the given data does not large enough to contain a 16-bit integer,
  85. /// an exception of class \c IncompleteRRTTL will be thrown.
  86. ///
  87. /// \param buffer A buffer storing the wire format data.
  88. explicit RRTTL(isc::util::InputBuffer& buffer);
  89. ///
  90. //@}
  91. ///
  92. /// \name Converter methods
  93. ///
  94. //@{
  95. /// \brief Convert the \c RRTTL to a string.
  96. ///
  97. /// This version of implementation simply converts the TTL value into the
  98. /// numeric textual representation. We may introduce more human-readable
  99. /// format depending on the context in future versions.
  100. ///
  101. /// If resource allocation in rendering process fails, a corresponding
  102. /// standard exception will be thrown.
  103. ///
  104. /// \return A string representation of the \c RRTTL.
  105. const std::string toText() const;
  106. /// \brief Render the \c RRTTL in the wire format.
  107. ///
  108. /// This method renders the TTL value in network byte order via \c renderer,
  109. /// which encapsulates output buffer and other rendering contexts.
  110. ///
  111. /// If resource allocation in rendering process fails, a corresponding
  112. /// standard exception will be thrown.
  113. ///
  114. /// \param renderer DNS message rendering context that encapsulates the
  115. /// output buffer in which the RRTTL is to be stored.
  116. void toWire(AbstractMessageRenderer& renderer) const;
  117. /// \brief Render the \c RRTTL in the wire format.
  118. ///
  119. /// This method renders the TTL value in network byte order into the
  120. /// \c buffer.
  121. ///
  122. /// If resource allocation in rendering process fails, a corresponding
  123. /// standard exception will be thrown.
  124. ///
  125. /// \param buffer An output buffer to store the wire data.
  126. void toWire(isc::util::OutputBuffer& buffer) const;
  127. //@}
  128. ///
  129. /// \name Getter Methods
  130. ///
  131. //@{
  132. /// \brief Returns the TTL value as a 32-bit unsigned integer.
  133. ///
  134. /// This method never throws an exception.
  135. ///
  136. /// \return An 32-bit integer corresponding to the RRTTL.
  137. uint32_t getValue() const { return (ttlval_); }
  138. //@}
  139. ///
  140. /// \name Comparison methods
  141. ///
  142. /// Comparison between two \c RRTTL objects is performed in a
  143. /// straightforward way, that is, comparing the corresponding TTL values
  144. /// (which is the result of the \c getValue() method) as 32-bit unsigned
  145. /// integers.
  146. //@{
  147. /// \brief Return true iff two RRTTLs are equal.
  148. ///
  149. /// This method never throws an exception.
  150. ///
  151. /// \param other the \c RRTTL object to compare against.
  152. bool equals(const RRTTL& other) const
  153. { return (ttlval_ == other.ttlval_); }
  154. /// \brief Same as \c equals().
  155. bool operator==(const RRTTL& other) const
  156. { return (ttlval_ == other.ttlval_); }
  157. /// \brief Return true iff two RRTTLs are not equal.
  158. ///
  159. /// This method never throws an exception.
  160. ///
  161. /// \param other the \c RRTTL object to compare against.
  162. bool nequals(const RRTTL& other) const
  163. { return (ttlval_ != other.ttlval_); }
  164. /// \brief Same as \c nequals().
  165. bool operator!=(const RRTTL& other) const
  166. { return (ttlval_ != other.ttlval_); }
  167. /// \brief Less-than or equal comparison for RRTTL against \c other.
  168. ///
  169. /// This method never throws an exception.
  170. ///
  171. /// \param other the \c RRTTL object to compare against.
  172. /// \return true if \c this RRTTL is less than or equal to the \c other;
  173. /// otherwise false.
  174. bool leq(const RRTTL& other) const
  175. { return (ttlval_ <= other.ttlval_); }
  176. /// Same as \c leq()
  177. bool operator<=(const RRTTL& other) const
  178. { return (ttlval_ <= other.ttlval_); }
  179. /// \brief Greater-than or equal comparison for RRTTL against \c other.
  180. ///
  181. /// This method never throws an exception.
  182. ///
  183. /// \param other the \c RRTTL object to compare against.
  184. /// \return true if \c this RRTTL is greater than or equal to the \c other;
  185. /// otherwise false.
  186. bool geq(const RRTTL& other) const
  187. { return (ttlval_ >= other.ttlval_); }
  188. /// Same as \c geq()
  189. bool operator>=(const RRTTL& other) const
  190. { return (ttlval_ >= other.ttlval_); }
  191. /// \brief Less-than comparison for RRTTL against \c other.
  192. ///
  193. /// This method never throws an exception.
  194. ///
  195. /// \param other the \c RRTTL object to compare against.
  196. /// \return true if \c this RRTTL is less than the \c other;
  197. /// otherwise false.
  198. bool lthan(const RRTTL& other) const
  199. { return (ttlval_ < other.ttlval_); }
  200. /// Same as \c lthan()
  201. bool operator<(const RRTTL& other) const
  202. { return (ttlval_ < other.ttlval_); }
  203. /// \brief Greater-than comparison for RRTTL against \c other.
  204. ///
  205. /// This method never throws an exception.
  206. ///
  207. /// \param other the \c RRTTL object to compare against.
  208. /// \return true if \c this RRTTL is greater than the \c other;
  209. /// otherwise false.
  210. bool gthan(const RRTTL& other) const
  211. { return (ttlval_ > other.ttlval_); }
  212. /// Same as \c gthan()
  213. bool operator>(const RRTTL& other) const
  214. { return (ttlval_ > other.ttlval_); }
  215. //@}
  216. private:
  217. uint32_t ttlval_;
  218. };
  219. ///
  220. /// \brief Insert the \c RRTTL as a string into stream.
  221. ///
  222. /// This method convert the \c rrttl into a string and inserts it into the
  223. /// output stream \c os.
  224. ///
  225. /// This function overloads the global operator<< to behave as described in
  226. /// ostream::operator<< but applied to \c RRTTL objects.
  227. ///
  228. /// \param os A \c std::ostream object on which the insertion operation is
  229. /// performed.
  230. /// \param rrttl The \c RRTTL object output by the operation.
  231. /// \return A reference to the same \c std::ostream object referenced by
  232. /// parameter \c os after the insertion operation.
  233. std::ostream&
  234. operator<<(std::ostream& os, const RRTTL& rrttl);
  235. }
  236. }
  237. #endif // __RRTTL_H
  238. // Local Variables:
  239. // mode: c++
  240. // End: