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