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_SOCKETADDRESS_H_ |
12 | #define RTC_BASE_SOCKETADDRESS_H_ |
13 | |
14 | #include <string> |
15 | #ifdef UNIT_TEST |
16 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
17 | #endif // UNIT_TEST |
18 | #include "rtc_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 | // 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 | #ifdef UNIT_TEST |
130 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
131 | std::ostream& os) { // no-presubmit-check TODO(webrtc:8982) |
132 | return os << HostAsURIString() << ":" << port(); |
133 | } |
134 | #endif // UNIT_TEST |
135 | |
136 | // Determines whether this represents a missing / any IP address. |
137 | // That is, 0.0.0.0 or ::. |
138 | // Hostname and/or port may be set. |
139 | bool IsAnyIP() const; |
140 | |
141 | // Determines whether the IP address refers to a loopback address. |
142 | // For v4 addresses this means the address is in the range 127.0.0.0/8. |
143 | // For v6 addresses this means the address is ::1. |
144 | bool IsLoopbackIP() const; |
145 | |
146 | // Determines whether the IP address is in one of the private ranges: |
147 | // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12. |
148 | // For v6: FE80::/16 and ::1. |
149 | bool IsPrivateIP() const; |
150 | |
151 | // Determines whether the hostname has been resolved to an IP. |
152 | bool IsUnresolvedIP() const; |
153 | |
154 | // Determines whether this address is identical to the given one. |
155 | bool operator==(const SocketAddress& addr) const; |
156 | inline bool operator!=(const SocketAddress& addr) const { |
157 | return !this->operator==(addr); |
158 | } |
159 | |
160 | // Compares based on IP and then port. |
161 | bool operator<(const SocketAddress& addr) const; |
162 | |
163 | // Determines whether this address has the same IP as the one given. |
164 | bool EqualIPs(const SocketAddress& addr) const; |
165 | |
166 | // Determines whether this address has the same port as the one given. |
167 | bool EqualPorts(const SocketAddress& addr) const; |
168 | |
169 | // Hashes this address into a small number. |
170 | size_t Hash() const; |
171 | |
172 | // Write this address to a sockaddr_in. |
173 | // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC. |
174 | void ToSockAddr(sockaddr_in* saddr) const; |
175 | |
176 | // Read this address from a sockaddr_in. |
177 | bool FromSockAddr(const sockaddr_in& saddr); |
178 | |
179 | // Read and write the address to/from a sockaddr_storage. |
180 | // Dual stack version always sets family to AF_INET6, and maps v4 addresses. |
181 | // The other version doesn't map, and outputs an AF_INET address for |
182 | // v4 or mapped addresses, and AF_INET6 addresses for others. |
183 | // Returns the size of the sockaddr_in or sockaddr_in6 structure that is |
184 | // written to the sockaddr_storage, or zero on failure. |
185 | size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const; |
186 | size_t ToSockAddrStorage(sockaddr_storage* saddr) const; |
187 | |
188 | private: |
189 | std::string hostname_; |
190 | IPAddress ip_; |
191 | uint16_t port_; |
192 | int scope_id_; |
193 | bool literal_; // Indicates that 'hostname_' contains a literal IP string. |
194 | }; |
195 | |
196 | bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr, |
197 | SocketAddress* out); |
198 | SocketAddress EmptySocketAddressWithFamily(int family); |
199 | |
200 | } // namespace rtc |
201 | |
202 | #endif // RTC_BASE_SOCKETADDRESS_H_ |
203 | |