afsdb_18.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright (C) 2011-2013 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. #include <string>
  15. #include <sstream>
  16. #include <util/buffer.h>
  17. #include <util/strutil.h>
  18. #include <dns/name.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/rdata.h>
  21. #include <dns/rdataclass.h>
  22. #include <boost/lexical_cast.hpp>
  23. #include <dns/rdata/generic/detail/lexer_util.h>
  24. using namespace std;
  25. using boost::lexical_cast;
  26. using namespace isc::util;
  27. using isc::dns::rdata::generic::detail::createNameFromLexer;
  28. // BEGIN_ISC_NAMESPACE
  29. // BEGIN_RDATA_NAMESPACE
  30. /// \brief Constructor from string.
  31. ///
  32. /// \c afsdb_str must be formatted as follows:
  33. /// \code <subtype> <server name>
  34. /// \endcode
  35. /// where server name field must represent a valid domain name.
  36. ///
  37. /// An example of valid string is:
  38. /// \code "1 server.example.com." \endcode
  39. ///
  40. /// <b>Exceptions</b>
  41. ///
  42. /// \exception InvalidRdataText The number of RDATA fields (must be 2) is
  43. /// incorrect.
  44. /// \exception std::bad_alloc Memory allocation fails.
  45. /// \exception Other The constructor of the \c Name class will throw if the
  46. /// names in the string is invalid.
  47. AFSDB::AFSDB(const std::string& afsdb_str) :
  48. subtype_(0), server_(Name::ROOT_NAME())
  49. {
  50. try {
  51. std::istringstream ss(afsdb_str);
  52. MasterLexer lexer;
  53. lexer.pushSource(ss);
  54. createFromLexer(lexer, NULL);
  55. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  56. isc_throw(InvalidRdataText, "extra input text for AFSDB: "
  57. << afsdb_str);
  58. }
  59. } catch (const MasterLexer::LexerError& ex) {
  60. isc_throw(InvalidRdataText, "Failed to construct AFSDB from '" <<
  61. afsdb_str << "': " << ex.what());
  62. }
  63. }
  64. /// \brief Constructor with a context of MasterLexer.
  65. ///
  66. /// The \c lexer should point to the beginning of valid textual representation
  67. /// of an AFSDB RDATA. The SERVER field can be non-absolute if \c origin
  68. /// is non-NULL, in which case \c origin is used to make it absolute.
  69. /// It must not be represented as a quoted string.
  70. ///
  71. /// The SUBTYPE field must be a valid decimal representation of an
  72. /// unsigned 16-bit integer.
  73. ///
  74. /// \throw MasterLexer::LexerError General parsing error such as missing field.
  75. /// \throw Other Exceptions from the Name and RRTTL constructors if
  76. /// construction of textual fields as these objects fail.
  77. ///
  78. /// \param lexer A \c MasterLexer object parsing a master file for the
  79. /// RDATA to be created
  80. /// \param origin If non NULL, specifies the origin of SERVER when it
  81. /// is non-absolute.
  82. AFSDB::AFSDB(MasterLexer& lexer, const Name* origin,
  83. MasterLoader::Options, MasterLoaderCallbacks&) :
  84. subtype_(0), server_(".")
  85. {
  86. createFromLexer(lexer, origin);
  87. }
  88. void
  89. AFSDB::createFromLexer(MasterLexer& lexer, const Name* origin)
  90. {
  91. const uint32_t num = lexer.getNextToken(MasterToken::NUMBER).getNumber();
  92. if (num > 65535) {
  93. isc_throw(InvalidRdataText, "Invalid AFSDB subtype: " << num);
  94. }
  95. subtype_ = static_cast<uint16_t>(num);
  96. server_ = createNameFromLexer(lexer, origin);
  97. }
  98. /// \brief Constructor from wire-format data.
  99. ///
  100. /// This constructor doesn't check the validity of the second parameter (rdata
  101. /// length) for parsing.
  102. /// If necessary, the caller will check consistency.
  103. ///
  104. /// \exception std::bad_alloc Memory allocation fails.
  105. /// \exception Other The constructor of the \c Name class will throw if the
  106. /// names in the wire is invalid.
  107. AFSDB::AFSDB(InputBuffer& buffer, size_t) :
  108. subtype_(buffer.readUint16()), server_(buffer)
  109. {}
  110. /// \brief Copy constructor.
  111. ///
  112. /// \exception std::bad_alloc Memory allocation fails in copying internal
  113. /// member variables (this should be very rare).
  114. AFSDB::AFSDB(const AFSDB& other) :
  115. Rdata(), subtype_(other.subtype_), server_(other.server_)
  116. {}
  117. AFSDB&
  118. AFSDB::operator=(const AFSDB& source) {
  119. subtype_ = source.subtype_;
  120. server_ = source.server_;
  121. return (*this);
  122. }
  123. /// \brief Convert the \c AFSDB to a string.
  124. ///
  125. /// The output of this method is formatted as described in the "from string"
  126. /// constructor (\c AFSDB(const std::string&))).
  127. ///
  128. /// \exception std::bad_alloc Internal resource allocation fails.
  129. ///
  130. /// \return A \c string object that represents the \c AFSDB object.
  131. string
  132. AFSDB::toText() const {
  133. return (lexical_cast<string>(subtype_) + " " + server_.toText());
  134. }
  135. /// \brief Render the \c AFSDB in the wire format without name compression.
  136. ///
  137. /// \exception std::bad_alloc Internal resource allocation fails.
  138. ///
  139. /// \param buffer An output buffer to store the wire data.
  140. void
  141. AFSDB::toWire(OutputBuffer& buffer) const {
  142. buffer.writeUint16(subtype_);
  143. server_.toWire(buffer);
  144. }
  145. /// \brief Render the \c AFSDB in the wire format with taking into account
  146. /// compression.
  147. ///
  148. /// As specified in RFC3597, TYPE AFSDB is not "well-known", the server
  149. /// field (domain name) will not be compressed.
  150. ///
  151. /// \exception std::bad_alloc Internal resource allocation fails.
  152. ///
  153. /// \param renderer DNS message rendering context that encapsulates the
  154. /// output buffer and name compression information.
  155. void
  156. AFSDB::toWire(AbstractMessageRenderer& renderer) const {
  157. renderer.writeUint16(subtype_);
  158. renderer.writeName(server_, false);
  159. }
  160. /// \brief Compare two instances of \c AFSDB RDATA.
  161. ///
  162. /// See documentation in \c Rdata.
  163. int
  164. AFSDB::compare(const Rdata& other) const {
  165. const AFSDB& other_afsdb = dynamic_cast<const AFSDB&>(other);
  166. if (subtype_ < other_afsdb.subtype_) {
  167. return (-1);
  168. } else if (subtype_ > other_afsdb.subtype_) {
  169. return (1);
  170. }
  171. return (compareNames(server_, other_afsdb.server_));
  172. }
  173. const Name&
  174. AFSDB::getServer() const {
  175. return (server_);
  176. }
  177. uint16_t
  178. AFSDB::getSubtype() const {
  179. return (subtype_);
  180. }
  181. // END_RDATA_NAMESPACE
  182. // END_ISC_NAMESPACE