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.

384 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/socketaddress.h"
  28. #ifdef POSIX
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #if defined(OPENBSD)
  33. #include <netinet/in_systm.h>
  34. #endif
  35. #include <netinet/ip.h>
  36. #include <arpa/inet.h>
  37. #include <netdb.h>
  38. #include <unistd.h>
  39. #endif
  40. #include <sstream>
  41. #include "talk/base/byteorder.h"
  42. #include "talk/base/common.h"
  43. #include "talk/base/logging.h"
  44. #include "talk/base/nethelpers.h"
  45. #ifdef WIN32
  46. #include "talk/base/win32.h"
  47. #endif
  48. namespace talk_base {
  49. SocketAddress::SocketAddress() {
  50. Clear();
  51. }
  52. SocketAddress::SocketAddress(const std::string& hostname, int port) {
  53. SetIP(hostname);
  54. SetPort(port);
  55. }
  56. SocketAddress::SocketAddress(uint32 ip_as_host_order_integer, int port) {
  57. SetIP(IPAddress(ip_as_host_order_integer));
  58. SetPort(port);
  59. }
  60. SocketAddress::SocketAddress(const IPAddress& ip, int port) {
  61. SetIP(ip);
  62. SetPort(port);
  63. }
  64. SocketAddress::SocketAddress(const SocketAddress& addr) {
  65. this->operator=(addr);
  66. }
  67. void SocketAddress::Clear() {
  68. hostname_.clear();
  69. literal_ = false;
  70. ip_ = IPAddress();
  71. port_ = 0;
  72. scope_id_ = 0;
  73. }
  74. bool SocketAddress::IsNil() const {
  75. return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
  76. }
  77. bool SocketAddress::IsComplete() const {
  78. return (!IPIsAny(ip_)) && (0 != port_);
  79. }
  80. SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
  81. hostname_ = addr.hostname_;
  82. ip_ = addr.ip_;
  83. port_ = addr.port_;
  84. literal_ = addr.literal_;
  85. scope_id_ = addr.scope_id_;
  86. return *this;
  87. }
  88. void SocketAddress::SetIP(uint32 ip_as_host_order_integer) {
  89. hostname_.clear();
  90. literal_ = false;
  91. ip_ = IPAddress(ip_as_host_order_integer);
  92. scope_id_ = 0;
  93. }
  94. void SocketAddress::SetIP(const IPAddress& ip) {
  95. hostname_.clear();
  96. literal_ = false;
  97. ip_ = ip;
  98. scope_id_ = 0;
  99. }
  100. void SocketAddress::SetIP(const std::string& hostname) {
  101. hostname_ = hostname;
  102. literal_ = IPFromString(hostname, &ip_);
  103. if (!literal_) {
  104. ip_ = IPAddress();
  105. }
  106. scope_id_ = 0;
  107. }
  108. void SocketAddress::SetResolvedIP(uint32 ip_as_host_order_integer) {
  109. ip_ = IPAddress(ip_as_host_order_integer);
  110. scope_id_ = 0;
  111. }
  112. void SocketAddress::SetResolvedIP(const IPAddress& ip) {
  113. ip_ = ip;
  114. scope_id_ = 0;
  115. }
  116. void SocketAddress::SetPort(int port) {
  117. ASSERT((0 <= port) && (port < 65536));
  118. port_ = port;
  119. }
  120. uint32 SocketAddress::ip() const {
  121. return ip_.v4AddressAsHostOrderInteger();
  122. }
  123. const IPAddress& SocketAddress::ipaddr() const {
  124. return ip_;
  125. }
  126. uint16 SocketAddress::port() const {
  127. return port_;
  128. }
  129. std::string SocketAddress::HostAsURIString() const {
  130. // If the hostname was a literal IP string, it may need to have square
  131. // brackets added (for SocketAddress::ToString()).
  132. if (!literal_ && !hostname_.empty())
  133. return hostname_;
  134. if (ip_.family() == AF_INET6) {
  135. return "[" + ip_.ToString() + "]";
  136. } else {
  137. return ip_.ToString();
  138. }
  139. }
  140. std::string SocketAddress::PortAsString() const {
  141. std::ostringstream ost;
  142. ost << port_;
  143. return ost.str();
  144. }
  145. std::string SocketAddress::ToString() const {
  146. std::ostringstream ost;
  147. ost << *this;
  148. return ost.str();
  149. }
  150. bool SocketAddress::FromString(const std::string& str) {
  151. if (str.at(0) == '[') {
  152. std::string::size_type closebracket = str.rfind(']');
  153. if (closebracket != std::string::npos) {
  154. std::string::size_type colon = str.find(':', closebracket);
  155. if (colon != std::string::npos && colon > closebracket) {
  156. SetPort(strtoul(str.substr(colon + 1).c_str(), NULL, 10));
  157. SetIP(str.substr(1, closebracket - 1));
  158. } else {
  159. return false;
  160. }
  161. }
  162. } else {
  163. std::string::size_type pos = str.find(':');
  164. if (std::string::npos == pos)
  165. return false;
  166. SetPort(strtoul(str.substr(pos + 1).c_str(), NULL, 10));
  167. SetIP(str.substr(0, pos));
  168. }
  169. return true;
  170. }
  171. std::ostream& operator<<(std::ostream& os, const SocketAddress& addr) {
  172. os << addr.HostAsURIString() << ":" << addr.port();
  173. return os;
  174. }
  175. bool SocketAddress::IsAnyIP() const {
  176. return IPIsAny(ip_);
  177. }
  178. bool SocketAddress::IsLoopbackIP() const {
  179. return IPIsLoopback(ip_) || (IPIsAny(ip_) &&
  180. 0 == strcmp(hostname_.c_str(), "localhost"));
  181. }
  182. bool SocketAddress::IsPrivateIP() const {
  183. return IPIsPrivate(ip_);
  184. }
  185. bool SocketAddress::IsUnresolvedIP() const {
  186. return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
  187. }
  188. bool SocketAddress::operator==(const SocketAddress& addr) const {
  189. return EqualIPs(addr) && EqualPorts(addr);
  190. }
  191. bool SocketAddress::operator<(const SocketAddress& addr) const {
  192. if (ip_ < addr.ip_)
  193. return true;
  194. else if (addr.ip_ < ip_)
  195. return false;
  196. // We only check hostnames if both IPs are zero. This matches EqualIPs()
  197. if (addr.IsAnyIP()) {
  198. if (hostname_ < addr.hostname_)
  199. return true;
  200. else if (addr.hostname_ < hostname_)
  201. return false;
  202. }
  203. return port_ < addr.port_;
  204. }
  205. bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
  206. return (ip_ == addr.ip_) &&
  207. ((!IPIsAny(ip_)) || (hostname_ == addr.hostname_));
  208. }
  209. bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
  210. return (port_ == addr.port_);
  211. }
  212. size_t SocketAddress::Hash() const {
  213. size_t h = 0;
  214. h ^= HashIP(ip_);
  215. h ^= port_ | (port_ << 16);
  216. return h;
  217. }
  218. void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
  219. memset(saddr, 0, sizeof(*saddr));
  220. if (ip_.family() != AF_INET) {
  221. saddr->sin_family = AF_UNSPEC;
  222. return;
  223. }
  224. saddr->sin_family = AF_INET;
  225. saddr->sin_port = HostToNetwork16(port_);
  226. if (IPIsAny(ip_)) {
  227. saddr->sin_addr.s_addr = INADDR_ANY;
  228. } else {
  229. saddr->sin_addr = ip_.ipv4_address();
  230. }
  231. }
  232. bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
  233. if (saddr.sin_family != AF_INET)
  234. return false;
  235. SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
  236. SetPort(NetworkToHost16(saddr.sin_port));
  237. literal_ = false;
  238. return true;
  239. }
  240. static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
  241. IPAddress ip, int port, int scope_id) {
  242. memset(addr, 0, sizeof(sockaddr_storage));
  243. addr->ss_family = ip.family();
  244. if (addr->ss_family == AF_INET6) {
  245. sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
  246. saddr->sin6_addr = ip.ipv6_address();
  247. saddr->sin6_port = HostToNetwork16(port);
  248. saddr->sin6_scope_id = scope_id;
  249. return sizeof(sockaddr_in6);
  250. } else if (addr->ss_family == AF_INET) {
  251. sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
  252. saddr->sin_addr = ip.ipv4_address();
  253. saddr->sin_port = HostToNetwork16(port);
  254. return sizeof(sockaddr_in);
  255. }
  256. return 0;
  257. }
  258. size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const {
  259. return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
  260. }
  261. size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
  262. return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
  263. }
  264. std::string SocketAddress::IPToString(uint32 ip_as_host_order_integer) {
  265. std::ostringstream ost;
  266. ost << ((ip_as_host_order_integer >> 24) & 0xff);
  267. ost << '.';
  268. ost << ((ip_as_host_order_integer >> 16) & 0xff);
  269. ost << '.';
  270. ost << ((ip_as_host_order_integer >> 8) & 0xff);
  271. ost << '.';
  272. ost << ((ip_as_host_order_integer >> 0) & 0xff);
  273. return ost.str();
  274. }
  275. bool SocketAddress::StringToIP(const std::string& hostname, uint32* ip) {
  276. in_addr addr;
  277. if (talk_base::inet_pton(AF_INET, hostname.c_str(), &addr) == 0)
  278. return false;
  279. *ip = NetworkToHost32(addr.s_addr);
  280. return true;
  281. }
  282. bool SocketAddress::StringToIP(const std::string& hostname, IPAddress* ip) {
  283. in_addr addr4;
  284. if (talk_base::inet_pton(AF_INET, hostname.c_str(), &addr4) > 0) {
  285. if (ip) {
  286. *ip = IPAddress(addr4);
  287. }
  288. return true;
  289. }
  290. in6_addr addr6;
  291. if (talk_base::inet_pton(AF_INET6, hostname.c_str(), &addr6) > 0) {
  292. if (ip) {
  293. *ip = IPAddress(addr6);
  294. }
  295. return true;
  296. }
  297. return false;
  298. }
  299. uint32 SocketAddress::StringToIP(const std::string& hostname) {
  300. uint32 ip = 0;
  301. StringToIP(hostname, &ip);
  302. return ip;
  303. }
  304. bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
  305. SocketAddress* out) {
  306. if (!out) {
  307. return false;
  308. }
  309. if (addr.ss_family == AF_INET) {
  310. const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
  311. *out = SocketAddress(IPAddress(saddr->sin_addr),
  312. NetworkToHost16(saddr->sin_port));
  313. return true;
  314. } else if (addr.ss_family == AF_INET6) {
  315. const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
  316. *out = SocketAddress(IPAddress(saddr->sin6_addr),
  317. NetworkToHost16(saddr->sin6_port));
  318. out->SetScopeID(saddr->sin6_scope_id);
  319. return true;
  320. }
  321. return false;
  322. }
  323. SocketAddress EmptySocketAddressWithFamily(int family) {
  324. if (family == AF_INET) {
  325. return SocketAddress(IPAddress(INADDR_ANY), 0);
  326. } else if (family == AF_INET6) {
  327. return SocketAddress(IPAddress(in6addr_any), 0);
  328. }
  329. return SocketAddress();
  330. }
  331. } // namespace talk_base