opcode.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdint.h>
  17. #include <ostream>
  18. #ifndef __OPCODE_H
  19. #define __OPCODE_H 1
  20. namespace isc {
  21. namespace dns {
  22. /// \brief The \c Opcode class objects represent standard OPCODEs
  23. /// of the header section of DNS messages as defined in RFC1035.
  24. ///
  25. /// This is a straightforward value class encapsulating the OPCODE code
  26. /// values. Since OPCODEs are 4-bit integers that are used in limited
  27. /// places and it's unlikely that new code values will be assigned, we could
  28. /// represent them as simple integers (via constant variables or enums).
  29. /// However, we define a separate class so that we can benefit from C++
  30. /// type safety as much as possible. For convenience we also provide
  31. /// an enum type for standard OPCDE values, but it is generally advisable
  32. /// to handle OPCODEs through this class. In fact, public interfaces of
  33. /// this library uses this class to pass or return OPCODEs instead of the
  34. /// bare code values.
  35. class Opcode {
  36. public:
  37. /// Constants for standard OPCODE values.
  38. enum CodeValue {
  39. QUERY_CODE = 0, ///< 0: Standard query (RFC1035)
  40. IQUERY_CODE = 1, ///< 1: Inverse query (RFC1035)
  41. STATUS_CODE = 2, ///< 2: Server status request (RFC1035)
  42. RESERVED3_CODE = 3, ///< 3: Reserved for future use (RFC1035)
  43. NOTIFY_CODE = 4, ///< 4: Notify (RFC1996)
  44. UPDATE_CODE = 5, ///< 5: Dynamic update (RFC2136)
  45. RESERVED6_CODE = 6, ///< 6: Reserved for future use (RFC1035)
  46. RESERVED7_CODE = 7, ///< 7: Reserved for future use (RFC1035)
  47. RESERVED8_CODE = 8, ///< 8: Reserved for future use (RFC1035)
  48. RESERVED9_CODE = 9, ///< 9: Reserved for future use (RFC1035)
  49. RESERVED10_CODE = 10, ///< 10: Reserved for future use (RFC1035)
  50. RESERVED11_CODE = 11, ///< 11: Reserved for future use (RFC1035)
  51. RESERVED12_CODE = 12, ///< 12: Reserved for future use (RFC1035)
  52. RESERVED13_CODE = 13, ///< 13: Reserved for future use (RFC1035)
  53. RESERVED14_CODE = 14, ///< 14: Reserved for future use (RFC1035)
  54. RESERVED15_CODE = 15 ///< 15: Reserved for future use (RFC1035)
  55. };
  56. /// \name Constructors and Destructor
  57. ///
  58. /// We use the default versions of destructor, copy constructor,
  59. /// and assignment operator.
  60. ///
  61. /// The default constructor is hidden as a result of defining the other
  62. /// constructors. This is intentional; we don't want to allow an
  63. /// \c Opcode object to be constructed with an invalid state.
  64. //@{
  65. /// \brief Constructor from the code value.
  66. ///
  67. /// Since OPCODEs are 4-bit values, parameters larger than 15 are invalid.
  68. /// If \c code is larger than 15 an exception of class \c isc::OutOfRange
  69. /// will be thrown.
  70. ///
  71. /// \param code The underlying code value of the \c Opcode.
  72. explicit Opcode(const uint8_t code);
  73. //@}
  74. /// \brief Returns the \c Opcode code value.
  75. ///
  76. /// This method never throws an exception.
  77. ///
  78. /// \return The underlying code value corresponding to the \c Opcode.
  79. CodeValue getCode() const { return (code_); }
  80. /// \brief Return true iff two Opcodes are equal.
  81. ///
  82. /// Two Opcodes are equal iff their type codes are equal.
  83. ///
  84. /// This method never throws an exception.
  85. ///
  86. /// \param other the \c Opcode object to compare against.
  87. /// \return true if the two Opcodes are equal; otherwise false.
  88. bool equals(const Opcode& other) const
  89. { return (code_ == other.code_); }
  90. /// \brief Same as \c equals().
  91. bool operator==(const Opcode& other) const { return (equals(other)); }
  92. /// \brief Return true iff two Opcodes are not equal.
  93. ///
  94. /// This method never throws an exception.
  95. ///
  96. /// \param other the \c Opcode object to compare against.
  97. /// \return true if the two Opcodes are not equal; otherwise false.
  98. bool nequals(const Opcode& other) const
  99. { return (code_ != other.code_); }
  100. /// \brief Same as \c nequals().
  101. bool operator!=(const Opcode& other) const { return (nequals(other)); }
  102. /// \brief Convert the \c Opcode to a string.
  103. ///
  104. /// This method returns a string representation of the "mnemonic' used
  105. /// for the enum and constant objects. For example, the string for
  106. /// code value 0 is "QUERY", etc.
  107. ///
  108. /// If resource allocation for the string fails, a corresponding standard
  109. /// exception will be thrown.
  110. ///
  111. /// \return A string representation of the \c Opcode.
  112. std::string toText() const;
  113. /// A constant object for the QUERY Opcode.
  114. static const Opcode& QUERY();
  115. /// A constant object for the IQUERY Opcode.
  116. static const Opcode& IQUERY();
  117. /// A constant object for the STATUS Opcode.
  118. static const Opcode& STATUS();
  119. /// A constant object for a reserved (code 3) Opcode.
  120. static const Opcode& RESERVED3();
  121. /// A constant object for the NOTIFY Opcode.
  122. static const Opcode& NOTIFY();
  123. /// A constant object for the UPDATE Opcode.
  124. static const Opcode& UPDATE();
  125. /// A constant object for a reserved (code 6) Opcode.
  126. static const Opcode& RESERVED6();
  127. /// A constant object for a reserved (code 7) Opcode.
  128. static const Opcode& RESERVED7();
  129. /// A constant object for a reserved (code 8) Opcode.
  130. static const Opcode& RESERVED8();
  131. /// A constant object for a reserved (code 9) Opcode.
  132. static const Opcode& RESERVED9();
  133. /// A constant object for a reserved (code 10) Opcode.
  134. static const Opcode& RESERVED10();
  135. /// A constant object for a reserved (code 11) Opcode.
  136. static const Opcode& RESERVED11();
  137. /// A constant object for a reserved (code 12) Opcode.
  138. static const Opcode& RESERVED12();
  139. /// A constant object for a reserved (code 13) Opcode.
  140. static const Opcode& RESERVED13();
  141. /// A constant object for a reserved (code 14) Opcode.
  142. static const Opcode& RESERVED14();
  143. /// A constant object for a reserved (code 15) Opcode.
  144. static const Opcode& RESERVED15();
  145. private:
  146. CodeValue code_;
  147. };
  148. inline const Opcode&
  149. Opcode::QUERY() {
  150. static Opcode c(0);
  151. return (c);
  152. }
  153. inline const Opcode&
  154. Opcode::IQUERY() {
  155. static Opcode c(1);
  156. return (c);
  157. }
  158. inline const Opcode&
  159. Opcode::STATUS() {
  160. static Opcode c(2);
  161. return (c);
  162. }
  163. inline const Opcode&
  164. Opcode::RESERVED3() {
  165. static Opcode c(3);
  166. return (c);
  167. }
  168. inline const Opcode&
  169. Opcode::NOTIFY() {
  170. static Opcode c(4);
  171. return (c);
  172. }
  173. inline const Opcode&
  174. Opcode::UPDATE() {
  175. static Opcode c(5);
  176. return (c);
  177. }
  178. inline const Opcode&
  179. Opcode::RESERVED6() {
  180. static Opcode c(6);
  181. return (c);
  182. }
  183. inline const Opcode&
  184. Opcode::RESERVED7() {
  185. static Opcode c(7);
  186. return (c);
  187. }
  188. inline const Opcode&
  189. Opcode::RESERVED8() {
  190. static Opcode c(8);
  191. return (c);
  192. }
  193. inline const Opcode&
  194. Opcode::RESERVED9() {
  195. static Opcode c(9);
  196. return (c);
  197. }
  198. inline const Opcode&
  199. Opcode::RESERVED10() {
  200. static Opcode c(10);
  201. return (c);
  202. }
  203. inline const Opcode&
  204. Opcode::RESERVED11() {
  205. static Opcode c(11);
  206. return (c);
  207. }
  208. inline const Opcode&
  209. Opcode::RESERVED12() {
  210. static Opcode c(12);
  211. return (c);
  212. }
  213. inline const Opcode&
  214. Opcode::RESERVED13() {
  215. static Opcode c(13);
  216. return (c);
  217. }
  218. inline const Opcode&
  219. Opcode::RESERVED14() {
  220. static Opcode c(14);
  221. return (c);
  222. }
  223. inline const Opcode&
  224. Opcode::RESERVED15() {
  225. static Opcode c(15);
  226. return (c);
  227. }
  228. /// \brief Insert the \c Opcode as a string into stream.
  229. ///
  230. /// This method convert \c opcode into a string and inserts it into the
  231. /// output stream \c os.
  232. ///
  233. /// \param os A \c std::ostream object on which the insertion operation is
  234. /// performed.
  235. /// \param opcode A reference to an \c Opcode object output by the operation.
  236. /// \return A reference to the same \c std::ostream object referenced by
  237. /// parameter \c os after the insertion operation.
  238. std::ostream& operator<<(std::ostream& os, const Opcode& opcode);
  239. }
  240. }
  241. #endif // OPCODE_H
  242. // Local Variables:
  243. // mode: c++
  244. // End: