blob: 8e2ce6caa92c6acacc0192cdcfcdc85a8abbb277 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/socketaddress.h"
12#include "rtc_base/safe_conversions.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
14#if defined(WEBRTC_POSIX)
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#if defined(OPENBSD)
19#include <netinet/in_systm.h>
20#endif
21#if !defined(__native_client__)
22#include <netinet/ip.h>
23#endif
24#include <arpa/inet.h>
25#include <netdb.h>
26#include <unistd.h>
27#endif
28
29#include <sstream>
30
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/byteorder.h"
32#include "rtc_base/checks.h"
33#include "rtc_base/logging.h"
34#include "rtc_base/nethelpers.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
36#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038#endif
39
40namespace rtc {
41
42SocketAddress::SocketAddress() {
43 Clear();
44}
45
46SocketAddress::SocketAddress(const std::string& hostname, int port) {
47 SetIP(hostname);
48 SetPort(port);
49}
50
Peter Boström0c4e06b2015-10-07 12:23:21 +020051SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 SetIP(IPAddress(ip_as_host_order_integer));
53 SetPort(port);
54}
55
56SocketAddress::SocketAddress(const IPAddress& ip, int port) {
57 SetIP(ip);
58 SetPort(port);
59}
60
61SocketAddress::SocketAddress(const SocketAddress& addr) {
62 this->operator=(addr);
63}
64
65void SocketAddress::Clear() {
66 hostname_.clear();
67 literal_ = false;
68 ip_ = IPAddress();
69 port_ = 0;
70 scope_id_ = 0;
71}
72
73bool SocketAddress::IsNil() const {
74 return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
75}
76
77bool SocketAddress::IsComplete() const {
78 return (!IPIsAny(ip_)) && (0 != port_);
79}
80
81SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
82 hostname_ = addr.hostname_;
83 ip_ = addr.ip_;
84 port_ = addr.port_;
85 literal_ = addr.literal_;
86 scope_id_ = addr.scope_id_;
87 return *this;
88}
89
Peter Boström0c4e06b2015-10-07 12:23:21 +020090void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091 hostname_.clear();
92 literal_ = false;
93 ip_ = IPAddress(ip_as_host_order_integer);
94 scope_id_ = 0;
95}
96
97void SocketAddress::SetIP(const IPAddress& ip) {
98 hostname_.clear();
99 literal_ = false;
100 ip_ = ip;
101 scope_id_ = 0;
102}
103
104void SocketAddress::SetIP(const std::string& hostname) {
105 hostname_ = hostname;
106 literal_ = IPFromString(hostname, &ip_);
107 if (!literal_) {
108 ip_ = IPAddress();
109 }
110 scope_id_ = 0;
111}
112
Peter Boström0c4e06b2015-10-07 12:23:21 +0200113void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 ip_ = IPAddress(ip_as_host_order_integer);
115 scope_id_ = 0;
116}
117
118void SocketAddress::SetResolvedIP(const IPAddress& ip) {
119 ip_ = ip;
120 scope_id_ = 0;
121}
122
123void SocketAddress::SetPort(int port) {
kwibergee89e782017-08-09 17:22:01 -0700124 port_ = rtc::dchecked_cast<uint16_t>(port);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125}
126
Peter Boström0c4e06b2015-10-07 12:23:21 +0200127uint32_t SocketAddress::ip() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 return ip_.v4AddressAsHostOrderInteger();
129}
130
131const IPAddress& SocketAddress::ipaddr() const {
132 return ip_;
133}
134
Peter Boström0c4e06b2015-10-07 12:23:21 +0200135uint16_t SocketAddress::port() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136 return port_;
137}
138
139std::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
151std::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
163std::string SocketAddress::PortAsString() const {
164 std::ostringstream ost;
165 ost << port_;
166 return ost.str();
167}
168
169std::string SocketAddress::ToString() const {
170 std::ostringstream ost;
171 ost << *this;
172 return ost.str();
173}
174
175std::string SocketAddress::ToSensitiveString() const {
176 std::ostringstream ost;
177 ost << HostAsSensitiveURIString() << ":" << port();
178 return ost.str();
179}
180
181bool 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) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800187 SetPort(strtoul(str.substr(colon + 1).c_str(), nullptr, 10));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188 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;
deadbeef37f5ecf2017-02-27 14:06:41 -0800197 SetPort(strtoul(str.substr(pos + 1).c_str(), nullptr, 10));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198 SetIP(str.substr(0, pos));
199 }
200 return true;
201}
202
203std::ostream& operator<<(std::ostream& os, const SocketAddress& addr) {
204 os << addr.HostAsURIString() << ":" << addr.port();
205 return os;
206}
207
208bool SocketAddress::IsAnyIP() const {
209 return IPIsAny(ip_);
210}
211
212bool SocketAddress::IsLoopbackIP() const {
213 return IPIsLoopback(ip_) || (IPIsAny(ip_) &&
214 0 == strcmp(hostname_.c_str(), "localhost"));
215}
216
217bool SocketAddress::IsPrivateIP() const {
218 return IPIsPrivate(ip_);
219}
220
221bool SocketAddress::IsUnresolvedIP() const {
222 return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
223}
224
225bool SocketAddress::operator==(const SocketAddress& addr) const {
226 return EqualIPs(addr) && EqualPorts(addr);
227}
228
229bool SocketAddress::operator<(const SocketAddress& addr) const {
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000230 if (ip_ != addr.ip_)
231 return ip_ < addr.ip_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000233 // 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_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237
238 return port_ < addr.port_;
239}
240
241bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
242 return (ip_ == addr.ip_) &&
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000243 ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244}
245
246bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
247 return (port_ == addr.port_);
248}
249
250size_t SocketAddress::Hash() const {
251 size_t h = 0;
252 h ^= HashIP(ip_);
253 h ^= port_ | (port_ << 16);
254 return h;
255}
256
257void 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
272bool 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
281static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200282 IPAddress ip,
283 uint16_t port,
284 int scope_id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 memset(addr, 0, sizeof(sockaddr_storage));
henrike@webrtc.org17111042014-09-10 22:10:24 +0000286 addr->ss_family = static_cast<unsigned short>(ip.family());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287 if (addr->ss_family == AF_INET6) {
288 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
289 saddr->sin6_addr = ip.ipv6_address();
290 saddr->sin6_port = HostToNetwork16(port);
291 saddr->sin6_scope_id = scope_id;
292 return sizeof(sockaddr_in6);
293 } else if (addr->ss_family == AF_INET) {
294 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
295 saddr->sin_addr = ip.ipv4_address();
296 saddr->sin_port = HostToNetwork16(port);
297 return sizeof(sockaddr_in);
298 }
299 return 0;
300}
301
302size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const {
303 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
304}
305
306size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
307 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
308}
309
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000310bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
311 SocketAddress* out) {
312 if (!out) {
313 return false;
314 }
315 if (addr.ss_family == AF_INET) {
316 const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
317 *out = SocketAddress(IPAddress(saddr->sin_addr),
318 NetworkToHost16(saddr->sin_port));
319 return true;
320 } else if (addr.ss_family == AF_INET6) {
321 const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
322 *out = SocketAddress(IPAddress(saddr->sin6_addr),
323 NetworkToHost16(saddr->sin6_port));
324 out->SetScopeID(saddr->sin6_scope_id);
325 return true;
326 }
327 return false;
328}
329
330SocketAddress EmptySocketAddressWithFamily(int family) {
331 if (family == AF_INET) {
332 return SocketAddress(IPAddress(INADDR_ANY), 0);
333 } else if (family == AF_INET6) {
334 return SocketAddress(IPAddress(in6addr_any), 0);
335 }
336 return SocketAddress();
337}
338
339} // namespace rtc