sockaddr_util.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #ifndef __SOCKADDR_UTIL_H_
  15. #define __SOCKADDR_UTIL_H_ 1
  16. #include <cassert>
  17. // This definitions in this file are for the convenience of internal
  18. // implementation and test code, and are not intended to be used publicly.
  19. // The namespace "internal" indicates the intent.
  20. namespace isc {
  21. namespace util {
  22. namespace io {
  23. namespace internal {
  24. inline socklen_t
  25. getSALength(const struct sockaddr& sa) {
  26. if (sa.sa_family == AF_INET) {
  27. return (sizeof(struct sockaddr_in));
  28. } else {
  29. assert(sa.sa_family == AF_INET6);
  30. return (sizeof(struct sockaddr_in6));
  31. }
  32. }
  33. // Lower level C-APIs require conversion between various variants of
  34. // sockaddr's, which is not friendly with C++. The following templates
  35. // are a shortcut of common workaround conversion in such cases.
  36. template <typename SAType>
  37. const struct sockaddr*
  38. convertSockAddr(const SAType* sa) {
  39. const void* p = sa;
  40. return (static_cast<const struct sockaddr*>(p));
  41. }
  42. template <typename SAType>
  43. struct sockaddr*
  44. convertSockAddr(SAType* sa) {
  45. void* p = sa;
  46. return (static_cast<struct sockaddr*>(p));
  47. }
  48. }
  49. }
  50. }
  51. }
  52. #endif // __SOCKADDR_UTIL_H_
  53. // Local Variables:
  54. // mode: c++
  55. // End: