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