You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

334 lines
8.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/socket_address.h"
  11. #include "rtc_base/numerics/safe_conversions.h"
  12. #if defined(WEBRTC_POSIX)
  13. #include <netinet/in.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #if defined(OPENBSD)
  17. #include <netinet/in_systm.h>
  18. #endif
  19. #if !defined(__native_client__)
  20. #include <netinet/ip.h>
  21. #endif
  22. #include <arpa/inet.h>
  23. #include <netdb.h>
  24. #include <unistd.h>
  25. #endif
  26. #include "rtc_base/byte_order.h"
  27. #include "rtc_base/checks.h"
  28. #include "rtc_base/logging.h"
  29. #include "rtc_base/net_helpers.h"
  30. #include "rtc_base/strings/string_builder.h"
  31. #if defined(WEBRTC_WIN)
  32. #include "rtc_base/win32.h"
  33. #endif
  34. namespace rtc {
  35. SocketAddress::SocketAddress() {
  36. Clear();
  37. }
  38. SocketAddress::SocketAddress(const std::string& hostname, int port) {
  39. SetIP(hostname);
  40. SetPort(port);
  41. }
  42. SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) {
  43. SetIP(IPAddress(ip_as_host_order_integer));
  44. SetPort(port);
  45. }
  46. SocketAddress::SocketAddress(const IPAddress& ip, int port) {
  47. SetIP(ip);
  48. SetPort(port);
  49. }
  50. SocketAddress::SocketAddress(const SocketAddress& addr) {
  51. this->operator=(addr);
  52. }
  53. void SocketAddress::Clear() {
  54. hostname_.clear();
  55. literal_ = false;
  56. ip_ = IPAddress();
  57. port_ = 0;
  58. scope_id_ = 0;
  59. }
  60. bool SocketAddress::IsNil() const {
  61. return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
  62. }
  63. bool SocketAddress::IsComplete() const {
  64. return (!IPIsAny(ip_)) && (0 != port_);
  65. }
  66. SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
  67. hostname_ = addr.hostname_;
  68. ip_ = addr.ip_;
  69. port_ = addr.port_;
  70. literal_ = addr.literal_;
  71. scope_id_ = addr.scope_id_;
  72. return *this;
  73. }
  74. void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) {
  75. hostname_.clear();
  76. literal_ = false;
  77. ip_ = IPAddress(ip_as_host_order_integer);
  78. scope_id_ = 0;
  79. }
  80. void SocketAddress::SetIP(const IPAddress& ip) {
  81. hostname_.clear();
  82. literal_ = false;
  83. ip_ = ip;
  84. scope_id_ = 0;
  85. }
  86. void SocketAddress::SetIP(const std::string& hostname) {
  87. hostname_ = hostname;
  88. literal_ = IPFromString(hostname, &ip_);
  89. if (!literal_) {
  90. ip_ = IPAddress();
  91. }
  92. scope_id_ = 0;
  93. }
  94. void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) {
  95. ip_ = IPAddress(ip_as_host_order_integer);
  96. scope_id_ = 0;
  97. }
  98. void SocketAddress::SetResolvedIP(const IPAddress& ip) {
  99. ip_ = ip;
  100. scope_id_ = 0;
  101. }
  102. void SocketAddress::SetPort(int port) {
  103. port_ = rtc::dchecked_cast<uint16_t>(port);
  104. }
  105. uint32_t SocketAddress::ip() const {
  106. return ip_.v4AddressAsHostOrderInteger();
  107. }
  108. const IPAddress& SocketAddress::ipaddr() const {
  109. return ip_;
  110. }
  111. uint16_t SocketAddress::port() const {
  112. return port_;
  113. }
  114. std::string SocketAddress::HostAsURIString() const {
  115. // If the hostname was a literal IP string, it may need to have square
  116. // brackets added (for SocketAddress::ToString()).
  117. if (!literal_ && !hostname_.empty())
  118. return hostname_;
  119. if (ip_.family() == AF_INET6) {
  120. return "[" + ip_.ToString() + "]";
  121. } else {
  122. return ip_.ToString();
  123. }
  124. }
  125. std::string SocketAddress::HostAsSensitiveURIString() const {
  126. // If the hostname was a literal IP string, it may need to have square
  127. // brackets added (for SocketAddress::ToString()).
  128. if (!literal_ && !hostname_.empty())
  129. return hostname_;
  130. if (ip_.family() == AF_INET6) {
  131. return "[" + ip_.ToSensitiveString() + "]";
  132. } else {
  133. return ip_.ToSensitiveString();
  134. }
  135. }
  136. std::string SocketAddress::PortAsString() const {
  137. return std::to_string(port_);
  138. }
  139. std::string SocketAddress::ToString() const {
  140. char buf[1024];
  141. rtc::SimpleStringBuilder sb(buf);
  142. sb << HostAsURIString() << ":" << port();
  143. return sb.str();
  144. }
  145. std::string SocketAddress::ToSensitiveString() const {
  146. char buf[1024];
  147. rtc::SimpleStringBuilder sb(buf);
  148. sb << HostAsSensitiveURIString() << ":" << port();
  149. return sb.str();
  150. }
  151. bool SocketAddress::FromString(const std::string& str) {
  152. if (str.at(0) == '[') {
  153. std::string::size_type closebracket = str.rfind(']');
  154. if (closebracket != std::string::npos) {
  155. std::string::size_type colon = str.find(':', closebracket);
  156. if (colon != std::string::npos && colon > closebracket) {
  157. SetPort(strtoul(str.substr(colon + 1).c_str(), nullptr, 10));
  158. SetIP(str.substr(1, closebracket - 1));
  159. } else {
  160. return false;
  161. }
  162. }
  163. } else {
  164. std::string::size_type pos = str.find(':');
  165. if (std::string::npos == pos)
  166. return false;
  167. SetPort(strtoul(str.substr(pos + 1).c_str(), nullptr, 10));
  168. SetIP(str.substr(0, pos));
  169. }
  170. return true;
  171. }
  172. bool SocketAddress::IsAnyIP() const {
  173. return IPIsAny(ip_);
  174. }
  175. bool SocketAddress::IsLoopbackIP() const {
  176. return IPIsLoopback(ip_) ||
  177. (IPIsAny(ip_) && 0 == strcmp(hostname_.c_str(), "localhost"));
  178. }
  179. bool SocketAddress::IsPrivateIP() const {
  180. return IPIsPrivate(ip_);
  181. }
  182. bool SocketAddress::IsUnresolvedIP() const {
  183. return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
  184. }
  185. bool SocketAddress::operator==(const SocketAddress& addr) const {
  186. return EqualIPs(addr) && EqualPorts(addr);
  187. }
  188. bool SocketAddress::operator<(const SocketAddress& addr) const {
  189. if (ip_ != addr.ip_)
  190. return ip_ < addr.ip_;
  191. // We only check hostnames if both IPs are ANY or unspecified. This matches
  192. // EqualIPs().
  193. if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_)
  194. return hostname_ < addr.hostname_;
  195. return port_ < addr.port_;
  196. }
  197. bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
  198. return (ip_ == addr.ip_) &&
  199. ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
  200. }
  201. bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
  202. return (port_ == addr.port_);
  203. }
  204. size_t SocketAddress::Hash() const {
  205. size_t h = 0;
  206. h ^= HashIP(ip_);
  207. h ^= port_ | (port_ << 16);
  208. return h;
  209. }
  210. void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
  211. memset(saddr, 0, sizeof(*saddr));
  212. if (ip_.family() != AF_INET) {
  213. saddr->sin_family = AF_UNSPEC;
  214. return;
  215. }
  216. saddr->sin_family = AF_INET;
  217. saddr->sin_port = HostToNetwork16(port_);
  218. if (IPIsAny(ip_)) {
  219. saddr->sin_addr.s_addr = INADDR_ANY;
  220. } else {
  221. saddr->sin_addr = ip_.ipv4_address();
  222. }
  223. }
  224. bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
  225. if (saddr.sin_family != AF_INET)
  226. return false;
  227. SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
  228. SetPort(NetworkToHost16(saddr.sin_port));
  229. literal_ = false;
  230. return true;
  231. }
  232. static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
  233. IPAddress ip,
  234. uint16_t port,
  235. int scope_id) {
  236. memset(addr, 0, sizeof(sockaddr_storage));
  237. addr->ss_family = static_cast<unsigned short>(ip.family());
  238. if (addr->ss_family == AF_INET6) {
  239. sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
  240. saddr->sin6_addr = ip.ipv6_address();
  241. saddr->sin6_port = HostToNetwork16(port);
  242. saddr->sin6_scope_id = scope_id;
  243. return sizeof(sockaddr_in6);
  244. } else if (addr->ss_family == AF_INET) {
  245. sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
  246. saddr->sin_addr = ip.ipv4_address();
  247. saddr->sin_port = HostToNetwork16(port);
  248. return sizeof(sockaddr_in);
  249. }
  250. return 0;
  251. }
  252. size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage* addr) const {
  253. return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
  254. }
  255. size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
  256. return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
  257. }
  258. bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
  259. SocketAddress* out) {
  260. if (!out) {
  261. return false;
  262. }
  263. if (addr.ss_family == AF_INET) {
  264. const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
  265. *out = SocketAddress(IPAddress(saddr->sin_addr),
  266. NetworkToHost16(saddr->sin_port));
  267. return true;
  268. } else if (addr.ss_family == AF_INET6) {
  269. const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
  270. *out = SocketAddress(IPAddress(saddr->sin6_addr),
  271. NetworkToHost16(saddr->sin6_port));
  272. out->SetScopeID(saddr->sin6_scope_id);
  273. return true;
  274. }
  275. return false;
  276. }
  277. SocketAddress EmptySocketAddressWithFamily(int family) {
  278. if (family == AF_INET) {
  279. return SocketAddress(IPAddress(INADDR_ANY), 0);
  280. } else if (family == AF_INET6) {
  281. return SocketAddress(IPAddress(in6addr_any), 0);
  282. }
  283. return SocketAddress();
  284. }
  285. } // namespace rtc