blob: ed6f81baf158feaa5f05153b67b435f07fb1b75d [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 */
Steve Anton10542f22019-01-11 09:11:00 -080010#include "rtc_base/physical_socket_server.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000011
12#if defined(_MSC_VER) && _MSC_VER < 1300
Yves Gerey665174f2018-06-19 15:03:05 +020013#pragma warning(disable : 4786)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#endif
15
pbos@webrtc.org27e58982014-10-07 17:56:53 +000016#ifdef MEMORY_SANITIZER
17#include <sanitizer/msan_interface.h>
18#endif
19
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#if defined(WEBRTC_POSIX)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021#include <fcntl.h>
Yves Gerey665174f2018-06-19 15:03:05 +020022#include <string.h>
jbauchde4db112017-05-31 13:09:18 -070023#if defined(WEBRTC_USE_EPOLL)
24// "poll" will be used to wait for the signal dispatcher.
25#include <poll.h>
26#endif
Yves Gerey665174f2018-06-19 15:03:05 +020027#include <sys/ioctl.h>
28#include <sys/select.h>
29#include <sys/time.h>
30#include <unistd.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031#endif
32
33#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034#include <windows.h>
35#include <winsock2.h>
36#include <ws2tcpip.h>
37#undef SetPort
38#endif
39
Patrik Höglunda8005cf2017-12-13 16:05:42 +010040#include <errno.h>
41
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042#include <algorithm>
43#include <map>
44
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/arraysize.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/checks.h"
48#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080049#include "rtc_base/network_monitor.h"
50#include "rtc_base/null_socket_server.h"
Niels Möller6d176022021-02-09 14:44:48 +010051#include "rtc_base/synchronization/mutex.h"
Steve Anton10542f22019-01-11 09:11:00 -080052#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
Emilio Cobos Álvarez68065502019-05-29 15:30:32 +020054#if defined(WEBRTC_LINUX)
55#include <linux/sockios.h>
56#endif
57
Patrik Höglunda8005cf2017-12-13 16:05:42 +010058#if defined(WEBRTC_WIN)
59#define LAST_SYSTEM_ERROR (::GetLastError())
60#elif defined(__native_client__) && __native_client__
61#define LAST_SYSTEM_ERROR (0)
62#elif defined(WEBRTC_POSIX)
63#define LAST_SYSTEM_ERROR (errno)
64#endif // WEBRTC_WIN
65
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066#if defined(WEBRTC_POSIX)
67#include <netinet/tcp.h> // for TCP_NODELAY
Yves Gerey665174f2018-06-19 15:03:05 +020068#define IP_MTU 14 // Until this is integrated from linux/in.h to netinet/in.h
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069typedef void* SockOptArg;
Stefan Holmer9131efd2016-05-23 18:19:26 +020070
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071#endif // WEBRTC_POSIX
72
Stefan Holmer3ebb3ef2016-05-23 20:26:11 +020073#if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) && !defined(__native_client__)
74
Stefan Holmer9131efd2016-05-23 18:19:26 +020075int64_t GetSocketRecvTimestamp(int socket) {
76 struct timeval tv_ioctl;
77 int ret = ioctl(socket, SIOCGSTAMP, &tv_ioctl);
78 if (ret != 0)
79 return -1;
80 int64_t timestamp =
81 rtc::kNumMicrosecsPerSec * static_cast<int64_t>(tv_ioctl.tv_sec) +
82 static_cast<int64_t>(tv_ioctl.tv_usec);
83 return timestamp;
84}
85
86#else
87
88int64_t GetSocketRecvTimestamp(int socket) {
89 return -1;
90}
91#endif
92
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093#if defined(WEBRTC_WIN)
94typedef char* SockOptArg;
95#endif
96
jbauchde4db112017-05-31 13:09:18 -070097#if defined(WEBRTC_USE_EPOLL)
98// POLLRDHUP / EPOLLRDHUP are only defined starting with Linux 2.6.17.
99#if !defined(POLLRDHUP)
100#define POLLRDHUP 0x2000
101#endif
102#if !defined(EPOLLRDHUP)
103#define EPOLLRDHUP 0x2000
104#endif
105#endif
106
Taylor Brandstetter7b69a442020-08-20 23:43:13 +0000107namespace {
108class ScopedSetTrue {
109 public:
110 ScopedSetTrue(bool* value) : value_(value) {
111 RTC_DCHECK(!*value_);
112 *value_ = true;
113 }
114 ~ScopedSetTrue() { *value_ = false; }
115
116 private:
117 bool* value_;
118};
119} // namespace
120
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121namespace rtc {
122
jbauch095ae152015-12-18 01:39:55 -0800123PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s)
Yves Gerey665174f2018-06-19 15:03:05 +0200124 : ss_(ss),
125 s_(s),
126 error_(0),
127 state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED),
128 resolver_(nullptr) {
jbauch095ae152015-12-18 01:39:55 -0800129 if (s_ != INVALID_SOCKET) {
jbauch577f5dc2017-05-17 16:32:26 -0700130 SetEnabledEvents(DE_READ | DE_WRITE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131
jbauch095ae152015-12-18 01:39:55 -0800132 int type = SOCK_STREAM;
133 socklen_t len = sizeof(type);
nissec16fa5e2017-02-07 07:18:43 -0800134 const int res =
135 getsockopt(s_, SOL_SOCKET, SO_TYPE, (SockOptArg)&type, &len);
136 RTC_DCHECK_EQ(0, res);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 udp_ = (SOCK_DGRAM == type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 }
jbauch095ae152015-12-18 01:39:55 -0800139}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140
jbauch095ae152015-12-18 01:39:55 -0800141PhysicalSocket::~PhysicalSocket() {
142 Close();
143}
144
145bool PhysicalSocket::Create(int family, int type) {
146 Close();
147 s_ = ::socket(family, type, 0);
148 udp_ = (SOCK_DGRAM == type);
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800149 family_ = family;
jbauch095ae152015-12-18 01:39:55 -0800150 UpdateLastError();
jbauch577f5dc2017-05-17 16:32:26 -0700151 if (udp_) {
152 SetEnabledEvents(DE_READ | DE_WRITE);
153 }
jbauch095ae152015-12-18 01:39:55 -0800154 return s_ != INVALID_SOCKET;
155}
156
157SocketAddress PhysicalSocket::GetLocalAddress() const {
Mirko Bonadei108a2f02019-11-20 19:43:38 +0100158 sockaddr_storage addr_storage = {};
jbauch095ae152015-12-18 01:39:55 -0800159 socklen_t addrlen = sizeof(addr_storage);
160 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
161 int result = ::getsockname(s_, addr, &addrlen);
162 SocketAddress address;
163 if (result >= 0) {
164 SocketAddressFromSockAddrStorage(addr_storage, &address);
165 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100166 RTC_LOG(LS_WARNING) << "GetLocalAddress: unable to get local addr, socket="
167 << s_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 }
jbauch095ae152015-12-18 01:39:55 -0800169 return address;
170}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171
jbauch095ae152015-12-18 01:39:55 -0800172SocketAddress PhysicalSocket::GetRemoteAddress() const {
Mirko Bonadei108a2f02019-11-20 19:43:38 +0100173 sockaddr_storage addr_storage = {};
jbauch095ae152015-12-18 01:39:55 -0800174 socklen_t addrlen = sizeof(addr_storage);
175 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
176 int result = ::getpeername(s_, addr, &addrlen);
177 SocketAddress address;
178 if (result >= 0) {
179 SocketAddressFromSockAddrStorage(addr_storage, &address);
180 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_WARNING)
182 << "GetRemoteAddress: unable to get remote addr, socket=" << s_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183 }
jbauch095ae152015-12-18 01:39:55 -0800184 return address;
185}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000186
jbauch095ae152015-12-18 01:39:55 -0800187int PhysicalSocket::Bind(const SocketAddress& bind_addr) {
deadbeefc874d122017-02-13 15:41:59 -0800188 SocketAddress copied_bind_addr = bind_addr;
189 // If a network binder is available, use it to bind a socket to an interface
190 // instead of bind(), since this is more reliable on an OS with a weak host
191 // model.
deadbeef9ffa13f2017-02-21 16:18:00 -0800192 if (ss_->network_binder() && !bind_addr.IsAnyIP()) {
deadbeefc874d122017-02-13 15:41:59 -0800193 NetworkBindingResult result =
194 ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr());
195 if (result == NetworkBindingResult::SUCCESS) {
196 // Since the network binder handled binding the socket to the desired
197 // network interface, we don't need to (and shouldn't) include an IP in
198 // the bind() call; bind() just needs to assign a port.
199 copied_bind_addr.SetIP(GetAnyIP(copied_bind_addr.ipaddr().family()));
200 } else if (result == NetworkBindingResult::NOT_IMPLEMENTED) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100201 RTC_LOG(LS_INFO) << "Can't bind socket to network because "
202 "network binding is not implemented for this OS.";
deadbeefc874d122017-02-13 15:41:59 -0800203 } else {
204 if (bind_addr.IsLoopbackIP()) {
205 // If we couldn't bind to a loopback IP (which should only happen in
206 // test scenarios), continue on. This may be expected behavior.
Paulina Hensmanb239a2e2020-03-31 16:16:11 +0200207 RTC_LOG(LS_VERBOSE) << "Binding socket to loopback address"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100208 << " failed; result: " << static_cast<int>(result);
deadbeefc874d122017-02-13 15:41:59 -0800209 } else {
Paulina Hensmanb239a2e2020-03-31 16:16:11 +0200210 RTC_LOG(LS_WARNING) << "Binding socket to network address"
Mirko Bonadei675513b2017-11-09 11:09:25 +0100211 << " failed; result: " << static_cast<int>(result);
deadbeefc874d122017-02-13 15:41:59 -0800212 // If a network binding was attempted and failed, we should stop here
213 // and not try to use the socket. Otherwise, we may end up sending
214 // packets with an invalid source address.
215 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7026
216 return -1;
217 }
218 }
219 }
jbauch095ae152015-12-18 01:39:55 -0800220 sockaddr_storage addr_storage;
deadbeefc874d122017-02-13 15:41:59 -0800221 size_t len = copied_bind_addr.ToSockAddrStorage(&addr_storage);
jbauch095ae152015-12-18 01:39:55 -0800222 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
223 int err = ::bind(s_, addr, static_cast<int>(len));
224 UpdateLastError();
tfarinaa41ab932015-10-30 16:08:48 -0700225#if !defined(NDEBUG)
jbauch095ae152015-12-18 01:39:55 -0800226 if (0 == err) {
227 dbg_addr_ = "Bound @ ";
228 dbg_addr_.append(GetLocalAddress().ToString());
229 }
tfarinaa41ab932015-10-30 16:08:48 -0700230#endif
jbauch095ae152015-12-18 01:39:55 -0800231 return err;
232}
233
234int PhysicalSocket::Connect(const SocketAddress& addr) {
235 // TODO(pthatcher): Implicit creation is required to reconnect...
236 // ...but should we make it more explicit?
237 if (state_ != CS_CLOSED) {
238 SetError(EALREADY);
239 return SOCKET_ERROR;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 }
jbauch095ae152015-12-18 01:39:55 -0800241 if (addr.IsUnresolvedIP()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100242 RTC_LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect";
jbauch095ae152015-12-18 01:39:55 -0800243 resolver_ = new AsyncResolver();
244 resolver_->SignalDone.connect(this, &PhysicalSocket::OnResolveResult);
245 resolver_->Start(addr);
246 state_ = CS_CONNECTING;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 return 0;
248 }
249
jbauch095ae152015-12-18 01:39:55 -0800250 return DoConnect(addr);
251}
252
253int PhysicalSocket::DoConnect(const SocketAddress& connect_addr) {
Yves Gerey665174f2018-06-19 15:03:05 +0200254 if ((s_ == INVALID_SOCKET) && !Create(connect_addr.family(), SOCK_STREAM)) {
jbauch095ae152015-12-18 01:39:55 -0800255 return SOCKET_ERROR;
256 }
257 sockaddr_storage addr_storage;
258 size_t len = connect_addr.ToSockAddrStorage(&addr_storage);
259 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
260 int err = ::connect(s_, addr, static_cast<int>(len));
261 UpdateLastError();
jbauch577f5dc2017-05-17 16:32:26 -0700262 uint8_t events = DE_READ | DE_WRITE;
jbauch095ae152015-12-18 01:39:55 -0800263 if (err == 0) {
264 state_ = CS_CONNECTED;
265 } else if (IsBlockingError(GetError())) {
266 state_ = CS_CONNECTING;
jbauch577f5dc2017-05-17 16:32:26 -0700267 events |= DE_CONNECT;
jbauch095ae152015-12-18 01:39:55 -0800268 } else {
269 return SOCKET_ERROR;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000270 }
271
jbauch577f5dc2017-05-17 16:32:26 -0700272 EnableEvents(events);
jbauch095ae152015-12-18 01:39:55 -0800273 return 0;
274}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275
jbauch095ae152015-12-18 01:39:55 -0800276int PhysicalSocket::GetError() const {
Niels Möller6d176022021-02-09 14:44:48 +0100277 webrtc::MutexLock lock(&mutex_);
jbauch095ae152015-12-18 01:39:55 -0800278 return error_;
279}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280
jbauch095ae152015-12-18 01:39:55 -0800281void PhysicalSocket::SetError(int error) {
Niels Möller6d176022021-02-09 14:44:48 +0100282 webrtc::MutexLock lock(&mutex_);
jbauch095ae152015-12-18 01:39:55 -0800283 error_ = error;
284}
285
286AsyncSocket::ConnState PhysicalSocket::GetState() const {
287 return state_;
288}
289
290int PhysicalSocket::GetOption(Option opt, int* value) {
291 int slevel;
292 int sopt;
293 if (TranslateOption(opt, &slevel, &sopt) == -1)
294 return -1;
295 socklen_t optlen = sizeof(*value);
296 int ret = ::getsockopt(s_, slevel, sopt, (SockOptArg)value, &optlen);
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800297 if (ret == -1) {
298 return -1;
299 }
300 if (opt == OPT_DONTFRAGMENT) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
jbauch095ae152015-12-18 01:39:55 -0800302 *value = (*value != IP_PMTUDISC_DONT) ? 1 : 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303#endif
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800304 } else if (opt == OPT_DSCP) {
305#if defined(WEBRTC_POSIX)
306 // unshift DSCP value to get six most significant bits of IP DiffServ field
307 *value >>= 2;
308#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 }
jbauch095ae152015-12-18 01:39:55 -0800310 return ret;
311}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000312
jbauch095ae152015-12-18 01:39:55 -0800313int PhysicalSocket::SetOption(Option opt, int value) {
314 int slevel;
315 int sopt;
316 if (TranslateOption(opt, &slevel, &sopt) == -1)
317 return -1;
318 if (opt == OPT_DONTFRAGMENT) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000319#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
jbauch095ae152015-12-18 01:39:55 -0800320 value = (value) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000321#endif
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800322 } else if (opt == OPT_DSCP) {
323#if defined(WEBRTC_POSIX)
324 // shift DSCP value to fit six most significant bits of IP DiffServ field
325 value <<= 2;
326#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327 }
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800328#if defined(WEBRTC_POSIX)
329 if (sopt == IPV6_TCLASS) {
330 // Set the IPv4 option in all cases to support dual-stack sockets.
Taylor Brandstetter9d269402020-11-25 15:24:53 -0800331 // Don't bother checking the return code, as this is expected to fail if
332 // it's not actually dual-stack.
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800333 ::setsockopt(s_, IPPROTO_IP, IP_TOS, (SockOptArg)&value, sizeof(value));
334 }
335#endif
Taylor Brandstetter9d269402020-11-25 15:24:53 -0800336 int result =
337 ::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value));
338 if (result != 0) {
339 UpdateLastError();
340 }
341 return result;
jbauch095ae152015-12-18 01:39:55 -0800342}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343
jbauch095ae152015-12-18 01:39:55 -0800344int PhysicalSocket::Send(const void* pv, size_t cb) {
Yves Gerey665174f2018-06-19 15:03:05 +0200345 int sent = DoSend(
346 s_, reinterpret_cast<const char*>(pv), static_cast<int>(cb),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000347#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
jbauch095ae152015-12-18 01:39:55 -0800348 // Suppress SIGPIPE. Without this, attempting to send on a socket whose
349 // other end is closed will result in a SIGPIPE signal being raised to
350 // our process, which by default will terminate the process, which we
351 // don't want. By specifying this flag, we'll just get the error EPIPE
352 // instead and can handle the error gracefully.
353 MSG_NOSIGNAL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000354#else
jbauch095ae152015-12-18 01:39:55 -0800355 0
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356#endif
Jonas Olssona4d87372019-07-05 19:08:33 +0200357 );
jbauch095ae152015-12-18 01:39:55 -0800358 UpdateLastError();
359 MaybeRemapSendError();
360 // We have seen minidumps where this may be false.
nisseede5da42017-01-12 05:15:36 -0800361 RTC_DCHECK(sent <= static_cast<int>(cb));
jbauchf2a2bf42016-02-03 16:45:32 -0800362 if ((sent > 0 && sent < static_cast<int>(cb)) ||
363 (sent < 0 && IsBlockingError(GetError()))) {
jbauch577f5dc2017-05-17 16:32:26 -0700364 EnableEvents(DE_WRITE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000365 }
jbauch095ae152015-12-18 01:39:55 -0800366 return sent;
367}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368
jbauch095ae152015-12-18 01:39:55 -0800369int PhysicalSocket::SendTo(const void* buffer,
370 size_t length,
371 const SocketAddress& addr) {
372 sockaddr_storage saddr;
373 size_t len = addr.ToSockAddrStorage(&saddr);
Yves Gerey665174f2018-06-19 15:03:05 +0200374 int sent =
375 DoSendTo(s_, static_cast<const char*>(buffer), static_cast<int>(length),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000376#if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)
Yves Gerey665174f2018-06-19 15:03:05 +0200377 // Suppress SIGPIPE. See above for explanation.
378 MSG_NOSIGNAL,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000379#else
Yves Gerey665174f2018-06-19 15:03:05 +0200380 0,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000381#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200382 reinterpret_cast<sockaddr*>(&saddr), static_cast<int>(len));
jbauch095ae152015-12-18 01:39:55 -0800383 UpdateLastError();
384 MaybeRemapSendError();
385 // We have seen minidumps where this may be false.
nisseede5da42017-01-12 05:15:36 -0800386 RTC_DCHECK(sent <= static_cast<int>(length));
jbauchf2a2bf42016-02-03 16:45:32 -0800387 if ((sent > 0 && sent < static_cast<int>(length)) ||
388 (sent < 0 && IsBlockingError(GetError()))) {
jbauch577f5dc2017-05-17 16:32:26 -0700389 EnableEvents(DE_WRITE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000390 }
jbauch095ae152015-12-18 01:39:55 -0800391 return sent;
392}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000393
Stefan Holmer9131efd2016-05-23 18:19:26 +0200394int PhysicalSocket::Recv(void* buffer, size_t length, int64_t* timestamp) {
Yves Gerey665174f2018-06-19 15:03:05 +0200395 int received =
396 ::recv(s_, static_cast<char*>(buffer), static_cast<int>(length), 0);
jbauch095ae152015-12-18 01:39:55 -0800397 if ((received == 0) && (length != 0)) {
398 // Note: on graceful shutdown, recv can return 0. In this case, we
399 // pretend it is blocking, and then signal close, so that simplifying
400 // assumptions can be made about Recv.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100401 RTC_LOG(LS_WARNING) << "EOF from socket; deferring close event";
jbauch095ae152015-12-18 01:39:55 -0800402 // Must turn this back on so that the select() loop will notice the close
403 // event.
jbauch577f5dc2017-05-17 16:32:26 -0700404 EnableEvents(DE_READ);
jbauch095ae152015-12-18 01:39:55 -0800405 SetError(EWOULDBLOCK);
406 return SOCKET_ERROR;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 }
Stefan Holmer9131efd2016-05-23 18:19:26 +0200408 if (timestamp) {
409 *timestamp = GetSocketRecvTimestamp(s_);
410 }
jbauch095ae152015-12-18 01:39:55 -0800411 UpdateLastError();
412 int error = GetError();
413 bool success = (received >= 0) || IsBlockingError(error);
414 if (udp_ || success) {
jbauch577f5dc2017-05-17 16:32:26 -0700415 EnableEvents(DE_READ);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000416 }
jbauch095ae152015-12-18 01:39:55 -0800417 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100418 RTC_LOG_F(LS_VERBOSE) << "Error = " << error;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419 }
jbauch095ae152015-12-18 01:39:55 -0800420 return received;
421}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000422
jbauch095ae152015-12-18 01:39:55 -0800423int PhysicalSocket::RecvFrom(void* buffer,
424 size_t length,
Stefan Holmer9131efd2016-05-23 18:19:26 +0200425 SocketAddress* out_addr,
426 int64_t* timestamp) {
jbauch095ae152015-12-18 01:39:55 -0800427 sockaddr_storage addr_storage;
428 socklen_t addr_len = sizeof(addr_storage);
429 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
430 int received = ::recvfrom(s_, static_cast<char*>(buffer),
431 static_cast<int>(length), 0, addr, &addr_len);
Stefan Holmer9131efd2016-05-23 18:19:26 +0200432 if (timestamp) {
433 *timestamp = GetSocketRecvTimestamp(s_);
434 }
jbauch095ae152015-12-18 01:39:55 -0800435 UpdateLastError();
436 if ((received >= 0) && (out_addr != nullptr))
437 SocketAddressFromSockAddrStorage(addr_storage, out_addr);
438 int error = GetError();
439 bool success = (received >= 0) || IsBlockingError(error);
440 if (udp_ || success) {
jbauch577f5dc2017-05-17 16:32:26 -0700441 EnableEvents(DE_READ);
jbauch095ae152015-12-18 01:39:55 -0800442 }
443 if (!success) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100444 RTC_LOG_F(LS_VERBOSE) << "Error = " << error;
jbauch095ae152015-12-18 01:39:55 -0800445 }
446 return received;
447}
448
449int PhysicalSocket::Listen(int backlog) {
450 int err = ::listen(s_, backlog);
451 UpdateLastError();
452 if (err == 0) {
453 state_ = CS_CONNECTING;
jbauch577f5dc2017-05-17 16:32:26 -0700454 EnableEvents(DE_ACCEPT);
jbauch095ae152015-12-18 01:39:55 -0800455#if !defined(NDEBUG)
456 dbg_addr_ = "Listening @ ";
457 dbg_addr_.append(GetLocalAddress().ToString());
458#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000459 }
jbauch095ae152015-12-18 01:39:55 -0800460 return err;
461}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000462
jbauch095ae152015-12-18 01:39:55 -0800463AsyncSocket* PhysicalSocket::Accept(SocketAddress* out_addr) {
464 // Always re-subscribe DE_ACCEPT to make sure new incoming connections will
465 // trigger an event even if DoAccept returns an error here.
jbauch577f5dc2017-05-17 16:32:26 -0700466 EnableEvents(DE_ACCEPT);
jbauch095ae152015-12-18 01:39:55 -0800467 sockaddr_storage addr_storage;
468 socklen_t addr_len = sizeof(addr_storage);
469 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
470 SOCKET s = DoAccept(s_, addr, &addr_len);
471 UpdateLastError();
472 if (s == INVALID_SOCKET)
473 return nullptr;
474 if (out_addr != nullptr)
475 SocketAddressFromSockAddrStorage(addr_storage, out_addr);
476 return ss_->WrapSocket(s);
477}
478
479int PhysicalSocket::Close() {
480 if (s_ == INVALID_SOCKET)
481 return 0;
482 int err = ::closesocket(s_);
483 UpdateLastError();
484 s_ = INVALID_SOCKET;
485 state_ = CS_CLOSED;
jbauch577f5dc2017-05-17 16:32:26 -0700486 SetEnabledEvents(0);
jbauch095ae152015-12-18 01:39:55 -0800487 if (resolver_) {
488 resolver_->Destroy(false);
489 resolver_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490 }
jbauch095ae152015-12-18 01:39:55 -0800491 return err;
492}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000493
jbauch095ae152015-12-18 01:39:55 -0800494SOCKET PhysicalSocket::DoAccept(SOCKET socket,
495 sockaddr* addr,
496 socklen_t* addrlen) {
497 return ::accept(socket, addr, addrlen);
498}
499
jbauchf2a2bf42016-02-03 16:45:32 -0800500int PhysicalSocket::DoSend(SOCKET socket, const char* buf, int len, int flags) {
501 return ::send(socket, buf, len, flags);
502}
503
504int PhysicalSocket::DoSendTo(SOCKET socket,
505 const char* buf,
506 int len,
507 int flags,
508 const struct sockaddr* dest_addr,
509 socklen_t addrlen) {
510 return ::sendto(socket, buf, len, flags, dest_addr, addrlen);
511}
512
jbauch095ae152015-12-18 01:39:55 -0800513void PhysicalSocket::OnResolveResult(AsyncResolverInterface* resolver) {
514 if (resolver != resolver_) {
515 return;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000516 }
517
jbauch095ae152015-12-18 01:39:55 -0800518 int error = resolver_->GetError();
519 if (error == 0) {
520 error = DoConnect(resolver_->address());
521 } else {
522 Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000523 }
524
jbauch095ae152015-12-18 01:39:55 -0800525 if (error) {
526 SetError(error);
527 SignalCloseEvent(this, error);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000528 }
jbauch095ae152015-12-18 01:39:55 -0800529}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000530
jbauch095ae152015-12-18 01:39:55 -0800531void PhysicalSocket::UpdateLastError() {
Patrik Höglunda8005cf2017-12-13 16:05:42 +0100532 SetError(LAST_SYSTEM_ERROR);
jbauch095ae152015-12-18 01:39:55 -0800533}
534
535void PhysicalSocket::MaybeRemapSendError() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000536#if defined(WEBRTC_MAC)
jbauch095ae152015-12-18 01:39:55 -0800537 // https://developer.apple.com/library/mac/documentation/Darwin/
538 // Reference/ManPages/man2/sendto.2.html
539 // ENOBUFS - The output queue for a network interface is full.
540 // This generally indicates that the interface has stopped sending,
541 // but may be caused by transient congestion.
542 if (GetError() == ENOBUFS) {
543 SetError(EWOULDBLOCK);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000544 }
jbauch095ae152015-12-18 01:39:55 -0800545#endif
546}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000547
jbauch577f5dc2017-05-17 16:32:26 -0700548void PhysicalSocket::SetEnabledEvents(uint8_t events) {
549 enabled_events_ = events;
550}
551
552void PhysicalSocket::EnableEvents(uint8_t events) {
553 enabled_events_ |= events;
554}
555
556void PhysicalSocket::DisableEvents(uint8_t events) {
557 enabled_events_ &= ~events;
558}
559
jbauch095ae152015-12-18 01:39:55 -0800560int PhysicalSocket::TranslateOption(Option opt, int* slevel, int* sopt) {
561 switch (opt) {
562 case OPT_DONTFRAGMENT:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000563#if defined(WEBRTC_WIN)
jbauch095ae152015-12-18 01:39:55 -0800564 *slevel = IPPROTO_IP;
565 *sopt = IP_DONTFRAGMENT;
566 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000567#elif defined(WEBRTC_MAC) || defined(BSD) || defined(__native_client__)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100568 RTC_LOG(LS_WARNING) << "Socket::OPT_DONTFRAGMENT not supported.";
jbauch095ae152015-12-18 01:39:55 -0800569 return -1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000570#elif defined(WEBRTC_POSIX)
jbauch095ae152015-12-18 01:39:55 -0800571 *slevel = IPPROTO_IP;
572 *sopt = IP_MTU_DISCOVER;
573 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000574#endif
jbauch095ae152015-12-18 01:39:55 -0800575 case OPT_RCVBUF:
576 *slevel = SOL_SOCKET;
577 *sopt = SO_RCVBUF;
578 break;
579 case OPT_SNDBUF:
580 *slevel = SOL_SOCKET;
581 *sopt = SO_SNDBUF;
582 break;
583 case OPT_NODELAY:
584 *slevel = IPPROTO_TCP;
585 *sopt = TCP_NODELAY;
586 break;
587 case OPT_DSCP:
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800588#if defined(WEBRTC_POSIX)
589 if (family_ == AF_INET6) {
590 *slevel = IPPROTO_IPV6;
591 *sopt = IPV6_TCLASS;
592 } else {
593 *slevel = IPPROTO_IP;
594 *sopt = IP_TOS;
595 }
596 break;
597#else
Mirko Bonadei675513b2017-11-09 11:09:25 +0100598 RTC_LOG(LS_WARNING) << "Socket::OPT_DSCP not supported.";
jbauch095ae152015-12-18 01:39:55 -0800599 return -1;
Taylor Brandstetterecd6fc82020-02-05 17:26:37 -0800600#endif
jbauch095ae152015-12-18 01:39:55 -0800601 case OPT_RTP_SENDTIME_EXTN_ID:
602 return -1; // No logging is necessary as this not a OS socket option.
603 default:
nissec80e7412017-01-11 05:56:46 -0800604 RTC_NOTREACHED();
jbauch095ae152015-12-18 01:39:55 -0800605 return -1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000606 }
jbauch095ae152015-12-18 01:39:55 -0800607 return 0;
608}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000609
Yves Gerey665174f2018-06-19 15:03:05 +0200610SocketDispatcher::SocketDispatcher(PhysicalSocketServer* ss)
jbauch4331fcd2016-01-06 22:20:28 -0800611#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200612 : PhysicalSocket(ss),
613 id_(0),
614 signal_close_(false)
jbauch4331fcd2016-01-06 22:20:28 -0800615#else
Yves Gerey665174f2018-06-19 15:03:05 +0200616 : PhysicalSocket(ss)
jbauch4331fcd2016-01-06 22:20:28 -0800617#endif
618{
619}
620
Yves Gerey665174f2018-06-19 15:03:05 +0200621SocketDispatcher::SocketDispatcher(SOCKET s, PhysicalSocketServer* ss)
jbauch4331fcd2016-01-06 22:20:28 -0800622#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200623 : PhysicalSocket(ss, s),
624 id_(0),
625 signal_close_(false)
jbauch4331fcd2016-01-06 22:20:28 -0800626#else
Yves Gerey665174f2018-06-19 15:03:05 +0200627 : PhysicalSocket(ss, s)
jbauch4331fcd2016-01-06 22:20:28 -0800628#endif
629{
630}
631
632SocketDispatcher::~SocketDispatcher() {
633 Close();
634}
635
636bool SocketDispatcher::Initialize() {
nisseede5da42017-01-12 05:15:36 -0800637 RTC_DCHECK(s_ != INVALID_SOCKET);
Yves Gerey665174f2018-06-19 15:03:05 +0200638// Must be a non-blocking
jbauch4331fcd2016-01-06 22:20:28 -0800639#if defined(WEBRTC_WIN)
640 u_long argp = 1;
641 ioctlsocket(s_, FIONBIO, &argp);
642#elif defined(WEBRTC_POSIX)
643 fcntl(s_, F_SETFL, fcntl(s_, F_GETFL, 0) | O_NONBLOCK);
644#endif
deadbeefeae45642017-05-26 16:27:09 -0700645#if defined(WEBRTC_IOS)
646 // iOS may kill sockets when the app is moved to the background
647 // (specifically, if the app doesn't use the "voip" UIBackgroundMode). When
648 // we attempt to write to such a socket, SIGPIPE will be raised, which by
649 // default will terminate the process, which we don't want. By specifying
650 // this socket option, SIGPIPE will be disabled for the socket.
651 int value = 1;
652 ::setsockopt(s_, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));
653#endif
jbauch4331fcd2016-01-06 22:20:28 -0800654 ss_->Add(this);
655 return true;
656}
657
658bool SocketDispatcher::Create(int type) {
659 return Create(AF_INET, type);
660}
661
662bool SocketDispatcher::Create(int family, int type) {
663 // Change the socket to be non-blocking.
664 if (!PhysicalSocket::Create(family, type))
665 return false;
666
667 if (!Initialize())
668 return false;
669
670#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +0200671 do {
672 id_ = ++next_id_;
673 } while (id_ == 0);
jbauch4331fcd2016-01-06 22:20:28 -0800674#endif
675 return true;
676}
677
678#if defined(WEBRTC_WIN)
679
680WSAEVENT SocketDispatcher::GetWSAEvent() {
681 return WSA_INVALID_EVENT;
682}
683
684SOCKET SocketDispatcher::GetSocket() {
685 return s_;
686}
687
688bool SocketDispatcher::CheckSignalClose() {
689 if (!signal_close_)
690 return false;
691
692 char ch;
693 if (recv(s_, &ch, 1, MSG_PEEK) > 0)
694 return false;
695
696 state_ = CS_CLOSED;
697 signal_close_ = false;
698 SignalCloseEvent(this, signal_err_);
699 return true;
700}
701
702int SocketDispatcher::next_id_ = 0;
703
704#elif defined(WEBRTC_POSIX)
705
706int SocketDispatcher::GetDescriptor() {
707 return s_;
708}
709
710bool SocketDispatcher::IsDescriptorClosed() {
deadbeeffaedf7f2017-02-09 15:09:22 -0800711 if (udp_) {
712 // The MSG_PEEK trick doesn't work for UDP, since (at least in some
713 // circumstances) it requires reading an entire UDP packet, which would be
Artem Titov96e3b992021-07-26 16:03:14 +0200714 // bad for performance here. So, just check whether `s_` has been closed,
deadbeeffaedf7f2017-02-09 15:09:22 -0800715 // which should be sufficient.
716 return s_ == INVALID_SOCKET;
717 }
jbauch4331fcd2016-01-06 22:20:28 -0800718 // We don't have a reliable way of distinguishing end-of-stream
719 // from readability. So test on each readable call. Is this
720 // inefficient? Probably.
721 char ch;
722 ssize_t res = ::recv(s_, &ch, 1, MSG_PEEK);
723 if (res > 0) {
724 // Data available, so not closed.
725 return false;
726 } else if (res == 0) {
727 // EOF, so closed.
728 return true;
729 } else { // error
730 switch (errno) {
731 // Returned if we've already closed s_.
732 case EBADF:
733 // Returned during ungraceful peer shutdown.
734 case ECONNRESET:
735 return true;
deadbeeffaedf7f2017-02-09 15:09:22 -0800736 // The normal blocking error; don't log anything.
737 case EWOULDBLOCK:
738 // Interrupted system call.
739 case EINTR:
740 return false;
jbauch4331fcd2016-01-06 22:20:28 -0800741 default:
742 // Assume that all other errors are just blocking errors, meaning the
743 // connection is still good but we just can't read from it right now.
744 // This should only happen when connecting (and at most once), because
745 // in all other cases this function is only called if the file
746 // descriptor is already known to be in the readable state. However,
747 // it's not necessary a problem if we spuriously interpret a
748 // "connection lost"-type error as a blocking error, because typically
749 // the next recv() will get EOF, so we'll still eventually notice that
750 // the socket is closed.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100751 RTC_LOG_ERR(LS_WARNING) << "Assuming benign blocking error";
jbauch4331fcd2016-01-06 22:20:28 -0800752 return false;
753 }
754 }
755}
756
Yves Gerey665174f2018-06-19 15:03:05 +0200757#endif // WEBRTC_POSIX
jbauch4331fcd2016-01-06 22:20:28 -0800758
759uint32_t SocketDispatcher::GetRequestedEvents() {
jbauch577f5dc2017-05-17 16:32:26 -0700760 return enabled_events();
jbauch4331fcd2016-01-06 22:20:28 -0800761}
762
jbauch4331fcd2016-01-06 22:20:28 -0800763#if defined(WEBRTC_WIN)
764
765void SocketDispatcher::OnEvent(uint32_t ff, int err) {
Niels Möller21347452021-02-12 11:19:52 +0100766 if ((ff & DE_CONNECT) != 0)
767 state_ = CS_CONNECTED;
768
769 // We set CS_CLOSED from CheckSignalClose.
770
jbauch4331fcd2016-01-06 22:20:28 -0800771 int cache_id = id_;
772 // Make sure we deliver connect/accept first. Otherwise, consumers may see
773 // something like a READ followed by a CONNECT, which would be odd.
774 if (((ff & DE_CONNECT) != 0) && (id_ == cache_id)) {
775 if (ff != DE_CONNECT)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100776 RTC_LOG(LS_VERBOSE) << "Signalled with DE_CONNECT: " << ff;
jbauch577f5dc2017-05-17 16:32:26 -0700777 DisableEvents(DE_CONNECT);
jbauch4331fcd2016-01-06 22:20:28 -0800778#if !defined(NDEBUG)
779 dbg_addr_ = "Connected @ ";
780 dbg_addr_.append(GetRemoteAddress().ToString());
781#endif
782 SignalConnectEvent(this);
783 }
784 if (((ff & DE_ACCEPT) != 0) && (id_ == cache_id)) {
jbauch577f5dc2017-05-17 16:32:26 -0700785 DisableEvents(DE_ACCEPT);
jbauch4331fcd2016-01-06 22:20:28 -0800786 SignalReadEvent(this);
787 }
788 if ((ff & DE_READ) != 0) {
jbauch577f5dc2017-05-17 16:32:26 -0700789 DisableEvents(DE_READ);
jbauch4331fcd2016-01-06 22:20:28 -0800790 SignalReadEvent(this);
791 }
792 if (((ff & DE_WRITE) != 0) && (id_ == cache_id)) {
jbauch577f5dc2017-05-17 16:32:26 -0700793 DisableEvents(DE_WRITE);
jbauch4331fcd2016-01-06 22:20:28 -0800794 SignalWriteEvent(this);
795 }
796 if (((ff & DE_CLOSE) != 0) && (id_ == cache_id)) {
797 signal_close_ = true;
798 signal_err_ = err;
799 }
800}
801
802#elif defined(WEBRTC_POSIX)
803
804void SocketDispatcher::OnEvent(uint32_t ff, int err) {
Niels Möller21347452021-02-12 11:19:52 +0100805 if ((ff & DE_CONNECT) != 0)
806 state_ = CS_CONNECTED;
807
808 if ((ff & DE_CLOSE) != 0)
809 state_ = CS_CLOSED;
810
jbauchde4db112017-05-31 13:09:18 -0700811#if defined(WEBRTC_USE_EPOLL)
812 // Remember currently enabled events so we can combine multiple changes
813 // into one update call later.
814 // The signal handlers might re-enable events disabled here, so we can't
815 // keep a list of events to disable at the end of the method. This list
816 // would not be updated with the events enabled by the signal handlers.
817 StartBatchedEventUpdates();
818#endif
jbauch4331fcd2016-01-06 22:20:28 -0800819 // Make sure we deliver connect/accept first. Otherwise, consumers may see
820 // something like a READ followed by a CONNECT, which would be odd.
821 if ((ff & DE_CONNECT) != 0) {
jbauch577f5dc2017-05-17 16:32:26 -0700822 DisableEvents(DE_CONNECT);
jbauch4331fcd2016-01-06 22:20:28 -0800823 SignalConnectEvent(this);
824 }
825 if ((ff & DE_ACCEPT) != 0) {
jbauch577f5dc2017-05-17 16:32:26 -0700826 DisableEvents(DE_ACCEPT);
jbauch4331fcd2016-01-06 22:20:28 -0800827 SignalReadEvent(this);
828 }
829 if ((ff & DE_READ) != 0) {
jbauch577f5dc2017-05-17 16:32:26 -0700830 DisableEvents(DE_READ);
jbauch4331fcd2016-01-06 22:20:28 -0800831 SignalReadEvent(this);
832 }
833 if ((ff & DE_WRITE) != 0) {
jbauch577f5dc2017-05-17 16:32:26 -0700834 DisableEvents(DE_WRITE);
jbauch4331fcd2016-01-06 22:20:28 -0800835 SignalWriteEvent(this);
836 }
837 if ((ff & DE_CLOSE) != 0) {
838 // The socket is now dead to us, so stop checking it.
jbauch577f5dc2017-05-17 16:32:26 -0700839 SetEnabledEvents(0);
jbauch4331fcd2016-01-06 22:20:28 -0800840 SignalCloseEvent(this, err);
841 }
jbauchde4db112017-05-31 13:09:18 -0700842#if defined(WEBRTC_USE_EPOLL)
843 FinishBatchedEventUpdates();
844#endif
jbauch4331fcd2016-01-06 22:20:28 -0800845}
846
Yves Gerey665174f2018-06-19 15:03:05 +0200847#endif // WEBRTC_POSIX
jbauch4331fcd2016-01-06 22:20:28 -0800848
jbauchde4db112017-05-31 13:09:18 -0700849#if defined(WEBRTC_USE_EPOLL)
850
Taylor Brandstetter7b69a442020-08-20 23:43:13 +0000851inline static int GetEpollEvents(uint32_t ff) {
jbauchde4db112017-05-31 13:09:18 -0700852 int events = 0;
853 if (ff & (DE_READ | DE_ACCEPT)) {
854 events |= EPOLLIN;
855 }
856 if (ff & (DE_WRITE | DE_CONNECT)) {
857 events |= EPOLLOUT;
858 }
859 return events;
860}
861
862void SocketDispatcher::StartBatchedEventUpdates() {
863 RTC_DCHECK_EQ(saved_enabled_events_, -1);
864 saved_enabled_events_ = enabled_events();
865}
866
867void SocketDispatcher::FinishBatchedEventUpdates() {
868 RTC_DCHECK_NE(saved_enabled_events_, -1);
869 uint8_t old_events = static_cast<uint8_t>(saved_enabled_events_);
870 saved_enabled_events_ = -1;
871 MaybeUpdateDispatcher(old_events);
872}
873
874void SocketDispatcher::MaybeUpdateDispatcher(uint8_t old_events) {
875 if (GetEpollEvents(enabled_events()) != GetEpollEvents(old_events) &&
876 saved_enabled_events_ == -1) {
877 ss_->Update(this);
878 }
879}
880
881void SocketDispatcher::SetEnabledEvents(uint8_t events) {
882 uint8_t old_events = enabled_events();
883 PhysicalSocket::SetEnabledEvents(events);
884 MaybeUpdateDispatcher(old_events);
885}
886
887void SocketDispatcher::EnableEvents(uint8_t events) {
888 uint8_t old_events = enabled_events();
889 PhysicalSocket::EnableEvents(events);
890 MaybeUpdateDispatcher(old_events);
891}
892
893void SocketDispatcher::DisableEvents(uint8_t events) {
894 uint8_t old_events = enabled_events();
895 PhysicalSocket::DisableEvents(events);
896 MaybeUpdateDispatcher(old_events);
897}
898
899#endif // WEBRTC_USE_EPOLL
900
jbauch4331fcd2016-01-06 22:20:28 -0800901int SocketDispatcher::Close() {
902 if (s_ == INVALID_SOCKET)
903 return 0;
904
905#if defined(WEBRTC_WIN)
906 id_ = 0;
907 signal_close_ = false;
908#endif
mmorrison25eeda12020-04-07 16:13:13 -0600909#if defined(WEBRTC_USE_EPOLL)
910 // If we're batching events, the socket can be closed and reopened
911 // during the batch. Set saved_enabled_events_ to 0 here so the new
912 // socket, if any, has the correct old events bitfield
913 if (saved_enabled_events_ != -1) {
914 saved_enabled_events_ = 0;
915 }
916#endif
jbauch4331fcd2016-01-06 22:20:28 -0800917 ss_->Remove(this);
918 return PhysicalSocket::Close();
919}
920
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000921#if defined(WEBRTC_POSIX)
Niels Möller21347452021-02-12 11:19:52 +0100922// Sets the value of a boolean value to false when signaled.
923class Signaler : public Dispatcher {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000924 public:
Niels Möller21347452021-02-12 11:19:52 +0100925 Signaler(PhysicalSocketServer* ss, bool& flag_to_clear)
926 : ss_(ss),
927 afd_([] {
928 std::array<int, 2> afd = {-1, -1};
929
930 if (pipe(afd.data()) < 0) {
931 RTC_LOG(LERROR) << "pipe failed";
932 }
933 return afd;
934 }()),
935 fSignaled_(false),
936 flag_to_clear_(flag_to_clear) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000937 ss_->Add(this);
938 }
939
Niels Möller21347452021-02-12 11:19:52 +0100940 ~Signaler() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000941 ss_->Remove(this);
942 close(afd_[0]);
943 close(afd_[1]);
944 }
945
946 virtual void Signal() {
Niels Möller6d176022021-02-09 14:44:48 +0100947 webrtc::MutexLock lock(&mutex_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000948 if (!fSignaled_) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200949 const uint8_t b[1] = {0};
nissec16fa5e2017-02-07 07:18:43 -0800950 const ssize_t res = write(afd_[1], b, sizeof(b));
951 RTC_DCHECK_EQ(1, res);
952 fSignaled_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000953 }
954 }
955
Peter Boström0c4e06b2015-10-07 12:23:21 +0200956 uint32_t GetRequestedEvents() override { return DE_READ; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000957
Niels Möller21347452021-02-12 11:19:52 +0100958 void OnEvent(uint32_t ff, int err) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000959 // It is not possible to perfectly emulate an auto-resetting event with
960 // pipes. This simulates it by resetting before the event is handled.
961
Niels Möller6d176022021-02-09 14:44:48 +0100962 webrtc::MutexLock lock(&mutex_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000963 if (fSignaled_) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200964 uint8_t b[4]; // Allow for reading more than 1 byte, but expect 1.
nissec16fa5e2017-02-07 07:18:43 -0800965 const ssize_t res = read(afd_[0], b, sizeof(b));
966 RTC_DCHECK_EQ(1, res);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000967 fSignaled_ = false;
968 }
Niels Möller21347452021-02-12 11:19:52 +0100969 flag_to_clear_ = false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000970 }
971
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000972 int GetDescriptor() override { return afd_[0]; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000973
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000974 bool IsDescriptorClosed() override { return false; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000975
976 private:
Niels Möller6d176022021-02-09 14:44:48 +0100977 PhysicalSocketServer* const ss_;
Niels Möller21347452021-02-12 11:19:52 +0100978 const std::array<int, 2> afd_;
Niels Möller6d176022021-02-09 14:44:48 +0100979 bool fSignaled_ RTC_GUARDED_BY(mutex_);
980 webrtc::Mutex mutex_;
Niels Möller21347452021-02-12 11:19:52 +0100981 bool& flag_to_clear_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000982};
983
Yves Gerey665174f2018-06-19 15:03:05 +0200984#endif // WEBRTC_POSIX
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000985
986#if defined(WEBRTC_WIN)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200987static uint32_t FlagsToEvents(uint32_t events) {
988 uint32_t ffFD = FD_CLOSE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000989 if (events & DE_READ)
990 ffFD |= FD_READ;
991 if (events & DE_WRITE)
992 ffFD |= FD_WRITE;
993 if (events & DE_CONNECT)
994 ffFD |= FD_CONNECT;
995 if (events & DE_ACCEPT)
996 ffFD |= FD_ACCEPT;
997 return ffFD;
998}
999
Niels Möller21347452021-02-12 11:19:52 +01001000// Sets the value of a boolean value to false when signaled.
1001class Signaler : public Dispatcher {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001002 public:
Niels Möller21347452021-02-12 11:19:52 +01001003 Signaler(PhysicalSocketServer* ss, bool& flag_to_clear)
1004 : ss_(ss), flag_to_clear_(flag_to_clear) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001005 hev_ = WSACreateEvent();
1006 if (hev_) {
1007 ss_->Add(this);
1008 }
1009 }
1010
Niels Möller21347452021-02-12 11:19:52 +01001011 ~Signaler() override {
deadbeef37f5ecf2017-02-27 14:06:41 -08001012 if (hev_ != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001013 ss_->Remove(this);
1014 WSACloseEvent(hev_);
deadbeef37f5ecf2017-02-27 14:06:41 -08001015 hev_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001016 }
1017 }
1018
1019 virtual void Signal() {
deadbeef37f5ecf2017-02-27 14:06:41 -08001020 if (hev_ != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001021 WSASetEvent(hev_);
1022 }
1023
Steve Anton9de3aac2017-10-24 10:08:26 -07001024 uint32_t GetRequestedEvents() override { return 0; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001025
Niels Möller21347452021-02-12 11:19:52 +01001026 void OnEvent(uint32_t ff, int err) override {
1027 WSAResetEvent(hev_);
1028 flag_to_clear_ = false;
1029 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001030
Steve Anton9de3aac2017-10-24 10:08:26 -07001031 WSAEVENT GetWSAEvent() override { return hev_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001032
Steve Anton9de3aac2017-10-24 10:08:26 -07001033 SOCKET GetSocket() override { return INVALID_SOCKET; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001034
Steve Anton9de3aac2017-10-24 10:08:26 -07001035 bool CheckSignalClose() override { return false; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001036
Steve Anton9de3aac2017-10-24 10:08:26 -07001037 private:
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001038 PhysicalSocketServer* ss_;
1039 WSAEVENT hev_;
Niels Möller21347452021-02-12 11:19:52 +01001040 bool& flag_to_clear_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001041};
honghaizcec0a082016-01-15 14:49:09 -08001042#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001043
Niels Möller611fba42020-05-13 14:42:22 +02001044PhysicalSocketServer::PhysicalSocketServer()
1045 :
jbauchde4db112017-05-31 13:09:18 -07001046#if defined(WEBRTC_USE_EPOLL)
Niels Möller611fba42020-05-13 14:42:22 +02001047 // Since Linux 2.6.8, the size argument is ignored, but must be greater
1048 // than zero. Before that the size served as hint to the kernel for the
1049 // amount of space to initially allocate in internal data structures.
1050 epoll_fd_(epoll_create(FD_SETSIZE)),
1051#endif
1052#if defined(WEBRTC_WIN)
1053 socket_ev_(WSACreateEvent()),
1054#endif
1055 fWait_(false) {
1056#if defined(WEBRTC_USE_EPOLL)
jbauchde4db112017-05-31 13:09:18 -07001057 if (epoll_fd_ == -1) {
1058 // Not an error, will fall back to "select" below.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001059 RTC_LOG_E(LS_WARNING, EN, errno) << "epoll_create";
Niels Möller611fba42020-05-13 14:42:22 +02001060 // Note that -1 == INVALID_SOCKET, the alias used by later checks.
jbauchde4db112017-05-31 13:09:18 -07001061 }
1062#endif
Niels Möller21347452021-02-12 11:19:52 +01001063 // The `fWait_` flag to be cleared by the Signaler.
1064 signal_wakeup_ = new Signaler(this, fWait_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001065}
1066
1067PhysicalSocketServer::~PhysicalSocketServer() {
1068#if defined(WEBRTC_WIN)
1069 WSACloseEvent(socket_ev_);
1070#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001071 delete signal_wakeup_;
jbauchde4db112017-05-31 13:09:18 -07001072#if defined(WEBRTC_USE_EPOLL)
1073 if (epoll_fd_ != INVALID_SOCKET) {
1074 close(epoll_fd_);
1075 }
1076#endif
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001077 RTC_DCHECK(dispatcher_by_key_.empty());
1078 RTC_DCHECK(key_by_dispatcher_.empty());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001079}
1080
1081void PhysicalSocketServer::WakeUp() {
1082 signal_wakeup_->Signal();
1083}
1084
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001085Socket* PhysicalSocketServer::CreateSocket(int family, int type) {
1086 PhysicalSocket* socket = new PhysicalSocket(this);
1087 if (socket->Create(family, type)) {
1088 return socket;
1089 } else {
1090 delete socket;
jbauch095ae152015-12-18 01:39:55 -08001091 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001092 }
1093}
1094
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001095AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int family, int type) {
1096 SocketDispatcher* dispatcher = new SocketDispatcher(this);
1097 if (dispatcher->Create(family, type)) {
1098 return dispatcher;
1099 } else {
1100 delete dispatcher;
jbauch095ae152015-12-18 01:39:55 -08001101 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001102 }
1103}
1104
1105AsyncSocket* PhysicalSocketServer::WrapSocket(SOCKET s) {
1106 SocketDispatcher* dispatcher = new SocketDispatcher(s, this);
1107 if (dispatcher->Initialize()) {
1108 return dispatcher;
1109 } else {
1110 delete dispatcher;
jbauch095ae152015-12-18 01:39:55 -08001111 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001112 }
1113}
1114
Yves Gerey665174f2018-06-19 15:03:05 +02001115void PhysicalSocketServer::Add(Dispatcher* pdispatcher) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001116 CritScope cs(&crit_);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001117 if (key_by_dispatcher_.count(pdispatcher)) {
1118 RTC_LOG(LS_WARNING)
1119 << "PhysicalSocketServer asked to add a duplicate dispatcher.";
1120 return;
jbauchde4db112017-05-31 13:09:18 -07001121 }
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001122 uint64_t key = next_dispatcher_key_++;
1123 dispatcher_by_key_.emplace(key, pdispatcher);
1124 key_by_dispatcher_.emplace(pdispatcher, key);
jbauchde4db112017-05-31 13:09:18 -07001125#if defined(WEBRTC_USE_EPOLL)
1126 if (epoll_fd_ != INVALID_SOCKET) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001127 AddEpoll(pdispatcher, key);
jbauchde4db112017-05-31 13:09:18 -07001128 }
1129#endif // WEBRTC_USE_EPOLL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001130}
1131
Yves Gerey665174f2018-06-19 15:03:05 +02001132void PhysicalSocketServer::Remove(Dispatcher* pdispatcher) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001133 CritScope cs(&crit_);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001134 if (!key_by_dispatcher_.count(pdispatcher)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001135 RTC_LOG(LS_WARNING)
1136 << "PhysicalSocketServer asked to remove a unknown "
Jonas Olssonb2b20312020-01-14 12:11:31 +01001137 "dispatcher, potentially from a duplicate call to Add.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001138 return;
1139 }
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001140 uint64_t key = key_by_dispatcher_.at(pdispatcher);
1141 key_by_dispatcher_.erase(pdispatcher);
1142 dispatcher_by_key_.erase(key);
jbauchde4db112017-05-31 13:09:18 -07001143#if defined(WEBRTC_USE_EPOLL)
1144 if (epoll_fd_ != INVALID_SOCKET) {
1145 RemoveEpoll(pdispatcher);
1146 }
1147#endif // WEBRTC_USE_EPOLL
1148}
1149
1150void PhysicalSocketServer::Update(Dispatcher* pdispatcher) {
1151#if defined(WEBRTC_USE_EPOLL)
1152 if (epoll_fd_ == INVALID_SOCKET) {
1153 return;
1154 }
1155
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001156 // Don't update dispatchers that haven't yet been added.
jbauchde4db112017-05-31 13:09:18 -07001157 CritScope cs(&crit_);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001158 if (!key_by_dispatcher_.count(pdispatcher)) {
jbauchde4db112017-05-31 13:09:18 -07001159 return;
1160 }
1161
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001162 UpdateEpoll(pdispatcher, key_by_dispatcher_.at(pdispatcher));
jbauchde4db112017-05-31 13:09:18 -07001163#endif
1164}
1165
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001166#if defined(WEBRTC_POSIX)
jbauchde4db112017-05-31 13:09:18 -07001167
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001168bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001169 // We don't support reentrant waiting.
1170 RTC_DCHECK(!waiting_);
1171 ScopedSetTrue s(&waiting_);
jbauchde4db112017-05-31 13:09:18 -07001172#if defined(WEBRTC_USE_EPOLL)
1173 // We don't keep a dedicated "epoll" descriptor containing only the non-IO
1174 // (i.e. signaling) dispatcher, so "poll" will be used instead of the default
1175 // "select" to support sockets larger than FD_SETSIZE.
1176 if (!process_io) {
1177 return WaitPoll(cmsWait, signal_wakeup_);
1178 } else if (epoll_fd_ != INVALID_SOCKET) {
1179 return WaitEpoll(cmsWait);
1180 }
1181#endif
1182 return WaitSelect(cmsWait, process_io);
1183}
1184
1185static void ProcessEvents(Dispatcher* dispatcher,
1186 bool readable,
1187 bool writable,
1188 bool check_error) {
1189 int errcode = 0;
1190 // TODO(pthatcher): Should we set errcode if getsockopt fails?
1191 if (check_error) {
1192 socklen_t len = sizeof(errcode);
1193 ::getsockopt(dispatcher->GetDescriptor(), SOL_SOCKET, SO_ERROR, &errcode,
1194 &len);
1195 }
1196
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001197 // Most often the socket is writable or readable or both, so make a single
1198 // virtual call to get requested events
1199 const uint32_t requested_events = dispatcher->GetRequestedEvents();
jbauchde4db112017-05-31 13:09:18 -07001200 uint32_t ff = 0;
1201
1202 // Check readable descriptors. If we're waiting on an accept, signal
1203 // that. Otherwise we're waiting for data, check to see if we're
1204 // readable or really closed.
1205 // TODO(pthatcher): Only peek at TCP descriptors.
1206 if (readable) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001207 if (requested_events & DE_ACCEPT) {
jbauchde4db112017-05-31 13:09:18 -07001208 ff |= DE_ACCEPT;
1209 } else if (errcode || dispatcher->IsDescriptorClosed()) {
1210 ff |= DE_CLOSE;
1211 } else {
1212 ff |= DE_READ;
1213 }
1214 }
1215
1216 // Check writable descriptors. If we're waiting on a connect, detect
1217 // success versus failure by the reaped error code.
1218 if (writable) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001219 if (requested_events & DE_CONNECT) {
jbauchde4db112017-05-31 13:09:18 -07001220 if (!errcode) {
1221 ff |= DE_CONNECT;
1222 } else {
1223 ff |= DE_CLOSE;
1224 }
1225 } else {
1226 ff |= DE_WRITE;
1227 }
1228 }
1229
1230 // Tell the descriptor about the event.
1231 if (ff != 0) {
jbauchde4db112017-05-31 13:09:18 -07001232 dispatcher->OnEvent(ff, errcode);
1233 }
1234}
1235
1236bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001237 // Calculate timing information
1238
deadbeef37f5ecf2017-02-27 14:06:41 -08001239 struct timeval* ptvWait = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001240 struct timeval tvWait;
Niels Möller689b5872018-08-29 09:55:44 +02001241 int64_t stop_us;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001242 if (cmsWait != kForever) {
1243 // Calculate wait timeval
1244 tvWait.tv_sec = cmsWait / 1000;
1245 tvWait.tv_usec = (cmsWait % 1000) * 1000;
1246 ptvWait = &tvWait;
1247
Niels Möller689b5872018-08-29 09:55:44 +02001248 // Calculate when to return
1249 stop_us = rtc::TimeMicros() + cmsWait * 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001250 }
1251
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001252
1253 fd_set fdsRead;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001254 fd_set fdsWrite;
Yves Gerey665174f2018-06-19 15:03:05 +02001255// Explicitly unpoison these FDs on MemorySanitizer which doesn't handle the
1256// inline assembly in FD_ZERO.
1257// http://crbug.com/344505
pbos@webrtc.org27e58982014-10-07 17:56:53 +00001258#ifdef MEMORY_SANITIZER
1259 __msan_unpoison(&fdsRead, sizeof(fdsRead));
1260 __msan_unpoison(&fdsWrite, sizeof(fdsWrite));
1261#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001262
1263 fWait_ = true;
1264
1265 while (fWait_) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001266 // Zero all fd_sets. Although select() zeros the descriptors not signaled,
1267 // we may need to do this for dispatchers that were deleted while
1268 // iterating.
1269 FD_ZERO(&fdsRead);
1270 FD_ZERO(&fdsWrite);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001271 int fdmax = -1;
1272 {
1273 CritScope cr(&crit_);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001274 current_dispatcher_keys_.clear();
1275 for (auto const& kv : dispatcher_by_key_) {
1276 uint64_t key = kv.first;
1277 Dispatcher* pdispatcher = kv.second;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001278 // Query dispatchers for read and write wait state
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001279 if (!process_io && (pdispatcher != signal_wakeup_))
1280 continue;
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001281 current_dispatcher_keys_.push_back(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001282 int fd = pdispatcher->GetDescriptor();
jbauchde4db112017-05-31 13:09:18 -07001283 // "select"ing a file descriptor that is equal to or larger than
1284 // FD_SETSIZE will result in undefined behavior.
1285 RTC_DCHECK_LT(fd, FD_SETSIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001286 if (fd > fdmax)
1287 fdmax = fd;
1288
Peter Boström0c4e06b2015-10-07 12:23:21 +02001289 uint32_t ff = pdispatcher->GetRequestedEvents();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001290 if (ff & (DE_READ | DE_ACCEPT))
1291 FD_SET(fd, &fdsRead);
1292 if (ff & (DE_WRITE | DE_CONNECT))
1293 FD_SET(fd, &fdsWrite);
1294 }
1295 }
1296
1297 // Wait then call handlers as appropriate
1298 // < 0 means error
1299 // 0 means timeout
1300 // > 0 means count of descriptors ready
deadbeef37f5ecf2017-02-27 14:06:41 -08001301 int n = select(fdmax + 1, &fdsRead, &fdsWrite, nullptr, ptvWait);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001302
1303 // If error, return error.
1304 if (n < 0) {
1305 if (errno != EINTR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001306 RTC_LOG_E(LS_ERROR, EN, errno) << "select";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001307 return false;
1308 }
1309 // Else ignore the error and keep going. If this EINTR was for one of the
1310 // signals managed by this PhysicalSocketServer, the
1311 // PosixSignalDeliveryDispatcher will be in the signaled state in the next
1312 // iteration.
1313 } else if (n == 0) {
1314 // If timeout, return success
1315 return true;
1316 } else {
1317 // We have signaled descriptors
1318 CritScope cr(&crit_);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001319 // Iterate only on the dispatchers whose sockets were passed into
1320 // WSAEventSelect; this avoids the ABA problem (a socket being
1321 // destroyed and a new one created with the same file descriptor).
1322 for (uint64_t key : current_dispatcher_keys_) {
1323 if (!dispatcher_by_key_.count(key))
1324 continue;
1325 Dispatcher* pdispatcher = dispatcher_by_key_.at(key);
1326
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001327 int fd = pdispatcher->GetDescriptor();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001328
jbauchde4db112017-05-31 13:09:18 -07001329 bool readable = FD_ISSET(fd, &fdsRead);
1330 if (readable) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001331 FD_CLR(fd, &fdsRead);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001332 }
1333
jbauchde4db112017-05-31 13:09:18 -07001334 bool writable = FD_ISSET(fd, &fdsWrite);
1335 if (writable) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001336 FD_CLR(fd, &fdsWrite);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001337 }
1338
jbauchde4db112017-05-31 13:09:18 -07001339 // The error code can be signaled through reads or writes.
1340 ProcessEvents(pdispatcher, readable, writable, readable || writable);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001341 }
1342 }
1343
1344 // Recalc the time remaining to wait. Doing it here means it doesn't get
1345 // calced twice the first time through the loop
1346 if (ptvWait) {
1347 ptvWait->tv_sec = 0;
1348 ptvWait->tv_usec = 0;
Niels Möller689b5872018-08-29 09:55:44 +02001349 int64_t time_left_us = stop_us - rtc::TimeMicros();
1350 if (time_left_us > 0) {
1351 ptvWait->tv_sec = time_left_us / rtc::kNumMicrosecsPerSec;
1352 ptvWait->tv_usec = time_left_us % rtc::kNumMicrosecsPerSec;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001353 }
1354 }
1355 }
1356
1357 return true;
1358}
1359
jbauchde4db112017-05-31 13:09:18 -07001360#if defined(WEBRTC_USE_EPOLL)
1361
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001362void PhysicalSocketServer::AddEpoll(Dispatcher* pdispatcher, uint64_t key) {
jbauchde4db112017-05-31 13:09:18 -07001363 RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
1364 int fd = pdispatcher->GetDescriptor();
1365 RTC_DCHECK(fd != INVALID_SOCKET);
1366 if (fd == INVALID_SOCKET) {
1367 return;
1368 }
1369
1370 struct epoll_event event = {0};
1371 event.events = GetEpollEvents(pdispatcher->GetRequestedEvents());
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001372 event.data.u64 = key;
jbauchde4db112017-05-31 13:09:18 -07001373 int err = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event);
1374 RTC_DCHECK_EQ(err, 0);
1375 if (err == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001376 RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_ADD";
jbauchde4db112017-05-31 13:09:18 -07001377 }
1378}
1379
1380void PhysicalSocketServer::RemoveEpoll(Dispatcher* pdispatcher) {
1381 RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
1382 int fd = pdispatcher->GetDescriptor();
1383 RTC_DCHECK(fd != INVALID_SOCKET);
1384 if (fd == INVALID_SOCKET) {
1385 return;
1386 }
1387
1388 struct epoll_event event = {0};
1389 int err = epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &event);
1390 RTC_DCHECK(err == 0 || errno == ENOENT);
1391 if (err == -1) {
1392 if (errno == ENOENT) {
1393 // Socket has already been closed.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001394 RTC_LOG_E(LS_VERBOSE, EN, errno) << "epoll_ctl EPOLL_CTL_DEL";
jbauchde4db112017-05-31 13:09:18 -07001395 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001396 RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_DEL";
jbauchde4db112017-05-31 13:09:18 -07001397 }
1398 }
1399}
1400
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001401void PhysicalSocketServer::UpdateEpoll(Dispatcher* pdispatcher, uint64_t key) {
jbauchde4db112017-05-31 13:09:18 -07001402 RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
1403 int fd = pdispatcher->GetDescriptor();
1404 RTC_DCHECK(fd != INVALID_SOCKET);
1405 if (fd == INVALID_SOCKET) {
1406 return;
1407 }
1408
1409 struct epoll_event event = {0};
1410 event.events = GetEpollEvents(pdispatcher->GetRequestedEvents());
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001411 event.data.u64 = key;
jbauchde4db112017-05-31 13:09:18 -07001412 int err = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &event);
1413 RTC_DCHECK_EQ(err, 0);
1414 if (err == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001415 RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_MOD";
jbauchde4db112017-05-31 13:09:18 -07001416 }
1417}
1418
1419bool PhysicalSocketServer::WaitEpoll(int cmsWait) {
1420 RTC_DCHECK(epoll_fd_ != INVALID_SOCKET);
1421 int64_t tvWait = -1;
1422 int64_t tvStop = -1;
1423 if (cmsWait != kForever) {
1424 tvWait = cmsWait;
1425 tvStop = TimeAfter(cmsWait);
1426 }
1427
jbauchde4db112017-05-31 13:09:18 -07001428 fWait_ = true;
jbauchde4db112017-05-31 13:09:18 -07001429 while (fWait_) {
1430 // Wait then call handlers as appropriate
1431 // < 0 means error
1432 // 0 means timeout
1433 // > 0 means count of descriptors ready
Markus Handellb7c63ab2020-05-26 18:09:55 +02001434 int n = epoll_wait(epoll_fd_, epoll_events_.data(), epoll_events_.size(),
jbauchde4db112017-05-31 13:09:18 -07001435 static_cast<int>(tvWait));
1436 if (n < 0) {
1437 if (errno != EINTR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001438 RTC_LOG_E(LS_ERROR, EN, errno) << "epoll";
jbauchde4db112017-05-31 13:09:18 -07001439 return false;
1440 }
1441 // Else ignore the error and keep going. If this EINTR was for one of the
1442 // signals managed by this PhysicalSocketServer, the
1443 // PosixSignalDeliveryDispatcher will be in the signaled state in the next
1444 // iteration.
1445 } else if (n == 0) {
1446 // If timeout, return success
1447 return true;
1448 } else {
1449 // We have signaled descriptors
1450 CritScope cr(&crit_);
1451 for (int i = 0; i < n; ++i) {
1452 const epoll_event& event = epoll_events_[i];
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001453 uint64_t key = event.data.u64;
1454 if (!dispatcher_by_key_.count(key)) {
jbauchde4db112017-05-31 13:09:18 -07001455 // The dispatcher for this socket no longer exists.
1456 continue;
1457 }
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001458 Dispatcher* pdispatcher = dispatcher_by_key_.at(key);
jbauchde4db112017-05-31 13:09:18 -07001459
1460 bool readable = (event.events & (EPOLLIN | EPOLLPRI));
1461 bool writable = (event.events & EPOLLOUT);
1462 bool check_error = (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP));
1463
1464 ProcessEvents(pdispatcher, readable, writable, check_error);
1465 }
1466 }
1467
jbauchde4db112017-05-31 13:09:18 -07001468 if (cmsWait != kForever) {
1469 tvWait = TimeDiff(tvStop, TimeMillis());
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001470 if (tvWait <= 0) {
jbauchde4db112017-05-31 13:09:18 -07001471 // Return success on timeout.
1472 return true;
1473 }
1474 }
1475 }
1476
1477 return true;
1478}
1479
1480bool PhysicalSocketServer::WaitPoll(int cmsWait, Dispatcher* dispatcher) {
1481 RTC_DCHECK(dispatcher);
1482 int64_t tvWait = -1;
1483 int64_t tvStop = -1;
1484 if (cmsWait != kForever) {
1485 tvWait = cmsWait;
1486 tvStop = TimeAfter(cmsWait);
1487 }
1488
1489 fWait_ = true;
1490
1491 struct pollfd fds = {0};
1492 int fd = dispatcher->GetDescriptor();
1493 fds.fd = fd;
1494
1495 while (fWait_) {
1496 uint32_t ff = dispatcher->GetRequestedEvents();
1497 fds.events = 0;
1498 if (ff & (DE_READ | DE_ACCEPT)) {
1499 fds.events |= POLLIN;
1500 }
1501 if (ff & (DE_WRITE | DE_CONNECT)) {
1502 fds.events |= POLLOUT;
1503 }
1504 fds.revents = 0;
1505
1506 // Wait then call handlers as appropriate
1507 // < 0 means error
1508 // 0 means timeout
1509 // > 0 means count of descriptors ready
1510 int n = poll(&fds, 1, static_cast<int>(tvWait));
1511 if (n < 0) {
1512 if (errno != EINTR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001513 RTC_LOG_E(LS_ERROR, EN, errno) << "poll";
jbauchde4db112017-05-31 13:09:18 -07001514 return false;
1515 }
1516 // Else ignore the error and keep going. If this EINTR was for one of the
1517 // signals managed by this PhysicalSocketServer, the
1518 // PosixSignalDeliveryDispatcher will be in the signaled state in the next
1519 // iteration.
1520 } else if (n == 0) {
1521 // If timeout, return success
1522 return true;
1523 } else {
1524 // We have signaled descriptors (should only be the passed dispatcher).
1525 RTC_DCHECK_EQ(n, 1);
1526 RTC_DCHECK_EQ(fds.fd, fd);
1527
1528 bool readable = (fds.revents & (POLLIN | POLLPRI));
1529 bool writable = (fds.revents & POLLOUT);
1530 bool check_error = (fds.revents & (POLLRDHUP | POLLERR | POLLHUP));
1531
1532 ProcessEvents(dispatcher, readable, writable, check_error);
1533 }
1534
1535 if (cmsWait != kForever) {
1536 tvWait = TimeDiff(tvStop, TimeMillis());
1537 if (tvWait < 0) {
1538 // Return success on timeout.
1539 return true;
1540 }
1541 }
1542 }
1543
1544 return true;
1545}
1546
1547#endif // WEBRTC_USE_EPOLL
1548
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001549#endif // WEBRTC_POSIX
1550
1551#if defined(WEBRTC_WIN)
1552bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001553 // We don't support reentrant waiting.
1554 RTC_DCHECK(!waiting_);
1555 ScopedSetTrue s(&waiting_);
1556
Honghai Zhang82d78622016-05-06 11:29:15 -07001557 int64_t cmsTotal = cmsWait;
1558 int64_t cmsElapsed = 0;
1559 int64_t msStart = Time();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001560
1561 fWait_ = true;
1562 while (fWait_) {
1563 std::vector<WSAEVENT> events;
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001564 std::vector<uint64_t> event_owners;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001565
1566 events.push_back(socket_ev_);
1567
1568 {
1569 CritScope cr(&crit_);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001570 // Get a snapshot of all current dispatchers; this is used to avoid the
1571 // ABA problem (see later comment) and avoids the dispatcher_by_key_
1572 // iterator being invalidated by calling CheckSignalClose, which may
1573 // remove the dispatcher from the list.
1574 current_dispatcher_keys_.clear();
1575 for (auto const& kv : dispatcher_by_key_) {
1576 current_dispatcher_keys_.push_back(kv.first);
1577 }
1578 for (uint64_t key : current_dispatcher_keys_) {
1579 if (!dispatcher_by_key_.count(key)) {
1580 continue;
1581 }
1582 Dispatcher* disp = dispatcher_by_key_.at(key);
1583 if (!disp)
1584 continue;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001585 if (!process_io && (disp != signal_wakeup_))
1586 continue;
1587 SOCKET s = disp->GetSocket();
1588 if (disp->CheckSignalClose()) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001589 // We just signalled close, don't poll this socket.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001590 } else if (s != INVALID_SOCKET) {
Yves Gerey665174f2018-06-19 15:03:05 +02001591 WSAEventSelect(s, events[0],
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001592 FlagsToEvents(disp->GetRequestedEvents()));
1593 } else {
1594 events.push_back(disp->GetWSAEvent());
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001595 event_owners.push_back(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001596 }
1597 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001598 }
1599
1600 // Which is shorter, the delay wait or the asked wait?
1601
Honghai Zhang82d78622016-05-06 11:29:15 -07001602 int64_t cmsNext;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001603 if (cmsWait == kForever) {
1604 cmsNext = cmsWait;
1605 } else {
Honghai Zhang82d78622016-05-06 11:29:15 -07001606 cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001607 }
1608
1609 // Wait for one of the events to signal
Yves Gerey665174f2018-06-19 15:03:05 +02001610 DWORD dw =
1611 WSAWaitForMultipleEvents(static_cast<DWORD>(events.size()), &events[0],
1612 false, static_cast<DWORD>(cmsNext), false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001613
1614 if (dw == WSA_WAIT_FAILED) {
1615 // Failed?
jbauch095ae152015-12-18 01:39:55 -08001616 // TODO(pthatcher): need a better strategy than this!
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001617 WSAGetLastError();
nissec80e7412017-01-11 05:56:46 -08001618 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001619 return false;
1620 } else if (dw == WSA_WAIT_TIMEOUT) {
1621 // Timeout?
1622 return true;
1623 } else {
1624 // Figure out which one it is and call it
1625 CritScope cr(&crit_);
1626 int index = dw - WSA_WAIT_EVENT_0;
1627 if (index > 0) {
Yves Gerey665174f2018-06-19 15:03:05 +02001628 --index; // The first event is the socket event
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001629 uint64_t key = event_owners[index];
1630 if (!dispatcher_by_key_.count(key)) {
1631 // The dispatcher could have been removed while waiting for events.
1632 continue;
jbauchde4db112017-05-31 13:09:18 -07001633 }
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001634 Dispatcher* disp = dispatcher_by_key_.at(key);
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001635 disp->OnEvent(0, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001636 } else if (process_io) {
Taylor Brandstetter7b69a442020-08-20 23:43:13 +00001637 // Iterate only on the dispatchers whose sockets were passed into
1638 // WSAEventSelect; this avoids the ABA problem (a socket being
1639 // destroyed and a new one created with the same SOCKET handle).
1640 for (uint64_t key : current_dispatcher_keys_) {
1641 if (!dispatcher_by_key_.count(key)) {
1642 continue;
1643 }
1644 Dispatcher* disp = dispatcher_by_key_.at(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001645 SOCKET s = disp->GetSocket();
1646 if (s == INVALID_SOCKET)
1647 continue;
1648
1649 WSANETWORKEVENTS wsaEvents;
1650 int err = WSAEnumNetworkEvents(s, events[0], &wsaEvents);
1651 if (err == 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001652 {
1653 if ((wsaEvents.lNetworkEvents & FD_READ) &&
1654 wsaEvents.iErrorCode[FD_READ_BIT] != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001655 RTC_LOG(WARNING)
1656 << "PhysicalSocketServer got FD_READ_BIT error "
1657 << wsaEvents.iErrorCode[FD_READ_BIT];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001658 }
1659 if ((wsaEvents.lNetworkEvents & FD_WRITE) &&
1660 wsaEvents.iErrorCode[FD_WRITE_BIT] != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001661 RTC_LOG(WARNING)
1662 << "PhysicalSocketServer got FD_WRITE_BIT error "
1663 << wsaEvents.iErrorCode[FD_WRITE_BIT];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001664 }
1665 if ((wsaEvents.lNetworkEvents & FD_CONNECT) &&
1666 wsaEvents.iErrorCode[FD_CONNECT_BIT] != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001667 RTC_LOG(WARNING)
1668 << "PhysicalSocketServer got FD_CONNECT_BIT error "
1669 << wsaEvents.iErrorCode[FD_CONNECT_BIT];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001670 }
1671 if ((wsaEvents.lNetworkEvents & FD_ACCEPT) &&
1672 wsaEvents.iErrorCode[FD_ACCEPT_BIT] != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001673 RTC_LOG(WARNING)
1674 << "PhysicalSocketServer got FD_ACCEPT_BIT error "
1675 << wsaEvents.iErrorCode[FD_ACCEPT_BIT];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001676 }
1677 if ((wsaEvents.lNetworkEvents & FD_CLOSE) &&
1678 wsaEvents.iErrorCode[FD_CLOSE_BIT] != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001679 RTC_LOG(WARNING)
1680 << "PhysicalSocketServer got FD_CLOSE_BIT error "
1681 << wsaEvents.iErrorCode[FD_CLOSE_BIT];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001682 }
1683 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001684 uint32_t ff = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001685 int errcode = 0;
1686 if (wsaEvents.lNetworkEvents & FD_READ)
1687 ff |= DE_READ;
1688 if (wsaEvents.lNetworkEvents & FD_WRITE)
1689 ff |= DE_WRITE;
1690 if (wsaEvents.lNetworkEvents & FD_CONNECT) {
1691 if (wsaEvents.iErrorCode[FD_CONNECT_BIT] == 0) {
1692 ff |= DE_CONNECT;
1693 } else {
1694 ff |= DE_CLOSE;
1695 errcode = wsaEvents.iErrorCode[FD_CONNECT_BIT];
1696 }
1697 }
1698 if (wsaEvents.lNetworkEvents & FD_ACCEPT)
1699 ff |= DE_ACCEPT;
1700 if (wsaEvents.lNetworkEvents & FD_CLOSE) {
1701 ff |= DE_CLOSE;
1702 errcode = wsaEvents.iErrorCode[FD_CLOSE_BIT];
1703 }
1704 if (ff != 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001705 disp->OnEvent(ff, errcode);
1706 }
1707 }
1708 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001709 }
1710
1711 // Reset the network event until new activity occurs
1712 WSAResetEvent(socket_ev_);
1713 }
1714
1715 // Break?
1716 if (!fWait_)
1717 break;
1718 cmsElapsed = TimeSince(msStart);
1719 if ((cmsWait != kForever) && (cmsElapsed >= cmsWait)) {
Yves Gerey665174f2018-06-19 15:03:05 +02001720 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001721 }
1722 }
1723
1724 // Done
1725 return true;
1726}
honghaizcec0a082016-01-15 14:49:09 -08001727#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001728
1729} // namespace rtc