henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [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 | |
| 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. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 39 | SocketAddress(uint32_t ip_as_host_order_integer, int port); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 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.. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 61 | void SetIP(uint32_t ip_as_host_order_integer); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 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. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 73 | void SetResolvedIP(uint32_t ip_as_host_order_integer); |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 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. |
| 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. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 87 | uint32_t ip() const; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 88 | |
| 89 | const IPAddress& ipaddr() const; |
| 90 | |
| 91 | int family() const {return ip_.family(); } |
| 92 | |
| 93 | // Returns the port part of this address. |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 94 | uint16_t port() const; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 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. |
| 101 | int scope_id() const {return scope_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; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 131 | |
| 132 | // Determines whether the IP address refers to a loopback address. |
| 133 | // For v4 addresses this means the address is in the range 127.0.0.0/8. |
| 134 | // For v6 addresses this means the address is ::1. |
| 135 | bool IsLoopbackIP() const; |
| 136 | |
| 137 | // Determines whether the IP address is in one of the private ranges: |
| 138 | // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12. |
| 139 | // For v6: FE80::/16 and ::1. |
| 140 | bool IsPrivateIP() const; |
| 141 | |
| 142 | // Determines whether the hostname has been resolved to an IP. |
| 143 | bool IsUnresolvedIP() const; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 144 | |
| 145 | // Determines whether this address is identical to the given one. |
| 146 | bool operator ==(const SocketAddress& addr) const; |
| 147 | inline bool operator !=(const SocketAddress& addr) const { |
| 148 | return !this->operator ==(addr); |
| 149 | } |
| 150 | |
| 151 | // Compares based on IP and then port. |
| 152 | bool operator <(const SocketAddress& addr) const; |
| 153 | |
| 154 | // Determines whether this address has the same IP as the one given. |
| 155 | bool EqualIPs(const SocketAddress& addr) const; |
| 156 | |
| 157 | // Determines whether this address has the same port as the one given. |
| 158 | bool EqualPorts(const SocketAddress& addr) const; |
| 159 | |
| 160 | // Hashes this address into a small number. |
| 161 | size_t Hash() const; |
| 162 | |
| 163 | // Write this address to a sockaddr_in. |
| 164 | // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC. |
| 165 | void ToSockAddr(sockaddr_in* saddr) const; |
| 166 | |
| 167 | // Read this address from a sockaddr_in. |
| 168 | bool FromSockAddr(const sockaddr_in& saddr); |
| 169 | |
| 170 | // Read and write the address to/from a sockaddr_storage. |
| 171 | // Dual stack version always sets family to AF_INET6, and maps v4 addresses. |
| 172 | // The other version doesn't map, and outputs an AF_INET address for |
| 173 | // v4 or mapped addresses, and AF_INET6 addresses for others. |
| 174 | // Returns the size of the sockaddr_in or sockaddr_in6 structure that is |
| 175 | // written to the sockaddr_storage, or zero on failure. |
| 176 | size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const; |
| 177 | size_t ToSockAddrStorage(sockaddr_storage* saddr) const; |
| 178 | |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 179 | private: |
| 180 | std::string hostname_; |
| 181 | IPAddress ip_; |
Peter Boström | 07e22e6 | 2015-10-07 10:23:21 | [diff] [blame] | 182 | uint16_t port_; |
henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 | [diff] [blame] | 183 | int scope_id_; |
| 184 | bool literal_; // Indicates that 'hostname_' contains a literal IP string. |
| 185 | }; |
| 186 | |
| 187 | bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr, |
| 188 | SocketAddress* out); |
| 189 | SocketAddress EmptySocketAddressWithFamily(int family); |
| 190 | |
| 191 | } // namespace rtc |
| 192 | |
| 193 | #endif // WEBRTC_BASE_SOCKETADDRESS_H_ |