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 #include "webrtc/base/socketaddress.h"
12
13 #if defined(WEBRTC_POSIX)
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #if defined(OPENBSD)
18 #include <netinet/in_systm.h>
19 #endif
20 #if !defined(__native_client__)
21 #include <netinet/ip.h>
22 #endif
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #include <unistd.h>
26 #endif
27
28 #include <sstream>
29
30 #include "webrtc/base/byteorder.h"
31 #include "webrtc/base/common.h"
32 #include "webrtc/base/logging.h"
33 #include "webrtc/base/nethelpers.h"
34
35 #if defined(WEBRTC_WIN)
36 #include "webrtc/base/win32.h"
37 #endif
38
39 namespace rtc {
40
SocketAddress()41 SocketAddress::SocketAddress() {
42 Clear();
43 }
44
SocketAddress(const std::string & hostname,int port)45 SocketAddress::SocketAddress(const std::string& hostname, int port) {
46 SetIP(hostname);
47 SetPort(port);
48 }
49
SocketAddress(uint32 ip_as_host_order_integer,int port)50 SocketAddress::SocketAddress(uint32 ip_as_host_order_integer, int port) {
51 SetIP(IPAddress(ip_as_host_order_integer));
52 SetPort(port);
53 }
54
SocketAddress(const IPAddress & ip,int port)55 SocketAddress::SocketAddress(const IPAddress& ip, int port) {
56 SetIP(ip);
57 SetPort(port);
58 }
59
SocketAddress(const SocketAddress & addr)60 SocketAddress::SocketAddress(const SocketAddress& addr) {
61 this->operator=(addr);
62 }
63
Clear()64 void SocketAddress::Clear() {
65 hostname_.clear();
66 literal_ = false;
67 ip_ = IPAddress();
68 port_ = 0;
69 scope_id_ = 0;
70 }
71
IsNil() const72 bool SocketAddress::IsNil() const {
73 return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
74 }
75
IsComplete() const76 bool SocketAddress::IsComplete() const {
77 return (!IPIsAny(ip_)) && (0 != port_);
78 }
79
operator =(const SocketAddress & addr)80 SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
81 hostname_ = addr.hostname_;
82 ip_ = addr.ip_;
83 port_ = addr.port_;
84 literal_ = addr.literal_;
85 scope_id_ = addr.scope_id_;
86 return *this;
87 }
88
SetIP(uint32 ip_as_host_order_integer)89 void SocketAddress::SetIP(uint32 ip_as_host_order_integer) {
90 hostname_.clear();
91 literal_ = false;
92 ip_ = IPAddress(ip_as_host_order_integer);
93 scope_id_ = 0;
94 }
95
SetIP(const IPAddress & ip)96 void SocketAddress::SetIP(const IPAddress& ip) {
97 hostname_.clear();
98 literal_ = false;
99 ip_ = ip;
100 scope_id_ = 0;
101 }
102
SetIP(const std::string & hostname)103 void SocketAddress::SetIP(const std::string& hostname) {
104 hostname_ = hostname;
105 literal_ = IPFromString(hostname, &ip_);
106 if (!literal_) {
107 ip_ = IPAddress();
108 }
109 scope_id_ = 0;
110 }
111
SetResolvedIP(uint32 ip_as_host_order_integer)112 void SocketAddress::SetResolvedIP(uint32 ip_as_host_order_integer) {
113 ip_ = IPAddress(ip_as_host_order_integer);
114 scope_id_ = 0;
115 }
116
SetResolvedIP(const IPAddress & ip)117 void SocketAddress::SetResolvedIP(const IPAddress& ip) {
118 ip_ = ip;
119 scope_id_ = 0;
120 }
121
SetPort(int port)122 void SocketAddress::SetPort(int port) {
123 ASSERT((0 <= port) && (port < 65536));
124 port_ = static_cast<uint16>(port);
125 }
126
ip() const127 uint32 SocketAddress::ip() const {
128 return ip_.v4AddressAsHostOrderInteger();
129 }
130
ipaddr() const131 const IPAddress& SocketAddress::ipaddr() const {
132 return ip_;
133 }
134
port() const135 uint16 SocketAddress::port() const {
136 return port_;
137 }
138
HostAsURIString() const139 std::string SocketAddress::HostAsURIString() const {
140 // If the hostname was a literal IP string, it may need to have square
141 // brackets added (for SocketAddress::ToString()).
142 if (!literal_ && !hostname_.empty())
143 return hostname_;
144 if (ip_.family() == AF_INET6) {
145 return "[" + ip_.ToString() + "]";
146 } else {
147 return ip_.ToString();
148 }
149 }
150
HostAsSensitiveURIString() const151 std::string SocketAddress::HostAsSensitiveURIString() const {
152 // If the hostname was a literal IP string, it may need to have square
153 // brackets added (for SocketAddress::ToString()).
154 if (!literal_ && !hostname_.empty())
155 return hostname_;
156 if (ip_.family() == AF_INET6) {
157 return "[" + ip_.ToSensitiveString() + "]";
158 } else {
159 return ip_.ToSensitiveString();
160 }
161 }
162
PortAsString() const163 std::string SocketAddress::PortAsString() const {
164 std::ostringstream ost;
165 ost << port_;
166 return ost.str();
167 }
168
ToString() const169 std::string SocketAddress::ToString() const {
170 std::ostringstream ost;
171 ost << *this;
172 return ost.str();
173 }
174
ToSensitiveString() const175 std::string SocketAddress::ToSensitiveString() const {
176 std::ostringstream ost;
177 ost << HostAsSensitiveURIString() << ":" << port();
178 return ost.str();
179 }
180
FromString(const std::string & str)181 bool SocketAddress::FromString(const std::string& str) {
182 if (str.at(0) == '[') {
183 std::string::size_type closebracket = str.rfind(']');
184 if (closebracket != std::string::npos) {
185 std::string::size_type colon = str.find(':', closebracket);
186 if (colon != std::string::npos && colon > closebracket) {
187 SetPort(strtoul(str.substr(colon + 1).c_str(), NULL, 10));
188 SetIP(str.substr(1, closebracket - 1));
189 } else {
190 return false;
191 }
192 }
193 } else {
194 std::string::size_type pos = str.find(':');
195 if (std::string::npos == pos)
196 return false;
197 SetPort(strtoul(str.substr(pos + 1).c_str(), NULL, 10));
198 SetIP(str.substr(0, pos));
199 }
200 return true;
201 }
202
operator <<(std::ostream & os,const SocketAddress & addr)203 std::ostream& operator<<(std::ostream& os, const SocketAddress& addr) {
204 os << addr.HostAsURIString() << ":" << addr.port();
205 return os;
206 }
207
IsAnyIP() const208 bool SocketAddress::IsAnyIP() const {
209 return IPIsAny(ip_);
210 }
211
IsLoopbackIP() const212 bool SocketAddress::IsLoopbackIP() const {
213 return IPIsLoopback(ip_) || (IPIsAny(ip_) &&
214 0 == strcmp(hostname_.c_str(), "localhost"));
215 }
216
IsPrivateIP() const217 bool SocketAddress::IsPrivateIP() const {
218 return IPIsPrivate(ip_);
219 }
220
IsUnresolvedIP() const221 bool SocketAddress::IsUnresolvedIP() const {
222 return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
223 }
224
operator ==(const SocketAddress & addr) const225 bool SocketAddress::operator==(const SocketAddress& addr) const {
226 return EqualIPs(addr) && EqualPorts(addr);
227 }
228
operator <(const SocketAddress & addr) const229 bool SocketAddress::operator<(const SocketAddress& addr) const {
230 if (ip_ != addr.ip_)
231 return ip_ < addr.ip_;
232
233 // We only check hostnames if both IPs are ANY or unspecified. This matches
234 // EqualIPs().
235 if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_)
236 return hostname_ < addr.hostname_;
237
238 return port_ < addr.port_;
239 }
240
EqualIPs(const SocketAddress & addr) const241 bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
242 return (ip_ == addr.ip_) &&
243 ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
244 }
245
EqualPorts(const SocketAddress & addr) const246 bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
247 return (port_ == addr.port_);
248 }
249
Hash() const250 size_t SocketAddress::Hash() const {
251 size_t h = 0;
252 h ^= HashIP(ip_);
253 h ^= port_ | (port_ << 16);
254 return h;
255 }
256
ToSockAddr(sockaddr_in * saddr) const257 void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
258 memset(saddr, 0, sizeof(*saddr));
259 if (ip_.family() != AF_INET) {
260 saddr->sin_family = AF_UNSPEC;
261 return;
262 }
263 saddr->sin_family = AF_INET;
264 saddr->sin_port = HostToNetwork16(port_);
265 if (IPIsAny(ip_)) {
266 saddr->sin_addr.s_addr = INADDR_ANY;
267 } else {
268 saddr->sin_addr = ip_.ipv4_address();
269 }
270 }
271
FromSockAddr(const sockaddr_in & saddr)272 bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
273 if (saddr.sin_family != AF_INET)
274 return false;
275 SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
276 SetPort(NetworkToHost16(saddr.sin_port));
277 literal_ = false;
278 return true;
279 }
280
ToSockAddrStorageHelper(sockaddr_storage * addr,IPAddress ip,uint16 port,int scope_id)281 static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
282 IPAddress ip, uint16 port, int scope_id) {
283 memset(addr, 0, sizeof(sockaddr_storage));
284 addr->ss_family = static_cast<unsigned short>(ip.family());
285 if (addr->ss_family == AF_INET6) {
286 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
287 saddr->sin6_addr = ip.ipv6_address();
288 saddr->sin6_port = HostToNetwork16(port);
289 saddr->sin6_scope_id = scope_id;
290 return sizeof(sockaddr_in6);
291 } else if (addr->ss_family == AF_INET) {
292 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
293 saddr->sin_addr = ip.ipv4_address();
294 saddr->sin_port = HostToNetwork16(port);
295 return sizeof(sockaddr_in);
296 }
297 return 0;
298 }
299
ToDualStackSockAddrStorage(sockaddr_storage * addr) const300 size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const {
301 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
302 }
303
ToSockAddrStorage(sockaddr_storage * addr) const304 size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
305 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
306 }
307
IPToString(uint32 ip_as_host_order_integer)308 std::string SocketAddress::IPToString(uint32 ip_as_host_order_integer) {
309 return IPAddress(ip_as_host_order_integer).ToString();
310 }
311
IPToSensitiveString(uint32 ip_as_host_order_integer)312 std::string IPToSensitiveString(uint32 ip_as_host_order_integer) {
313 return IPAddress(ip_as_host_order_integer).ToSensitiveString();
314 }
315
StringToIP(const std::string & hostname,uint32 * ip)316 bool SocketAddress::StringToIP(const std::string& hostname, uint32* ip) {
317 in_addr addr;
318 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr) == 0)
319 return false;
320 *ip = NetworkToHost32(addr.s_addr);
321 return true;
322 }
323
StringToIP(const std::string & hostname,IPAddress * ip)324 bool SocketAddress::StringToIP(const std::string& hostname, IPAddress* ip) {
325 in_addr addr4;
326 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr4) > 0) {
327 if (ip) {
328 *ip = IPAddress(addr4);
329 }
330 return true;
331 }
332
333 in6_addr addr6;
334 if (rtc::inet_pton(AF_INET6, hostname.c_str(), &addr6) > 0) {
335 if (ip) {
336 *ip = IPAddress(addr6);
337 }
338 return true;
339 }
340 return false;
341 }
342
StringToIP(const std::string & hostname)343 uint32 SocketAddress::StringToIP(const std::string& hostname) {
344 uint32 ip = 0;
345 StringToIP(hostname, &ip);
346 return ip;
347 }
348
SocketAddressFromSockAddrStorage(const sockaddr_storage & addr,SocketAddress * out)349 bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
350 SocketAddress* out) {
351 if (!out) {
352 return false;
353 }
354 if (addr.ss_family == AF_INET) {
355 const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
356 *out = SocketAddress(IPAddress(saddr->sin_addr),
357 NetworkToHost16(saddr->sin_port));
358 return true;
359 } else if (addr.ss_family == AF_INET6) {
360 const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
361 *out = SocketAddress(IPAddress(saddr->sin6_addr),
362 NetworkToHost16(saddr->sin6_port));
363 out->SetScopeID(saddr->sin6_scope_id);
364 return true;
365 }
366 return false;
367 }
368
EmptySocketAddressWithFamily(int family)369 SocketAddress EmptySocketAddressWithFamily(int family) {
370 if (family == AF_INET) {
371 return SocketAddress(IPAddress(INADDR_ANY), 0);
372 } else if (family == AF_INET6) {
373 return SocketAddress(IPAddress(in6addr_any), 0);
374 }
375 return SocketAddress();
376 }
377
378 } // namespace rtc
379