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 11 #ifndef RTC_BASE_SOCKET_ADDRESS_H_ 12 #define RTC_BASE_SOCKET_ADDRESS_H_ 13 14 #include <string> 15 16 #include "absl/strings/string_view.h" 17 #ifdef WEBRTC_UNIT_TEST 18 #include <ostream> // no-presubmit-check TODO(webrtc:8982) 19 #endif // WEBRTC_UNIT_TEST 20 #include "rtc_base/ip_address.h" 21 #include "rtc_base/system/rtc_export.h" 22 23 #undef SetPort 24 25 struct sockaddr_in; 26 struct sockaddr_storage; 27 28 namespace rtc { 29 30 // Records an IP address and port. 31 class RTC_EXPORT SocketAddress { 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). 39 SocketAddress(absl::string_view hostname, int port); 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. 74 void SetIP(absl::string_view hostname); 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. 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 family()98 int family() const { return ip_.family(); } 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. scope_id()108 int scope_id() const { return scope_id_; } SetScopeID(int id)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 129 // Returns hostname:port string if address is resolved, otherwise returns 130 // empty string. 131 std::string ToResolvedSensitiveString() const; 132 133 // Parses hostname:port and [hostname]:port. 134 bool FromString(absl::string_view str); 135 136 #ifdef WEBRTC_UNIT_TEST 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 } 141 #endif // WEBRTC_UNIT_TEST 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. 162 bool operator==(const SocketAddress& addr) const; 163 inline bool operator!=(const SocketAddress& addr) const { 164 return !this->operator==(addr); 165 } 166 167 // Compares based on IP and then port. 168 bool operator<(const SocketAddress& addr) const; 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 203 RTC_EXPORT bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr, 204 SocketAddress* out); 205 SocketAddress EmptySocketAddressWithFamily(int family); 206 207 } // namespace rtc 208 209 #endif // RTC_BASE_SOCKET_ADDRESS_H_ 210