blob: a58c001d32c6c8e87c5f7bfbeea454e82d51f5bf [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"
Karl Wiberge40468b2017-11-22 10:42:26 +010012#include "rtc_base/numerics/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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/byteorder.h"
30#include "rtc_base/checks.h"
31#include "rtc_base/logging.h"
32#include "rtc_base/nethelpers.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020033#include "rtc_base/strings/string_builder.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034
35#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037#endif
38
39namespace rtc {
40
41SocketAddress::SocketAddress() {
42 Clear();
43}
44
45SocketAddress::SocketAddress(const std::string& hostname, int port) {
46 SetIP(hostname);
47 SetPort(port);
48}
49
Peter Boström0c4e06b2015-10-07 12:23:21 +020050SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051 SetIP(IPAddress(ip_as_host_order_integer));
52 SetPort(port);
53}
54
55SocketAddress::SocketAddress(const IPAddress& ip, int port) {
56 SetIP(ip);
57 SetPort(port);
58}
59
60SocketAddress::SocketAddress(const SocketAddress& addr) {
61 this->operator=(addr);
62}
63
64void SocketAddress::Clear() {
65 hostname_.clear();
66 literal_ = false;
67 ip_ = IPAddress();
68 port_ = 0;
69 scope_id_ = 0;
70}
71
72bool SocketAddress::IsNil() const {
73 return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
74}
75
76bool SocketAddress::IsComplete() const {
77 return (!IPIsAny(ip_)) && (0 != port_);
78}
79
80SocketAddress& 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
Peter Boström0c4e06b2015-10-07 12:23:21 +020089void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090 hostname_.clear();
91 literal_ = false;
92 ip_ = IPAddress(ip_as_host_order_integer);
93 scope_id_ = 0;
94}
95
96void SocketAddress::SetIP(const IPAddress& ip) {
97 hostname_.clear();
98 literal_ = false;
99 ip_ = ip;
100 scope_id_ = 0;
101}
102
103void 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
Peter Boström0c4e06b2015-10-07 12:23:21 +0200112void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 ip_ = IPAddress(ip_as_host_order_integer);
114 scope_id_ = 0;
115}
116
117void SocketAddress::SetResolvedIP(const IPAddress& ip) {
118 ip_ = ip;
119 scope_id_ = 0;
120}
121
122void SocketAddress::SetPort(int port) {
kwibergee89e782017-08-09 17:22:01 -0700123 port_ = rtc::dchecked_cast<uint16_t>(port);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124}
125
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126uint32_t SocketAddress::ip() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127 return ip_.v4AddressAsHostOrderInteger();
128}
129
130const IPAddress& SocketAddress::ipaddr() const {
131 return ip_;
132}
133
Peter Boström0c4e06b2015-10-07 12:23:21 +0200134uint16_t SocketAddress::port() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 return port_;
136}
137
138std::string SocketAddress::HostAsURIString() const {
139 // If the hostname was a literal IP string, it may need to have square
140 // brackets added (for SocketAddress::ToString()).
141 if (!literal_ && !hostname_.empty())
142 return hostname_;
143 if (ip_.family() == AF_INET6) {
144 return "[" + ip_.ToString() + "]";
145 } else {
146 return ip_.ToString();
147 }
148}
149
150std::string SocketAddress::HostAsSensitiveURIString() const {
151 // If the hostname was a literal IP string, it may need to have square
152 // brackets added (for SocketAddress::ToString()).
153 if (!literal_ && !hostname_.empty())
154 return hostname_;
155 if (ip_.family() == AF_INET6) {
156 return "[" + ip_.ToSensitiveString() + "]";
157 } else {
158 return ip_.ToSensitiveString();
159 }
160}
161
162std::string SocketAddress::PortAsString() const {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200163 return std::to_string(port_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164}
165
166std::string SocketAddress::ToString() const {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200167 char buf[1024];
168 rtc::SimpleStringBuilder sb(buf);
169 sb << HostAsURIString() << ":" << port();
170 return sb.str();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171}
172
173std::string SocketAddress::ToSensitiveString() const {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200174 char buf[1024];
175 rtc::SimpleStringBuilder sb(buf);
176 sb << HostAsSensitiveURIString() << ":" << port();
177 return sb.str();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178}
179
180bool SocketAddress::FromString(const std::string& str) {
181 if (str.at(0) == '[') {
182 std::string::size_type closebracket = str.rfind(']');
183 if (closebracket != std::string::npos) {
184 std::string::size_type colon = str.find(':', closebracket);
185 if (colon != std::string::npos && colon > closebracket) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800186 SetPort(strtoul(str.substr(colon + 1).c_str(), nullptr, 10));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 SetIP(str.substr(1, closebracket - 1));
188 } else {
189 return false;
190 }
191 }
192 } else {
193 std::string::size_type pos = str.find(':');
194 if (std::string::npos == pos)
195 return false;
deadbeef37f5ecf2017-02-27 14:06:41 -0800196 SetPort(strtoul(str.substr(pos + 1).c_str(), nullptr, 10));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197 SetIP(str.substr(0, pos));
198 }
199 return true;
200}
201
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202bool SocketAddress::IsAnyIP() const {
203 return IPIsAny(ip_);
204}
205
206bool SocketAddress::IsLoopbackIP() const {
207 return IPIsLoopback(ip_) || (IPIsAny(ip_) &&
208 0 == strcmp(hostname_.c_str(), "localhost"));
209}
210
211bool SocketAddress::IsPrivateIP() const {
212 return IPIsPrivate(ip_);
213}
214
215bool SocketAddress::IsUnresolvedIP() const {
216 return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
217}
218
219bool SocketAddress::operator==(const SocketAddress& addr) const {
220 return EqualIPs(addr) && EqualPorts(addr);
221}
222
223bool SocketAddress::operator<(const SocketAddress& addr) const {
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000224 if (ip_ != addr.ip_)
225 return ip_ < addr.ip_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000227 // We only check hostnames if both IPs are ANY or unspecified. This matches
228 // EqualIPs().
229 if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_)
230 return hostname_ < addr.hostname_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231
232 return port_ < addr.port_;
233}
234
235bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
236 return (ip_ == addr.ip_) &&
henrike@webrtc.orgb614d062014-07-10 22:47:02 +0000237 ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000238}
239
240bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
241 return (port_ == addr.port_);
242}
243
244size_t SocketAddress::Hash() const {
245 size_t h = 0;
246 h ^= HashIP(ip_);
247 h ^= port_ | (port_ << 16);
248 return h;
249}
250
251void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
252 memset(saddr, 0, sizeof(*saddr));
253 if (ip_.family() != AF_INET) {
254 saddr->sin_family = AF_UNSPEC;
255 return;
256 }
257 saddr->sin_family = AF_INET;
258 saddr->sin_port = HostToNetwork16(port_);
259 if (IPIsAny(ip_)) {
260 saddr->sin_addr.s_addr = INADDR_ANY;
261 } else {
262 saddr->sin_addr = ip_.ipv4_address();
263 }
264}
265
266bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
267 if (saddr.sin_family != AF_INET)
268 return false;
269 SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
270 SetPort(NetworkToHost16(saddr.sin_port));
271 literal_ = false;
272 return true;
273}
274
275static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200276 IPAddress ip,
277 uint16_t port,
278 int scope_id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 memset(addr, 0, sizeof(sockaddr_storage));
henrike@webrtc.org17111042014-09-10 22:10:24 +0000280 addr->ss_family = static_cast<unsigned short>(ip.family());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000281 if (addr->ss_family == AF_INET6) {
282 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
283 saddr->sin6_addr = ip.ipv6_address();
284 saddr->sin6_port = HostToNetwork16(port);
285 saddr->sin6_scope_id = scope_id;
286 return sizeof(sockaddr_in6);
287 } else if (addr->ss_family == AF_INET) {
288 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
289 saddr->sin_addr = ip.ipv4_address();
290 saddr->sin_port = HostToNetwork16(port);
291 return sizeof(sockaddr_in);
292 }
293 return 0;
294}
295
296size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const {
297 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
298}
299
300size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
301 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
302}
303
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
305 SocketAddress* out) {
306 if (!out) {
307 return false;
308 }
309 if (addr.ss_family == AF_INET) {
310 const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
311 *out = SocketAddress(IPAddress(saddr->sin_addr),
312 NetworkToHost16(saddr->sin_port));
313 return true;
314 } else if (addr.ss_family == AF_INET6) {
315 const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
316 *out = SocketAddress(IPAddress(saddr->sin6_addr),
317 NetworkToHost16(saddr->sin6_port));
318 out->SetScopeID(saddr->sin6_scope_id);
319 return true;
320 }
321 return false;
322}
323
324SocketAddress EmptySocketAddressWithFamily(int family) {
325 if (family == AF_INET) {
326 return SocketAddress(IPAddress(INADDR_ANY), 0);
327 } else if (family == AF_INET6) {
328 return SocketAddress(IPAddress(in6addr_any), 0);
329 }
330 return SocketAddress();
331}
332
333} // namespace rtc