[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/ip_endpoint.h" |
| 6 | |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 7 | #include "build/build_config.h" |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame] | 8 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 9 | #if defined(OS_WIN) |
| 10 | #include <winsock2.h> |
tfarina | e39d6031 | 2015-12-24 14:03:57 | [diff] [blame] | 11 | #include <ws2bth.h> |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 12 | #elif defined(OS_POSIX) |
| 13 | #include <netinet/in.h> |
| 14 | #endif |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 15 | |
| 16 | #include <tuple> |
| 17 | |
| 18 | #include "base/logging.h" |
| 19 | #include "base/strings/string_number_conversions.h" |
| 20 | #include "base/sys_byteorder.h" |
| 21 | #include "net/base/ip_address.h" |
tfarina | c5336ca | 2016-01-11 20:49:36 | [diff] [blame] | 22 | |
| 23 | #if defined(OS_WIN) |
| 24 | #include "net/base/winsock_util.h" |
| 25 | #endif |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 26 | |
| 27 | namespace net { |
| 28 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 29 | namespace { |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 30 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 31 | // By definition, socklen_t is large enough to hold both sizes. |
| 32 | const socklen_t kSockaddrInSize = sizeof(struct sockaddr_in); |
| 33 | const socklen_t kSockaddrIn6Size = sizeof(struct sockaddr_in6); |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 34 | |
tfarina | e39d6031 | 2015-12-24 14:03:57 | [diff] [blame] | 35 | // Extracts the address and port portions of a sockaddr. |
| 36 | bool GetIPAddressFromSockAddr(const struct sockaddr* sock_addr, |
| 37 | socklen_t sock_addr_len, |
| 38 | const uint8_t** address, |
| 39 | size_t* address_len, |
| 40 | uint16_t* port) { |
| 41 | if (sock_addr->sa_family == AF_INET) { |
| 42 | if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in))) |
| 43 | return false; |
| 44 | const struct sockaddr_in* addr = |
| 45 | reinterpret_cast<const struct sockaddr_in*>(sock_addr); |
| 46 | *address = reinterpret_cast<const uint8_t*>(&addr->sin_addr); |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 47 | *address_len = IPAddress::kIPv4AddressSize; |
tfarina | e39d6031 | 2015-12-24 14:03:57 | [diff] [blame] | 48 | if (port) |
| 49 | *port = base::NetToHost16(addr->sin_port); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | if (sock_addr->sa_family == AF_INET6) { |
| 54 | if (sock_addr_len < static_cast<socklen_t>(sizeof(struct sockaddr_in6))) |
| 55 | return false; |
| 56 | const struct sockaddr_in6* addr = |
| 57 | reinterpret_cast<const struct sockaddr_in6*>(sock_addr); |
| 58 | *address = reinterpret_cast<const uint8_t*>(&addr->sin6_addr); |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 59 | *address_len = IPAddress::kIPv6AddressSize; |
tfarina | e39d6031 | 2015-12-24 14:03:57 | [diff] [blame] | 60 | if (port) |
| 61 | *port = base::NetToHost16(addr->sin6_port); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | #if defined(OS_WIN) |
| 66 | if (sock_addr->sa_family == AF_BTH) { |
| 67 | if (sock_addr_len < static_cast<socklen_t>(sizeof(SOCKADDR_BTH))) |
| 68 | return false; |
| 69 | const SOCKADDR_BTH* addr = reinterpret_cast<const SOCKADDR_BTH*>(sock_addr); |
| 70 | *address = reinterpret_cast<const uint8_t*>(&addr->btAddr); |
| 71 | *address_len = kBluetoothAddressSize; |
| 72 | if (port) |
| 73 | *port = static_cast<uint16_t>(addr->port); |
| 74 | return true; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | return false; // Unrecognized |sa_family|. |
| 79 | } |
| 80 | |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 81 | } // namespace |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 82 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 83 | IPEndPoint::IPEndPoint() : port_(0) {} |
| 84 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 85 | IPEndPoint::~IPEndPoint() = default; |
[email protected] | 034714b | 2011-03-04 04:09:14 | [diff] [blame] | 86 | |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 87 | IPEndPoint::IPEndPoint(const IPAddress& address, uint16_t port) |
martijn | 98acb9b | 2016-01-27 07:14:07 | [diff] [blame] | 88 | : address_(address), port_(port) {} |
tfarina | 0dc490e | 2015-12-22 20:51:46 | [diff] [blame] | 89 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 90 | IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { |
| 91 | address_ = endpoint.address_; |
| 92 | port_ = endpoint.port_; |
| 93 | } |
| 94 | |
[email protected] | e466aaf | 2012-12-13 01:46:44 | [diff] [blame] | 95 | AddressFamily IPEndPoint::GetFamily() const { |
martijn | 46f5edc | 2016-04-12 13:54:21 | [diff] [blame] | 96 | return GetAddressFamily(address_); |
[email protected] | e466aaf | 2012-12-13 01:46:44 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int IPEndPoint::GetSockAddrFamily() const { |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 100 | switch (address_.size()) { |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 101 | case IPAddress::kIPv4AddressSize: |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 102 | return AF_INET; |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 103 | case IPAddress::kIPv6AddressSize: |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 104 | return AF_INET6; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 105 | default: |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 106 | NOTREACHED() << "Bad IP address"; |
[email protected] | 48e410fb | 2011-04-07 17:10:46 | [diff] [blame] | 107 | return AF_UNSPEC; |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | bool IPEndPoint::ToSockAddr(struct sockaddr* address, |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 112 | socklen_t* address_length) const { |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 113 | DCHECK(address); |
| 114 | DCHECK(address_length); |
| 115 | switch (address_.size()) { |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 116 | case IPAddress::kIPv4AddressSize: { |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 117 | if (*address_length < kSockaddrInSize) |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 118 | return false; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 119 | *address_length = kSockaddrInSize; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 120 | struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in*>(address); |
| 121 | memset(addr, 0, sizeof(struct sockaddr_in)); |
| 122 | addr->sin_family = AF_INET; |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 123 | addr->sin_port = base::HostToNet16(port_); |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 124 | memcpy(&addr->sin_addr, address_.bytes().data(), |
| 125 | IPAddress::kIPv4AddressSize); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 126 | break; |
| 127 | } |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 128 | case IPAddress::kIPv6AddressSize: { |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 129 | if (*address_length < kSockaddrIn6Size) |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 130 | return false; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 131 | *address_length = kSockaddrIn6Size; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 132 | struct sockaddr_in6* addr6 = |
| 133 | reinterpret_cast<struct sockaddr_in6*>(address); |
| 134 | memset(addr6, 0, sizeof(struct sockaddr_in6)); |
| 135 | addr6->sin6_family = AF_INET6; |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 136 | addr6->sin6_port = base::HostToNet16(port_); |
martijn | 56506d0 | 2016-05-03 18:44:00 | [diff] [blame] | 137 | memcpy(&addr6->sin6_addr, address_.bytes().data(), |
| 138 | IPAddress::kIPv6AddressSize); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 139 | break; |
| 140 | } |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 141 | default: |
| 142 | return false; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
[email protected] | 0c36540 | 2012-09-08 09:44:22 | [diff] [blame] | 147 | bool IPEndPoint::FromSockAddr(const struct sockaddr* sock_addr, |
| 148 | socklen_t sock_addr_len) { |
| 149 | DCHECK(sock_addr); |
| 150 | |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 151 | const uint8_t* address; |
[email protected] | 0c36540 | 2012-09-08 09:44:22 | [diff] [blame] | 152 | size_t address_len; |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 153 | uint16_t port; |
[email protected] | 0c36540 | 2012-09-08 09:44:22 | [diff] [blame] | 154 | if (!GetIPAddressFromSockAddr(sock_addr, sock_addr_len, &address, |
| 155 | &address_len, &port)) { |
| 156 | return false; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 157 | } |
[email protected] | 0c36540 | 2012-09-08 09:44:22 | [diff] [blame] | 158 | |
martijn | 98acb9b | 2016-01-27 07:14:07 | [diff] [blame] | 159 | address_ = net::IPAddress(address, address_len); |
[email protected] | 0c36540 | 2012-09-08 09:44:22 | [diff] [blame] | 160 | port_ = port; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 164 | std::string IPEndPoint::ToString() const { |
martijn | 884d81a | 2016-04-30 13:52:18 | [diff] [blame] | 165 | return IPAddressToStringWithPort(address_, port_); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | std::string IPEndPoint::ToStringWithoutPort() const { |
martijn | 98acb9b | 2016-01-27 07:14:07 | [diff] [blame] | 169 | return address_.ToString(); |
[email protected] | 1a2123c6 | 2011-03-12 04:42:39 | [diff] [blame] | 170 | } |
| 171 | |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame] | 172 | bool IPEndPoint::operator<(const IPEndPoint& other) const { |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 173 | // Sort IPv4 before IPv6. |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame] | 174 | if (address_.size() != other.address_.size()) { |
| 175 | return address_.size() < other.address_.size(); |
[email protected] | 17e06f1 | 2011-04-05 23:10:10 | [diff] [blame] | 176 | } |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame] | 177 | return std::tie(address_, port_) < std::tie(other.address_, other.port_); |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 178 | } |
| 179 | |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame] | 180 | bool IPEndPoint::operator==(const IPEndPoint& other) const { |
| 181 | return address_ == other.address_ && port_ == other.port_; |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 182 | } |
| 183 | |
Victor Vasiliev | bee79ea | 2019-05-15 01:25:48 | [diff] [blame^] | 184 | bool IPEndPoint::operator!=(const IPEndPoint& that) const { |
| 185 | return !(*this == that); |
| 186 | } |
| 187 | |
[email protected] | 76ff86a | 2011-03-04 03:21:31 | [diff] [blame] | 188 | } // namespace net |