henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 10 | #include "rtc_base/physical_socket_server.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 11 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 12 | #include <cstdint> |
| 13 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #pragma warning(disable : 4786) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #endif |
| 17 | |
pbos@webrtc.org | 27e5898 | 2014-10-07 17:56:53 +0000 | [diff] [blame] | 18 | #ifdef MEMORY_SANITIZER |
| 19 | #include <sanitizer/msan_interface.h> |
| 20 | #endif |
| 21 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #if defined(WEBRTC_POSIX) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | #include <string.h> |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 25 | #if defined(WEBRTC_USE_EPOLL) |
| 26 | // "poll" will be used to wait for the signal dispatcher. |
| 27 | #include <poll.h> |
| 28 | #endif |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/select.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <unistd.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | #include <windows.h> |
| 37 | #include <winsock2.h> |
| 38 | #include <ws2tcpip.h> |
| 39 | #undef SetPort |
| 40 | #endif |
| 41 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 42 | #include <errno.h> |
| 43 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | #include <algorithm> |
| 45 | #include <map> |
| 46 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 47 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 48 | #include "rtc_base/byte_order.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 49 | #include "rtc_base/checks.h" |
| 50 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 51 | #include "rtc_base/network_monitor.h" |
| 52 | #include "rtc_base/null_socket_server.h" |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 53 | #include "rtc_base/synchronization/mutex.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 54 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | |
Emilio Cobos Álvarez | 6806550 | 2019-05-29 15:30:32 +0200 | [diff] [blame] | 56 | #if defined(WEBRTC_LINUX) |
| 57 | #include <linux/sockios.h> |
| 58 | #endif |
| 59 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 60 | #if defined(WEBRTC_WIN) |
| 61 | #define LAST_SYSTEM_ERROR (::GetLastError()) |
| 62 | #elif defined(__native_client__) && __native_client__ |
| 63 | #define LAST_SYSTEM_ERROR (0) |
| 64 | #elif defined(WEBRTC_POSIX) |
| 65 | #define LAST_SYSTEM_ERROR (errno) |
| 66 | #endif // WEBRTC_WIN |
| 67 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | #if defined(WEBRTC_POSIX) |
| 69 | #include <netinet/tcp.h> // for TCP_NODELAY |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | #define IP_MTU 14 // Until this is integrated from linux/in.h to netinet/in.h |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | typedef void* SockOptArg; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 72 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 73 | #endif // WEBRTC_POSIX |
| 74 | |
Stefan Holmer | 3ebb3ef | 2016-05-23 20:26:11 +0200 | [diff] [blame] | 75 | #if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) && !defined(__native_client__) |
| 76 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 77 | int64_t GetSocketRecvTimestamp(int socket) { |
| 78 | struct timeval tv_ioctl; |
| 79 | int ret = ioctl(socket, SIOCGSTAMP, &tv_ioctl); |
| 80 | if (ret != 0) |
| 81 | return -1; |
| 82 | int64_t timestamp = |
| 83 | rtc::kNumMicrosecsPerSec * static_cast<int64_t>(tv_ioctl.tv_sec) + |
| 84 | static_cast<int64_t>(tv_ioctl.tv_usec); |
| 85 | return timestamp; |
| 86 | } |
| 87 | |
| 88 | #else |
| 89 | |
| 90 | int64_t GetSocketRecvTimestamp(int socket) { |
| 91 | return -1; |
| 92 | } |
| 93 | #endif |
| 94 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | #if defined(WEBRTC_WIN) |
| 96 | typedef char* SockOptArg; |
| 97 | #endif |
| 98 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 99 | #if defined(WEBRTC_USE_EPOLL) |
| 100 | // POLLRDHUP / EPOLLRDHUP are only defined starting with Linux 2.6.17. |
| 101 | #if !defined(POLLRDHUP) |
| 102 | #define POLLRDHUP 0x2000 |
| 103 | #endif |
| 104 | #if !defined(EPOLLRDHUP) |
| 105 | #define EPOLLRDHUP 0x2000 |
| 106 | #endif |
| 107 | #endif |
| 108 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 109 | namespace { |
| 110 | class ScopedSetTrue { |
| 111 | public: |
| 112 | ScopedSetTrue(bool* value) : value_(value) { |
| 113 | RTC_DCHECK(!*value_); |
| 114 | *value_ = true; |
| 115 | } |
| 116 | ~ScopedSetTrue() { *value_ = false; } |
| 117 | |
| 118 | private: |
| 119 | bool* value_; |
| 120 | }; |
| 121 | } // namespace |
| 122 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | namespace rtc { |
| 124 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 125 | PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 126 | : ss_(ss), |
| 127 | s_(s), |
| 128 | error_(0), |
| 129 | state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), |
| 130 | resolver_(nullptr) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 131 | if (s_ != INVALID_SOCKET) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 132 | SetEnabledEvents(DE_READ | DE_WRITE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 134 | int type = SOCK_STREAM; |
| 135 | socklen_t len = sizeof(type); |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 136 | const int res = |
| 137 | getsockopt(s_, SOL_SOCKET, SO_TYPE, (SockOptArg)&type, &len); |
| 138 | RTC_DCHECK_EQ(0, res); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | udp_ = (SOCK_DGRAM == type); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 141 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 143 | PhysicalSocket::~PhysicalSocket() { |
| 144 | Close(); |
| 145 | } |
| 146 | |
| 147 | bool PhysicalSocket::Create(int family, int type) { |
| 148 | Close(); |
| 149 | s_ = ::socket(family, type, 0); |
| 150 | udp_ = (SOCK_DGRAM == type); |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 151 | family_ = family; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 152 | UpdateLastError(); |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 153 | if (udp_) { |
| 154 | SetEnabledEvents(DE_READ | DE_WRITE); |
| 155 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 156 | return s_ != INVALID_SOCKET; |
| 157 | } |
| 158 | |
| 159 | SocketAddress PhysicalSocket::GetLocalAddress() const { |
Mirko Bonadei | 108a2f0 | 2019-11-20 19:43:38 +0100 | [diff] [blame] | 160 | sockaddr_storage addr_storage = {}; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 161 | socklen_t addrlen = sizeof(addr_storage); |
| 162 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 163 | int result = ::getsockname(s_, addr, &addrlen); |
| 164 | SocketAddress address; |
| 165 | if (result >= 0) { |
| 166 | SocketAddressFromSockAddrStorage(addr_storage, &address); |
| 167 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 168 | RTC_LOG(LS_WARNING) << "GetLocalAddress: unable to get local addr, socket=" |
| 169 | << s_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 171 | return address; |
| 172 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 174 | SocketAddress PhysicalSocket::GetRemoteAddress() const { |
Mirko Bonadei | 108a2f0 | 2019-11-20 19:43:38 +0100 | [diff] [blame] | 175 | sockaddr_storage addr_storage = {}; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 176 | socklen_t addrlen = sizeof(addr_storage); |
| 177 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 178 | int result = ::getpeername(s_, addr, &addrlen); |
| 179 | SocketAddress address; |
| 180 | if (result >= 0) { |
| 181 | SocketAddressFromSockAddrStorage(addr_storage, &address); |
| 182 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 183 | RTC_LOG(LS_WARNING) |
| 184 | << "GetRemoteAddress: unable to get remote addr, socket=" << s_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 186 | return address; |
| 187 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 188 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 189 | int PhysicalSocket::Bind(const SocketAddress& bind_addr) { |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 190 | SocketAddress copied_bind_addr = bind_addr; |
| 191 | // If a network binder is available, use it to bind a socket to an interface |
| 192 | // instead of bind(), since this is more reliable on an OS with a weak host |
| 193 | // model. |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 194 | if (ss_->network_binder() && !bind_addr.IsAnyIP()) { |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 195 | NetworkBindingResult result = |
| 196 | ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr()); |
| 197 | if (result == NetworkBindingResult::SUCCESS) { |
| 198 | // Since the network binder handled binding the socket to the desired |
| 199 | // network interface, we don't need to (and shouldn't) include an IP in |
| 200 | // the bind() call; bind() just needs to assign a port. |
| 201 | copied_bind_addr.SetIP(GetAnyIP(copied_bind_addr.ipaddr().family())); |
| 202 | } else if (result == NetworkBindingResult::NOT_IMPLEMENTED) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 203 | RTC_LOG(LS_INFO) << "Can't bind socket to network because " |
| 204 | "network binding is not implemented for this OS."; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 205 | } else { |
| 206 | if (bind_addr.IsLoopbackIP()) { |
| 207 | // If we couldn't bind to a loopback IP (which should only happen in |
| 208 | // test scenarios), continue on. This may be expected behavior. |
Paulina Hensman | b239a2e | 2020-03-31 16:16:11 +0200 | [diff] [blame] | 209 | RTC_LOG(LS_VERBOSE) << "Binding socket to loopback address" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 210 | << " failed; result: " << static_cast<int>(result); |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 211 | } else { |
Paulina Hensman | b239a2e | 2020-03-31 16:16:11 +0200 | [diff] [blame] | 212 | RTC_LOG(LS_WARNING) << "Binding socket to network address" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 213 | << " failed; result: " << static_cast<int>(result); |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 214 | // If a network binding was attempted and failed, we should stop here |
| 215 | // and not try to use the socket. Otherwise, we may end up sending |
| 216 | // packets with an invalid source address. |
| 217 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7026 |
| 218 | return -1; |
| 219 | } |
| 220 | } |
| 221 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 222 | sockaddr_storage addr_storage; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 223 | size_t len = copied_bind_addr.ToSockAddrStorage(&addr_storage); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 224 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 225 | int err = ::bind(s_, addr, static_cast<int>(len)); |
| 226 | UpdateLastError(); |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 227 | #if !defined(NDEBUG) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 228 | if (0 == err) { |
| 229 | dbg_addr_ = "Bound @ "; |
| 230 | dbg_addr_.append(GetLocalAddress().ToString()); |
| 231 | } |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 232 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 233 | return err; |
| 234 | } |
| 235 | |
| 236 | int PhysicalSocket::Connect(const SocketAddress& addr) { |
| 237 | // TODO(pthatcher): Implicit creation is required to reconnect... |
| 238 | // ...but should we make it more explicit? |
| 239 | if (state_ != CS_CLOSED) { |
| 240 | SetError(EALREADY); |
| 241 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 243 | if (addr.IsUnresolvedIP()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 244 | RTC_LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect"; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 245 | resolver_ = new AsyncResolver(); |
| 246 | resolver_->SignalDone.connect(this, &PhysicalSocket::OnResolveResult); |
| 247 | resolver_->Start(addr); |
| 248 | state_ = CS_CONNECTING; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 252 | return DoConnect(addr); |
| 253 | } |
| 254 | |
| 255 | int PhysicalSocket::DoConnect(const SocketAddress& connect_addr) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 256 | if ((s_ == INVALID_SOCKET) && !Create(connect_addr.family(), SOCK_STREAM)) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 257 | return SOCKET_ERROR; |
| 258 | } |
| 259 | sockaddr_storage addr_storage; |
| 260 | size_t len = connect_addr.ToSockAddrStorage(&addr_storage); |
| 261 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 262 | int err = ::connect(s_, addr, static_cast<int>(len)); |
| 263 | UpdateLastError(); |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 264 | uint8_t events = DE_READ | DE_WRITE; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 265 | if (err == 0) { |
| 266 | state_ = CS_CONNECTED; |
| 267 | } else if (IsBlockingError(GetError())) { |
| 268 | state_ = CS_CONNECTING; |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 269 | events |= DE_CONNECT; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 270 | } else { |
| 271 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 272 | } |
| 273 | |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 274 | EnableEvents(events); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 275 | return 0; |
| 276 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 277 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 278 | int PhysicalSocket::GetError() const { |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 279 | webrtc::MutexLock lock(&mutex_); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 280 | return error_; |
| 281 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 282 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 283 | void PhysicalSocket::SetError(int error) { |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 284 | webrtc::MutexLock lock(&mutex_); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 285 | error_ = error; |
| 286 | } |
| 287 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 288 | Socket::ConnState PhysicalSocket::GetState() const { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 289 | return state_; |
| 290 | } |
| 291 | |
| 292 | int PhysicalSocket::GetOption(Option opt, int* value) { |
| 293 | int slevel; |
| 294 | int sopt; |
| 295 | if (TranslateOption(opt, &slevel, &sopt) == -1) |
| 296 | return -1; |
| 297 | socklen_t optlen = sizeof(*value); |
| 298 | int ret = ::getsockopt(s_, slevel, sopt, (SockOptArg)value, &optlen); |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 299 | if (ret == -1) { |
| 300 | return -1; |
| 301 | } |
| 302 | if (opt == OPT_DONTFRAGMENT) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 303 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 304 | *value = (*value != IP_PMTUDISC_DONT) ? 1 : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 305 | #endif |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 306 | } else if (opt == OPT_DSCP) { |
| 307 | #if defined(WEBRTC_POSIX) |
| 308 | // unshift DSCP value to get six most significant bits of IP DiffServ field |
| 309 | *value >>= 2; |
| 310 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 311 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 312 | return ret; |
| 313 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 314 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 315 | int PhysicalSocket::SetOption(Option opt, int value) { |
| 316 | int slevel; |
| 317 | int sopt; |
| 318 | if (TranslateOption(opt, &slevel, &sopt) == -1) |
| 319 | return -1; |
| 320 | if (opt == OPT_DONTFRAGMENT) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 321 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 322 | value = (value) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 323 | #endif |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 324 | } else if (opt == OPT_DSCP) { |
| 325 | #if defined(WEBRTC_POSIX) |
| 326 | // shift DSCP value to fit six most significant bits of IP DiffServ field |
| 327 | value <<= 2; |
| 328 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 329 | } |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 330 | #if defined(WEBRTC_POSIX) |
| 331 | if (sopt == IPV6_TCLASS) { |
| 332 | // Set the IPv4 option in all cases to support dual-stack sockets. |
Taylor Brandstetter | 9d26940 | 2020-11-25 15:24:53 -0800 | [diff] [blame] | 333 | // Don't bother checking the return code, as this is expected to fail if |
| 334 | // it's not actually dual-stack. |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 335 | ::setsockopt(s_, IPPROTO_IP, IP_TOS, (SockOptArg)&value, sizeof(value)); |
| 336 | } |
| 337 | #endif |
Taylor Brandstetter | 9d26940 | 2020-11-25 15:24:53 -0800 | [diff] [blame] | 338 | int result = |
| 339 | ::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value)); |
| 340 | if (result != 0) { |
| 341 | UpdateLastError(); |
| 342 | } |
| 343 | return result; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 344 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 345 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 346 | int PhysicalSocket::Send(const void* pv, size_t cb) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 347 | int sent = DoSend( |
| 348 | s_, reinterpret_cast<const char*>(pv), static_cast<int>(cb), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 349 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 350 | // Suppress SIGPIPE. Without this, attempting to send on a socket whose |
| 351 | // other end is closed will result in a SIGPIPE signal being raised to |
| 352 | // our process, which by default will terminate the process, which we |
| 353 | // don't want. By specifying this flag, we'll just get the error EPIPE |
| 354 | // instead and can handle the error gracefully. |
| 355 | MSG_NOSIGNAL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 356 | #else |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 357 | 0 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 358 | #endif |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 359 | ); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 360 | UpdateLastError(); |
| 361 | MaybeRemapSendError(); |
| 362 | // We have seen minidumps where this may be false. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 363 | RTC_DCHECK(sent <= static_cast<int>(cb)); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 364 | if ((sent > 0 && sent < static_cast<int>(cb)) || |
| 365 | (sent < 0 && IsBlockingError(GetError()))) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 366 | EnableEvents(DE_WRITE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 367 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 368 | return sent; |
| 369 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 370 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 371 | int PhysicalSocket::SendTo(const void* buffer, |
| 372 | size_t length, |
| 373 | const SocketAddress& addr) { |
| 374 | sockaddr_storage saddr; |
| 375 | size_t len = addr.ToSockAddrStorage(&saddr); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 376 | int sent = |
| 377 | DoSendTo(s_, static_cast<const char*>(buffer), static_cast<int>(length), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 378 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 379 | // Suppress SIGPIPE. See above for explanation. |
| 380 | MSG_NOSIGNAL, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 381 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 382 | 0, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 383 | #endif |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 384 | reinterpret_cast<sockaddr*>(&saddr), static_cast<int>(len)); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 385 | UpdateLastError(); |
| 386 | MaybeRemapSendError(); |
| 387 | // We have seen minidumps where this may be false. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 388 | RTC_DCHECK(sent <= static_cast<int>(length)); |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 389 | if ((sent > 0 && sent < static_cast<int>(length)) || |
| 390 | (sent < 0 && IsBlockingError(GetError()))) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 391 | EnableEvents(DE_WRITE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 392 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 393 | return sent; |
| 394 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 395 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 396 | int PhysicalSocket::Recv(void* buffer, size_t length, int64_t* timestamp) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 397 | int received = |
| 398 | ::recv(s_, static_cast<char*>(buffer), static_cast<int>(length), 0); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 399 | if ((received == 0) && (length != 0)) { |
| 400 | // Note: on graceful shutdown, recv can return 0. In this case, we |
| 401 | // pretend it is blocking, and then signal close, so that simplifying |
| 402 | // assumptions can be made about Recv. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 403 | RTC_LOG(LS_WARNING) << "EOF from socket; deferring close event"; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 404 | // Must turn this back on so that the select() loop will notice the close |
| 405 | // event. |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 406 | EnableEvents(DE_READ); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 407 | SetError(EWOULDBLOCK); |
| 408 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 409 | } |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 410 | if (timestamp) { |
| 411 | *timestamp = GetSocketRecvTimestamp(s_); |
| 412 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 413 | UpdateLastError(); |
| 414 | int error = GetError(); |
| 415 | bool success = (received >= 0) || IsBlockingError(error); |
| 416 | if (udp_ || success) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 417 | EnableEvents(DE_READ); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 418 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 419 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 420 | RTC_LOG_F(LS_VERBOSE) << "Error = " << error; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 421 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 422 | return received; |
| 423 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 424 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 425 | int PhysicalSocket::RecvFrom(void* buffer, |
| 426 | size_t length, |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 427 | SocketAddress* out_addr, |
| 428 | int64_t* timestamp) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 429 | sockaddr_storage addr_storage; |
| 430 | socklen_t addr_len = sizeof(addr_storage); |
| 431 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 432 | int received = ::recvfrom(s_, static_cast<char*>(buffer), |
| 433 | static_cast<int>(length), 0, addr, &addr_len); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 434 | if (timestamp) { |
| 435 | *timestamp = GetSocketRecvTimestamp(s_); |
| 436 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 437 | UpdateLastError(); |
| 438 | if ((received >= 0) && (out_addr != nullptr)) |
| 439 | SocketAddressFromSockAddrStorage(addr_storage, out_addr); |
| 440 | int error = GetError(); |
| 441 | bool success = (received >= 0) || IsBlockingError(error); |
| 442 | if (udp_ || success) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 443 | EnableEvents(DE_READ); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 444 | } |
| 445 | if (!success) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 446 | RTC_LOG_F(LS_VERBOSE) << "Error = " << error; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 447 | } |
| 448 | return received; |
| 449 | } |
| 450 | |
| 451 | int PhysicalSocket::Listen(int backlog) { |
| 452 | int err = ::listen(s_, backlog); |
| 453 | UpdateLastError(); |
| 454 | if (err == 0) { |
| 455 | state_ = CS_CONNECTING; |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 456 | EnableEvents(DE_ACCEPT); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 457 | #if !defined(NDEBUG) |
| 458 | dbg_addr_ = "Listening @ "; |
| 459 | dbg_addr_.append(GetLocalAddress().ToString()); |
| 460 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 461 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 462 | return err; |
| 463 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 464 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 465 | Socket* PhysicalSocket::Accept(SocketAddress* out_addr) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 466 | // Always re-subscribe DE_ACCEPT to make sure new incoming connections will |
| 467 | // trigger an event even if DoAccept returns an error here. |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 468 | EnableEvents(DE_ACCEPT); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 469 | sockaddr_storage addr_storage; |
| 470 | socklen_t addr_len = sizeof(addr_storage); |
| 471 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 472 | SOCKET s = DoAccept(s_, addr, &addr_len); |
| 473 | UpdateLastError(); |
| 474 | if (s == INVALID_SOCKET) |
| 475 | return nullptr; |
| 476 | if (out_addr != nullptr) |
| 477 | SocketAddressFromSockAddrStorage(addr_storage, out_addr); |
| 478 | return ss_->WrapSocket(s); |
| 479 | } |
| 480 | |
| 481 | int PhysicalSocket::Close() { |
| 482 | if (s_ == INVALID_SOCKET) |
| 483 | return 0; |
| 484 | int err = ::closesocket(s_); |
| 485 | UpdateLastError(); |
| 486 | s_ = INVALID_SOCKET; |
| 487 | state_ = CS_CLOSED; |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 488 | SetEnabledEvents(0); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 489 | if (resolver_) { |
| 490 | resolver_->Destroy(false); |
| 491 | resolver_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 492 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 493 | return err; |
| 494 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 495 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 496 | SOCKET PhysicalSocket::DoAccept(SOCKET socket, |
| 497 | sockaddr* addr, |
| 498 | socklen_t* addrlen) { |
| 499 | return ::accept(socket, addr, addrlen); |
| 500 | } |
| 501 | |
jbauch | f2a2bf4 | 2016-02-03 16:45:32 -0800 | [diff] [blame] | 502 | int PhysicalSocket::DoSend(SOCKET socket, const char* buf, int len, int flags) { |
| 503 | return ::send(socket, buf, len, flags); |
| 504 | } |
| 505 | |
| 506 | int PhysicalSocket::DoSendTo(SOCKET socket, |
| 507 | const char* buf, |
| 508 | int len, |
| 509 | int flags, |
| 510 | const struct sockaddr* dest_addr, |
| 511 | socklen_t addrlen) { |
| 512 | return ::sendto(socket, buf, len, flags, dest_addr, addrlen); |
| 513 | } |
| 514 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 515 | void PhysicalSocket::OnResolveResult(AsyncResolverInterface* resolver) { |
| 516 | if (resolver != resolver_) { |
| 517 | return; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 518 | } |
| 519 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 520 | int error = resolver_->GetError(); |
| 521 | if (error == 0) { |
| 522 | error = DoConnect(resolver_->address()); |
| 523 | } else { |
| 524 | Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 525 | } |
| 526 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 527 | if (error) { |
| 528 | SetError(error); |
| 529 | SignalCloseEvent(this, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 530 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 531 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 532 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 533 | void PhysicalSocket::UpdateLastError() { |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 534 | SetError(LAST_SYSTEM_ERROR); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | void PhysicalSocket::MaybeRemapSendError() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 538 | #if defined(WEBRTC_MAC) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 539 | // https://developer.apple.com/library/mac/documentation/Darwin/ |
| 540 | // Reference/ManPages/man2/sendto.2.html |
| 541 | // ENOBUFS - The output queue for a network interface is full. |
| 542 | // This generally indicates that the interface has stopped sending, |
| 543 | // but may be caused by transient congestion. |
| 544 | if (GetError() == ENOBUFS) { |
| 545 | SetError(EWOULDBLOCK); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 546 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 547 | #endif |
| 548 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 549 | |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 550 | void PhysicalSocket::SetEnabledEvents(uint8_t events) { |
| 551 | enabled_events_ = events; |
| 552 | } |
| 553 | |
| 554 | void PhysicalSocket::EnableEvents(uint8_t events) { |
| 555 | enabled_events_ |= events; |
| 556 | } |
| 557 | |
| 558 | void PhysicalSocket::DisableEvents(uint8_t events) { |
| 559 | enabled_events_ &= ~events; |
| 560 | } |
| 561 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 562 | int PhysicalSocket::TranslateOption(Option opt, int* slevel, int* sopt) { |
| 563 | switch (opt) { |
| 564 | case OPT_DONTFRAGMENT: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 565 | #if defined(WEBRTC_WIN) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 566 | *slevel = IPPROTO_IP; |
| 567 | *sopt = IP_DONTFRAGMENT; |
| 568 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 569 | #elif defined(WEBRTC_MAC) || defined(BSD) || defined(__native_client__) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 570 | RTC_LOG(LS_WARNING) << "Socket::OPT_DONTFRAGMENT not supported."; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 571 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 572 | #elif defined(WEBRTC_POSIX) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 573 | *slevel = IPPROTO_IP; |
| 574 | *sopt = IP_MTU_DISCOVER; |
| 575 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 576 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 577 | case OPT_RCVBUF: |
| 578 | *slevel = SOL_SOCKET; |
| 579 | *sopt = SO_RCVBUF; |
| 580 | break; |
| 581 | case OPT_SNDBUF: |
| 582 | *slevel = SOL_SOCKET; |
| 583 | *sopt = SO_SNDBUF; |
| 584 | break; |
| 585 | case OPT_NODELAY: |
| 586 | *slevel = IPPROTO_TCP; |
| 587 | *sopt = TCP_NODELAY; |
| 588 | break; |
| 589 | case OPT_DSCP: |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 590 | #if defined(WEBRTC_POSIX) |
| 591 | if (family_ == AF_INET6) { |
| 592 | *slevel = IPPROTO_IPV6; |
| 593 | *sopt = IPV6_TCLASS; |
| 594 | } else { |
| 595 | *slevel = IPPROTO_IP; |
| 596 | *sopt = IP_TOS; |
| 597 | } |
| 598 | break; |
| 599 | #else |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 600 | RTC_LOG(LS_WARNING) << "Socket::OPT_DSCP not supported."; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 601 | return -1; |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 602 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 603 | case OPT_RTP_SENDTIME_EXTN_ID: |
| 604 | return -1; // No logging is necessary as this not a OS socket option. |
| 605 | default: |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 606 | RTC_DCHECK_NOTREACHED(); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 607 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 608 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 609 | return 0; |
| 610 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 611 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 612 | SocketDispatcher::SocketDispatcher(PhysicalSocketServer* ss) |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 613 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 614 | : PhysicalSocket(ss), |
| 615 | id_(0), |
| 616 | signal_close_(false) |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 617 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 618 | : PhysicalSocket(ss) |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 619 | #endif |
| 620 | { |
| 621 | } |
| 622 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 623 | SocketDispatcher::SocketDispatcher(SOCKET s, PhysicalSocketServer* ss) |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 624 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 625 | : PhysicalSocket(ss, s), |
| 626 | id_(0), |
| 627 | signal_close_(false) |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 628 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 629 | : PhysicalSocket(ss, s) |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 630 | #endif |
| 631 | { |
| 632 | } |
| 633 | |
| 634 | SocketDispatcher::~SocketDispatcher() { |
| 635 | Close(); |
| 636 | } |
| 637 | |
| 638 | bool SocketDispatcher::Initialize() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 639 | RTC_DCHECK(s_ != INVALID_SOCKET); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 640 | // Must be a non-blocking |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 641 | #if defined(WEBRTC_WIN) |
| 642 | u_long argp = 1; |
| 643 | ioctlsocket(s_, FIONBIO, &argp); |
| 644 | #elif defined(WEBRTC_POSIX) |
| 645 | fcntl(s_, F_SETFL, fcntl(s_, F_GETFL, 0) | O_NONBLOCK); |
| 646 | #endif |
deadbeef | eae4564 | 2017-05-26 16:27:09 -0700 | [diff] [blame] | 647 | #if defined(WEBRTC_IOS) |
| 648 | // iOS may kill sockets when the app is moved to the background |
| 649 | // (specifically, if the app doesn't use the "voip" UIBackgroundMode). When |
| 650 | // we attempt to write to such a socket, SIGPIPE will be raised, which by |
| 651 | // default will terminate the process, which we don't want. By specifying |
| 652 | // this socket option, SIGPIPE will be disabled for the socket. |
| 653 | int value = 1; |
| 654 | ::setsockopt(s_, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value)); |
| 655 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 656 | ss_->Add(this); |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | bool SocketDispatcher::Create(int type) { |
| 661 | return Create(AF_INET, type); |
| 662 | } |
| 663 | |
| 664 | bool SocketDispatcher::Create(int family, int type) { |
| 665 | // Change the socket to be non-blocking. |
| 666 | if (!PhysicalSocket::Create(family, type)) |
| 667 | return false; |
| 668 | |
| 669 | if (!Initialize()) |
| 670 | return false; |
| 671 | |
| 672 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 673 | do { |
| 674 | id_ = ++next_id_; |
| 675 | } while (id_ == 0); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 676 | #endif |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | #if defined(WEBRTC_WIN) |
| 681 | |
| 682 | WSAEVENT SocketDispatcher::GetWSAEvent() { |
| 683 | return WSA_INVALID_EVENT; |
| 684 | } |
| 685 | |
| 686 | SOCKET SocketDispatcher::GetSocket() { |
| 687 | return s_; |
| 688 | } |
| 689 | |
| 690 | bool SocketDispatcher::CheckSignalClose() { |
| 691 | if (!signal_close_) |
| 692 | return false; |
| 693 | |
| 694 | char ch; |
| 695 | if (recv(s_, &ch, 1, MSG_PEEK) > 0) |
| 696 | return false; |
| 697 | |
| 698 | state_ = CS_CLOSED; |
| 699 | signal_close_ = false; |
| 700 | SignalCloseEvent(this, signal_err_); |
| 701 | return true; |
| 702 | } |
| 703 | |
| 704 | int SocketDispatcher::next_id_ = 0; |
| 705 | |
| 706 | #elif defined(WEBRTC_POSIX) |
| 707 | |
| 708 | int SocketDispatcher::GetDescriptor() { |
| 709 | return s_; |
| 710 | } |
| 711 | |
| 712 | bool SocketDispatcher::IsDescriptorClosed() { |
deadbeef | faedf7f | 2017-02-09 15:09:22 -0800 | [diff] [blame] | 713 | if (udp_) { |
| 714 | // The MSG_PEEK trick doesn't work for UDP, since (at least in some |
| 715 | // circumstances) it requires reading an entire UDP packet, which would be |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 716 | // bad for performance here. So, just check whether `s_` has been closed, |
deadbeef | faedf7f | 2017-02-09 15:09:22 -0800 | [diff] [blame] | 717 | // which should be sufficient. |
| 718 | return s_ == INVALID_SOCKET; |
| 719 | } |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 720 | // We don't have a reliable way of distinguishing end-of-stream |
| 721 | // from readability. So test on each readable call. Is this |
| 722 | // inefficient? Probably. |
| 723 | char ch; |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 724 | ssize_t res; |
| 725 | // Retry if the system call was interrupted. |
| 726 | do { |
| 727 | res = ::recv(s_, &ch, 1, MSG_PEEK); |
| 728 | } while (res < 0 && errno == EINTR); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 729 | if (res > 0) { |
| 730 | // Data available, so not closed. |
| 731 | return false; |
| 732 | } else if (res == 0) { |
| 733 | // EOF, so closed. |
| 734 | return true; |
| 735 | } else { // error |
| 736 | switch (errno) { |
| 737 | // Returned if we've already closed s_. |
| 738 | case EBADF: |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 739 | // This is dangerous: if we keep attempting to access a FD after close, |
| 740 | // it could be reopened by something else making us think it's still |
| 741 | // open. Note that this is only a DCHECK. |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 742 | RTC_DCHECK_NOTREACHED(); |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 743 | return true; |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 744 | // Returned during ungraceful peer shutdown. |
| 745 | case ECONNRESET: |
| 746 | return true; |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 747 | case ECONNABORTED: |
| 748 | return true; |
| 749 | case EPIPE: |
| 750 | return true; |
deadbeef | faedf7f | 2017-02-09 15:09:22 -0800 | [diff] [blame] | 751 | // The normal blocking error; don't log anything. |
| 752 | case EWOULDBLOCK: |
deadbeef | faedf7f | 2017-02-09 15:09:22 -0800 | [diff] [blame] | 753 | return false; |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 754 | default: |
| 755 | // Assume that all other errors are just blocking errors, meaning the |
| 756 | // connection is still good but we just can't read from it right now. |
| 757 | // This should only happen when connecting (and at most once), because |
| 758 | // in all other cases this function is only called if the file |
| 759 | // descriptor is already known to be in the readable state. However, |
| 760 | // it's not necessary a problem if we spuriously interpret a |
| 761 | // "connection lost"-type error as a blocking error, because typically |
| 762 | // the next recv() will get EOF, so we'll still eventually notice that |
| 763 | // the socket is closed. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 764 | RTC_LOG_ERR(LS_WARNING) << "Assuming benign blocking error"; |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 770 | #endif // WEBRTC_POSIX |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 771 | |
| 772 | uint32_t SocketDispatcher::GetRequestedEvents() { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 773 | return enabled_events(); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 774 | } |
| 775 | |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 776 | #if defined(WEBRTC_WIN) |
| 777 | |
| 778 | void SocketDispatcher::OnEvent(uint32_t ff, int err) { |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 779 | if ((ff & DE_CONNECT) != 0) |
| 780 | state_ = CS_CONNECTED; |
| 781 | |
| 782 | // We set CS_CLOSED from CheckSignalClose. |
| 783 | |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 784 | int cache_id = id_; |
| 785 | // Make sure we deliver connect/accept first. Otherwise, consumers may see |
| 786 | // something like a READ followed by a CONNECT, which would be odd. |
| 787 | if (((ff & DE_CONNECT) != 0) && (id_ == cache_id)) { |
| 788 | if (ff != DE_CONNECT) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 789 | RTC_LOG(LS_VERBOSE) << "Signalled with DE_CONNECT: " << ff; |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 790 | DisableEvents(DE_CONNECT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 791 | #if !defined(NDEBUG) |
| 792 | dbg_addr_ = "Connected @ "; |
| 793 | dbg_addr_.append(GetRemoteAddress().ToString()); |
| 794 | #endif |
| 795 | SignalConnectEvent(this); |
| 796 | } |
| 797 | if (((ff & DE_ACCEPT) != 0) && (id_ == cache_id)) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 798 | DisableEvents(DE_ACCEPT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 799 | SignalReadEvent(this); |
| 800 | } |
| 801 | if ((ff & DE_READ) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 802 | DisableEvents(DE_READ); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 803 | SignalReadEvent(this); |
| 804 | } |
| 805 | if (((ff & DE_WRITE) != 0) && (id_ == cache_id)) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 806 | DisableEvents(DE_WRITE); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 807 | SignalWriteEvent(this); |
| 808 | } |
| 809 | if (((ff & DE_CLOSE) != 0) && (id_ == cache_id)) { |
| 810 | signal_close_ = true; |
| 811 | signal_err_ = err; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | #elif defined(WEBRTC_POSIX) |
| 816 | |
| 817 | void SocketDispatcher::OnEvent(uint32_t ff, int err) { |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 818 | if ((ff & DE_CONNECT) != 0) |
| 819 | state_ = CS_CONNECTED; |
| 820 | |
| 821 | if ((ff & DE_CLOSE) != 0) |
| 822 | state_ = CS_CLOSED; |
| 823 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 824 | #if defined(WEBRTC_USE_EPOLL) |
| 825 | // Remember currently enabled events so we can combine multiple changes |
| 826 | // into one update call later. |
| 827 | // The signal handlers might re-enable events disabled here, so we can't |
| 828 | // keep a list of events to disable at the end of the method. This list |
| 829 | // would not be updated with the events enabled by the signal handlers. |
| 830 | StartBatchedEventUpdates(); |
| 831 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 832 | // Make sure we deliver connect/accept first. Otherwise, consumers may see |
| 833 | // something like a READ followed by a CONNECT, which would be odd. |
| 834 | if ((ff & DE_CONNECT) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 835 | DisableEvents(DE_CONNECT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 836 | SignalConnectEvent(this); |
| 837 | } |
| 838 | if ((ff & DE_ACCEPT) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 839 | DisableEvents(DE_ACCEPT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 840 | SignalReadEvent(this); |
| 841 | } |
| 842 | if ((ff & DE_READ) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 843 | DisableEvents(DE_READ); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 844 | SignalReadEvent(this); |
| 845 | } |
| 846 | if ((ff & DE_WRITE) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 847 | DisableEvents(DE_WRITE); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 848 | SignalWriteEvent(this); |
| 849 | } |
| 850 | if ((ff & DE_CLOSE) != 0) { |
| 851 | // The socket is now dead to us, so stop checking it. |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 852 | SetEnabledEvents(0); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 853 | SignalCloseEvent(this, err); |
| 854 | } |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 855 | #if defined(WEBRTC_USE_EPOLL) |
| 856 | FinishBatchedEventUpdates(); |
| 857 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 860 | #endif // WEBRTC_POSIX |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 861 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 862 | #if defined(WEBRTC_USE_EPOLL) |
| 863 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 864 | inline static int GetEpollEvents(uint32_t ff) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 865 | int events = 0; |
| 866 | if (ff & (DE_READ | DE_ACCEPT)) { |
| 867 | events |= EPOLLIN; |
| 868 | } |
| 869 | if (ff & (DE_WRITE | DE_CONNECT)) { |
| 870 | events |= EPOLLOUT; |
| 871 | } |
| 872 | return events; |
| 873 | } |
| 874 | |
| 875 | void SocketDispatcher::StartBatchedEventUpdates() { |
| 876 | RTC_DCHECK_EQ(saved_enabled_events_, -1); |
| 877 | saved_enabled_events_ = enabled_events(); |
| 878 | } |
| 879 | |
| 880 | void SocketDispatcher::FinishBatchedEventUpdates() { |
| 881 | RTC_DCHECK_NE(saved_enabled_events_, -1); |
| 882 | uint8_t old_events = static_cast<uint8_t>(saved_enabled_events_); |
| 883 | saved_enabled_events_ = -1; |
| 884 | MaybeUpdateDispatcher(old_events); |
| 885 | } |
| 886 | |
| 887 | void SocketDispatcher::MaybeUpdateDispatcher(uint8_t old_events) { |
| 888 | if (GetEpollEvents(enabled_events()) != GetEpollEvents(old_events) && |
| 889 | saved_enabled_events_ == -1) { |
| 890 | ss_->Update(this); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | void SocketDispatcher::SetEnabledEvents(uint8_t events) { |
| 895 | uint8_t old_events = enabled_events(); |
| 896 | PhysicalSocket::SetEnabledEvents(events); |
| 897 | MaybeUpdateDispatcher(old_events); |
| 898 | } |
| 899 | |
| 900 | void SocketDispatcher::EnableEvents(uint8_t events) { |
| 901 | uint8_t old_events = enabled_events(); |
| 902 | PhysicalSocket::EnableEvents(events); |
| 903 | MaybeUpdateDispatcher(old_events); |
| 904 | } |
| 905 | |
| 906 | void SocketDispatcher::DisableEvents(uint8_t events) { |
| 907 | uint8_t old_events = enabled_events(); |
| 908 | PhysicalSocket::DisableEvents(events); |
| 909 | MaybeUpdateDispatcher(old_events); |
| 910 | } |
| 911 | |
| 912 | #endif // WEBRTC_USE_EPOLL |
| 913 | |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 914 | int SocketDispatcher::Close() { |
| 915 | if (s_ == INVALID_SOCKET) |
| 916 | return 0; |
| 917 | |
| 918 | #if defined(WEBRTC_WIN) |
| 919 | id_ = 0; |
| 920 | signal_close_ = false; |
| 921 | #endif |
mmorrison | 25eeda1 | 2020-04-07 16:13:13 -0600 | [diff] [blame] | 922 | #if defined(WEBRTC_USE_EPOLL) |
| 923 | // If we're batching events, the socket can be closed and reopened |
| 924 | // during the batch. Set saved_enabled_events_ to 0 here so the new |
| 925 | // socket, if any, has the correct old events bitfield |
| 926 | if (saved_enabled_events_ != -1) { |
| 927 | saved_enabled_events_ = 0; |
| 928 | } |
| 929 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 930 | ss_->Remove(this); |
| 931 | return PhysicalSocket::Close(); |
| 932 | } |
| 933 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 934 | #if defined(WEBRTC_POSIX) |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 935 | // Sets the value of a boolean value to false when signaled. |
| 936 | class Signaler : public Dispatcher { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 937 | public: |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 938 | Signaler(PhysicalSocketServer* ss, bool& flag_to_clear) |
| 939 | : ss_(ss), |
| 940 | afd_([] { |
| 941 | std::array<int, 2> afd = {-1, -1}; |
| 942 | |
| 943 | if (pipe(afd.data()) < 0) { |
Harald Alvestrand | 5f34130 | 2021-11-24 10:01:32 +0000 | [diff] [blame] | 944 | RTC_LOG(LS_ERROR) << "pipe failed"; |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 945 | } |
| 946 | return afd; |
| 947 | }()), |
| 948 | fSignaled_(false), |
| 949 | flag_to_clear_(flag_to_clear) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 950 | ss_->Add(this); |
| 951 | } |
| 952 | |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 953 | ~Signaler() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 954 | ss_->Remove(this); |
| 955 | close(afd_[0]); |
| 956 | close(afd_[1]); |
| 957 | } |
| 958 | |
| 959 | virtual void Signal() { |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 960 | webrtc::MutexLock lock(&mutex_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 961 | if (!fSignaled_) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 962 | const uint8_t b[1] = {0}; |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 963 | const ssize_t res = write(afd_[1], b, sizeof(b)); |
| 964 | RTC_DCHECK_EQ(1, res); |
| 965 | fSignaled_ = true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 969 | uint32_t GetRequestedEvents() override { return DE_READ; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 970 | |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 971 | void OnEvent(uint32_t ff, int err) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 972 | // It is not possible to perfectly emulate an auto-resetting event with |
| 973 | // pipes. This simulates it by resetting before the event is handled. |
| 974 | |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 975 | webrtc::MutexLock lock(&mutex_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 976 | if (fSignaled_) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 977 | uint8_t b[4]; // Allow for reading more than 1 byte, but expect 1. |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 978 | const ssize_t res = read(afd_[0], b, sizeof(b)); |
| 979 | RTC_DCHECK_EQ(1, res); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 980 | fSignaled_ = false; |
| 981 | } |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 982 | flag_to_clear_ = false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 983 | } |
| 984 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 985 | int GetDescriptor() override { return afd_[0]; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 986 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 987 | bool IsDescriptorClosed() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 988 | |
| 989 | private: |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 990 | PhysicalSocketServer* const ss_; |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 991 | const std::array<int, 2> afd_; |
Niels Möller | 6d17602 | 2021-02-09 14:44:48 +0100 | [diff] [blame] | 992 | bool fSignaled_ RTC_GUARDED_BY(mutex_); |
| 993 | webrtc::Mutex mutex_; |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 994 | bool& flag_to_clear_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 995 | }; |
| 996 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 997 | #endif // WEBRTC_POSIX |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 998 | |
| 999 | #if defined(WEBRTC_WIN) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1000 | static uint32_t FlagsToEvents(uint32_t events) { |
| 1001 | uint32_t ffFD = FD_CLOSE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1002 | if (events & DE_READ) |
| 1003 | ffFD |= FD_READ; |
| 1004 | if (events & DE_WRITE) |
| 1005 | ffFD |= FD_WRITE; |
| 1006 | if (events & DE_CONNECT) |
| 1007 | ffFD |= FD_CONNECT; |
| 1008 | if (events & DE_ACCEPT) |
| 1009 | ffFD |= FD_ACCEPT; |
| 1010 | return ffFD; |
| 1011 | } |
| 1012 | |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 1013 | // Sets the value of a boolean value to false when signaled. |
| 1014 | class Signaler : public Dispatcher { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1015 | public: |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 1016 | Signaler(PhysicalSocketServer* ss, bool& flag_to_clear) |
| 1017 | : ss_(ss), flag_to_clear_(flag_to_clear) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1018 | hev_ = WSACreateEvent(); |
| 1019 | if (hev_) { |
| 1020 | ss_->Add(this); |
| 1021 | } |
| 1022 | } |
| 1023 | |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 1024 | ~Signaler() override { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1025 | if (hev_ != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1026 | ss_->Remove(this); |
| 1027 | WSACloseEvent(hev_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1028 | hev_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | virtual void Signal() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1033 | if (hev_ != nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1034 | WSASetEvent(hev_); |
| 1035 | } |
| 1036 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1037 | uint32_t GetRequestedEvents() override { return 0; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1038 | |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 1039 | void OnEvent(uint32_t ff, int err) override { |
| 1040 | WSAResetEvent(hev_); |
| 1041 | flag_to_clear_ = false; |
| 1042 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1043 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1044 | WSAEVENT GetWSAEvent() override { return hev_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1045 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1046 | SOCKET GetSocket() override { return INVALID_SOCKET; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1047 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1048 | bool CheckSignalClose() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1049 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1050 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1051 | PhysicalSocketServer* ss_; |
| 1052 | WSAEVENT hev_; |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 1053 | bool& flag_to_clear_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1054 | }; |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1055 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1056 | |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 1057 | PhysicalSocketServer::PhysicalSocketServer() |
| 1058 | : |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1059 | #if defined(WEBRTC_USE_EPOLL) |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 1060 | // Since Linux 2.6.8, the size argument is ignored, but must be greater |
| 1061 | // than zero. Before that the size served as hint to the kernel for the |
| 1062 | // amount of space to initially allocate in internal data structures. |
| 1063 | epoll_fd_(epoll_create(FD_SETSIZE)), |
| 1064 | #endif |
| 1065 | #if defined(WEBRTC_WIN) |
| 1066 | socket_ev_(WSACreateEvent()), |
| 1067 | #endif |
| 1068 | fWait_(false) { |
| 1069 | #if defined(WEBRTC_USE_EPOLL) |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1070 | if (epoll_fd_ == -1) { |
| 1071 | // Not an error, will fall back to "select" below. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1072 | RTC_LOG_E(LS_WARNING, EN, errno) << "epoll_create"; |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 1073 | // Note that -1 == INVALID_SOCKET, the alias used by later checks. |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1074 | } |
| 1075 | #endif |
Niels Möller | 2134745 | 2021-02-12 11:19:52 +0100 | [diff] [blame] | 1076 | // The `fWait_` flag to be cleared by the Signaler. |
| 1077 | signal_wakeup_ = new Signaler(this, fWait_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | PhysicalSocketServer::~PhysicalSocketServer() { |
| 1081 | #if defined(WEBRTC_WIN) |
| 1082 | WSACloseEvent(socket_ev_); |
| 1083 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1084 | delete signal_wakeup_; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1085 | #if defined(WEBRTC_USE_EPOLL) |
| 1086 | if (epoll_fd_ != INVALID_SOCKET) { |
| 1087 | close(epoll_fd_); |
| 1088 | } |
| 1089 | #endif |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1090 | RTC_DCHECK(dispatcher_by_key_.empty()); |
| 1091 | RTC_DCHECK(key_by_dispatcher_.empty()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | void PhysicalSocketServer::WakeUp() { |
| 1095 | signal_wakeup_->Signal(); |
| 1096 | } |
| 1097 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1098 | Socket* PhysicalSocketServer::CreateSocket(int family, int type) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1099 | SocketDispatcher* dispatcher = new SocketDispatcher(this); |
| 1100 | if (dispatcher->Create(family, type)) { |
| 1101 | return dispatcher; |
| 1102 | } else { |
| 1103 | delete dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1104 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 1108 | Socket* PhysicalSocketServer::WrapSocket(SOCKET s) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1109 | SocketDispatcher* dispatcher = new SocketDispatcher(s, this); |
| 1110 | if (dispatcher->Initialize()) { |
| 1111 | return dispatcher; |
| 1112 | } else { |
| 1113 | delete dispatcher; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1114 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1118 | void PhysicalSocketServer::Add(Dispatcher* pdispatcher) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1119 | CritScope cs(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1120 | if (key_by_dispatcher_.count(pdispatcher)) { |
| 1121 | RTC_LOG(LS_WARNING) |
| 1122 | << "PhysicalSocketServer asked to add a duplicate dispatcher."; |
| 1123 | return; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1124 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1125 | uint64_t key = next_dispatcher_key_++; |
| 1126 | dispatcher_by_key_.emplace(key, pdispatcher); |
| 1127 | key_by_dispatcher_.emplace(pdispatcher, key); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1128 | #if defined(WEBRTC_USE_EPOLL) |
| 1129 | if (epoll_fd_ != INVALID_SOCKET) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1130 | AddEpoll(pdispatcher, key); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1131 | } |
| 1132 | #endif // WEBRTC_USE_EPOLL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1135 | void PhysicalSocketServer::Remove(Dispatcher* pdispatcher) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1136 | CritScope cs(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1137 | if (!key_by_dispatcher_.count(pdispatcher)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1138 | RTC_LOG(LS_WARNING) |
| 1139 | << "PhysicalSocketServer asked to remove a unknown " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 1140 | "dispatcher, potentially from a duplicate call to Add."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1143 | uint64_t key = key_by_dispatcher_.at(pdispatcher); |
| 1144 | key_by_dispatcher_.erase(pdispatcher); |
| 1145 | dispatcher_by_key_.erase(key); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1146 | #if defined(WEBRTC_USE_EPOLL) |
| 1147 | if (epoll_fd_ != INVALID_SOCKET) { |
| 1148 | RemoveEpoll(pdispatcher); |
| 1149 | } |
| 1150 | #endif // WEBRTC_USE_EPOLL |
| 1151 | } |
| 1152 | |
| 1153 | void PhysicalSocketServer::Update(Dispatcher* pdispatcher) { |
| 1154 | #if defined(WEBRTC_USE_EPOLL) |
| 1155 | if (epoll_fd_ == INVALID_SOCKET) { |
| 1156 | return; |
| 1157 | } |
| 1158 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1159 | // Don't update dispatchers that haven't yet been added. |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1160 | CritScope cs(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1161 | if (!key_by_dispatcher_.count(pdispatcher)) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1162 | return; |
| 1163 | } |
| 1164 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1165 | UpdateEpoll(pdispatcher, key_by_dispatcher_.at(pdispatcher)); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1166 | #endif |
| 1167 | } |
| 1168 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1169 | int PhysicalSocketServer::ToCmsWait(webrtc::TimeDelta max_wait_duration) { |
| 1170 | return max_wait_duration == Event::kForever |
| 1171 | ? kForeverMs |
| 1172 | : max_wait_duration.RoundUpTo(webrtc::TimeDelta::Millis(1)).ms(); |
| 1173 | } |
| 1174 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1175 | #if defined(WEBRTC_POSIX) |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1176 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1177 | bool PhysicalSocketServer::Wait(webrtc::TimeDelta max_wait_duration, |
| 1178 | bool process_io) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1179 | // We don't support reentrant waiting. |
| 1180 | RTC_DCHECK(!waiting_); |
| 1181 | ScopedSetTrue s(&waiting_); |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1182 | const int cmsWait = ToCmsWait(max_wait_duration); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1183 | #if defined(WEBRTC_USE_EPOLL) |
| 1184 | // We don't keep a dedicated "epoll" descriptor containing only the non-IO |
| 1185 | // (i.e. signaling) dispatcher, so "poll" will be used instead of the default |
| 1186 | // "select" to support sockets larger than FD_SETSIZE. |
| 1187 | if (!process_io) { |
| 1188 | return WaitPoll(cmsWait, signal_wakeup_); |
| 1189 | } else if (epoll_fd_ != INVALID_SOCKET) { |
| 1190 | return WaitEpoll(cmsWait); |
| 1191 | } |
| 1192 | #endif |
| 1193 | return WaitSelect(cmsWait, process_io); |
| 1194 | } |
| 1195 | |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1196 | // `error_event` is true if we are responding to an event where we know an |
| 1197 | // error has occurred, which is possible with the poll/epoll implementations |
| 1198 | // but not the select implementation. |
| 1199 | // |
| 1200 | // `check_error` is true if there is the possibility of an error. |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1201 | static void ProcessEvents(Dispatcher* dispatcher, |
| 1202 | bool readable, |
| 1203 | bool writable, |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1204 | bool error_event, |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1205 | bool check_error) { |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1206 | RTC_DCHECK(!(error_event && !check_error)); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1207 | int errcode = 0; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1208 | if (check_error) { |
| 1209 | socklen_t len = sizeof(errcode); |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1210 | int res = ::getsockopt(dispatcher->GetDescriptor(), SOL_SOCKET, SO_ERROR, |
| 1211 | &errcode, &len); |
| 1212 | if (res < 0) { |
| 1213 | // If we are sure an error has occurred, or if getsockopt failed for a |
| 1214 | // socket descriptor, make sure we set the error code to a nonzero value. |
| 1215 | if (error_event || errno != ENOTSOCK) { |
| 1216 | errcode = EBADF; |
| 1217 | } |
| 1218 | } |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1221 | // Most often the socket is writable or readable or both, so make a single |
| 1222 | // virtual call to get requested events |
| 1223 | const uint32_t requested_events = dispatcher->GetRequestedEvents(); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1224 | uint32_t ff = 0; |
| 1225 | |
| 1226 | // Check readable descriptors. If we're waiting on an accept, signal |
| 1227 | // that. Otherwise we're waiting for data, check to see if we're |
| 1228 | // readable or really closed. |
| 1229 | // TODO(pthatcher): Only peek at TCP descriptors. |
| 1230 | if (readable) { |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1231 | if (errcode || dispatcher->IsDescriptorClosed()) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1232 | ff |= DE_CLOSE; |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1233 | } else if (requested_events & DE_ACCEPT) { |
| 1234 | ff |= DE_ACCEPT; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1235 | } else { |
| 1236 | ff |= DE_READ; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | // Check writable descriptors. If we're waiting on a connect, detect |
| 1241 | // success versus failure by the reaped error code. |
| 1242 | if (writable) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1243 | if (requested_events & DE_CONNECT) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1244 | if (!errcode) { |
| 1245 | ff |= DE_CONNECT; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1246 | } |
| 1247 | } else { |
| 1248 | ff |= DE_WRITE; |
| 1249 | } |
| 1250 | } |
| 1251 | |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1252 | // Make sure we report any errors regardless of whether readable or writable. |
| 1253 | if (errcode) { |
| 1254 | ff |= DE_CLOSE; |
| 1255 | } |
| 1256 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1257 | // Tell the descriptor about the event. |
| 1258 | if (ff != 0) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1259 | dispatcher->OnEvent(ff, errcode); |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1264 | // Calculate timing information |
| 1265 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1266 | struct timeval* ptvWait = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1267 | struct timeval tvWait; |
Niels Möller | 689b587 | 2018-08-29 09:55:44 +0200 | [diff] [blame] | 1268 | int64_t stop_us; |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1269 | if (cmsWait != kForeverMs) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1270 | // Calculate wait timeval |
| 1271 | tvWait.tv_sec = cmsWait / 1000; |
| 1272 | tvWait.tv_usec = (cmsWait % 1000) * 1000; |
| 1273 | ptvWait = &tvWait; |
| 1274 | |
Niels Möller | 689b587 | 2018-08-29 09:55:44 +0200 | [diff] [blame] | 1275 | // Calculate when to return |
| 1276 | stop_us = rtc::TimeMicros() + cmsWait * 1000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1279 | fd_set fdsRead; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1280 | fd_set fdsWrite; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1281 | // Explicitly unpoison these FDs on MemorySanitizer which doesn't handle the |
| 1282 | // inline assembly in FD_ZERO. |
| 1283 | // http://crbug.com/344505 |
pbos@webrtc.org | 27e5898 | 2014-10-07 17:56:53 +0000 | [diff] [blame] | 1284 | #ifdef MEMORY_SANITIZER |
| 1285 | __msan_unpoison(&fdsRead, sizeof(fdsRead)); |
| 1286 | __msan_unpoison(&fdsWrite, sizeof(fdsWrite)); |
| 1287 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1288 | |
| 1289 | fWait_ = true; |
| 1290 | |
| 1291 | while (fWait_) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1292 | // Zero all fd_sets. Although select() zeros the descriptors not signaled, |
| 1293 | // we may need to do this for dispatchers that were deleted while |
| 1294 | // iterating. |
| 1295 | FD_ZERO(&fdsRead); |
| 1296 | FD_ZERO(&fdsWrite); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1297 | int fdmax = -1; |
| 1298 | { |
| 1299 | CritScope cr(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1300 | current_dispatcher_keys_.clear(); |
| 1301 | for (auto const& kv : dispatcher_by_key_) { |
| 1302 | uint64_t key = kv.first; |
| 1303 | Dispatcher* pdispatcher = kv.second; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1304 | // Query dispatchers for read and write wait state |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1305 | if (!process_io && (pdispatcher != signal_wakeup_)) |
| 1306 | continue; |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1307 | current_dispatcher_keys_.push_back(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1308 | int fd = pdispatcher->GetDescriptor(); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1309 | // "select"ing a file descriptor that is equal to or larger than |
| 1310 | // FD_SETSIZE will result in undefined behavior. |
| 1311 | RTC_DCHECK_LT(fd, FD_SETSIZE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1312 | if (fd > fdmax) |
| 1313 | fdmax = fd; |
| 1314 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1315 | uint32_t ff = pdispatcher->GetRequestedEvents(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1316 | if (ff & (DE_READ | DE_ACCEPT)) |
| 1317 | FD_SET(fd, &fdsRead); |
| 1318 | if (ff & (DE_WRITE | DE_CONNECT)) |
| 1319 | FD_SET(fd, &fdsWrite); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | // Wait then call handlers as appropriate |
| 1324 | // < 0 means error |
| 1325 | // 0 means timeout |
| 1326 | // > 0 means count of descriptors ready |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1327 | int n = select(fdmax + 1, &fdsRead, &fdsWrite, nullptr, ptvWait); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1328 | |
| 1329 | // If error, return error. |
| 1330 | if (n < 0) { |
| 1331 | if (errno != EINTR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1332 | RTC_LOG_E(LS_ERROR, EN, errno) << "select"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1333 | return false; |
| 1334 | } |
| 1335 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1336 | // signals managed by this PhysicalSocketServer, the |
| 1337 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1338 | // iteration. |
| 1339 | } else if (n == 0) { |
| 1340 | // If timeout, return success |
| 1341 | return true; |
| 1342 | } else { |
| 1343 | // We have signaled descriptors |
| 1344 | CritScope cr(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1345 | // Iterate only on the dispatchers whose sockets were passed into |
| 1346 | // WSAEventSelect; this avoids the ABA problem (a socket being |
| 1347 | // destroyed and a new one created with the same file descriptor). |
| 1348 | for (uint64_t key : current_dispatcher_keys_) { |
| 1349 | if (!dispatcher_by_key_.count(key)) |
| 1350 | continue; |
| 1351 | Dispatcher* pdispatcher = dispatcher_by_key_.at(key); |
| 1352 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1353 | int fd = pdispatcher->GetDescriptor(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1354 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1355 | bool readable = FD_ISSET(fd, &fdsRead); |
| 1356 | if (readable) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1357 | FD_CLR(fd, &fdsRead); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1360 | bool writable = FD_ISSET(fd, &fdsWrite); |
| 1361 | if (writable) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1362 | FD_CLR(fd, &fdsWrite); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1365 | // The error code can be signaled through reads or writes. |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1366 | ProcessEvents(pdispatcher, readable, writable, /*error_event=*/false, |
| 1367 | readable || writable); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | // Recalc the time remaining to wait. Doing it here means it doesn't get |
| 1372 | // calced twice the first time through the loop |
| 1373 | if (ptvWait) { |
| 1374 | ptvWait->tv_sec = 0; |
| 1375 | ptvWait->tv_usec = 0; |
Niels Möller | 689b587 | 2018-08-29 09:55:44 +0200 | [diff] [blame] | 1376 | int64_t time_left_us = stop_us - rtc::TimeMicros(); |
| 1377 | if (time_left_us > 0) { |
| 1378 | ptvWait->tv_sec = time_left_us / rtc::kNumMicrosecsPerSec; |
| 1379 | ptvWait->tv_usec = time_left_us % rtc::kNumMicrosecsPerSec; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1387 | #if defined(WEBRTC_USE_EPOLL) |
| 1388 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1389 | void PhysicalSocketServer::AddEpoll(Dispatcher* pdispatcher, uint64_t key) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1390 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1391 | int fd = pdispatcher->GetDescriptor(); |
| 1392 | RTC_DCHECK(fd != INVALID_SOCKET); |
| 1393 | if (fd == INVALID_SOCKET) { |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | struct epoll_event event = {0}; |
| 1398 | event.events = GetEpollEvents(pdispatcher->GetRequestedEvents()); |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1399 | if (event.events == 0u) { |
| 1400 | // Don't add at all if we don't have any requested events. Could indicate a |
| 1401 | // closed socket. |
| 1402 | return; |
| 1403 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1404 | event.data.u64 = key; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1405 | int err = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event); |
| 1406 | RTC_DCHECK_EQ(err, 0); |
| 1407 | if (err == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1408 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_ADD"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | void PhysicalSocketServer::RemoveEpoll(Dispatcher* pdispatcher) { |
| 1413 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1414 | int fd = pdispatcher->GetDescriptor(); |
| 1415 | RTC_DCHECK(fd != INVALID_SOCKET); |
| 1416 | if (fd == INVALID_SOCKET) { |
| 1417 | return; |
| 1418 | } |
| 1419 | |
| 1420 | struct epoll_event event = {0}; |
| 1421 | int err = epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &event); |
| 1422 | RTC_DCHECK(err == 0 || errno == ENOENT); |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1423 | // Ignore ENOENT, which could occur if this descriptor wasn't added due to |
| 1424 | // having no requested events. |
| 1425 | if (err == -1 && errno != ENOENT) { |
| 1426 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_DEL"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1430 | void PhysicalSocketServer::UpdateEpoll(Dispatcher* pdispatcher, uint64_t key) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1431 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1432 | int fd = pdispatcher->GetDescriptor(); |
| 1433 | RTC_DCHECK(fd != INVALID_SOCKET); |
| 1434 | if (fd == INVALID_SOCKET) { |
| 1435 | return; |
| 1436 | } |
| 1437 | |
| 1438 | struct epoll_event event = {0}; |
| 1439 | event.events = GetEpollEvents(pdispatcher->GetRequestedEvents()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1440 | event.data.u64 = key; |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1441 | // Remove if we don't have any requested events. Could indicate a closed |
| 1442 | // socket. |
| 1443 | if (event.events == 0u) { |
| 1444 | epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &event); |
| 1445 | } else { |
| 1446 | int err = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &event); |
| 1447 | RTC_DCHECK(err == 0 || errno == ENOENT); |
| 1448 | if (err == -1) { |
| 1449 | // Could have been removed earlier due to no requested events. |
| 1450 | if (errno == ENOENT) { |
| 1451 | err = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event); |
| 1452 | if (err == -1) { |
| 1453 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_ADD"; |
| 1454 | } |
| 1455 | } else { |
| 1456 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_MOD"; |
| 1457 | } |
| 1458 | } |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | bool PhysicalSocketServer::WaitEpoll(int cmsWait) { |
| 1463 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1464 | int64_t tvWait = -1; |
| 1465 | int64_t tvStop = -1; |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1466 | if (cmsWait != kForeverMs) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1467 | tvWait = cmsWait; |
| 1468 | tvStop = TimeAfter(cmsWait); |
| 1469 | } |
| 1470 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1471 | fWait_ = true; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1472 | while (fWait_) { |
| 1473 | // Wait then call handlers as appropriate |
| 1474 | // < 0 means error |
| 1475 | // 0 means timeout |
| 1476 | // > 0 means count of descriptors ready |
Markus Handell | b7c63ab | 2020-05-26 18:09:55 +0200 | [diff] [blame] | 1477 | int n = epoll_wait(epoll_fd_, epoll_events_.data(), epoll_events_.size(), |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1478 | static_cast<int>(tvWait)); |
| 1479 | if (n < 0) { |
| 1480 | if (errno != EINTR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1481 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1482 | return false; |
| 1483 | } |
| 1484 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1485 | // signals managed by this PhysicalSocketServer, the |
| 1486 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1487 | // iteration. |
| 1488 | } else if (n == 0) { |
| 1489 | // If timeout, return success |
| 1490 | return true; |
| 1491 | } else { |
| 1492 | // We have signaled descriptors |
| 1493 | CritScope cr(&crit_); |
| 1494 | for (int i = 0; i < n; ++i) { |
| 1495 | const epoll_event& event = epoll_events_[i]; |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1496 | uint64_t key = event.data.u64; |
| 1497 | if (!dispatcher_by_key_.count(key)) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1498 | // The dispatcher for this socket no longer exists. |
| 1499 | continue; |
| 1500 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1501 | Dispatcher* pdispatcher = dispatcher_by_key_.at(key); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1502 | |
| 1503 | bool readable = (event.events & (EPOLLIN | EPOLLPRI)); |
| 1504 | bool writable = (event.events & EPOLLOUT); |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1505 | bool error = (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP)); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1506 | |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1507 | ProcessEvents(pdispatcher, readable, writable, error, error); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1511 | if (cmsWait != kForeverMs) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1512 | tvWait = TimeDiff(tvStop, TimeMillis()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1513 | if (tvWait <= 0) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1514 | // Return success on timeout. |
| 1515 | return true; |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
| 1523 | bool PhysicalSocketServer::WaitPoll(int cmsWait, Dispatcher* dispatcher) { |
| 1524 | RTC_DCHECK(dispatcher); |
| 1525 | int64_t tvWait = -1; |
| 1526 | int64_t tvStop = -1; |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1527 | if (cmsWait != kForeverMs) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1528 | tvWait = cmsWait; |
| 1529 | tvStop = TimeAfter(cmsWait); |
| 1530 | } |
| 1531 | |
| 1532 | fWait_ = true; |
| 1533 | |
| 1534 | struct pollfd fds = {0}; |
| 1535 | int fd = dispatcher->GetDescriptor(); |
| 1536 | fds.fd = fd; |
| 1537 | |
| 1538 | while (fWait_) { |
| 1539 | uint32_t ff = dispatcher->GetRequestedEvents(); |
| 1540 | fds.events = 0; |
| 1541 | if (ff & (DE_READ | DE_ACCEPT)) { |
| 1542 | fds.events |= POLLIN; |
| 1543 | } |
| 1544 | if (ff & (DE_WRITE | DE_CONNECT)) { |
| 1545 | fds.events |= POLLOUT; |
| 1546 | } |
| 1547 | fds.revents = 0; |
| 1548 | |
| 1549 | // Wait then call handlers as appropriate |
| 1550 | // < 0 means error |
| 1551 | // 0 means timeout |
| 1552 | // > 0 means count of descriptors ready |
| 1553 | int n = poll(&fds, 1, static_cast<int>(tvWait)); |
| 1554 | if (n < 0) { |
| 1555 | if (errno != EINTR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1556 | RTC_LOG_E(LS_ERROR, EN, errno) << "poll"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1557 | return false; |
| 1558 | } |
| 1559 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1560 | // signals managed by this PhysicalSocketServer, the |
| 1561 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1562 | // iteration. |
| 1563 | } else if (n == 0) { |
| 1564 | // If timeout, return success |
| 1565 | return true; |
| 1566 | } else { |
| 1567 | // We have signaled descriptors (should only be the passed dispatcher). |
| 1568 | RTC_DCHECK_EQ(n, 1); |
| 1569 | RTC_DCHECK_EQ(fds.fd, fd); |
| 1570 | |
| 1571 | bool readable = (fds.revents & (POLLIN | POLLPRI)); |
| 1572 | bool writable = (fds.revents & POLLOUT); |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1573 | bool error = (fds.revents & (POLLRDHUP | POLLERR | POLLHUP)); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1574 | |
Taylor Brandstetter | 3041eb2 | 2021-11-02 19:46:40 +0000 | [diff] [blame] | 1575 | ProcessEvents(dispatcher, readable, writable, error, error); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1578 | if (cmsWait != kForeverMs) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1579 | tvWait = TimeDiff(tvStop, TimeMillis()); |
| 1580 | if (tvWait < 0) { |
| 1581 | // Return success on timeout. |
| 1582 | return true; |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | return true; |
| 1588 | } |
| 1589 | |
| 1590 | #endif // WEBRTC_USE_EPOLL |
| 1591 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1592 | #endif // WEBRTC_POSIX |
| 1593 | |
| 1594 | #if defined(WEBRTC_WIN) |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1595 | bool PhysicalSocketServer::Wait(webrtc::TimeDelta max_wait_duration, |
| 1596 | bool process_io) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1597 | // We don't support reentrant waiting. |
| 1598 | RTC_DCHECK(!waiting_); |
| 1599 | ScopedSetTrue s(&waiting_); |
| 1600 | |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1601 | int cmsWait = ToCmsWait(max_wait_duration); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1602 | int64_t cmsTotal = cmsWait; |
| 1603 | int64_t cmsElapsed = 0; |
| 1604 | int64_t msStart = Time(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1605 | |
| 1606 | fWait_ = true; |
| 1607 | while (fWait_) { |
| 1608 | std::vector<WSAEVENT> events; |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1609 | std::vector<uint64_t> event_owners; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1610 | |
| 1611 | events.push_back(socket_ev_); |
| 1612 | |
| 1613 | { |
| 1614 | CritScope cr(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1615 | // Get a snapshot of all current dispatchers; this is used to avoid the |
| 1616 | // ABA problem (see later comment) and avoids the dispatcher_by_key_ |
| 1617 | // iterator being invalidated by calling CheckSignalClose, which may |
| 1618 | // remove the dispatcher from the list. |
| 1619 | current_dispatcher_keys_.clear(); |
| 1620 | for (auto const& kv : dispatcher_by_key_) { |
| 1621 | current_dispatcher_keys_.push_back(kv.first); |
| 1622 | } |
| 1623 | for (uint64_t key : current_dispatcher_keys_) { |
| 1624 | if (!dispatcher_by_key_.count(key)) { |
| 1625 | continue; |
| 1626 | } |
| 1627 | Dispatcher* disp = dispatcher_by_key_.at(key); |
| 1628 | if (!disp) |
| 1629 | continue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1630 | if (!process_io && (disp != signal_wakeup_)) |
| 1631 | continue; |
| 1632 | SOCKET s = disp->GetSocket(); |
| 1633 | if (disp->CheckSignalClose()) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1634 | // We just signalled close, don't poll this socket. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1635 | } else if (s != INVALID_SOCKET) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1636 | WSAEventSelect(s, events[0], |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1637 | FlagsToEvents(disp->GetRequestedEvents())); |
| 1638 | } else { |
| 1639 | events.push_back(disp->GetWSAEvent()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1640 | event_owners.push_back(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | // Which is shorter, the delay wait or the asked wait? |
| 1646 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1647 | int64_t cmsNext; |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1648 | if (cmsWait == kForeverMs) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1649 | cmsNext = cmsWait; |
| 1650 | } else { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1651 | cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | // Wait for one of the events to signal |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1655 | DWORD dw = |
| 1656 | WSAWaitForMultipleEvents(static_cast<DWORD>(events.size()), &events[0], |
| 1657 | false, static_cast<DWORD>(cmsNext), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (dw == WSA_WAIT_FAILED) { |
| 1660 | // Failed? |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1661 | // TODO(pthatcher): need a better strategy than this! |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1662 | WSAGetLastError(); |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 1663 | RTC_DCHECK_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1664 | return false; |
| 1665 | } else if (dw == WSA_WAIT_TIMEOUT) { |
| 1666 | // Timeout? |
| 1667 | return true; |
| 1668 | } else { |
| 1669 | // Figure out which one it is and call it |
| 1670 | CritScope cr(&crit_); |
| 1671 | int index = dw - WSA_WAIT_EVENT_0; |
| 1672 | if (index > 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1673 | --index; // The first event is the socket event |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1674 | uint64_t key = event_owners[index]; |
| 1675 | if (!dispatcher_by_key_.count(key)) { |
| 1676 | // The dispatcher could have been removed while waiting for events. |
| 1677 | continue; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1678 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1679 | Dispatcher* disp = dispatcher_by_key_.at(key); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1680 | disp->OnEvent(0, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1681 | } else if (process_io) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame] | 1682 | // Iterate only on the dispatchers whose sockets were passed into |
| 1683 | // WSAEventSelect; this avoids the ABA problem (a socket being |
| 1684 | // destroyed and a new one created with the same SOCKET handle). |
| 1685 | for (uint64_t key : current_dispatcher_keys_) { |
| 1686 | if (!dispatcher_by_key_.count(key)) { |
| 1687 | continue; |
| 1688 | } |
| 1689 | Dispatcher* disp = dispatcher_by_key_.at(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1690 | SOCKET s = disp->GetSocket(); |
| 1691 | if (s == INVALID_SOCKET) |
| 1692 | continue; |
| 1693 | |
| 1694 | WSANETWORKEVENTS wsaEvents; |
| 1695 | int err = WSAEnumNetworkEvents(s, events[0], &wsaEvents); |
| 1696 | if (err == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1697 | { |
| 1698 | if ((wsaEvents.lNetworkEvents & FD_READ) && |
| 1699 | wsaEvents.iErrorCode[FD_READ_BIT] != 0) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 1700 | RTC_LOG(LS_WARNING) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1701 | << "PhysicalSocketServer got FD_READ_BIT error " |
| 1702 | << wsaEvents.iErrorCode[FD_READ_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1703 | } |
| 1704 | if ((wsaEvents.lNetworkEvents & FD_WRITE) && |
| 1705 | wsaEvents.iErrorCode[FD_WRITE_BIT] != 0) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 1706 | RTC_LOG(LS_WARNING) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1707 | << "PhysicalSocketServer got FD_WRITE_BIT error " |
| 1708 | << wsaEvents.iErrorCode[FD_WRITE_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1709 | } |
| 1710 | if ((wsaEvents.lNetworkEvents & FD_CONNECT) && |
| 1711 | wsaEvents.iErrorCode[FD_CONNECT_BIT] != 0) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 1712 | RTC_LOG(LS_WARNING) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1713 | << "PhysicalSocketServer got FD_CONNECT_BIT error " |
| 1714 | << wsaEvents.iErrorCode[FD_CONNECT_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1715 | } |
| 1716 | if ((wsaEvents.lNetworkEvents & FD_ACCEPT) && |
| 1717 | wsaEvents.iErrorCode[FD_ACCEPT_BIT] != 0) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 1718 | RTC_LOG(LS_WARNING) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1719 | << "PhysicalSocketServer got FD_ACCEPT_BIT error " |
| 1720 | << wsaEvents.iErrorCode[FD_ACCEPT_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1721 | } |
| 1722 | if ((wsaEvents.lNetworkEvents & FD_CLOSE) && |
| 1723 | wsaEvents.iErrorCode[FD_CLOSE_BIT] != 0) { |
Harald Alvestrand | ef5b21e | 2021-11-27 21:31:08 +0000 | [diff] [blame] | 1724 | RTC_LOG(LS_WARNING) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1725 | << "PhysicalSocketServer got FD_CLOSE_BIT error " |
| 1726 | << wsaEvents.iErrorCode[FD_CLOSE_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1727 | } |
| 1728 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1729 | uint32_t ff = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1730 | int errcode = 0; |
| 1731 | if (wsaEvents.lNetworkEvents & FD_READ) |
| 1732 | ff |= DE_READ; |
| 1733 | if (wsaEvents.lNetworkEvents & FD_WRITE) |
| 1734 | ff |= DE_WRITE; |
| 1735 | if (wsaEvents.lNetworkEvents & FD_CONNECT) { |
| 1736 | if (wsaEvents.iErrorCode[FD_CONNECT_BIT] == 0) { |
| 1737 | ff |= DE_CONNECT; |
| 1738 | } else { |
| 1739 | ff |= DE_CLOSE; |
| 1740 | errcode = wsaEvents.iErrorCode[FD_CONNECT_BIT]; |
| 1741 | } |
| 1742 | } |
| 1743 | if (wsaEvents.lNetworkEvents & FD_ACCEPT) |
| 1744 | ff |= DE_ACCEPT; |
| 1745 | if (wsaEvents.lNetworkEvents & FD_CLOSE) { |
| 1746 | ff |= DE_CLOSE; |
| 1747 | errcode = wsaEvents.iErrorCode[FD_CLOSE_BIT]; |
| 1748 | } |
| 1749 | if (ff != 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1750 | disp->OnEvent(ff, errcode); |
| 1751 | } |
| 1752 | } |
| 1753 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | // Reset the network event until new activity occurs |
| 1757 | WSAResetEvent(socket_ev_); |
| 1758 | } |
| 1759 | |
| 1760 | // Break? |
| 1761 | if (!fWait_) |
| 1762 | break; |
| 1763 | cmsElapsed = TimeSince(msStart); |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 +0000 | [diff] [blame] | 1764 | if ((cmsWait != kForeverMs) && (cmsElapsed >= cmsWait)) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1765 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | // Done |
| 1770 | return true; |
| 1771 | } |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1772 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1773 | |
| 1774 | } // namespace rtc |