mx_15.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2010 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. // $Id$
  15. #include <config.h>
  16. #include <string>
  17. #include <boost/lexical_cast.hpp>
  18. #include <exceptions/exceptions.h>
  19. #include <dns/buffer.h>
  20. #include <dns/name.h>
  21. #include <dns/messagerenderer.h>
  22. #include <dns/rdata.h>
  23. #include <dns/rdataclass.h>
  24. using namespace std;
  25. using namespace boost;
  26. // BEGIN_ISC_NAMESPACE
  27. // BEGIN_RDATA_NAMESPACE
  28. MX::MX(InputBuffer& buffer, size_t rdata_len UNUSED_PARAM) :
  29. preference_(buffer.readUint16()), mxname_(buffer)
  30. {
  31. // we don't need rdata_len for parsing. if necessary, the caller will
  32. // check consistency.
  33. }
  34. MX::MX(const std::string& mx_str) :
  35. preference_(0), mxname_(".")
  36. {
  37. istringstream iss(mx_str);
  38. uint16_t pref;
  39. string mxname;
  40. iss >> pref >> mxname;
  41. if (iss.bad() || iss.fail() || !iss.eof()) {
  42. isc_throw(InvalidRdataText, "Invalid MX text format");
  43. }
  44. preference_ = pref;
  45. mxname_ = Name(mxname);
  46. }
  47. MX::MX(uint16_t preference, const Name& mxname) :
  48. preference_(preference), mxname_(mxname)
  49. {}
  50. MX::MX(const MX& other) :
  51. Rdata(), preference_(other.preference_), mxname_(other.mxname_)
  52. {}
  53. void
  54. MX::toWire(OutputBuffer& buffer) const
  55. {
  56. buffer.writeUint16(preference_);
  57. mxname_.toWire(buffer);
  58. }
  59. void
  60. MX::toWire(MessageRenderer& renderer) const
  61. {
  62. renderer.writeUint16(preference_);
  63. renderer.writeName(mxname_);
  64. }
  65. string
  66. MX::toText() const
  67. {
  68. return (lexical_cast<string>(preference_) + " " + mxname_.toText());
  69. }
  70. int
  71. MX::compare(const Rdata& other) const
  72. {
  73. const MX& other_mx = dynamic_cast<const MX&>(other);
  74. if (preference_ < other_mx.preference_) {
  75. return (-1);
  76. } else if (preference_ > other_mx.preference_) {
  77. return (1);
  78. }
  79. return (compareNames(mxname_, other_mx.mxname_));
  80. }
  81. const Name&
  82. MX::getMXName() const
  83. {
  84. return (mxname_);
  85. }
  86. uint16_t
  87. MX::getMXPref() const
  88. {
  89. return (preference_);
  90. }
  91. // END_RDATA_NAMESPACE
  92. // END_ISC_NAMESPACE