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