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