minfo_14.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2011 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. using namespace std;
  22. using namespace isc::dns;
  23. // BEGIN_ISC_NAMESPACE
  24. // BEGIN_RDATA_NAMESPACE
  25. /// \brief Constructor from string.
  26. ///
  27. /// \c minfo_str must be formatted as follows:
  28. /// \code <rmailbox name> <emailbox name>
  29. /// \endcode
  30. /// where both fields must represent a valid domain name.
  31. ///
  32. /// An example of valid string is:
  33. /// \code "rmail.example.com. email.example.com." \endcode
  34. ///
  35. /// <b>Exceptions</b>
  36. ///
  37. /// \exception InvalidRdataText The number of RDATA fields (must be 2) is
  38. /// incorrect.
  39. /// \exception std::bad_alloc Memory allocation for names fails.
  40. /// \exception Other The constructor of the \c Name class will throw if the
  41. /// names in the string is invalid.
  42. MINFO::MINFO(const std::string& minfo_str) :
  43. // We cannot construct both names in the initialization list due to the
  44. // necessary text processing, so we have to initialize them with a dummy
  45. // name and replace them later.
  46. rmailbox_(Name::ROOT_NAME()), emailbox_(Name::ROOT_NAME())
  47. {
  48. istringstream iss(minfo_str);
  49. string rmailbox_str, emailbox_str;
  50. iss >> rmailbox_str >> emailbox_str;
  51. // Validation: A valid MINFO RR must have exactly two fields.
  52. if (iss.bad() || iss.fail()) {
  53. isc_throw(InvalidRdataText, "Invalid MINFO text: " << minfo_str);
  54. }
  55. if (!iss.eof()) {
  56. isc_throw(InvalidRdataText, "Invalid MINFO text (redundant field): "
  57. << minfo_str);
  58. }
  59. rmailbox_ = Name(rmailbox_str);
  60. emailbox_ = Name(emailbox_str);
  61. }
  62. /// \brief Constructor from wire-format data.
  63. ///
  64. /// This constructor doesn't check the validity of the second parameter (rdata
  65. /// length) for parsing.
  66. /// If necessary, the caller will check consistency.
  67. ///
  68. /// \exception std::bad_alloc Memory allocation for names fails.
  69. /// \exception Other The constructor of the \c Name class will throw if the
  70. /// names in the wire is invalid.
  71. MINFO::MINFO(InputBuffer& buffer, size_t) :
  72. rmailbox_(buffer), emailbox_(buffer)
  73. {}
  74. /// \brief Copy constructor.
  75. ///
  76. /// \exception std::bad_alloc Memory allocation fails in copying internal
  77. /// member variables (this should be very rare).
  78. MINFO::MINFO(const MINFO& other) :
  79. Rdata(), rmailbox_(other.rmailbox_), emailbox_(other.emailbox_)
  80. {}
  81. /// \brief Convert the \c MINFO to a string.
  82. ///
  83. /// The output of this method is formatted as described in the "from string"
  84. /// constructor (\c MINFO(const std::string&))).
  85. ///
  86. /// \exception std::bad_alloc Internal resource allocation fails.
  87. ///
  88. /// \return A \c string object that represents the \c MINFO object.
  89. std::string
  90. MINFO::toText() const {
  91. return (rmailbox_.toText() + " " + emailbox_.toText());
  92. }
  93. /// \brief Render the \c MINFO in the wire format without name compression.
  94. ///
  95. /// \exception std::bad_alloc Internal resource allocation fails.
  96. ///
  97. /// \param buffer An output buffer to store the wire data.
  98. void
  99. MINFO::toWire(OutputBuffer& buffer) const {
  100. rmailbox_.toWire(buffer);
  101. emailbox_.toWire(buffer);
  102. }
  103. MINFO&
  104. MINFO::operator=(const MINFO& source) {
  105. rmailbox_ = source.rmailbox_;
  106. emailbox_ = source.emailbox_;
  107. return (*this);
  108. }
  109. /// \brief Render the \c MINFO in the wire format with taking into account
  110. /// compression.
  111. ///
  112. /// As specified in RFC3597, TYPE MINFO is "well-known", the rmailbox and
  113. /// emailbox fields (domain names) will be compressed.
  114. ///
  115. /// \exception std::bad_alloc Internal resource allocation fails.
  116. ///
  117. /// \param renderer DNS message rendering context that encapsulates the
  118. /// output buffer and name compression information.
  119. void
  120. MINFO::toWire(AbstractMessageRenderer& renderer) const {
  121. renderer.writeName(rmailbox_);
  122. renderer.writeName(emailbox_);
  123. }
  124. /// \brief Compare two instances of \c MINFO RDATA.
  125. ///
  126. /// See documentation in \c Rdata.
  127. int
  128. MINFO::compare(const Rdata& other) const {
  129. const MINFO& other_minfo = dynamic_cast<const MINFO&>(other);
  130. const int cmp = compareNames(rmailbox_, other_minfo.rmailbox_);
  131. if (cmp != 0) {
  132. return (cmp);
  133. }
  134. return (compareNames(emailbox_, other_minfo.emailbox_));
  135. }
  136. // END_RDATA_NAMESPACE
  137. // END_ISC_NAMESPACE