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