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