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