srv_33.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <iostream>
  15. #include <sstream>
  16. #include <boost/lexical_cast.hpp>
  17. #include <util/buffer.h>
  18. #include <util/strutil.h>
  19. #include <dns/messagerenderer.h>
  20. #include <dns/name.h>
  21. #include <dns/rdata.h>
  22. #include <dns/rdataclass.h>
  23. using namespace std;
  24. using namespace isc::util;
  25. using namespace isc::util::str;
  26. // BEGIN_ISC_NAMESPACE
  27. // BEGIN_RDATA_NAMESPACE
  28. struct SRVImpl {
  29. // straightforward representation of SRV RDATA fields
  30. SRVImpl(uint16_t priority, uint16_t weight, uint16_t port,
  31. const Name& target) :
  32. priority_(priority), weight_(weight), port_(port),
  33. target_(target)
  34. {}
  35. uint16_t priority_;
  36. uint16_t weight_;
  37. uint16_t port_;
  38. Name target_;
  39. };
  40. /// \brief Constructor from string.
  41. ///
  42. /// \c srv_str must be formatted as follows:
  43. /// \code <Priority> <Weight> <Port> <Target>
  44. /// \endcode
  45. /// where
  46. /// - &lt;Priority&gt;, &lt;Weight&gt;, and &lt;Port&gt; are an unsigned
  47. /// 16-bit decimal integer.
  48. /// - &lt;Target&gt; is a valid textual representation of domain name.
  49. ///
  50. /// An example of valid string is:
  51. /// \code "1 5 1500 example.com." \endcode
  52. ///
  53. /// <b>Exceptions</b>
  54. ///
  55. /// If &lt;Target&gt; is not a valid domain name, a corresponding exception
  56. /// from the \c Name class will be thrown;
  57. /// if %any of the other bullet points above is not met, an exception of
  58. /// class \c InvalidRdataText will be thrown.
  59. /// This constructor internally involves resource allocation, and if it fails
  60. /// a corresponding standard exception will be thrown.
  61. SRV::SRV(const std::string& srv_str) :
  62. impl_(NULL)
  63. {
  64. istringstream iss(srv_str);
  65. try {
  66. const int32_t priority = tokenToNum<int32_t, 16>(getToken(iss));
  67. const int32_t weight = tokenToNum<int32_t, 16>(getToken(iss));
  68. const int32_t port = tokenToNum<int32_t, 16>(getToken(iss));
  69. const Name targetname(getToken(iss));
  70. if (!iss.eof()) {
  71. isc_throw(InvalidRdataText, "Unexpected input for SRV RDATA: " <<
  72. srv_str);
  73. }
  74. impl_ = new SRVImpl(priority, weight, port, targetname);
  75. } catch (const StringTokenError& ste) {
  76. isc_throw(InvalidRdataText, "Invalid SRV text: " <<
  77. ste.what() << ": " << srv_str);
  78. }
  79. }
  80. /// \brief Constructor from wire-format data.
  81. ///
  82. /// When a read operation on \c buffer fails (e.g., due to a corrupted
  83. /// message) a corresponding exception from the \c InputBuffer class will
  84. /// be thrown.
  85. /// If the wire-format data does not end with a valid domain name,
  86. /// a corresponding exception from the \c Name class will be thrown.
  87. /// In addition, this constructor internally involves resource allocation,
  88. /// and if it fails a corresponding standard exception will be thrown.
  89. ///
  90. /// According to RFC2782, the Target field must be a non compressed form
  91. /// of domain name. But this implementation accepts a %SRV RR even if that
  92. /// field is compressed as suggested in RFC3597.
  93. ///
  94. /// \param buffer A buffer storing the wire format data.
  95. /// \param rdata_len The length of the RDATA in bytes, normally expected
  96. /// to be the value of the RDLENGTH field of the corresponding RR.
  97. SRV::SRV(InputBuffer& buffer, size_t rdata_len) {
  98. if (rdata_len < 6) {
  99. isc_throw(InvalidRdataLength, "SRV too short");
  100. }
  101. uint16_t priority = buffer.readUint16();
  102. uint16_t weight = buffer.readUint16();
  103. uint16_t port = buffer.readUint16();
  104. const Name targetname(buffer);
  105. impl_ = new SRVImpl(priority, weight, port, targetname);
  106. }
  107. /// \brief The copy constructor.
  108. ///
  109. /// It internally allocates a resource, and if it fails a corresponding
  110. /// standard exception will be thrown.
  111. /// This constructor never throws an exception otherwise.
  112. SRV::SRV(const SRV& source) :
  113. Rdata(), impl_(new SRVImpl(*source.impl_))
  114. {}
  115. SRV&
  116. SRV::operator=(const SRV& source) {
  117. if (impl_ == source.impl_) {
  118. return (*this);
  119. }
  120. SRVImpl* newimpl = new SRVImpl(*source.impl_);
  121. delete impl_;
  122. impl_ = newimpl;
  123. return (*this);
  124. }
  125. SRV::~SRV() {
  126. delete impl_;
  127. }
  128. /// \brief Convert the \c SRV to a string.
  129. ///
  130. /// The output of this method is formatted as described in the "from string"
  131. /// constructor (\c SRV(const std::string&))).
  132. ///
  133. /// If internal resource allocation fails, a corresponding
  134. /// standard exception will be thrown.
  135. ///
  136. /// \return A \c string object that represents the \c SRV object.
  137. string
  138. SRV::toText() const {
  139. using namespace boost;
  140. return (lexical_cast<string>(impl_->priority_) +
  141. " " + lexical_cast<string>(impl_->weight_) +
  142. " " + lexical_cast<string>(impl_->port_) +
  143. " " + impl_->target_.toText());
  144. }
  145. /// \brief Render the \c SRV in the wire format without name compression.
  146. ///
  147. /// If internal resource allocation fails, a corresponding
  148. /// standard exception will be thrown.
  149. /// This method never throws an exception otherwise.
  150. ///
  151. /// \param buffer An output buffer to store the wire data.
  152. void
  153. SRV::toWire(OutputBuffer& buffer) const {
  154. buffer.writeUint16(impl_->priority_);
  155. buffer.writeUint16(impl_->weight_);
  156. buffer.writeUint16(impl_->port_);
  157. impl_->target_.toWire(buffer);
  158. }
  159. /// \brief Render the \c SRV in the wire format with taking into account
  160. /// compression.
  161. ///
  162. /// As specified in RFC2782, the Target field (a domain name) will not be
  163. /// compressed. However, the domain name could be a target of compression
  164. /// of other compressible names (though pretty unlikely), the offset
  165. /// information of the algorithm name may be recorded in \c renderer.
  166. ///
  167. /// If internal resource allocation fails, a corresponding
  168. /// standard exception will be thrown.
  169. /// This method never throws an exception otherwise.
  170. ///
  171. /// \param renderer DNS message rendering context that encapsulates the
  172. /// output buffer and name compression information.
  173. void
  174. SRV::toWire(AbstractMessageRenderer& renderer) const {
  175. renderer.writeUint16(impl_->priority_);
  176. renderer.writeUint16(impl_->weight_);
  177. renderer.writeUint16(impl_->port_);
  178. renderer.writeName(impl_->target_, false);
  179. }
  180. /// \brief Compare two instances of \c SRV RDATA.
  181. ///
  182. /// See documentation in \c Rdata.
  183. int
  184. SRV::compare(const Rdata& other) const {
  185. const SRV& other_srv = dynamic_cast<const SRV&>(other);
  186. if (impl_->priority_ != other_srv.impl_->priority_) {
  187. return (impl_->priority_ < other_srv.impl_->priority_ ? -1 : 1);
  188. }
  189. if (impl_->weight_ != other_srv.impl_->weight_) {
  190. return (impl_->weight_ < other_srv.impl_->weight_ ? -1 : 1);
  191. }
  192. if (impl_->port_ != other_srv.impl_->port_) {
  193. return (impl_->port_ < other_srv.impl_->port_ ? -1 : 1);
  194. }
  195. return (compareNames(impl_->target_, other_srv.impl_->target_));
  196. }
  197. uint16_t
  198. SRV::getPriority() const {
  199. return (impl_->priority_);
  200. }
  201. uint16_t
  202. SRV::getWeight() const {
  203. return (impl_->weight_);
  204. }
  205. uint16_t
  206. SRV::getPort() const {
  207. return (impl_->port_);
  208. }
  209. const Name&
  210. SRV::getTarget() const {
  211. return (impl_->target_);
  212. }
  213. // END_RDATA_NAMESPACE
  214. // END_ISC_NAMESPACE