Branch data Line data Source code
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 : :
17 : : #include <stdint.h>
18 : :
19 : : #include <ostream>
20 : :
21 : : #ifndef __OPCODE_H
22 : : #define __OPCODE_H 1
23 : :
24 : : namespace isc {
25 : : namespace dns {
26 : :
27 : : /// \brief The \c Opcode class objects represent standard OPCODEs
28 : : /// of the header section of DNS messages as defined in RFC1035.
29 : : ///
30 : : /// This is a straightforward value class encapsulating the OPCODE code
31 : : /// values. Since OPCODEs are 4-bit integers that are used in limited
32 : : /// places and it's unlikely that new code values will be assigned, we could
33 : : /// represent them as simple integers (via constant variables or enums).
34 : : /// However, we define a separate class so that we can benefit from C++
35 : : /// type safety as much as possible. For convenience we also provide
36 : : /// an enum type for standard OPCDE values, but it is generally advisable
37 : : /// to handle OPCODEs through this class. In fact, public interfaces of
38 : : /// this library uses this class to pass or return OPCODEs instead of the
39 : : /// bare code values.
40 : : class Opcode {
41 : : public:
42 : : /// Constants for standard OPCODE values.
43 : : enum CodeValue {
44 : : QUERY_CODE = 0, ///< 0: Standard query (RFC1035)
45 : : IQUERY_CODE = 1, ///< 1: Inverse query (RFC1035)
46 : : STATUS_CODE = 2, ///< 2: Server status request (RFC1035)
47 : : RESERVED3_CODE = 3, ///< 3: Reserved for future use (RFC1035)
48 : : NOTIFY_CODE = 4, ///< 4: Notify (RFC1996)
49 : : UPDATE_CODE = 5, ///< 5: Dynamic update (RFC2136)
50 : : RESERVED6_CODE = 6, ///< 6: Reserved for future use (RFC1035)
51 : : RESERVED7_CODE = 7, ///< 7: Reserved for future use (RFC1035)
52 : : RESERVED8_CODE = 8, ///< 8: Reserved for future use (RFC1035)
53 : : RESERVED9_CODE = 9, ///< 9: Reserved for future use (RFC1035)
54 : : RESERVED10_CODE = 10, ///< 10: Reserved for future use (RFC1035)
55 : : RESERVED11_CODE = 11, ///< 11: Reserved for future use (RFC1035)
56 : : RESERVED12_CODE = 12, ///< 12: Reserved for future use (RFC1035)
57 : : RESERVED13_CODE = 13, ///< 13: Reserved for future use (RFC1035)
58 : : RESERVED14_CODE = 14, ///< 14: Reserved for future use (RFC1035)
59 : : RESERVED15_CODE = 15 ///< 15: Reserved for future use (RFC1035)
60 : : };
61 : :
62 : : /// \name Constructors and Destructor
63 : : ///
64 : : /// We use the default versions of destructor, copy constructor,
65 : : /// and assignment operator.
66 : : ///
67 : : /// The default constructor is hidden as a result of defining the other
68 : : /// constructors. This is intentional; we don't want to allow an
69 : : /// \c Opcode object to be constructed with an invalid state.
70 : : //@{
71 : : /// \brief Constructor from the code value.
72 : : ///
73 : : /// Since OPCODEs are 4-bit values, parameters larger than 15 are invalid.
74 : : /// If \c code is larger than 15 an exception of class \c isc::OutOfRange
75 : : /// will be thrown.
76 : : ///
77 : : /// \param code The underlying code value of the \c Opcode.
78 : : explicit Opcode(const uint8_t code);
79 : : //@}
80 : :
81 : : /// \brief Returns the \c Opcode code value.
82 : : ///
83 : : /// This method never throws an exception.
84 : : ///
85 : : /// \return The underlying code value corresponding to the \c Opcode.
86 : 1441 : CodeValue getCode() const { return (code_); }
87 : :
88 : : /// \brief Return true iff two Opcodes are equal.
89 : : ///
90 : : /// Two Opcodes are equal iff their type codes are equal.
91 : : ///
92 : : /// This method never throws an exception.
93 : : ///
94 : : /// \param other the \c Opcode object to compare against.
95 : : /// \return true if the two Opcodes are equal; otherwise false.
96 : : bool equals(const Opcode& other) const
97 : 633 : { return (code_ == other.code_); }
98 : :
99 : : /// \brief Same as \c equals().
100 : : bool operator==(const Opcode& other) const { return (equals(other)); }
101 : :
102 : : /// \brief Return true iff two Opcodes are not equal.
103 : : ///
104 : : /// This method never throws an exception.
105 : : ///
106 : : /// \param other the \c Opcode object to compare against.
107 : : /// \return true if the two Opcodes are not equal; otherwise false.
108 : : bool nequals(const Opcode& other) const
109 : 298 : { return (code_ != other.code_); }
110 : :
111 : : /// \brief Same as \c nequals().
112 : : bool operator!=(const Opcode& other) const { return (nequals(other)); }
113 : :
114 : : /// \brief Convert the \c Opcode to a string.
115 : : ///
116 : : /// This method returns a string representation of the "mnemonic' used
117 : : /// for the enum and constant objects. For example, the string for
118 : : /// code value 0 is "QUERY", etc.
119 : : ///
120 : : /// If resource allocation for the string fails, a corresponding standard
121 : : /// exception will be thrown.
122 : : ///
123 : : /// \return A string representation of the \c Opcode.
124 : : std::string toText() const;
125 : :
126 : : /// A constant object for the QUERY Opcode.
127 : : static const Opcode& QUERY();
128 : :
129 : : /// A constant object for the IQUERY Opcode.
130 : : static const Opcode& IQUERY();
131 : :
132 : : /// A constant object for the STATUS Opcode.
133 : : static const Opcode& STATUS();
134 : :
135 : : /// A constant object for a reserved (code 3) Opcode.
136 : : static const Opcode& RESERVED3();
137 : :
138 : : /// A constant object for the NOTIFY Opcode.
139 : : static const Opcode& NOTIFY();
140 : :
141 : : /// A constant object for the UPDATE Opcode.
142 : : static const Opcode& UPDATE();
143 : :
144 : : /// A constant object for a reserved (code 6) Opcode.
145 : : static const Opcode& RESERVED6();
146 : :
147 : : /// A constant object for a reserved (code 7) Opcode.
148 : : static const Opcode& RESERVED7();
149 : :
150 : : /// A constant object for a reserved (code 8) Opcode.
151 : : static const Opcode& RESERVED8();
152 : :
153 : : /// A constant object for a reserved (code 9) Opcode.
154 : : static const Opcode& RESERVED9();
155 : :
156 : : /// A constant object for a reserved (code 10) Opcode.
157 : : static const Opcode& RESERVED10();
158 : :
159 : : /// A constant object for a reserved (code 11) Opcode.
160 : : static const Opcode& RESERVED11();
161 : :
162 : : /// A constant object for a reserved (code 12) Opcode.
163 : : static const Opcode& RESERVED12();
164 : :
165 : : /// A constant object for a reserved (code 13) Opcode.
166 : : static const Opcode& RESERVED13();
167 : :
168 : : /// A constant object for a reserved (code 14) Opcode.
169 : : static const Opcode& RESERVED14();
170 : :
171 : : /// A constant object for a reserved (code 15) Opcode.
172 : : static const Opcode& RESERVED15();
173 : : private:
174 : : CodeValue code_;
175 : : };
176 : :
177 : : inline const Opcode&
178 : 929 : Opcode::QUERY() {
179 [ + + ][ + - ]: 1588 : static Opcode c(0);
[ + - ][ + + ]
[ + - ][ + - ]
180 : 929 : return (c);
181 : : }
182 : :
183 : : inline const Opcode&
184 : 5 : Opcode::IQUERY() {
185 [ + + ][ + - ]: 9 : static Opcode c(1);
[ + - ]
186 : 5 : return (c);
187 : : }
188 : :
189 : : inline const Opcode&
190 : : Opcode::STATUS() {
191 [ + + ][ + - ]: 2 : static Opcode c(2);
[ + - ]
192 : : return (c);
193 : : }
194 : :
195 : : inline const Opcode&
196 : : Opcode::RESERVED3() {
197 [ + + ][ + - ]: 2 : static Opcode c(3);
[ + - ]
198 : : return (c);
199 : : }
200 : :
201 : : inline const Opcode&
202 : 419 : Opcode::NOTIFY() {
203 [ + + ][ + - ]: 463 : static Opcode c(4);
[ + - ]
204 : 419 : return (c);
205 : : }
206 : :
207 : : inline const Opcode&
208 : 3 : Opcode::UPDATE() {
209 [ + + ][ + - ]: 6 : static Opcode c(5);
[ + - ]
210 : 3 : return (c);
211 : : }
212 : :
213 : : inline const Opcode&
214 : : Opcode::RESERVED6() {
215 [ + + ][ + - ]: 2 : static Opcode c(6);
[ + - ]
216 : : return (c);
217 : : }
218 : :
219 : : inline const Opcode&
220 : : Opcode::RESERVED7() {
221 [ + + ][ + - ]: 2 : static Opcode c(7);
[ + - ]
222 : : return (c);
223 : : }
224 : :
225 : : inline const Opcode&
226 : : Opcode::RESERVED8() {
227 [ + + ][ + - ]: 2 : static Opcode c(8);
[ + - ]
228 : : return (c);
229 : : }
230 : :
231 : : inline const Opcode&
232 : : Opcode::RESERVED9() {
233 [ + + ][ + - ]: 2 : static Opcode c(9);
[ + - ]
234 : : return (c);
235 : : }
236 : :
237 : : inline const Opcode&
238 : : Opcode::RESERVED10() {
239 [ + + ][ + - ]: 2 : static Opcode c(10);
[ + - ]
240 : : return (c);
241 : : }
242 : :
243 : : inline const Opcode&
244 : : Opcode::RESERVED11() {
245 [ + + ][ + - ]: 2 : static Opcode c(11);
[ + - ]
246 : : return (c);
247 : : }
248 : :
249 : : inline const Opcode&
250 : : Opcode::RESERVED12() {
251 [ + + ][ + - ]: 2 : static Opcode c(12);
[ + - ]
252 : : return (c);
253 : : }
254 : :
255 : : inline const Opcode&
256 : : Opcode::RESERVED13() {
257 [ + + ][ + - ]: 2 : static Opcode c(13);
[ + - ]
258 : : return (c);
259 : : }
260 : :
261 : : inline const Opcode&
262 : 3 : Opcode::RESERVED14() {
263 [ + + ][ + - ]: 5 : static Opcode c(14);
[ + - ]
264 : 3 : return (c);
265 : : }
266 : :
267 : : inline const Opcode&
268 : 5 : Opcode::RESERVED15() {
269 [ + + ][ + - ]: 8 : static Opcode c(15);
[ + - ]
270 : 5 : return (c);
271 : : }
272 : :
273 : : /// \brief Insert the \c Opcode as a string into stream.
274 : : ///
275 : : /// This method convert \c opcode into a string and inserts it into the
276 : : /// output stream \c os.
277 : : ///
278 : : /// \param os A \c std::ostream object on which the insertion operation is
279 : : /// performed.
280 : : /// \param opcode A reference to an \c Opcode object output by the operation.
281 : : /// \return A reference to the same \c std::ostream object referenced by
282 : : /// parameter \c os after the insertion operation.
283 : : std::ostream& operator<<(std::ostream& os, const Opcode& opcode);
284 : : }
285 : : }
286 : : #endif // OPCODE_H
287 : :
288 : : // Local Variables:
289 : : // mode: c++
290 : : // End:
|