addr6.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.h>
  15. #include <arpa/inet.h>
  16. #include <ostream>
  17. #include "dhcp6/addr6.h"
  18. std::ostream&
  19. isc::operator << (std::ostream & out, const isc::Addr6& addr) {
  20. out << addr.getPlain();
  21. return out;
  22. }
  23. using namespace std;
  24. using namespace isc;
  25. Addr6::Addr6(const char* addr, bool plain /*=false*/) {
  26. if (plain) {
  27. inet_pton(AF_INET6, addr, addr_);
  28. } else {
  29. memcpy(addr_, addr, 16);
  30. }
  31. }
  32. Addr6::Addr6() {
  33. memset(addr_, 0, 16);
  34. }
  35. Addr6::Addr6(in6_addr* addr) {
  36. memcpy(addr_, addr, 16);
  37. }
  38. Addr6::Addr6(sockaddr_in6* addr) {
  39. memcpy(addr_, &addr->sin6_addr, 16);
  40. }
  41. bool
  42. Addr6::linkLocal() const {
  43. if ( ( (unsigned char)addr_[0]==0xfe) &&
  44. ( (unsigned char)addr_[1]==0x80) ) {
  45. return (true);
  46. } else {
  47. return (false);
  48. }
  49. }
  50. bool
  51. Addr6::multicast() const {
  52. if ( (unsigned char)addr_[0]==0xff) {
  53. return (true);
  54. } else {
  55. return (false);
  56. }
  57. }
  58. std::string
  59. Addr6::getPlain() const {
  60. char buf[MAX_ADDRESS_STRING_LEN];
  61. inet_ntop(AF_INET6, addr_, buf, MAX_ADDRESS_STRING_LEN);
  62. return (string(buf));
  63. }
  64. bool
  65. Addr6::equals(const Addr6& other) const {
  66. if (!memcmp(addr_, other.addr_, 16)) {
  67. return (true);
  68. } else {
  69. return (false);
  70. }
  71. // return !memcmp() would be shorter, but less readable
  72. }
  73. bool
  74. Addr6::operator==(const Addr6& other) const {
  75. return (equals(other));
  76. }