utp_packedsockaddr.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // vim:set ts=4 sw=4 ai:
  2. /*
  3. * Copyright (c) 2010-2013 BitTorrent, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <stdio.h>
  26. #include "utp_types.h"
  27. #include "utp_hash.h"
  28. #include "utp_packedsockaddr.h"
  29. #include "libutp_inet_ntop.h"
  30. byte PackedSockAddr::get_family() const
  31. {
  32. #if defined(__sh__)
  33. return ((_sin6d[0] == 0) && (_sin6d[1] == 0) && (_sin6d[2] == htonl(0xffff)) != 0) ?
  34. AF_INET : AF_INET6;
  35. #else
  36. return (IN6_IS_ADDR_V4MAPPED(&_in._in6addr) != 0) ? AF_INET : AF_INET6;
  37. #endif // defined(__sh__)
  38. }
  39. bool PackedSockAddr::operator==(const PackedSockAddr& rhs) const
  40. {
  41. if (&rhs == this)
  42. return true;
  43. if (_port != rhs._port)
  44. return false;
  45. return memcmp(_sin6, rhs._sin6, sizeof(_sin6)) == 0;
  46. }
  47. bool PackedSockAddr::operator!=(const PackedSockAddr& rhs) const
  48. {
  49. return !(*this == rhs);
  50. }
  51. uint32 PackedSockAddr::compute_hash() const {
  52. return utp_hash_mem(&_in, sizeof(_in)) ^ _port;
  53. }
  54. void PackedSockAddr::set(const SOCKADDR_STORAGE* sa, socklen_t len)
  55. {
  56. if (sa->ss_family == AF_INET) {
  57. assert(len >= sizeof(sockaddr_in));
  58. const sockaddr_in *sin = (sockaddr_in*)sa;
  59. _sin6w[0] = 0;
  60. _sin6w[1] = 0;
  61. _sin6w[2] = 0;
  62. _sin6w[3] = 0;
  63. _sin6w[4] = 0;
  64. _sin6w[5] = 0xffff;
  65. _sin4 = sin->sin_addr.s_addr;
  66. _port = ntohs(sin->sin_port);
  67. } else {
  68. assert(len >= sizeof(sockaddr_in6));
  69. const sockaddr_in6 *sin6 = (sockaddr_in6*)sa;
  70. _in._in6addr = sin6->sin6_addr;
  71. _port = ntohs(sin6->sin6_port);
  72. }
  73. }
  74. PackedSockAddr::PackedSockAddr(const SOCKADDR_STORAGE* sa, socklen_t len)
  75. {
  76. set(sa, len);
  77. }
  78. PackedSockAddr::PackedSockAddr(void)
  79. {
  80. SOCKADDR_STORAGE sa;
  81. socklen_t len = sizeof(SOCKADDR_STORAGE);
  82. memset(&sa, 0, len);
  83. sa.ss_family = AF_INET;
  84. set(&sa, len);
  85. }
  86. SOCKADDR_STORAGE PackedSockAddr::get_sockaddr_storage(socklen_t *len = NULL) const
  87. {
  88. SOCKADDR_STORAGE sa;
  89. const byte family = get_family();
  90. if (family == AF_INET) {
  91. sockaddr_in *sin = (sockaddr_in*)&sa;
  92. if (len) *len = sizeof(sockaddr_in);
  93. memset(sin, 0, sizeof(sockaddr_in));
  94. sin->sin_family = family;
  95. sin->sin_port = htons(_port);
  96. sin->sin_addr.s_addr = _sin4;
  97. } else {
  98. sockaddr_in6 *sin6 = (sockaddr_in6*)&sa;
  99. memset(sin6, 0, sizeof(sockaddr_in6));
  100. if (len) *len = sizeof(sockaddr_in6);
  101. sin6->sin6_family = family;
  102. sin6->sin6_addr = _in._in6addr;
  103. sin6->sin6_port = htons(_port);
  104. }
  105. return sa;
  106. }
  107. // #define addrfmt(x, s) x.fmt(s, sizeof(s))
  108. cstr PackedSockAddr::fmt(str s, size_t len) const
  109. {
  110. memset(s, 0, len);
  111. const byte family = get_family();
  112. str i;
  113. if (family == AF_INET) {
  114. INET_NTOP(family, (uint32*)&_sin4, s, len);
  115. i = s;
  116. while (*++i) {}
  117. } else {
  118. i = s;
  119. *i++ = '[';
  120. INET_NTOP(family, (in6_addr*)&_in._in6addr, i, len-1);
  121. while (*++i) {}
  122. *i++ = ']';
  123. }
  124. snprintf(i, len - (i-s), ":%u", _port);
  125. return s;
  126. }