ip_check.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <sys/socket.h>
  15. #include <exceptions/exceptions.h>
  16. #include <acl/ip_check.h>
  17. using namespace isc;
  18. namespace isc {
  19. namespace acl {
  20. namespace {
  21. const uint8_t*
  22. getSockAddrData(const struct sockaddr& sa) {
  23. const void* sa_ptr = &sa;
  24. const void* data_ptr;
  25. if (sa.sa_family == AF_INET) {
  26. const struct sockaddr_in* sin =
  27. static_cast<const struct sockaddr_in*>(sa_ptr);
  28. data_ptr = &sin->sin_addr;
  29. } else if (sa.sa_family == AF_INET6) {
  30. const struct sockaddr_in6* sin6 =
  31. static_cast<const struct sockaddr_in6*>(sa_ptr);
  32. data_ptr = &sin6->sin6_addr;
  33. } else {
  34. isc_throw(BadValue, "Unsupported address family for IPAddress: " <<
  35. static_cast<int>(sa.sa_family));
  36. }
  37. return (static_cast<const uint8_t*>(data_ptr));
  38. }
  39. }
  40. IPAddress::IPAddress(const struct sockaddr& sa) :
  41. family(sa.sa_family),
  42. data(getSockAddrData(sa)),
  43. length(family == AF_INET ?
  44. sizeof(struct in_addr) : sizeof(struct in6_addr))
  45. {}
  46. } // namespace acl
  47. } // namespace isc