rrtype.h 11.9 KB
 JINMEI Tatuya committed Jan 26, 2010 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 ``````// Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC") // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. // \$Id\$ #ifndef __RRTYPE_H #define __RRTYPE_H 1 #include "exceptions.h" namespace isc { namespace dns { // forward declarations class InputBuffer; class OutputBuffer; class MessageRenderer; /// /// \brief A standard DNS module exception that is thrown if an RRType object /// is being constructed from an unrecognized string. /// class InvalidRRType : public Exception { public: InvalidRRType(const char* file, size_t line, const char* what) : isc::dns::Exception(file, line, what) {} }; /// /// \brief A standard DNS module exception that is thrown if an RRType object /// is being constructed from a incomplete (too short) wire-format data. /// class IncompleteRRType : public Exception { public: IncompleteRRType(const char* file, size_t line, const char* what) : isc::dns::Exception(file, line, what) {} }; /// /// The \c RRType class encapsulates DNS resource record types. /// /// This class manages the 16-bit integer type codes in quite a straightforward /// way. The only non trivial task is to handle textual representations of /// RR types, such as "A", "AAAA", or "TYPE65534". /// /// This class consults a helper \c RRParamRegistry class, which is a registry /// of RR related parameters and has the singleton object. This registry /// provides a mapping between RR type codes and their "well-known" textual /// representations. /// Parameters of RR types defined by DNS protocol standards are automatically /// registered at initialization time and are ensured to be always available for /// applications unless the application explicitly modifies the registry. /// /// For convenience, this class defines constant class objects corresponding to /// standard RR types. These are generally referred to as the form of /// RRType::{type-text}(). /// For example, \c RRType::NS() is an \c RRType object corresponding to the NS /// resource record (type code 2). /// Note that these constants are used through a "proxy" function. /// This is because they may be used to initialize another non-local (e.g. /// global or namespace-scope) static object as follows: /// /// \code /// namespace foo { /// const RRType default_type = RRType::A(); /// } \endcode /// /// In order to ensure that the constant RRType object has been initialized /// before the initialization for \c default_type, we need help from /// the proxy function. /// /// In the current implementation, the initialization of the well-known /// static objects is not thread safe. The same consideration as the /// \c RRParamRegistry class applies. We may extend the implementation so /// that the initialization is ensured to be thread safe in a future version. /// /// Note to developers: since it's expected that some of these constant /// \c RRType objects are frequently used in a performance sensitive path, /// we define these proxy functions as inline. This makes sense only when /// the corresponding static objects are defined only once even if they used /// in different source files. Sufficiently modern compilers should meet /// this assumption, but if we encounter memory bloat due to this problem with /// particular compilers we need to revisit the design or think about /// workaround. class RRType { public: /// /// \name Constructors and Destructor /// //@{ /// Constructor from an integer type code. /// /// This constructor never throws an exception. /// /// \param typecode An 16-bit integer code corresponding to the RRType. explicit RRType(uint16_t typecode) : typecode_(typecode) {} /// Constructor from a string. /// /// A valid string is one of "well-known" textual type representations /// such as "A", "AAAA", or "NS", or in the standard format for "unknown" /// RR types as defined in RFC3597, i.e., "TYPEnnnn". /// /// More precisely, the "well-known" representations are the ones stored /// in the \c RRParamRegistry registry (see the class description). /// /// As for the format of "TYPEnnnn", "nnnn" must represent a valid 16-bit /// unsigned integer, which may contain leading 0's as long as it consists /// of at most 5 characters (inclusive). /// For example, "TYPE1" and "TYPE001" are valid and represent the same /// RR type, but "TYPE65536" and "TYPE000001" are invalid. /// A "TYPEnnnn" representation is valid even if the corresponding type code /// is registered in the \c RRParamRegistry object. For example, both /// "A" and "TYPE1" are valid and represent the same RR type. /// /// All of these representations are case insensitive; "NS" and "ns", and /// "TYPE1" and "type1" are all valid and represent the same RR types, /// respectively. /// /// If the given string is not recognized as a valid representation of /// an RR type, an exception of class \c InvalidRRType will be thrown. /// /// \param typestr A string representation of the \c RRType explicit RRType(const std::string& typestr); /// Constructor from wire-format data. /// /// The \c buffer parameter normally stores a complete DNS message /// containing the RRType to be constructed. The current read position of /// the buffer points to the head of the type. /// /// If the given data does not large enough to contain a 16-bit integer, /// an exception of class \c IncompleteRRType will be thrown. /// /// \param buffer A buffer storing the wire format data. explicit RRType(InputBuffer& buffer); /// /// We use the default copy constructor intentionally. //@} /// We use the default copy assignment operator intentionally. /// /// /// \name Converter methods /// //@{ /// \brief Convert the \c RRType to a string. /// /// If a "well known" textual representation for the type code is registered /// in the RR parameter registry (see the class description), that will be /// used as the return value of this method. Otherwise, this method creates /// a new string for an "unknown" RR type in the format defined in RFC3597, /// i.e., "TYPEnnnn", and returns it. /// /// If resource allocation for the string fails, a corresponding standard /// exception will be thrown. /// /// \return A string representation of the \c RRType. const std::string toText() const; /// \brief Render the \c RRType in the wire format. /// /// This method renders the type code in network byte order via \c renderer, /// which encapsulates output buffer and other rendering contexts. /// /// If resource allocation in rendering process fails, a corresponding /// standard exception will be thrown. /// /// \param buffer An output buffer to store the wire data. void toWire(MessageRenderer& renderer) const; /// \brief Render the \c RRType in the wire format. /// /// This method renders the type code in network byte order into the /// \c buffer. /// /// If resource allocation in rendering process fails, a corresponding /// standard exception will be thrown. /// /// \param renderer DNS message rendering context that encapsulates the /// output buffer in which the RRType is to be stored. void toWire(OutputBuffer& buffer) const; //@} /// /// \name Getter Methods /// //@{ /// \brief Returns the RR type code as a 16-bit unsigned integer. /// /// This method never throws an exception. /// /// \return An 16-bit integer code corresponding to the RRType. uint16_t getCode() const { return (typecode_); } //@} /// /// \name Comparison methods /// //@{ /// \brief Return true iff two RRTypes are equal. /// /// Two RRTypes are equal iff their type codes are equal. /// /// This method never throws an exception. /// /// \param other the \c RRType object to compare against. /// \return true if the two RRTypes are equal; otherwise false. bool equals(const RRType& other) const { return (typecode_ == other.typecode_); } /// \brief Same as \c equals(). bool operator==(const RRType& other) const { return equals(other); } /// \brief Return true iff two RRTypes are equal. /// /// This method never throws an exception. /// /// \param other the \c RRType object to compare against. /// \return true if the two RRTypes are not equal; otherwise false. bool nequals(const RRType& other) const { return (typecode_ != other.typecode_); } /// \brief Same as \c nequals(). bool operator!=(const RRType& other) const { return nequals(other); } /// \brief Less-than comparison for RRType against \c other /// /// We define the less-than relationship based on their type codes; /// one RRType is less than the other iff the code of the former is less /// than that of the other as unsigned integers. /// The relationship is meaningless in terms of DNS protocol; the only /// reason we define this method is that RRType objects can be stored in /// STL containers without requiring user-defined less-than relationship. /// We therefore don't define other comparison operators. /// /// This method never throws an exception. /// /// \param other the \c RRType object to compare against. /// \return true if \c this RRType is less than the \c other; otherwise /// false. bool operator<(const RRType& other) const { return (typecode_ < other.typecode_); } //@} // (Some) Well-known RRtype "constants" // Note: we'll auto-generate these in a near future version. These are // hard-coded for a proof of concept. static const RRType& A(); static const RRType& NS(); `````` JINMEI Tatuya committed Jan 26, 2010 256 257 258 259 `````` static const RRType& MX(); static const RRType& SOA(); static const RRType& TXT(); static const RRType& AAAA(); `````` JINMEI Tatuya committed Jan 26, 2010 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 `````` private: uint16_t typecode_; }; // We'll probably auto generate this code. But in this preliminary version // we simply hard code some selected types. // Describing these will be deferred until that point. inline const RRType& RRType::A() { static RRType rrtype(1); return (rrtype); } inline const RRType& RRType::NS() { static RRType rrtype(2); return (rrtype); } `````` JINMEI Tatuya committed Jan 26, 2010 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 ``````inline const RRType& RRType::SOA() { static RRType rrtype(6); return (rrtype); } inline const RRType& RRType::MX() { static RRType rrtype(15); return (rrtype); } inline const RRType& RRType::TXT() { static RRType rrtype(16); return (rrtype); } inline const RRType& RRType::AAAA() { static RRType rrtype(28); return (rrtype); } `````` JINMEI Tatuya committed Jan 26, 2010 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 ``````/// /// \brief Insert the \c RRType as a string into stream. /// /// This method convert the \c rrtype into a string and inserts it into the /// output stream \c os. /// /// This function overloads the global operator<< to behave as described in /// ostream::operator<< but applied to \c RRType objects. /// /// \param os A \c std::ostream object on which the insertion operation is /// performed. /// \param rrtype The \c RRType object output by the operation. /// \return A reference to the same \c std::ostream object referenced by /// parameter \c os after the insertion operation. std::ostream& operator<<(std::ostream& os, const RRType& rrtype); } } #endif // __RRTYPE_H // Local Variables: // mode: c++ // End: ``````