rp_17.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <dns/messagerenderer.h>
  18. #include <dns/name.h>
  19. #include <dns/rdata.h>
  20. #include <dns/rdataclass.h>
  21. #include <dns/rdata/generic/detail/lexer_util.h>
  22. using namespace std;
  23. using namespace isc::dns;
  24. using namespace isc::util;
  25. using isc::dns::rdata::generic::detail::createNameFromLexer;
  26. // BEGIN_ISC_NAMESPACE
  27. // BEGIN_RDATA_NAMESPACE
  28. // helper function for string and lexer constructors
  29. void RP::constructFromLexer(MasterLexer& lexer, const Name* origin) {
  30. mailbox_ = createNameFromLexer(lexer, origin);
  31. text_ = createNameFromLexer(lexer, origin);
  32. }
  33. /// \brief Constructor from string.
  34. ///
  35. /// \c rp_str must be formatted as follows:
  36. /// \code <mailbox name> <text name>
  37. /// \endcode
  38. /// where both fields must represent a valid domain name.
  39. ///
  40. /// \throw InvalidRdataText The number of RDATA fields (must be 2) is
  41. /// incorrect.
  42. /// \throw std::bad_alloc Memory allocation for names fails.
  43. /// \throw Other The constructor of the \c Name class will throw if the
  44. /// given name is invalid.
  45. RP::RP(const std::string& rp_str) :
  46. // We cannot construct both names in the initialization list due to the
  47. // necessary text processing, so we have to initialize them with a dummy
  48. // name and replace them later.
  49. mailbox_(Name::ROOT_NAME()), text_(Name::ROOT_NAME())
  50. {
  51. try {
  52. std::istringstream ss(rp_str);
  53. MasterLexer lexer;
  54. lexer.pushSource(ss);
  55. constructFromLexer(lexer, NULL);
  56. if (lexer.getNextToken().getType() != MasterToken::END_OF_FILE) {
  57. isc_throw(InvalidRdataText, "extra input text for RP: "
  58. << rp_str);
  59. }
  60. } catch (const MasterLexer::LexerError& ex) {
  61. isc_throw(InvalidRdataText, "Failed to construct RP from '" <<
  62. rp_str << "': " << ex.what());
  63. }
  64. }
  65. /// \brief Constructor with a context of MasterLexer.
  66. ///
  67. /// The \c lexer should point to the beginning of valid textual representation
  68. /// of an RP RDATA. The MAILBOX and TEXT fields can be non-absolute if \c
  69. /// origin is non-NULL, in which case \c origin is used to make them absolute.
  70. ///
  71. /// \throw MasterLexer::LexerError General parsing error such as missing field.
  72. /// \throw Other Exceptions from the Name and constructors if construction of
  73. /// textual fields as these objects fail.
  74. ///
  75. /// \param lexer A \c MasterLexer object parsing a master file for the
  76. /// RDATA to be created
  77. /// \param origin If non NULL, specifies the origin of SERVER when it
  78. /// is non-absolute.
  79. RP::RP(MasterLexer& lexer, const Name* origin,
  80. MasterLoader::Options, MasterLoaderCallbacks&) :
  81. mailbox_(Name::ROOT_NAME()), text_(Name::ROOT_NAME())
  82. {
  83. constructFromLexer(lexer, origin);
  84. }
  85. /// \brief Constructor from wire-format data.
  86. ///
  87. /// This constructor doesn't check the validity of the second parameter (rdata
  88. /// length) for parsing.
  89. /// If necessary, the caller will check consistency.
  90. ///
  91. /// \throw std::bad_alloc Memory allocation for names fails.
  92. /// \throw Other The constructor of the \c Name class will throw if the
  93. /// names in the wire is invalid.
  94. RP::RP(InputBuffer& buffer, size_t) : mailbox_(buffer), text_(buffer) {
  95. }
  96. /// \brief Copy constructor.
  97. ///
  98. /// \throw std::bad_alloc Memory allocation fails in copying internal
  99. /// member variables (this should be very rare).
  100. RP::RP(const RP& other) :
  101. Rdata(), mailbox_(other.mailbox_), text_(other.text_)
  102. {}
  103. /// \brief Convert the \c RP to a string.
  104. ///
  105. /// The output of this method is formatted as described in the "from string"
  106. /// constructor (\c RP(const std::string&))).
  107. ///
  108. /// \throw std::bad_alloc Internal resource allocation fails.
  109. ///
  110. /// \return A \c string object that represents the \c RP object.
  111. std::string
  112. RP::toText() const {
  113. return (mailbox_.toText() + " " + text_.toText());
  114. }
  115. /// \brief Render the \c RP in the wire format without name compression.
  116. ///
  117. /// \throw std::bad_alloc Internal resource allocation fails.
  118. ///
  119. /// \param buffer An output buffer to store the wire data.
  120. void
  121. RP::toWire(OutputBuffer& buffer) const {
  122. mailbox_.toWire(buffer);
  123. text_.toWire(buffer);
  124. }
  125. /// \brief Render the \c RP in the wire format with taking into account
  126. /// compression.
  127. ///
  128. // Type RP is not "well-known", and name compression must be disabled
  129. // per RFC3597.
  130. ///
  131. /// \throw std::bad_alloc Internal resource allocation fails.
  132. ///
  133. /// \param renderer DNS message rendering context that encapsulates the
  134. /// output buffer and name compression information.
  135. void
  136. RP::toWire(AbstractMessageRenderer& renderer) const {
  137. renderer.writeName(mailbox_, false);
  138. renderer.writeName(text_, false);
  139. }
  140. /// \brief Compare two instances of \c RP RDATA.
  141. ///
  142. /// See documentation in \c Rdata.
  143. int
  144. RP::compare(const Rdata& other) const {
  145. const RP& other_rp = dynamic_cast<const RP&>(other);
  146. const int cmp = compareNames(mailbox_, other_rp.mailbox_);
  147. if (cmp != 0) {
  148. return (cmp);
  149. }
  150. return (compareNames(text_, other_rp.text_));
  151. }
  152. // END_RDATA_NAMESPACE
  153. // END_ISC_NAMESPACE