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" |
| 51 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | |
Emilio Cobos Álvarez | 6806550 | 2019-05-29 15:30:32 +0200 | [diff] [blame] | 53 | #if defined(WEBRTC_LINUX) |
| 54 | #include <linux/sockios.h> |
| 55 | #endif |
| 56 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 57 | #if defined(WEBRTC_WIN) |
| 58 | #define LAST_SYSTEM_ERROR (::GetLastError()) |
| 59 | #elif defined(__native_client__) && __native_client__ |
| 60 | #define LAST_SYSTEM_ERROR (0) |
| 61 | #elif defined(WEBRTC_POSIX) |
| 62 | #define LAST_SYSTEM_ERROR (errno) |
| 63 | #endif // WEBRTC_WIN |
| 64 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | #if defined(WEBRTC_POSIX) |
| 66 | #include <netinet/tcp.h> // for TCP_NODELAY |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | #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] | 68 | typedef void* SockOptArg; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 69 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | #endif // WEBRTC_POSIX |
| 71 | |
Stefan Holmer | 3ebb3ef | 2016-05-23 20:26:11 +0200 | [diff] [blame] | 72 | #if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) && !defined(__native_client__) |
| 73 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 74 | int64_t GetSocketRecvTimestamp(int socket) { |
| 75 | struct timeval tv_ioctl; |
| 76 | int ret = ioctl(socket, SIOCGSTAMP, &tv_ioctl); |
| 77 | if (ret != 0) |
| 78 | return -1; |
| 79 | int64_t timestamp = |
| 80 | rtc::kNumMicrosecsPerSec * static_cast<int64_t>(tv_ioctl.tv_sec) + |
| 81 | static_cast<int64_t>(tv_ioctl.tv_usec); |
| 82 | return timestamp; |
| 83 | } |
| 84 | |
| 85 | #else |
| 86 | |
| 87 | int64_t GetSocketRecvTimestamp(int socket) { |
| 88 | return -1; |
| 89 | } |
| 90 | #endif |
| 91 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | #if defined(WEBRTC_WIN) |
| 93 | typedef char* SockOptArg; |
| 94 | #endif |
| 95 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 96 | #if defined(WEBRTC_USE_EPOLL) |
| 97 | // POLLRDHUP / EPOLLRDHUP are only defined starting with Linux 2.6.17. |
| 98 | #if !defined(POLLRDHUP) |
| 99 | #define POLLRDHUP 0x2000 |
| 100 | #endif |
| 101 | #if !defined(EPOLLRDHUP) |
| 102 | #define EPOLLRDHUP 0x2000 |
| 103 | #endif |
| 104 | #endif |
| 105 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 106 | namespace { |
| 107 | class ScopedSetTrue { |
| 108 | public: |
| 109 | ScopedSetTrue(bool* value) : value_(value) { |
| 110 | RTC_DCHECK(!*value_); |
| 111 | *value_ = true; |
| 112 | } |
| 113 | ~ScopedSetTrue() { *value_ = false; } |
| 114 | |
| 115 | private: |
| 116 | bool* value_; |
| 117 | }; |
| 118 | } // namespace |
| 119 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | namespace rtc { |
| 121 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 122 | std::unique_ptr<SocketServer> SocketServer::CreateDefault() { |
| 123 | #if defined(__native_client__) |
| 124 | return std::unique_ptr<SocketServer>(new rtc::NullSocketServer); |
| 125 | #else |
| 126 | return std::unique_ptr<SocketServer>(new rtc::PhysicalSocketServer); |
| 127 | #endif |
| 128 | } |
| 129 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 130 | PhysicalSocket::PhysicalSocket(PhysicalSocketServer* ss, SOCKET s) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 131 | : ss_(ss), |
| 132 | s_(s), |
| 133 | error_(0), |
| 134 | state_((s == INVALID_SOCKET) ? CS_CLOSED : CS_CONNECTED), |
| 135 | resolver_(nullptr) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 136 | if (s_ != INVALID_SOCKET) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 137 | SetEnabledEvents(DE_READ | DE_WRITE); |
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 | int type = SOCK_STREAM; |
| 140 | socklen_t len = sizeof(type); |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 141 | const int res = |
| 142 | getsockopt(s_, SOL_SOCKET, SO_TYPE, (SockOptArg)&type, &len); |
| 143 | RTC_DCHECK_EQ(0, res); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 144 | udp_ = (SOCK_DGRAM == type); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 146 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 148 | PhysicalSocket::~PhysicalSocket() { |
| 149 | Close(); |
| 150 | } |
| 151 | |
| 152 | bool PhysicalSocket::Create(int family, int type) { |
| 153 | Close(); |
| 154 | s_ = ::socket(family, type, 0); |
| 155 | udp_ = (SOCK_DGRAM == type); |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 156 | family_ = family; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 157 | UpdateLastError(); |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 158 | if (udp_) { |
| 159 | SetEnabledEvents(DE_READ | DE_WRITE); |
| 160 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 161 | return s_ != INVALID_SOCKET; |
| 162 | } |
| 163 | |
| 164 | SocketAddress PhysicalSocket::GetLocalAddress() const { |
Mirko Bonadei | 108a2f0 | 2019-11-20 19:43:38 +0100 | [diff] [blame] | 165 | sockaddr_storage addr_storage = {}; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 166 | socklen_t addrlen = sizeof(addr_storage); |
| 167 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 168 | int result = ::getsockname(s_, addr, &addrlen); |
| 169 | SocketAddress address; |
| 170 | if (result >= 0) { |
| 171 | SocketAddressFromSockAddrStorage(addr_storage, &address); |
| 172 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 173 | RTC_LOG(LS_WARNING) << "GetLocalAddress: unable to get local addr, socket=" |
| 174 | << s_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 176 | return address; |
| 177 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 179 | SocketAddress PhysicalSocket::GetRemoteAddress() const { |
Mirko Bonadei | 108a2f0 | 2019-11-20 19:43:38 +0100 | [diff] [blame] | 180 | sockaddr_storage addr_storage = {}; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 181 | socklen_t addrlen = sizeof(addr_storage); |
| 182 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 183 | int result = ::getpeername(s_, addr, &addrlen); |
| 184 | SocketAddress address; |
| 185 | if (result >= 0) { |
| 186 | SocketAddressFromSockAddrStorage(addr_storage, &address); |
| 187 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 188 | RTC_LOG(LS_WARNING) |
| 189 | << "GetRemoteAddress: unable to get remote addr, socket=" << s_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 190 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 191 | return address; |
| 192 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 193 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 194 | int PhysicalSocket::Bind(const SocketAddress& bind_addr) { |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 195 | SocketAddress copied_bind_addr = bind_addr; |
| 196 | // If a network binder is available, use it to bind a socket to an interface |
| 197 | // instead of bind(), since this is more reliable on an OS with a weak host |
| 198 | // model. |
deadbeef | 9ffa13f | 2017-02-21 16:18:00 -0800 | [diff] [blame] | 199 | if (ss_->network_binder() && !bind_addr.IsAnyIP()) { |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 200 | NetworkBindingResult result = |
| 201 | ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr()); |
| 202 | if (result == NetworkBindingResult::SUCCESS) { |
| 203 | // Since the network binder handled binding the socket to the desired |
| 204 | // network interface, we don't need to (and shouldn't) include an IP in |
| 205 | // the bind() call; bind() just needs to assign a port. |
| 206 | copied_bind_addr.SetIP(GetAnyIP(copied_bind_addr.ipaddr().family())); |
| 207 | } else if (result == NetworkBindingResult::NOT_IMPLEMENTED) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 208 | RTC_LOG(LS_INFO) << "Can't bind socket to network because " |
| 209 | "network binding is not implemented for this OS."; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 210 | } else { |
| 211 | if (bind_addr.IsLoopbackIP()) { |
| 212 | // If we couldn't bind to a loopback IP (which should only happen in |
| 213 | // test scenarios), continue on. This may be expected behavior. |
Paulina Hensman | b239a2e | 2020-03-31 16:16:11 +0200 | [diff] [blame] | 214 | RTC_LOG(LS_VERBOSE) << "Binding socket to loopback address" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 215 | << " failed; result: " << static_cast<int>(result); |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 216 | } else { |
Paulina Hensman | b239a2e | 2020-03-31 16:16:11 +0200 | [diff] [blame] | 217 | RTC_LOG(LS_WARNING) << "Binding socket to network address" |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 218 | << " failed; result: " << static_cast<int>(result); |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 219 | // If a network binding was attempted and failed, we should stop here |
| 220 | // and not try to use the socket. Otherwise, we may end up sending |
| 221 | // packets with an invalid source address. |
| 222 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7026 |
| 223 | return -1; |
| 224 | } |
| 225 | } |
| 226 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 227 | sockaddr_storage addr_storage; |
deadbeef | c874d12 | 2017-02-13 15:41:59 -0800 | [diff] [blame] | 228 | size_t len = copied_bind_addr.ToSockAddrStorage(&addr_storage); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 229 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 230 | int err = ::bind(s_, addr, static_cast<int>(len)); |
| 231 | UpdateLastError(); |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 232 | #if !defined(NDEBUG) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 233 | if (0 == err) { |
| 234 | dbg_addr_ = "Bound @ "; |
| 235 | dbg_addr_.append(GetLocalAddress().ToString()); |
| 236 | } |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 237 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 238 | return err; |
| 239 | } |
| 240 | |
| 241 | int PhysicalSocket::Connect(const SocketAddress& addr) { |
| 242 | // TODO(pthatcher): Implicit creation is required to reconnect... |
| 243 | // ...but should we make it more explicit? |
| 244 | if (state_ != CS_CLOSED) { |
| 245 | SetError(EALREADY); |
| 246 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 247 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 248 | if (addr.IsUnresolvedIP()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 249 | RTC_LOG(LS_VERBOSE) << "Resolving addr in PhysicalSocket::Connect"; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 250 | resolver_ = new AsyncResolver(); |
| 251 | resolver_->SignalDone.connect(this, &PhysicalSocket::OnResolveResult); |
| 252 | resolver_->Start(addr); |
| 253 | state_ = CS_CONNECTING; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 257 | return DoConnect(addr); |
| 258 | } |
| 259 | |
| 260 | int PhysicalSocket::DoConnect(const SocketAddress& connect_addr) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 261 | if ((s_ == INVALID_SOCKET) && !Create(connect_addr.family(), SOCK_STREAM)) { |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 262 | return SOCKET_ERROR; |
| 263 | } |
| 264 | sockaddr_storage addr_storage; |
| 265 | size_t len = connect_addr.ToSockAddrStorage(&addr_storage); |
| 266 | sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); |
| 267 | int err = ::connect(s_, addr, static_cast<int>(len)); |
| 268 | UpdateLastError(); |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 269 | uint8_t events = DE_READ | DE_WRITE; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 270 | if (err == 0) { |
| 271 | state_ = CS_CONNECTED; |
| 272 | } else if (IsBlockingError(GetError())) { |
| 273 | state_ = CS_CONNECTING; |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 274 | events |= DE_CONNECT; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 275 | } else { |
| 276 | return SOCKET_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 277 | } |
| 278 | |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 279 | EnableEvents(events); |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 280 | return 0; |
| 281 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 282 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 283 | int PhysicalSocket::GetError() const { |
| 284 | CritScope cs(&crit_); |
| 285 | return error_; |
| 286 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 287 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 288 | void PhysicalSocket::SetError(int error) { |
| 289 | CritScope cs(&crit_); |
| 290 | error_ = error; |
| 291 | } |
| 292 | |
| 293 | AsyncSocket::ConnState PhysicalSocket::GetState() const { |
| 294 | return state_; |
| 295 | } |
| 296 | |
| 297 | int PhysicalSocket::GetOption(Option opt, int* value) { |
| 298 | int slevel; |
| 299 | int sopt; |
| 300 | if (TranslateOption(opt, &slevel, &sopt) == -1) |
| 301 | return -1; |
| 302 | socklen_t optlen = sizeof(*value); |
| 303 | int ret = ::getsockopt(s_, slevel, sopt, (SockOptArg)value, &optlen); |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 304 | if (ret == -1) { |
| 305 | return -1; |
| 306 | } |
| 307 | if (opt == OPT_DONTFRAGMENT) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 308 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 309 | *value = (*value != IP_PMTUDISC_DONT) ? 1 : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 310 | #endif |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 311 | } else if (opt == OPT_DSCP) { |
| 312 | #if defined(WEBRTC_POSIX) |
| 313 | // unshift DSCP value to get six most significant bits of IP DiffServ field |
| 314 | *value >>= 2; |
| 315 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | } |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 317 | return ret; |
| 318 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 319 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 320 | int PhysicalSocket::SetOption(Option opt, int value) { |
| 321 | int slevel; |
| 322 | int sopt; |
| 323 | if (TranslateOption(opt, &slevel, &sopt) == -1) |
| 324 | return -1; |
| 325 | if (opt == OPT_DONTFRAGMENT) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 326 | #if defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID) |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 327 | value = (value) ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 328 | #endif |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 329 | } else if (opt == OPT_DSCP) { |
| 330 | #if defined(WEBRTC_POSIX) |
| 331 | // shift DSCP value to fit six most significant bits of IP DiffServ field |
| 332 | value <<= 2; |
| 333 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 334 | } |
Taylor Brandstetter | ecd6fc8 | 2020-02-05 17:26:37 -0800 | [diff] [blame] | 335 | #if defined(WEBRTC_POSIX) |
| 336 | if (sopt == IPV6_TCLASS) { |
| 337 | // Set the IPv4 option in all cases to support dual-stack sockets. |
| 338 | ::setsockopt(s_, IPPROTO_IP, IP_TOS, (SockOptArg)&value, sizeof(value)); |
| 339 | } |
| 340 | #endif |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 341 | return ::setsockopt(s_, slevel, sopt, (SockOptArg)&value, sizeof(value)); |
| 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 | |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 463 | AsyncSocket* PhysicalSocket::Accept(SocketAddress* out_addr) { |
| 464 | // Always re-subscribe DE_ACCEPT to make sure new incoming connections will |
| 465 | // trigger an event even if DoAccept returns an error here. |
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: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 604 | RTC_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 |
| 714 | // bad for performance here. So, just check whether |s_| has been closed, |
| 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; |
| 722 | ssize_t res = ::recv(s_, &ch, 1, MSG_PEEK); |
| 723 | if (res > 0) { |
| 724 | // Data available, so not closed. |
| 725 | return false; |
| 726 | } else if (res == 0) { |
| 727 | // EOF, so closed. |
| 728 | return true; |
| 729 | } else { // error |
| 730 | switch (errno) { |
| 731 | // Returned if we've already closed s_. |
| 732 | case EBADF: |
| 733 | // Returned during ungraceful peer shutdown. |
| 734 | case ECONNRESET: |
| 735 | return true; |
deadbeef | faedf7f | 2017-02-09 15:09:22 -0800 | [diff] [blame] | 736 | // The normal blocking error; don't log anything. |
| 737 | case EWOULDBLOCK: |
| 738 | // Interrupted system call. |
| 739 | case EINTR: |
| 740 | return false; |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 741 | default: |
| 742 | // Assume that all other errors are just blocking errors, meaning the |
| 743 | // connection is still good but we just can't read from it right now. |
| 744 | // This should only happen when connecting (and at most once), because |
| 745 | // in all other cases this function is only called if the file |
| 746 | // descriptor is already known to be in the readable state. However, |
| 747 | // it's not necessary a problem if we spuriously interpret a |
| 748 | // "connection lost"-type error as a blocking error, because typically |
| 749 | // the next recv() will get EOF, so we'll still eventually notice that |
| 750 | // the socket is closed. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 751 | RTC_LOG_ERR(LS_WARNING) << "Assuming benign blocking error"; |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 752 | return false; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 757 | #endif // WEBRTC_POSIX |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 758 | |
| 759 | uint32_t SocketDispatcher::GetRequestedEvents() { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 760 | return enabled_events(); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | void SocketDispatcher::OnPreEvent(uint32_t ff) { |
| 764 | if ((ff & DE_CONNECT) != 0) |
| 765 | state_ = CS_CONNECTED; |
| 766 | |
| 767 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 768 | // We set CS_CLOSED from CheckSignalClose. |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 769 | #elif defined(WEBRTC_POSIX) |
| 770 | if ((ff & DE_CLOSE) != 0) |
| 771 | state_ = CS_CLOSED; |
| 772 | #endif |
| 773 | } |
| 774 | |
| 775 | #if defined(WEBRTC_WIN) |
| 776 | |
| 777 | void SocketDispatcher::OnEvent(uint32_t ff, int err) { |
| 778 | int cache_id = id_; |
| 779 | // Make sure we deliver connect/accept first. Otherwise, consumers may see |
| 780 | // something like a READ followed by a CONNECT, which would be odd. |
| 781 | if (((ff & DE_CONNECT) != 0) && (id_ == cache_id)) { |
| 782 | if (ff != DE_CONNECT) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 783 | RTC_LOG(LS_VERBOSE) << "Signalled with DE_CONNECT: " << ff; |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 784 | DisableEvents(DE_CONNECT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 785 | #if !defined(NDEBUG) |
| 786 | dbg_addr_ = "Connected @ "; |
| 787 | dbg_addr_.append(GetRemoteAddress().ToString()); |
| 788 | #endif |
| 789 | SignalConnectEvent(this); |
| 790 | } |
| 791 | if (((ff & DE_ACCEPT) != 0) && (id_ == cache_id)) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 792 | DisableEvents(DE_ACCEPT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 793 | SignalReadEvent(this); |
| 794 | } |
| 795 | if ((ff & DE_READ) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 796 | DisableEvents(DE_READ); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 797 | SignalReadEvent(this); |
| 798 | } |
| 799 | if (((ff & DE_WRITE) != 0) && (id_ == cache_id)) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 800 | DisableEvents(DE_WRITE); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 801 | SignalWriteEvent(this); |
| 802 | } |
| 803 | if (((ff & DE_CLOSE) != 0) && (id_ == cache_id)) { |
| 804 | signal_close_ = true; |
| 805 | signal_err_ = err; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | #elif defined(WEBRTC_POSIX) |
| 810 | |
| 811 | void SocketDispatcher::OnEvent(uint32_t ff, int err) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 812 | #if defined(WEBRTC_USE_EPOLL) |
| 813 | // Remember currently enabled events so we can combine multiple changes |
| 814 | // into one update call later. |
| 815 | // The signal handlers might re-enable events disabled here, so we can't |
| 816 | // keep a list of events to disable at the end of the method. This list |
| 817 | // would not be updated with the events enabled by the signal handlers. |
| 818 | StartBatchedEventUpdates(); |
| 819 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 820 | // Make sure we deliver connect/accept first. Otherwise, consumers may see |
| 821 | // something like a READ followed by a CONNECT, which would be odd. |
| 822 | if ((ff & DE_CONNECT) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 823 | DisableEvents(DE_CONNECT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 824 | SignalConnectEvent(this); |
| 825 | } |
| 826 | if ((ff & DE_ACCEPT) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 827 | DisableEvents(DE_ACCEPT); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 828 | SignalReadEvent(this); |
| 829 | } |
| 830 | if ((ff & DE_READ) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 831 | DisableEvents(DE_READ); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 832 | SignalReadEvent(this); |
| 833 | } |
| 834 | if ((ff & DE_WRITE) != 0) { |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 835 | DisableEvents(DE_WRITE); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 836 | SignalWriteEvent(this); |
| 837 | } |
| 838 | if ((ff & DE_CLOSE) != 0) { |
| 839 | // The socket is now dead to us, so stop checking it. |
jbauch | 577f5dc | 2017-05-17 16:32:26 -0700 | [diff] [blame] | 840 | SetEnabledEvents(0); |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 841 | SignalCloseEvent(this, err); |
| 842 | } |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 843 | #if defined(WEBRTC_USE_EPOLL) |
| 844 | FinishBatchedEventUpdates(); |
| 845 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 848 | #endif // WEBRTC_POSIX |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 849 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 850 | #if defined(WEBRTC_USE_EPOLL) |
| 851 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 852 | inline static int GetEpollEvents(uint32_t ff) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 853 | int events = 0; |
| 854 | if (ff & (DE_READ | DE_ACCEPT)) { |
| 855 | events |= EPOLLIN; |
| 856 | } |
| 857 | if (ff & (DE_WRITE | DE_CONNECT)) { |
| 858 | events |= EPOLLOUT; |
| 859 | } |
| 860 | return events; |
| 861 | } |
| 862 | |
| 863 | void SocketDispatcher::StartBatchedEventUpdates() { |
| 864 | RTC_DCHECK_EQ(saved_enabled_events_, -1); |
| 865 | saved_enabled_events_ = enabled_events(); |
| 866 | } |
| 867 | |
| 868 | void SocketDispatcher::FinishBatchedEventUpdates() { |
| 869 | RTC_DCHECK_NE(saved_enabled_events_, -1); |
| 870 | uint8_t old_events = static_cast<uint8_t>(saved_enabled_events_); |
| 871 | saved_enabled_events_ = -1; |
| 872 | MaybeUpdateDispatcher(old_events); |
| 873 | } |
| 874 | |
| 875 | void SocketDispatcher::MaybeUpdateDispatcher(uint8_t old_events) { |
| 876 | if (GetEpollEvents(enabled_events()) != GetEpollEvents(old_events) && |
| 877 | saved_enabled_events_ == -1) { |
| 878 | ss_->Update(this); |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | void SocketDispatcher::SetEnabledEvents(uint8_t events) { |
| 883 | uint8_t old_events = enabled_events(); |
| 884 | PhysicalSocket::SetEnabledEvents(events); |
| 885 | MaybeUpdateDispatcher(old_events); |
| 886 | } |
| 887 | |
| 888 | void SocketDispatcher::EnableEvents(uint8_t events) { |
| 889 | uint8_t old_events = enabled_events(); |
| 890 | PhysicalSocket::EnableEvents(events); |
| 891 | MaybeUpdateDispatcher(old_events); |
| 892 | } |
| 893 | |
| 894 | void SocketDispatcher::DisableEvents(uint8_t events) { |
| 895 | uint8_t old_events = enabled_events(); |
| 896 | PhysicalSocket::DisableEvents(events); |
| 897 | MaybeUpdateDispatcher(old_events); |
| 898 | } |
| 899 | |
| 900 | #endif // WEBRTC_USE_EPOLL |
| 901 | |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 902 | int SocketDispatcher::Close() { |
| 903 | if (s_ == INVALID_SOCKET) |
| 904 | return 0; |
| 905 | |
| 906 | #if defined(WEBRTC_WIN) |
| 907 | id_ = 0; |
| 908 | signal_close_ = false; |
| 909 | #endif |
mmorrison | 25eeda1 | 2020-04-07 16:13:13 -0600 | [diff] [blame] | 910 | #if defined(WEBRTC_USE_EPOLL) |
| 911 | // If we're batching events, the socket can be closed and reopened |
| 912 | // during the batch. Set saved_enabled_events_ to 0 here so the new |
| 913 | // socket, if any, has the correct old events bitfield |
| 914 | if (saved_enabled_events_ != -1) { |
| 915 | saved_enabled_events_ = 0; |
| 916 | } |
| 917 | #endif |
jbauch | 4331fcd | 2016-01-06 22:20:28 -0800 | [diff] [blame] | 918 | ss_->Remove(this); |
| 919 | return PhysicalSocket::Close(); |
| 920 | } |
| 921 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 922 | #if defined(WEBRTC_POSIX) |
| 923 | class EventDispatcher : public Dispatcher { |
| 924 | public: |
| 925 | EventDispatcher(PhysicalSocketServer* ss) : ss_(ss), fSignaled_(false) { |
| 926 | if (pipe(afd_) < 0) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 927 | RTC_LOG(LERROR) << "pipe failed"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 928 | ss_->Add(this); |
| 929 | } |
| 930 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 931 | ~EventDispatcher() override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 932 | ss_->Remove(this); |
| 933 | close(afd_[0]); |
| 934 | close(afd_[1]); |
| 935 | } |
| 936 | |
| 937 | virtual void Signal() { |
| 938 | CritScope cs(&crit_); |
| 939 | if (!fSignaled_) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 940 | const uint8_t b[1] = {0}; |
nisse | c16fa5e | 2017-02-07 07:18:43 -0800 | [diff] [blame] | 941 | const ssize_t res = write(afd_[1], b, sizeof(b)); |
| 942 | RTC_DCHECK_EQ(1, res); |
| 943 | fSignaled_ = true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 947 | uint32_t GetRequestedEvents() override { return DE_READ; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 948 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 949 | void OnPreEvent(uint32_t ff) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 950 | // It is not possible to perfectly emulate an auto-resetting event with |
| 951 | // pipes. This simulates it by resetting before the event is handled. |
| 952 | |
| 953 | CritScope cs(&crit_); |
| 954 | if (fSignaled_) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 955 | 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] | 956 | const ssize_t res = read(afd_[0], b, sizeof(b)); |
| 957 | RTC_DCHECK_EQ(1, res); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 958 | fSignaled_ = false; |
| 959 | } |
| 960 | } |
| 961 | |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 962 | void OnEvent(uint32_t ff, int err) override { RTC_NOTREACHED(); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 963 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 964 | int GetDescriptor() override { return afd_[0]; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 965 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 966 | bool IsDescriptorClosed() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 967 | |
| 968 | private: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 969 | PhysicalSocketServer* ss_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 970 | int afd_[2]; |
| 971 | bool fSignaled_; |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 972 | RecursiveCriticalSection crit_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 973 | }; |
| 974 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 975 | #endif // WEBRTC_POSIX |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 976 | |
| 977 | #if defined(WEBRTC_WIN) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 978 | static uint32_t FlagsToEvents(uint32_t events) { |
| 979 | uint32_t ffFD = FD_CLOSE; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 980 | if (events & DE_READ) |
| 981 | ffFD |= FD_READ; |
| 982 | if (events & DE_WRITE) |
| 983 | ffFD |= FD_WRITE; |
| 984 | if (events & DE_CONNECT) |
| 985 | ffFD |= FD_CONNECT; |
| 986 | if (events & DE_ACCEPT) |
| 987 | ffFD |= FD_ACCEPT; |
| 988 | return ffFD; |
| 989 | } |
| 990 | |
| 991 | class EventDispatcher : public Dispatcher { |
| 992 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 993 | EventDispatcher(PhysicalSocketServer* ss) : ss_(ss) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 994 | hev_ = WSACreateEvent(); |
| 995 | if (hev_) { |
| 996 | ss_->Add(this); |
| 997 | } |
| 998 | } |
| 999 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1000 | ~EventDispatcher() override { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1001 | if (hev_ != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1002 | ss_->Remove(this); |
| 1003 | WSACloseEvent(hev_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1004 | hev_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | virtual void Signal() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1009 | if (hev_ != nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1010 | WSASetEvent(hev_); |
| 1011 | } |
| 1012 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1013 | uint32_t GetRequestedEvents() override { return 0; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1014 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1015 | void OnPreEvent(uint32_t ff) override { WSAResetEvent(hev_); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1016 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1017 | void OnEvent(uint32_t ff, int err) override {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1018 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1019 | WSAEVENT GetWSAEvent() override { return hev_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1020 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1021 | SOCKET GetSocket() override { return INVALID_SOCKET; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1022 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1023 | bool CheckSignalClose() override { return false; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1024 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 1025 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1026 | PhysicalSocketServer* ss_; |
| 1027 | WSAEVENT hev_; |
| 1028 | }; |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1029 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1030 | |
| 1031 | // Sets the value of a boolean value to false when signaled. |
| 1032 | class Signaler : public EventDispatcher { |
| 1033 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1034 | Signaler(PhysicalSocketServer* ss, bool* pf) : EventDispatcher(ss), pf_(pf) {} |
| 1035 | ~Signaler() override {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1036 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1037 | void OnEvent(uint32_t ff, int err) override { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1038 | if (pf_) |
| 1039 | *pf_ = false; |
| 1040 | } |
| 1041 | |
| 1042 | private: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1043 | bool* pf_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1044 | }; |
| 1045 | |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 1046 | PhysicalSocketServer::PhysicalSocketServer() |
| 1047 | : |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1048 | #if defined(WEBRTC_USE_EPOLL) |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 1049 | // Since Linux 2.6.8, the size argument is ignored, but must be greater |
| 1050 | // than zero. Before that the size served as hint to the kernel for the |
| 1051 | // amount of space to initially allocate in internal data structures. |
| 1052 | epoll_fd_(epoll_create(FD_SETSIZE)), |
| 1053 | #endif |
| 1054 | #if defined(WEBRTC_WIN) |
| 1055 | socket_ev_(WSACreateEvent()), |
| 1056 | #endif |
| 1057 | fWait_(false) { |
| 1058 | #if defined(WEBRTC_USE_EPOLL) |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1059 | if (epoll_fd_ == -1) { |
| 1060 | // Not an error, will fall back to "select" below. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1061 | RTC_LOG_E(LS_WARNING, EN, errno) << "epoll_create"; |
Niels Möller | 611fba4 | 2020-05-13 14:42:22 +0200 | [diff] [blame] | 1062 | // Note that -1 == INVALID_SOCKET, the alias used by later checks. |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1063 | } |
| 1064 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1065 | signal_wakeup_ = new Signaler(this, &fWait_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | PhysicalSocketServer::~PhysicalSocketServer() { |
| 1069 | #if defined(WEBRTC_WIN) |
| 1070 | WSACloseEvent(socket_ev_); |
| 1071 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1072 | delete signal_wakeup_; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1073 | #if defined(WEBRTC_USE_EPOLL) |
| 1074 | if (epoll_fd_ != INVALID_SOCKET) { |
| 1075 | close(epoll_fd_); |
| 1076 | } |
| 1077 | #endif |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1078 | RTC_DCHECK(dispatcher_by_key_.empty()); |
| 1079 | RTC_DCHECK(key_by_dispatcher_.empty()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | void PhysicalSocketServer::WakeUp() { |
| 1083 | signal_wakeup_->Signal(); |
| 1084 | } |
| 1085 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1086 | Socket* PhysicalSocketServer::CreateSocket(int family, int type) { |
| 1087 | PhysicalSocket* socket = new PhysicalSocket(this); |
| 1088 | if (socket->Create(family, type)) { |
| 1089 | return socket; |
| 1090 | } else { |
| 1091 | delete socket; |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1092 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1096 | AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int family, int type) { |
| 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 | |
| 1106 | AsyncSocket* PhysicalSocketServer::WrapSocket(SOCKET s) { |
| 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 | |
| 1186 | static void ProcessEvents(Dispatcher* dispatcher, |
| 1187 | bool readable, |
| 1188 | bool writable, |
| 1189 | bool check_error) { |
| 1190 | int errcode = 0; |
| 1191 | // TODO(pthatcher): Should we set errcode if getsockopt fails? |
| 1192 | if (check_error) { |
| 1193 | socklen_t len = sizeof(errcode); |
| 1194 | ::getsockopt(dispatcher->GetDescriptor(), SOL_SOCKET, SO_ERROR, &errcode, |
| 1195 | &len); |
| 1196 | } |
| 1197 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1198 | // Most often the socket is writable or readable or both, so make a single |
| 1199 | // virtual call to get requested events |
| 1200 | const uint32_t requested_events = dispatcher->GetRequestedEvents(); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1201 | uint32_t ff = 0; |
| 1202 | |
| 1203 | // Check readable descriptors. If we're waiting on an accept, signal |
| 1204 | // that. Otherwise we're waiting for data, check to see if we're |
| 1205 | // readable or really closed. |
| 1206 | // TODO(pthatcher): Only peek at TCP descriptors. |
| 1207 | if (readable) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1208 | if (requested_events & DE_ACCEPT) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1209 | ff |= DE_ACCEPT; |
| 1210 | } else if (errcode || dispatcher->IsDescriptorClosed()) { |
| 1211 | ff |= DE_CLOSE; |
| 1212 | } else { |
| 1213 | ff |= DE_READ; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | // Check writable descriptors. If we're waiting on a connect, detect |
| 1218 | // success versus failure by the reaped error code. |
| 1219 | if (writable) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1220 | if (requested_events & DE_CONNECT) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1221 | if (!errcode) { |
| 1222 | ff |= DE_CONNECT; |
| 1223 | } else { |
| 1224 | ff |= DE_CLOSE; |
| 1225 | } |
| 1226 | } else { |
| 1227 | ff |= DE_WRITE; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | // Tell the descriptor about the event. |
| 1232 | if (ff != 0) { |
| 1233 | dispatcher->OnPreEvent(ff); |
| 1234 | dispatcher->OnEvent(ff, errcode); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | bool PhysicalSocketServer::WaitSelect(int cmsWait, bool process_io) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1239 | // Calculate timing information |
| 1240 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1241 | struct timeval* ptvWait = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1242 | struct timeval tvWait; |
Niels Möller | 689b587 | 2018-08-29 09:55:44 +0200 | [diff] [blame] | 1243 | int64_t stop_us; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1244 | if (cmsWait != kForever) { |
| 1245 | // Calculate wait timeval |
| 1246 | tvWait.tv_sec = cmsWait / 1000; |
| 1247 | tvWait.tv_usec = (cmsWait % 1000) * 1000; |
| 1248 | ptvWait = &tvWait; |
| 1249 | |
Niels Möller | 689b587 | 2018-08-29 09:55:44 +0200 | [diff] [blame] | 1250 | // Calculate when to return |
| 1251 | stop_us = rtc::TimeMicros() + cmsWait * 1000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1254 | |
| 1255 | fd_set fdsRead; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1256 | fd_set fdsWrite; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1257 | // Explicitly unpoison these FDs on MemorySanitizer which doesn't handle the |
| 1258 | // inline assembly in FD_ZERO. |
| 1259 | // http://crbug.com/344505 |
pbos@webrtc.org | 27e5898 | 2014-10-07 17:56:53 +0000 | [diff] [blame] | 1260 | #ifdef MEMORY_SANITIZER |
| 1261 | __msan_unpoison(&fdsRead, sizeof(fdsRead)); |
| 1262 | __msan_unpoison(&fdsWrite, sizeof(fdsWrite)); |
| 1263 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1264 | |
| 1265 | fWait_ = true; |
| 1266 | |
| 1267 | while (fWait_) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1268 | // Zero all fd_sets. Although select() zeros the descriptors not signaled, |
| 1269 | // we may need to do this for dispatchers that were deleted while |
| 1270 | // iterating. |
| 1271 | FD_ZERO(&fdsRead); |
| 1272 | FD_ZERO(&fdsWrite); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1273 | int fdmax = -1; |
| 1274 | { |
| 1275 | CritScope cr(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1276 | current_dispatcher_keys_.clear(); |
| 1277 | for (auto const& kv : dispatcher_by_key_) { |
| 1278 | uint64_t key = kv.first; |
| 1279 | Dispatcher* pdispatcher = kv.second; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1280 | // Query dispatchers for read and write wait state |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1281 | if (!process_io && (pdispatcher != signal_wakeup_)) |
| 1282 | continue; |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1283 | current_dispatcher_keys_.push_back(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1284 | int fd = pdispatcher->GetDescriptor(); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1285 | // "select"ing a file descriptor that is equal to or larger than |
| 1286 | // FD_SETSIZE will result in undefined behavior. |
| 1287 | RTC_DCHECK_LT(fd, FD_SETSIZE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1288 | if (fd > fdmax) |
| 1289 | fdmax = fd; |
| 1290 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1291 | uint32_t ff = pdispatcher->GetRequestedEvents(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1292 | if (ff & (DE_READ | DE_ACCEPT)) |
| 1293 | FD_SET(fd, &fdsRead); |
| 1294 | if (ff & (DE_WRITE | DE_CONNECT)) |
| 1295 | FD_SET(fd, &fdsWrite); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | // Wait then call handlers as appropriate |
| 1300 | // < 0 means error |
| 1301 | // 0 means timeout |
| 1302 | // > 0 means count of descriptors ready |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1303 | int n = select(fdmax + 1, &fdsRead, &fdsWrite, nullptr, ptvWait); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1304 | |
| 1305 | // If error, return error. |
| 1306 | if (n < 0) { |
| 1307 | if (errno != EINTR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1308 | RTC_LOG_E(LS_ERROR, EN, errno) << "select"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1309 | return false; |
| 1310 | } |
| 1311 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1312 | // signals managed by this PhysicalSocketServer, the |
| 1313 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1314 | // iteration. |
| 1315 | } else if (n == 0) { |
| 1316 | // If timeout, return success |
| 1317 | return true; |
| 1318 | } else { |
| 1319 | // We have signaled descriptors |
| 1320 | CritScope cr(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1321 | // Iterate only on the dispatchers whose sockets were passed into |
| 1322 | // WSAEventSelect; this avoids the ABA problem (a socket being |
| 1323 | // destroyed and a new one created with the same file descriptor). |
| 1324 | for (uint64_t key : current_dispatcher_keys_) { |
| 1325 | if (!dispatcher_by_key_.count(key)) |
| 1326 | continue; |
| 1327 | Dispatcher* pdispatcher = dispatcher_by_key_.at(key); |
| 1328 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1329 | int fd = pdispatcher->GetDescriptor(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1330 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1331 | bool readable = FD_ISSET(fd, &fdsRead); |
| 1332 | if (readable) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1333 | FD_CLR(fd, &fdsRead); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1336 | bool writable = FD_ISSET(fd, &fdsWrite); |
| 1337 | if (writable) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1338 | FD_CLR(fd, &fdsWrite); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1341 | // The error code can be signaled through reads or writes. |
| 1342 | ProcessEvents(pdispatcher, readable, writable, readable || writable); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | // Recalc the time remaining to wait. Doing it here means it doesn't get |
| 1347 | // calced twice the first time through the loop |
| 1348 | if (ptvWait) { |
| 1349 | ptvWait->tv_sec = 0; |
| 1350 | ptvWait->tv_usec = 0; |
Niels Möller | 689b587 | 2018-08-29 09:55:44 +0200 | [diff] [blame] | 1351 | int64_t time_left_us = stop_us - rtc::TimeMicros(); |
| 1352 | if (time_left_us > 0) { |
| 1353 | ptvWait->tv_sec = time_left_us / rtc::kNumMicrosecsPerSec; |
| 1354 | ptvWait->tv_usec = time_left_us % rtc::kNumMicrosecsPerSec; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1362 | #if defined(WEBRTC_USE_EPOLL) |
| 1363 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1364 | void PhysicalSocketServer::AddEpoll(Dispatcher* pdispatcher, uint64_t key) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1365 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1366 | int fd = pdispatcher->GetDescriptor(); |
| 1367 | RTC_DCHECK(fd != INVALID_SOCKET); |
| 1368 | if (fd == INVALID_SOCKET) { |
| 1369 | return; |
| 1370 | } |
| 1371 | |
| 1372 | struct epoll_event event = {0}; |
| 1373 | event.events = GetEpollEvents(pdispatcher->GetRequestedEvents()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1374 | event.data.u64 = key; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1375 | int err = epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event); |
| 1376 | RTC_DCHECK_EQ(err, 0); |
| 1377 | if (err == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1378 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_ADD"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | void PhysicalSocketServer::RemoveEpoll(Dispatcher* pdispatcher) { |
| 1383 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1384 | int fd = pdispatcher->GetDescriptor(); |
| 1385 | RTC_DCHECK(fd != INVALID_SOCKET); |
| 1386 | if (fd == INVALID_SOCKET) { |
| 1387 | return; |
| 1388 | } |
| 1389 | |
| 1390 | struct epoll_event event = {0}; |
| 1391 | int err = epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &event); |
| 1392 | RTC_DCHECK(err == 0 || errno == ENOENT); |
| 1393 | if (err == -1) { |
| 1394 | if (errno == ENOENT) { |
| 1395 | // Socket has already been closed. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1396 | RTC_LOG_E(LS_VERBOSE, EN, errno) << "epoll_ctl EPOLL_CTL_DEL"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1397 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1398 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_DEL"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1403 | void PhysicalSocketServer::UpdateEpoll(Dispatcher* pdispatcher, uint64_t key) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 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 | event.events = GetEpollEvents(pdispatcher->GetRequestedEvents()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1413 | event.data.u64 = key; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1414 | int err = epoll_ctl(epoll_fd_, EPOLL_CTL_MOD, fd, &event); |
| 1415 | RTC_DCHECK_EQ(err, 0); |
| 1416 | if (err == -1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1417 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll_ctl EPOLL_CTL_MOD"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | bool PhysicalSocketServer::WaitEpoll(int cmsWait) { |
| 1422 | RTC_DCHECK(epoll_fd_ != INVALID_SOCKET); |
| 1423 | int64_t tvWait = -1; |
| 1424 | int64_t tvStop = -1; |
| 1425 | if (cmsWait != kForever) { |
| 1426 | tvWait = cmsWait; |
| 1427 | tvStop = TimeAfter(cmsWait); |
| 1428 | } |
| 1429 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1430 | fWait_ = true; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1431 | while (fWait_) { |
| 1432 | // Wait then call handlers as appropriate |
| 1433 | // < 0 means error |
| 1434 | // 0 means timeout |
| 1435 | // > 0 means count of descriptors ready |
Markus Handell | b7c63ab | 2020-05-26 18:09:55 +0200 | [diff] [blame] | 1436 | int n = epoll_wait(epoll_fd_, epoll_events_.data(), epoll_events_.size(), |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1437 | static_cast<int>(tvWait)); |
| 1438 | if (n < 0) { |
| 1439 | if (errno != EINTR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1440 | RTC_LOG_E(LS_ERROR, EN, errno) << "epoll"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1441 | return false; |
| 1442 | } |
| 1443 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1444 | // signals managed by this PhysicalSocketServer, the |
| 1445 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1446 | // iteration. |
| 1447 | } else if (n == 0) { |
| 1448 | // If timeout, return success |
| 1449 | return true; |
| 1450 | } else { |
| 1451 | // We have signaled descriptors |
| 1452 | CritScope cr(&crit_); |
| 1453 | for (int i = 0; i < n; ++i) { |
| 1454 | const epoll_event& event = epoll_events_[i]; |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1455 | uint64_t key = event.data.u64; |
| 1456 | if (!dispatcher_by_key_.count(key)) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1457 | // The dispatcher for this socket no longer exists. |
| 1458 | continue; |
| 1459 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1460 | Dispatcher* pdispatcher = dispatcher_by_key_.at(key); |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1461 | |
| 1462 | bool readable = (event.events & (EPOLLIN | EPOLLPRI)); |
| 1463 | bool writable = (event.events & EPOLLOUT); |
| 1464 | bool check_error = (event.events & (EPOLLRDHUP | EPOLLERR | EPOLLHUP)); |
| 1465 | |
| 1466 | ProcessEvents(pdispatcher, readable, writable, check_error); |
| 1467 | } |
| 1468 | } |
| 1469 | |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1470 | if (cmsWait != kForever) { |
| 1471 | tvWait = TimeDiff(tvStop, TimeMillis()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1472 | if (tvWait <= 0) { |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1473 | // Return success on timeout. |
| 1474 | return true; |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
| 1482 | bool PhysicalSocketServer::WaitPoll(int cmsWait, Dispatcher* dispatcher) { |
| 1483 | RTC_DCHECK(dispatcher); |
| 1484 | int64_t tvWait = -1; |
| 1485 | int64_t tvStop = -1; |
| 1486 | if (cmsWait != kForever) { |
| 1487 | tvWait = cmsWait; |
| 1488 | tvStop = TimeAfter(cmsWait); |
| 1489 | } |
| 1490 | |
| 1491 | fWait_ = true; |
| 1492 | |
| 1493 | struct pollfd fds = {0}; |
| 1494 | int fd = dispatcher->GetDescriptor(); |
| 1495 | fds.fd = fd; |
| 1496 | |
| 1497 | while (fWait_) { |
| 1498 | uint32_t ff = dispatcher->GetRequestedEvents(); |
| 1499 | fds.events = 0; |
| 1500 | if (ff & (DE_READ | DE_ACCEPT)) { |
| 1501 | fds.events |= POLLIN; |
| 1502 | } |
| 1503 | if (ff & (DE_WRITE | DE_CONNECT)) { |
| 1504 | fds.events |= POLLOUT; |
| 1505 | } |
| 1506 | fds.revents = 0; |
| 1507 | |
| 1508 | // Wait then call handlers as appropriate |
| 1509 | // < 0 means error |
| 1510 | // 0 means timeout |
| 1511 | // > 0 means count of descriptors ready |
| 1512 | int n = poll(&fds, 1, static_cast<int>(tvWait)); |
| 1513 | if (n < 0) { |
| 1514 | if (errno != EINTR) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1515 | RTC_LOG_E(LS_ERROR, EN, errno) << "poll"; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1516 | return false; |
| 1517 | } |
| 1518 | // Else ignore the error and keep going. If this EINTR was for one of the |
| 1519 | // signals managed by this PhysicalSocketServer, the |
| 1520 | // PosixSignalDeliveryDispatcher will be in the signaled state in the next |
| 1521 | // iteration. |
| 1522 | } else if (n == 0) { |
| 1523 | // If timeout, return success |
| 1524 | return true; |
| 1525 | } else { |
| 1526 | // We have signaled descriptors (should only be the passed dispatcher). |
| 1527 | RTC_DCHECK_EQ(n, 1); |
| 1528 | RTC_DCHECK_EQ(fds.fd, fd); |
| 1529 | |
| 1530 | bool readable = (fds.revents & (POLLIN | POLLPRI)); |
| 1531 | bool writable = (fds.revents & POLLOUT); |
| 1532 | bool check_error = (fds.revents & (POLLRDHUP | POLLERR | POLLHUP)); |
| 1533 | |
| 1534 | ProcessEvents(dispatcher, readable, writable, check_error); |
| 1535 | } |
| 1536 | |
| 1537 | if (cmsWait != kForever) { |
| 1538 | tvWait = TimeDiff(tvStop, TimeMillis()); |
| 1539 | if (tvWait < 0) { |
| 1540 | // Return success on timeout. |
| 1541 | return true; |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | return true; |
| 1547 | } |
| 1548 | |
| 1549 | #endif // WEBRTC_USE_EPOLL |
| 1550 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1551 | #endif // WEBRTC_POSIX |
| 1552 | |
| 1553 | #if defined(WEBRTC_WIN) |
| 1554 | bool PhysicalSocketServer::Wait(int cmsWait, bool process_io) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1555 | // We don't support reentrant waiting. |
| 1556 | RTC_DCHECK(!waiting_); |
| 1557 | ScopedSetTrue s(&waiting_); |
| 1558 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1559 | int64_t cmsTotal = cmsWait; |
| 1560 | int64_t cmsElapsed = 0; |
| 1561 | int64_t msStart = Time(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1562 | |
| 1563 | fWait_ = true; |
| 1564 | while (fWait_) { |
| 1565 | std::vector<WSAEVENT> events; |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1566 | std::vector<uint64_t> event_owners; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1567 | |
| 1568 | events.push_back(socket_ev_); |
| 1569 | |
| 1570 | { |
| 1571 | CritScope cr(&crit_); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1572 | // Get a snapshot of all current dispatchers; this is used to avoid the |
| 1573 | // ABA problem (see later comment) and avoids the dispatcher_by_key_ |
| 1574 | // iterator being invalidated by calling CheckSignalClose, which may |
| 1575 | // remove the dispatcher from the list. |
| 1576 | current_dispatcher_keys_.clear(); |
| 1577 | for (auto const& kv : dispatcher_by_key_) { |
| 1578 | current_dispatcher_keys_.push_back(kv.first); |
| 1579 | } |
| 1580 | for (uint64_t key : current_dispatcher_keys_) { |
| 1581 | if (!dispatcher_by_key_.count(key)) { |
| 1582 | continue; |
| 1583 | } |
| 1584 | Dispatcher* disp = dispatcher_by_key_.at(key); |
| 1585 | if (!disp) |
| 1586 | continue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1587 | if (!process_io && (disp != signal_wakeup_)) |
| 1588 | continue; |
| 1589 | SOCKET s = disp->GetSocket(); |
| 1590 | if (disp->CheckSignalClose()) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1591 | // We just signalled close, don't poll this socket. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1592 | } else if (s != INVALID_SOCKET) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1593 | WSAEventSelect(s, events[0], |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1594 | FlagsToEvents(disp->GetRequestedEvents())); |
| 1595 | } else { |
| 1596 | events.push_back(disp->GetWSAEvent()); |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1597 | event_owners.push_back(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | // Which is shorter, the delay wait or the asked wait? |
| 1603 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1604 | int64_t cmsNext; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1605 | if (cmsWait == kForever) { |
| 1606 | cmsNext = cmsWait; |
| 1607 | } else { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1608 | cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | // Wait for one of the events to signal |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1612 | DWORD dw = |
| 1613 | WSAWaitForMultipleEvents(static_cast<DWORD>(events.size()), &events[0], |
| 1614 | false, static_cast<DWORD>(cmsNext), false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (dw == WSA_WAIT_FAILED) { |
| 1617 | // Failed? |
jbauch | 095ae15 | 2015-12-18 01:39:55 -0800 | [diff] [blame] | 1618 | // TODO(pthatcher): need a better strategy than this! |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1619 | WSAGetLastError(); |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1620 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1621 | return false; |
| 1622 | } else if (dw == WSA_WAIT_TIMEOUT) { |
| 1623 | // Timeout? |
| 1624 | return true; |
| 1625 | } else { |
| 1626 | // Figure out which one it is and call it |
| 1627 | CritScope cr(&crit_); |
| 1628 | int index = dw - WSA_WAIT_EVENT_0; |
| 1629 | if (index > 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1630 | --index; // The first event is the socket event |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1631 | uint64_t key = event_owners[index]; |
| 1632 | if (!dispatcher_by_key_.count(key)) { |
| 1633 | // The dispatcher could have been removed while waiting for events. |
| 1634 | continue; |
jbauch | de4db11 | 2017-05-31 13:09:18 -0700 | [diff] [blame] | 1635 | } |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1636 | Dispatcher* disp = dispatcher_by_key_.at(key); |
| 1637 | disp->OnPreEvent(0); |
| 1638 | disp->OnEvent(0, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1639 | } else if (process_io) { |
Taylor Brandstetter | 7b69a44 | 2020-08-20 23:43:13 +0000 | [diff] [blame^] | 1640 | // Iterate only on the dispatchers whose sockets were passed into |
| 1641 | // WSAEventSelect; this avoids the ABA problem (a socket being |
| 1642 | // destroyed and a new one created with the same SOCKET handle). |
| 1643 | for (uint64_t key : current_dispatcher_keys_) { |
| 1644 | if (!dispatcher_by_key_.count(key)) { |
| 1645 | continue; |
| 1646 | } |
| 1647 | Dispatcher* disp = dispatcher_by_key_.at(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1648 | SOCKET s = disp->GetSocket(); |
| 1649 | if (s == INVALID_SOCKET) |
| 1650 | continue; |
| 1651 | |
| 1652 | WSANETWORKEVENTS wsaEvents; |
| 1653 | int err = WSAEnumNetworkEvents(s, events[0], &wsaEvents); |
| 1654 | if (err == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1655 | { |
| 1656 | if ((wsaEvents.lNetworkEvents & FD_READ) && |
| 1657 | wsaEvents.iErrorCode[FD_READ_BIT] != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1658 | RTC_LOG(WARNING) |
| 1659 | << "PhysicalSocketServer got FD_READ_BIT error " |
| 1660 | << wsaEvents.iErrorCode[FD_READ_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1661 | } |
| 1662 | if ((wsaEvents.lNetworkEvents & FD_WRITE) && |
| 1663 | wsaEvents.iErrorCode[FD_WRITE_BIT] != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1664 | RTC_LOG(WARNING) |
| 1665 | << "PhysicalSocketServer got FD_WRITE_BIT error " |
| 1666 | << wsaEvents.iErrorCode[FD_WRITE_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1667 | } |
| 1668 | if ((wsaEvents.lNetworkEvents & FD_CONNECT) && |
| 1669 | wsaEvents.iErrorCode[FD_CONNECT_BIT] != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1670 | RTC_LOG(WARNING) |
| 1671 | << "PhysicalSocketServer got FD_CONNECT_BIT error " |
| 1672 | << wsaEvents.iErrorCode[FD_CONNECT_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1673 | } |
| 1674 | if ((wsaEvents.lNetworkEvents & FD_ACCEPT) && |
| 1675 | wsaEvents.iErrorCode[FD_ACCEPT_BIT] != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1676 | RTC_LOG(WARNING) |
| 1677 | << "PhysicalSocketServer got FD_ACCEPT_BIT error " |
| 1678 | << wsaEvents.iErrorCode[FD_ACCEPT_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1679 | } |
| 1680 | if ((wsaEvents.lNetworkEvents & FD_CLOSE) && |
| 1681 | wsaEvents.iErrorCode[FD_CLOSE_BIT] != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1682 | RTC_LOG(WARNING) |
| 1683 | << "PhysicalSocketServer got FD_CLOSE_BIT error " |
| 1684 | << wsaEvents.iErrorCode[FD_CLOSE_BIT]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1685 | } |
| 1686 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1687 | uint32_t ff = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1688 | int errcode = 0; |
| 1689 | if (wsaEvents.lNetworkEvents & FD_READ) |
| 1690 | ff |= DE_READ; |
| 1691 | if (wsaEvents.lNetworkEvents & FD_WRITE) |
| 1692 | ff |= DE_WRITE; |
| 1693 | if (wsaEvents.lNetworkEvents & FD_CONNECT) { |
| 1694 | if (wsaEvents.iErrorCode[FD_CONNECT_BIT] == 0) { |
| 1695 | ff |= DE_CONNECT; |
| 1696 | } else { |
| 1697 | ff |= DE_CLOSE; |
| 1698 | errcode = wsaEvents.iErrorCode[FD_CONNECT_BIT]; |
| 1699 | } |
| 1700 | } |
| 1701 | if (wsaEvents.lNetworkEvents & FD_ACCEPT) |
| 1702 | ff |= DE_ACCEPT; |
| 1703 | if (wsaEvents.lNetworkEvents & FD_CLOSE) { |
| 1704 | ff |= DE_CLOSE; |
| 1705 | errcode = wsaEvents.iErrorCode[FD_CLOSE_BIT]; |
| 1706 | } |
| 1707 | if (ff != 0) { |
| 1708 | disp->OnPreEvent(ff); |
| 1709 | disp->OnEvent(ff, errcode); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | // Reset the network event until new activity occurs |
| 1716 | WSAResetEvent(socket_ev_); |
| 1717 | } |
| 1718 | |
| 1719 | // Break? |
| 1720 | if (!fWait_) |
| 1721 | break; |
| 1722 | cmsElapsed = TimeSince(msStart); |
| 1723 | if ((cmsWait != kForever) && (cmsElapsed >= cmsWait)) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1724 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | // Done |
| 1729 | return true; |
| 1730 | } |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 1731 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1732 | |
| 1733 | } // namespace rtc |