henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_SOCKET_ADDRESS_H_ |
| 12 | #define RTC_BASE_SOCKET_ADDRESS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <string> |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 15 | |
| 16 | #include "absl/strings/string_view.h" |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 | [diff] [blame] | 17 | #ifdef WEBRTC_UNIT_TEST |
Jonas Olsson | 3e18c82 | 2018-04-18 08:11:07 | [diff] [blame] | 18 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 | [diff] [blame] | 19 | #endif // WEBRTC_UNIT_TEST |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "rtc_base/ip_address.h" |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 21 | #include "rtc_base/system/rtc_export.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 22 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 23 | #undef SetPort |
| 24 | |
| 25 | struct sockaddr_in; |
| 26 | struct sockaddr_storage; |
| 27 | |
| 28 | namespace rtc { |
| 29 | |
| 30 | // Records an IP address and port. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 31 | class RTC_EXPORT SocketAddress { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 32 | public: |
| 33 | // Creates a nil address. |
| 34 | SocketAddress(); |
| 35 | |
| 36 | // Creates the address with the given host and port. Host may be a |
| 37 | // literal IP string or a hostname to be resolved later. |
| 38 | // DCHECKs that port is in valid range (0 to 2^16-1). |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 39 | SocketAddress(absl::string_view hostname, int port); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 40 | |
| 41 | // Creates the address with the given IP and port. |
| 42 | // IP is given as an integer in host byte order. V4 only, to be deprecated. |
| 43 | // DCHECKs that port is in valid range (0 to 2^16-1). |
| 44 | SocketAddress(uint32_t ip_as_host_order_integer, int port); |
| 45 | |
| 46 | // Creates the address with the given IP and port. |
| 47 | // DCHECKs that port is in valid range (0 to 2^16-1). |
| 48 | SocketAddress(const IPAddress& ip, int port); |
| 49 | |
| 50 | // Creates a copy of the given address. |
| 51 | SocketAddress(const SocketAddress& addr); |
| 52 | |
| 53 | // Resets to the nil address. |
| 54 | void Clear(); |
| 55 | |
| 56 | // Determines if this is a nil address (empty hostname, any IP, null port) |
| 57 | bool IsNil() const; |
| 58 | |
| 59 | // Returns true if ip and port are set. |
| 60 | bool IsComplete() const; |
| 61 | |
| 62 | // Replaces our address with the given one. |
| 63 | SocketAddress& operator=(const SocketAddress& addr); |
| 64 | |
| 65 | // Changes the IP of this address to the given one, and clears the hostname |
| 66 | // IP is given as an integer in host byte order. V4 only, to be deprecated.. |
| 67 | void SetIP(uint32_t ip_as_host_order_integer); |
| 68 | |
| 69 | // Changes the IP of this address to the given one, and clears the hostname. |
| 70 | void SetIP(const IPAddress& ip); |
| 71 | |
| 72 | // Changes the hostname of this address to the given one. |
| 73 | // Does not resolve the address; use Resolve to do so. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 74 | void SetIP(absl::string_view hostname); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 75 | |
| 76 | // Sets the IP address while retaining the hostname. Useful for bypassing |
| 77 | // DNS for a pre-resolved IP. |
| 78 | // IP is given as an integer in host byte order. V4 only, to be deprecated. |
| 79 | void SetResolvedIP(uint32_t ip_as_host_order_integer); |
| 80 | |
| 81 | // Sets the IP address while retaining the hostname. Useful for bypassing |
| 82 | // DNS for a pre-resolved IP. |
| 83 | void SetResolvedIP(const IPAddress& ip); |
| 84 | |
| 85 | // Changes the port of this address to the given one. |
| 86 | // DCHECKs that port is in valid range (0 to 2^16-1). |
| 87 | void SetPort(int port); |
| 88 | |
| 89 | // Returns the hostname. |
| 90 | const std::string& hostname() const { return hostname_; } |
| 91 | |
| 92 | // Returns the IP address as a host byte order integer. |
| 93 | // Returns 0 for non-v4 addresses. |
| 94 | uint32_t ip() const; |
| 95 | |
| 96 | const IPAddress& ipaddr() const; |
| 97 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 98 | int family() const { return ip_.family(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 99 | |
| 100 | // Returns the port part of this address. |
| 101 | uint16_t port() const; |
| 102 | |
| 103 | // Returns the scope ID associated with this address. Scope IDs are a |
| 104 | // necessary addition to IPv6 link-local addresses, with different network |
| 105 | // interfaces having different scope-ids for their link-local addresses. |
| 106 | // IPv4 address do not have scope_ids and sockaddr_in structures do not have |
| 107 | // a field for them. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 108 | int scope_id() const { return scope_id_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 109 | void SetScopeID(int id) { scope_id_ = id; } |
| 110 | |
| 111 | // Returns the 'host' portion of the address (hostname or IP) in a form |
| 112 | // suitable for use in a URI. If both IP and hostname are present, hostname |
| 113 | // is preferred. IPv6 addresses are enclosed in square brackets ('[' and ']'). |
| 114 | std::string HostAsURIString() const; |
| 115 | |
| 116 | // Same as HostAsURIString but anonymizes IP addresses by hiding the last |
| 117 | // part. |
| 118 | std::string HostAsSensitiveURIString() const; |
| 119 | |
| 120 | // Returns the port as a string. |
| 121 | std::string PortAsString() const; |
| 122 | |
| 123 | // Returns hostname:port or [hostname]:port. |
| 124 | std::string ToString() const; |
| 125 | |
| 126 | // Same as ToString but anonymizes it by hiding the last part. |
| 127 | std::string ToSensitiveString() const; |
| 128 | |
Yury Yarashevich | 41010f9 | 2023-01-19 16:56:03 | [diff] [blame] | 129 | // Returns sensitive description of address in a form which both includes |
| 130 | // resolved and unresolved addresses based on their availability. |
| 131 | std::string ToSensitiveNameAndAddressString() const; |
Yura Yaroshevich | f97afd6 | 2021-04-12 12:56:08 | [diff] [blame] | 132 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 133 | // Parses hostname:port and [hostname]:port. |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 134 | bool FromString(absl::string_view str); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 135 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 | [diff] [blame] | 136 | #ifdef WEBRTC_UNIT_TEST |
Jonas Olsson | 3e18c82 | 2018-04-18 08:11:07 | [diff] [blame] | 137 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 138 | std::ostream& os) { // no-presubmit-check TODO(webrtc:8982) |
| 139 | return os << HostAsURIString() << ":" << port(); |
| 140 | } |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 | [diff] [blame] | 141 | #endif // WEBRTC_UNIT_TEST |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 142 | |
| 143 | // Determines whether this represents a missing / any IP address. |
| 144 | // That is, 0.0.0.0 or ::. |
| 145 | // Hostname and/or port may be set. |
| 146 | bool IsAnyIP() const; |
| 147 | |
| 148 | // Determines whether the IP address refers to a loopback address. |
| 149 | // For v4 addresses this means the address is in the range 127.0.0.0/8. |
| 150 | // For v6 addresses this means the address is ::1. |
| 151 | bool IsLoopbackIP() const; |
| 152 | |
| 153 | // Determines whether the IP address is in one of the private ranges: |
| 154 | // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12. |
| 155 | // For v6: FE80::/16 and ::1. |
| 156 | bool IsPrivateIP() const; |
| 157 | |
| 158 | // Determines whether the hostname has been resolved to an IP. |
| 159 | bool IsUnresolvedIP() const; |
| 160 | |
| 161 | // Determines whether this address is identical to the given one. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 162 | bool operator==(const SocketAddress& addr) const; |
| 163 | inline bool operator!=(const SocketAddress& addr) const { |
| 164 | return !this->operator==(addr); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Compares based on IP and then port. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 168 | bool operator<(const SocketAddress& addr) const; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 169 | |
| 170 | // Determines whether this address has the same IP as the one given. |
| 171 | bool EqualIPs(const SocketAddress& addr) const; |
| 172 | |
| 173 | // Determines whether this address has the same port as the one given. |
| 174 | bool EqualPorts(const SocketAddress& addr) const; |
| 175 | |
| 176 | // Hashes this address into a small number. |
| 177 | size_t Hash() const; |
| 178 | |
| 179 | // Write this address to a sockaddr_in. |
| 180 | // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC. |
| 181 | void ToSockAddr(sockaddr_in* saddr) const; |
| 182 | |
| 183 | // Read this address from a sockaddr_in. |
| 184 | bool FromSockAddr(const sockaddr_in& saddr); |
| 185 | |
| 186 | // Read and write the address to/from a sockaddr_storage. |
| 187 | // Dual stack version always sets family to AF_INET6, and maps v4 addresses. |
| 188 | // The other version doesn't map, and outputs an AF_INET address for |
| 189 | // v4 or mapped addresses, and AF_INET6 addresses for others. |
| 190 | // Returns the size of the sockaddr_in or sockaddr_in6 structure that is |
| 191 | // written to the sockaddr_storage, or zero on failure. |
| 192 | size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const; |
| 193 | size_t ToSockAddrStorage(sockaddr_storage* saddr) const; |
| 194 | |
| 195 | private: |
| 196 | std::string hostname_; |
| 197 | IPAddress ip_; |
| 198 | uint16_t port_; |
| 199 | int scope_id_; |
| 200 | bool literal_; // Indicates that 'hostname_' contains a literal IP string. |
| 201 | }; |
| 202 | |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 203 | RTC_EXPORT bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr, |
| 204 | SocketAddress* out); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 205 | SocketAddress EmptySocketAddressWithFamily(int family); |
| 206 | |
| 207 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 208 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 209 | #endif // RTC_BASE_SOCKET_ADDRESS_H_ |