aaaa_28.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <stdint.h>
  17. #include <string.h>
  18. #include <string>
  19. #include <arpa/inet.h> // XXX: for inet_pton/ntop(), not exist in C++ standards
  20. #include <sys/socket.h> // for AF_INET/AF_INET6
  21. #include <exceptions/exceptions.h>
  22. #include "buffer.h"
  23. #include "exceptions.h"
  24. #include "messagerenderer.h"
  25. #include "rdata.h"
  26. #include "rdataclass.h"
  27. using namespace std;
  28. // BEGIN_ISC_NAMESPACE
  29. // BEGIN_RDATA_NAMESPACE
  30. AAAA::AAAA(const string& addrstr) {
  31. if (inet_pton(AF_INET6, addrstr.c_str(), &addr_) != 1) {
  32. isc_throw(InvalidRdataText,
  33. "IN/AAAA RDATA construction from text failed: "
  34. "Address cannot be converted: " << addrstr);
  35. }
  36. }
  37. AAAA::AAAA(InputBuffer& buffer, size_t rdata_len) {
  38. if (rdata_len != sizeof(addr_)) {
  39. isc_throw(DNSMessageFORMERR,
  40. "IN/AAAA RDATA construction from wire failed: "
  41. "Invalid length: " << rdata_len);
  42. }
  43. if (buffer.getLength() - buffer.getPosition() < sizeof(addr_)) {
  44. isc_throw(DNSMessageFORMERR,
  45. "IN/AAAA RDATA construction from wire failed: "
  46. "insufficient buffer length: "
  47. << buffer.getLength() - buffer.getPosition());
  48. }
  49. buffer.readData(&addr_, sizeof(addr_));
  50. }
  51. AAAA::AAAA(const AAAA& other) : Rdata() {
  52. memcpy(addr_, other.addr_, sizeof(addr_));
  53. }
  54. void
  55. AAAA::toWire(OutputBuffer& buffer) const {
  56. buffer.writeData(&addr_, sizeof(addr_));
  57. }
  58. void
  59. AAAA::toWire(MessageRenderer& renderer) const {
  60. renderer.writeData(&addr_, sizeof(addr_));
  61. }
  62. string
  63. AAAA::toText() const {
  64. char addr_string[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  65. if (inet_ntop(AF_INET6, &addr_, addr_string, sizeof(addr_string)) == NULL) {
  66. isc_throw(Unexpected,
  67. "Failed to convert IN/AAAA RDATA to textual IPv6 address");
  68. }
  69. return (string(addr_string));
  70. }
  71. int
  72. AAAA::compare(const Rdata& other) const {
  73. const AAAA& other_a = dynamic_cast<const AAAA&>(other);
  74. return (memcmp(&addr_, &other_a.addr_, sizeof(addr_)));
  75. }
  76. // END_RDATA_NAMESPACE
  77. // END_ISC_NAMESPACE