blob: 8601fc9040a4f34b03cea8606a73104785ec523c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/socket_address.h"
Jonas Olssona4d87372019-07-05 17:08:3312
Ali Tofigh7fa90572022-03-17 14:47:4913#include "absl/strings/string_view.h"
Karl Wiberge40468b2017-11-22 09:42:2614#include "rtc_base/numerics/safe_conversions.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2615
16#if defined(WEBRTC_POSIX)
henrike@webrtc.orgf0488722014-05-13 18:00:2617#include <netinet/in.h>
Yves Gerey665174f2018-06-19 13:03:0518#include <sys/socket.h>
19#include <sys/types.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2620#if defined(OPENBSD)
21#include <netinet/in_systm.h>
22#endif
23#if !defined(__native_client__)
24#include <netinet/ip.h>
25#endif
26#include <arpa/inet.h>
27#include <netdb.h>
28#include <unistd.h>
29#endif
30
Steve Anton10542f22019-01-11 17:11:0031#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3132#include "rtc_base/checks.h"
33#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 17:11:0034#include "rtc_base/net_helpers.h"
Jonas Olssonabbe8412018-04-03 11:40:0535#include "rtc_base/strings/string_builder.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2636
37#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 04:47:3138#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2639#endif
40
41namespace rtc {
42
43SocketAddress::SocketAddress() {
44 Clear();
45}
46
Ali Tofigh7fa90572022-03-17 14:47:4947SocketAddress::SocketAddress(absl::string_view hostname, int port) {
henrike@webrtc.orgf0488722014-05-13 18:00:2648 SetIP(hostname);
49 SetPort(port);
50}
51
Peter Boström0c4e06b2015-10-07 10:23:2152SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) {
henrike@webrtc.orgf0488722014-05-13 18:00:2653 SetIP(IPAddress(ip_as_host_order_integer));
54 SetPort(port);
55}
56
57SocketAddress::SocketAddress(const IPAddress& ip, int port) {
58 SetIP(ip);
59 SetPort(port);
60}
61
62SocketAddress::SocketAddress(const SocketAddress& addr) {
63 this->operator=(addr);
64}
65
66void SocketAddress::Clear() {
67 hostname_.clear();
68 literal_ = false;
69 ip_ = IPAddress();
70 port_ = 0;
71 scope_id_ = 0;
72}
73
74bool SocketAddress::IsNil() const {
75 return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
76}
77
78bool SocketAddress::IsComplete() const {
79 return (!IPIsAny(ip_)) && (0 != port_);
80}
81
82SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
83 hostname_ = addr.hostname_;
84 ip_ = addr.ip_;
85 port_ = addr.port_;
86 literal_ = addr.literal_;
87 scope_id_ = addr.scope_id_;
88 return *this;
89}
90
Peter Boström0c4e06b2015-10-07 10:23:2191void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) {
henrike@webrtc.orgf0488722014-05-13 18:00:2692 hostname_.clear();
93 literal_ = false;
94 ip_ = IPAddress(ip_as_host_order_integer);
95 scope_id_ = 0;
96}
97
98void SocketAddress::SetIP(const IPAddress& ip) {
99 hostname_.clear();
100 literal_ = false;
101 ip_ = ip;
102 scope_id_ = 0;
103}
104
Ali Tofigh7fa90572022-03-17 14:47:49105void SocketAddress::SetIP(absl::string_view hostname) {
106 hostname_ = std::string(hostname);
henrike@webrtc.orgf0488722014-05-13 18:00:26107 literal_ = IPFromString(hostname, &ip_);
108 if (!literal_) {
109 ip_ = IPAddress();
110 }
111 scope_id_ = 0;
112}
113
Peter Boström0c4e06b2015-10-07 10:23:21114void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26115 ip_ = IPAddress(ip_as_host_order_integer);
116 scope_id_ = 0;
117}
118
119void SocketAddress::SetResolvedIP(const IPAddress& ip) {
120 ip_ = ip;
121 scope_id_ = 0;
122}
123
124void SocketAddress::SetPort(int port) {
kwibergee89e782017-08-10 00:22:01125 port_ = rtc::dchecked_cast<uint16_t>(port);
henrike@webrtc.orgf0488722014-05-13 18:00:26126}
127
Peter Boström0c4e06b2015-10-07 10:23:21128uint32_t SocketAddress::ip() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26129 return ip_.v4AddressAsHostOrderInteger();
130}
131
132const IPAddress& SocketAddress::ipaddr() const {
133 return ip_;
134}
135
Peter Boström0c4e06b2015-10-07 10:23:21136uint16_t SocketAddress::port() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26137 return port_;
138}
139
140std::string SocketAddress::HostAsURIString() const {
141 // If the hostname was a literal IP string, it may need to have square
142 // brackets added (for SocketAddress::ToString()).
143 if (!literal_ && !hostname_.empty())
144 return hostname_;
145 if (ip_.family() == AF_INET6) {
146 return "[" + ip_.ToString() + "]";
147 } else {
148 return ip_.ToString();
149 }
150}
151
152std::string SocketAddress::HostAsSensitiveURIString() const {
153 // If the hostname was a literal IP string, it may need to have square
154 // brackets added (for SocketAddress::ToString()).
155 if (!literal_ && !hostname_.empty())
156 return hostname_;
157 if (ip_.family() == AF_INET6) {
158 return "[" + ip_.ToSensitiveString() + "]";
159 } else {
160 return ip_.ToSensitiveString();
161 }
162}
163
164std::string SocketAddress::PortAsString() const {
Jonas Olssonabbe8412018-04-03 11:40:05165 return std::to_string(port_);
henrike@webrtc.orgf0488722014-05-13 18:00:26166}
167
168std::string SocketAddress::ToString() const {
Jonas Olssonabbe8412018-04-03 11:40:05169 char buf[1024];
170 rtc::SimpleStringBuilder sb(buf);
171 sb << HostAsURIString() << ":" << port();
172 return sb.str();
henrike@webrtc.orgf0488722014-05-13 18:00:26173}
174
175std::string SocketAddress::ToSensitiveString() const {
Jonas Olssonabbe8412018-04-03 11:40:05176 char buf[1024];
177 rtc::SimpleStringBuilder sb(buf);
178 sb << HostAsSensitiveURIString() << ":" << port();
179 return sb.str();
henrike@webrtc.orgf0488722014-05-13 18:00:26180}
181
Yury Yarashevich41010f92023-01-19 16:56:03182std::string SocketAddress::ToSensitiveNameAndAddressString() const {
183 if (IsUnresolvedIP() || literal_ || hostname_.empty()) {
184 return ToSensitiveString();
Yura Yaroshevichf97afd62021-04-12 12:56:08185 }
186 char buf[1024];
187 rtc::SimpleStringBuilder sb(buf);
Yury Yarashevich41010f92023-01-19 16:56:03188 sb << HostAsSensitiveURIString() << ":" << port();
189 sb << " (";
190 if (ip_.family() == AF_INET6) {
191 sb << "[" << ipaddr().ToSensitiveString() << "]";
192 } else {
193 sb << ipaddr().ToSensitiveString();
194 }
195 sb << ":" << port() << ")";
196
Yura Yaroshevichf97afd62021-04-12 12:56:08197 return sb.str();
198}
199
Ali Tofigh7fa90572022-03-17 14:47:49200bool SocketAddress::FromString(absl::string_view str) {
henrike@webrtc.orgf0488722014-05-13 18:00:26201 if (str.at(0) == '[') {
Ali Tofigh7fa90572022-03-17 14:47:49202 absl::string_view::size_type closebracket = str.rfind(']');
203 if (closebracket != absl::string_view::npos) {
204 absl::string_view::size_type colon = str.find(':', closebracket);
205 if (colon != absl::string_view::npos && colon > closebracket) {
206 SetPort(
207 strtoul(std::string(str.substr(colon + 1)).c_str(), nullptr, 10));
henrike@webrtc.orgf0488722014-05-13 18:00:26208 SetIP(str.substr(1, closebracket - 1));
209 } else {
210 return false;
211 }
212 }
213 } else {
Ali Tofigh7fa90572022-03-17 14:47:49214 absl::string_view::size_type pos = str.find(':');
215 if (absl::string_view::npos == pos)
henrike@webrtc.orgf0488722014-05-13 18:00:26216 return false;
Ali Tofigh7fa90572022-03-17 14:47:49217 SetPort(strtoul(std::string(str.substr(pos + 1)).c_str(), nullptr, 10));
henrike@webrtc.orgf0488722014-05-13 18:00:26218 SetIP(str.substr(0, pos));
219 }
220 return true;
221}
222
henrike@webrtc.orgf0488722014-05-13 18:00:26223bool SocketAddress::IsAnyIP() const {
224 return IPIsAny(ip_);
225}
226
227bool SocketAddress::IsLoopbackIP() const {
Yves Gerey665174f2018-06-19 13:03:05228 return IPIsLoopback(ip_) ||
229 (IPIsAny(ip_) && 0 == strcmp(hostname_.c_str(), "localhost"));
henrike@webrtc.orgf0488722014-05-13 18:00:26230}
231
232bool SocketAddress::IsPrivateIP() const {
233 return IPIsPrivate(ip_);
234}
235
236bool SocketAddress::IsUnresolvedIP() const {
237 return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
238}
239
240bool SocketAddress::operator==(const SocketAddress& addr) const {
241 return EqualIPs(addr) && EqualPorts(addr);
242}
243
244bool SocketAddress::operator<(const SocketAddress& addr) const {
henrike@webrtc.orgb614d062014-07-10 22:47:02245 if (ip_ != addr.ip_)
246 return ip_ < addr.ip_;
henrike@webrtc.orgf0488722014-05-13 18:00:26247
henrike@webrtc.orgb614d062014-07-10 22:47:02248 // We only check hostnames if both IPs are ANY or unspecified. This matches
249 // EqualIPs().
250 if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_)
251 return hostname_ < addr.hostname_;
henrike@webrtc.orgf0488722014-05-13 18:00:26252
253 return port_ < addr.port_;
254}
255
256bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
257 return (ip_ == addr.ip_) &&
Yves Gerey665174f2018-06-19 13:03:05258 ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
henrike@webrtc.orgf0488722014-05-13 18:00:26259}
260
261bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
262 return (port_ == addr.port_);
263}
264
265size_t SocketAddress::Hash() const {
266 size_t h = 0;
267 h ^= HashIP(ip_);
268 h ^= port_ | (port_ << 16);
269 return h;
270}
271
272void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
273 memset(saddr, 0, sizeof(*saddr));
274 if (ip_.family() != AF_INET) {
275 saddr->sin_family = AF_UNSPEC;
276 return;
277 }
278 saddr->sin_family = AF_INET;
279 saddr->sin_port = HostToNetwork16(port_);
280 if (IPIsAny(ip_)) {
281 saddr->sin_addr.s_addr = INADDR_ANY;
282 } else {
283 saddr->sin_addr = ip_.ipv4_address();
284 }
285}
286
287bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
288 if (saddr.sin_family != AF_INET)
289 return false;
290 SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
291 SetPort(NetworkToHost16(saddr.sin_port));
292 literal_ = false;
293 return true;
294}
295
296static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
Yves Gerey2257c082020-01-03 11:37:56297 const IPAddress& ip,
Peter Boström0c4e06b2015-10-07 10:23:21298 uint16_t port,
299 int scope_id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26300 memset(addr, 0, sizeof(sockaddr_storage));
henrike@webrtc.org17111042014-09-10 22:10:24301 addr->ss_family = static_cast<unsigned short>(ip.family());
henrike@webrtc.orgf0488722014-05-13 18:00:26302 if (addr->ss_family == AF_INET6) {
303 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
304 saddr->sin6_addr = ip.ipv6_address();
305 saddr->sin6_port = HostToNetwork16(port);
306 saddr->sin6_scope_id = scope_id;
307 return sizeof(sockaddr_in6);
308 } else if (addr->ss_family == AF_INET) {
309 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
310 saddr->sin_addr = ip.ipv4_address();
311 saddr->sin_port = HostToNetwork16(port);
312 return sizeof(sockaddr_in);
313 }
314 return 0;
315}
316
Yves Gerey665174f2018-06-19 13:03:05317size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage* addr) const {
henrike@webrtc.orgf0488722014-05-13 18:00:26318 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
319}
320
321size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
322 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
323}
324
henrike@webrtc.orgf0488722014-05-13 18:00:26325bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
326 SocketAddress* out) {
327 if (!out) {
328 return false;
329 }
330 if (addr.ss_family == AF_INET) {
331 const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
332 *out = SocketAddress(IPAddress(saddr->sin_addr),
333 NetworkToHost16(saddr->sin_port));
334 return true;
335 } else if (addr.ss_family == AF_INET6) {
336 const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
337 *out = SocketAddress(IPAddress(saddr->sin6_addr),
338 NetworkToHost16(saddr->sin6_port));
339 out->SetScopeID(saddr->sin6_scope_id);
340 return true;
341 }
342 return false;
343}
344
345SocketAddress EmptySocketAddressWithFamily(int family) {
346 if (family == AF_INET) {
347 return SocketAddress(IPAddress(INADDR_ANY), 0);
348 } else if (family == AF_INET6) {
349 return SocketAddress(IPAddress(in6addr_any), 0);
350 }
351 return SocketAddress();
352}
353
354} // namespace rtc