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 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/virtual_socket_server.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #include <errno.h> |
| 14 | #include <math.h> |
| 15 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include <map> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 17 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 20 | #include "absl/algorithm/container.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 22 | #include "rtc_base/deprecated/recursive_critical_section.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/physical_socket_server.h" |
| 26 | #include "rtc_base/socket_address_pair.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | |
| 30 | namespace rtc { |
| 31 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | const in_addr kInitialNextIPv4 = {{{0x01, 0, 0, 0}}}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | #else |
| 34 | // This value is entirely arbitrary, hence the lack of concern about endianness. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 35 | const in_addr kInitialNextIPv4 = {0x01000000}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | #endif |
| 37 | // Starts at ::2 so as to not cause confusion with ::1. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | const in6_addr kInitialNextIPv6 = { |
| 39 | {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}}}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 41 | const uint16_t kFirstEphemeralPort = 49152; |
| 42 | const uint16_t kLastEphemeralPort = 65535; |
| 43 | const uint16_t kEphemeralPortCount = |
| 44 | kLastEphemeralPort - kFirstEphemeralPort + 1; |
| 45 | const uint32_t kDefaultNetworkCapacity = 64 * 1024; |
| 46 | const uint32_t kDefaultTcpBufferSize = 32 * 1024; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 48 | const uint32_t UDP_HEADER_SIZE = 28; // IP + UDP headers |
| 49 | const uint32_t TCP_HEADER_SIZE = 40; // IP + TCP headers |
| 50 | const uint32_t TCP_MSS = 1400; // Maximum segment size |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | |
| 52 | // Note: The current algorithm doesn't work for sample sizes smaller than this. |
| 53 | const int NUM_SAMPLES = 1000; |
| 54 | |
| 55 | enum { |
| 56 | MSG_ID_PACKET, |
| 57 | MSG_ID_CONNECT, |
| 58 | MSG_ID_DISCONNECT, |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 59 | MSG_ID_SIGNALREADEVENT, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | // Packets are passed between sockets as messages. We copy the data just like |
| 63 | // the kernel does. |
| 64 | class Packet : public MessageData { |
| 65 | public: |
| 66 | Packet(const char* data, size_t size, const SocketAddress& from) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | : size_(size), consumed_(0), from_(from) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 68 | RTC_DCHECK(nullptr != data); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | data_ = new char[size_]; |
| 70 | memcpy(data_, data, size_); |
| 71 | } |
| 72 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | ~Packet() override { delete[] data_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | |
| 75 | const char* data() const { return data_ + consumed_; } |
| 76 | size_t size() const { return size_ - consumed_; } |
| 77 | const SocketAddress& from() const { return from_; } |
| 78 | |
| 79 | // Remove the first size bytes from the data. |
| 80 | void Consume(size_t size) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 81 | RTC_DCHECK(size + consumed_ < size_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | consumed_ += size; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | char* data_; |
| 87 | size_t size_, consumed_; |
| 88 | SocketAddress from_; |
| 89 | }; |
| 90 | |
| 91 | struct MessageAddress : public MessageData { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | explicit MessageAddress(const SocketAddress& a) : addr(a) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | SocketAddress addr; |
| 94 | }; |
| 95 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 96 | VirtualSocket::VirtualSocket(VirtualSocketServer* server, |
| 97 | int family, |
| 98 | int type, |
| 99 | bool async) |
| 100 | : server_(server), |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 101 | type_(type), |
| 102 | async_(async), |
| 103 | state_(CS_CLOSED), |
| 104 | error_(0), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 105 | listen_queue_(nullptr), |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 106 | network_size_(0), |
| 107 | recv_buffer_size_(0), |
| 108 | bound_(false), |
| 109 | was_any_(false) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 110 | RTC_DCHECK((type_ == SOCK_DGRAM) || (type_ == SOCK_STREAM)); |
| 111 | RTC_DCHECK(async_ || |
| 112 | (type_ != SOCK_STREAM)); // We only support async streams |
| 113 | server->SignalReadyToSend.connect(this, |
| 114 | &VirtualSocket::OnSocketServerReadyToSend); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | VirtualSocket::~VirtualSocket() { |
| 118 | Close(); |
| 119 | |
| 120 | for (RecvBuffer::iterator it = recv_buffer_.begin(); it != recv_buffer_.end(); |
| 121 | ++it) { |
| 122 | delete *it; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 124 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 126 | SocketAddress VirtualSocket::GetLocalAddress() const { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 127 | return local_addr_; |
| 128 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 130 | SocketAddress VirtualSocket::GetRemoteAddress() const { |
| 131 | return remote_addr_; |
| 132 | } |
| 133 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 134 | void VirtualSocket::SetLocalAddress(const SocketAddress& addr) { |
| 135 | local_addr_ = addr; |
| 136 | } |
| 137 | |
| 138 | int VirtualSocket::Bind(const SocketAddress& addr) { |
| 139 | if (!local_addr_.IsNil()) { |
| 140 | error_ = EINVAL; |
| 141 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 143 | local_addr_ = addr; |
| 144 | int result = server_->Bind(this, &local_addr_); |
| 145 | if (result != 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | local_addr_.Clear(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 147 | error_ = EADDRINUSE; |
| 148 | } else { |
| 149 | bound_ = true; |
| 150 | was_any_ = addr.IsAnyIP(); |
| 151 | } |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | int VirtualSocket::Connect(const SocketAddress& addr) { |
| 156 | return InitiateConnect(addr, true); |
| 157 | } |
| 158 | |
| 159 | int VirtualSocket::Close() { |
| 160 | if (!local_addr_.IsNil() && bound_) { |
| 161 | // Remove from the binding table. |
| 162 | server_->Unbind(local_addr_, this); |
| 163 | bound_ = false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | } |
| 165 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 166 | if (SOCK_STREAM == type_) { |
| 167 | // Cancel pending sockets |
| 168 | if (listen_queue_) { |
| 169 | while (!listen_queue_->empty()) { |
| 170 | SocketAddress addr = listen_queue_->front(); |
| 171 | |
| 172 | // Disconnect listening socket. |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 173 | server_->Disconnect(addr); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 174 | listen_queue_->pop_front(); |
| 175 | } |
| 176 | delete listen_queue_; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 177 | listen_queue_ = nullptr; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 178 | } |
| 179 | // Disconnect stream sockets |
| 180 | if (CS_CONNECTED == state_) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 181 | server_->Disconnect(local_addr_, remote_addr_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 182 | } |
| 183 | // Cancel potential connects |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 184 | server_->CancelConnects(this); |
Tomas Gunnarsson | d966347 | 2020-11-21 16:20:23 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Clear incoming packets and disconnect messages |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 188 | server_->Clear(this); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 189 | |
| 190 | state_ = CS_CLOSED; |
| 191 | local_addr_.Clear(); |
| 192 | remote_addr_.Clear(); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int VirtualSocket::Send(const void* pv, size_t cb) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 197 | if (CS_CONNECTED != state_) { |
| 198 | error_ = ENOTCONN; |
| 199 | return -1; |
| 200 | } |
| 201 | if (SOCK_DGRAM == type_) { |
| 202 | return SendUdp(pv, cb, remote_addr_); |
| 203 | } else { |
| 204 | return SendTcp(pv, cb); |
| 205 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 206 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 207 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 208 | int VirtualSocket::SendTo(const void* pv, |
| 209 | size_t cb, |
| 210 | const SocketAddress& addr) { |
| 211 | if (SOCK_DGRAM == type_) { |
| 212 | return SendUdp(pv, cb, addr); |
| 213 | } else { |
| 214 | if (CS_CONNECTED != state_) { |
| 215 | error_ = ENOTCONN; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | return -1; |
| 217 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 218 | return SendTcp(pv, cb); |
| 219 | } |
| 220 | } |
| 221 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 222 | int VirtualSocket::Recv(void* pv, size_t cb, int64_t* timestamp) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 223 | SocketAddress addr; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 224 | return RecvFrom(pv, cb, &addr, timestamp); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 227 | int VirtualSocket::RecvFrom(void* pv, |
| 228 | size_t cb, |
| 229 | SocketAddress* paddr, |
| 230 | int64_t* timestamp) { |
| 231 | if (timestamp) { |
| 232 | *timestamp = -1; |
| 233 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 234 | // If we don't have a packet, then either error or wait for one to arrive. |
| 235 | if (recv_buffer_.empty()) { |
| 236 | if (async_) { |
| 237 | error_ = EAGAIN; |
| 238 | return -1; |
| 239 | } |
| 240 | while (recv_buffer_.empty()) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 241 | server_->ProcessOneMessage(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 242 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 243 | } |
| 244 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 245 | // Return the packet at the front of the queue. |
| 246 | Packet* packet = recv_buffer_.front(); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 247 | size_t data_read = std::min(cb, packet->size()); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 248 | memcpy(pv, packet->data(), data_read); |
| 249 | *paddr = packet->from(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 250 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 251 | if (data_read < packet->size()) { |
| 252 | packet->Consume(data_read); |
| 253 | } else { |
| 254 | recv_buffer_.pop_front(); |
| 255 | delete packet; |
| 256 | } |
| 257 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 258 | // To behave like a real socket, SignalReadEvent should fire in the next |
| 259 | // message loop pass if there's still data buffered. |
| 260 | if (!recv_buffer_.empty()) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 261 | server_->PostSignalReadEvent(this); |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 262 | } |
| 263 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 264 | if (SOCK_STREAM == type_) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 265 | bool was_full = (recv_buffer_size_ == server_->recv_buffer_capacity()); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 266 | recv_buffer_size_ -= data_read; |
| 267 | if (was_full) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 268 | server_->SendTcp(remote_addr_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return static_cast<int>(data_read); |
| 273 | } |
| 274 | |
| 275 | int VirtualSocket::Listen(int backlog) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 276 | RTC_DCHECK(SOCK_STREAM == type_); |
| 277 | RTC_DCHECK(CS_CLOSED == state_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 278 | if (local_addr_.IsNil()) { |
| 279 | error_ = EINVAL; |
| 280 | return -1; |
| 281 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 282 | RTC_DCHECK(nullptr == listen_queue_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 283 | listen_queue_ = new ListenQueue; |
| 284 | state_ = CS_CONNECTING; |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | VirtualSocket* VirtualSocket::Accept(SocketAddress* paddr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 289 | if (nullptr == listen_queue_) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 290 | error_ = EINVAL; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 291 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 292 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 293 | while (!listen_queue_->empty()) { |
| 294 | VirtualSocket* socket = new VirtualSocket(server_, AF_INET, type_, async_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 295 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 296 | // Set the new local address to the same as this server socket. |
| 297 | socket->SetLocalAddress(local_addr_); |
| 298 | // Sockets made from a socket that 'was Any' need to inherit that. |
| 299 | socket->set_was_any(was_any_); |
| 300 | SocketAddress remote_addr(listen_queue_->front()); |
| 301 | int result = socket->InitiateConnect(remote_addr, false); |
| 302 | listen_queue_->pop_front(); |
| 303 | if (result != 0) { |
| 304 | delete socket; |
| 305 | continue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 306 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 307 | socket->CompleteConnect(remote_addr, false); |
| 308 | if (paddr) { |
| 309 | *paddr = remote_addr; |
| 310 | } |
| 311 | return socket; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 312 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 313 | error_ = EWOULDBLOCK; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 314 | return nullptr; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 315 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 317 | int VirtualSocket::GetError() const { |
| 318 | return error_; |
| 319 | } |
| 320 | |
| 321 | void VirtualSocket::SetError(int error) { |
| 322 | error_ = error; |
| 323 | } |
| 324 | |
| 325 | Socket::ConnState VirtualSocket::GetState() const { |
| 326 | return state_; |
| 327 | } |
| 328 | |
| 329 | int VirtualSocket::GetOption(Option opt, int* value) { |
| 330 | OptionsMap::const_iterator it = options_map_.find(opt); |
| 331 | if (it == options_map_.end()) { |
| 332 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 333 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 334 | *value = it->second; |
| 335 | return 0; // 0 is success to emulate getsockopt() |
| 336 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 337 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 338 | int VirtualSocket::SetOption(Option opt, int value) { |
| 339 | options_map_[opt] = value; |
| 340 | return 0; // 0 is success to emulate setsockopt() |
| 341 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 342 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 343 | void VirtualSocket::OnMessage(Message* pmsg) { |
| 344 | if (pmsg->message_id == MSG_ID_PACKET) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 345 | RTC_DCHECK(nullptr != pmsg->pdata); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 346 | Packet* packet = static_cast<Packet*>(pmsg->pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 347 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 348 | recv_buffer_.push_back(packet); |
| 349 | |
| 350 | if (async_) { |
| 351 | SignalReadEvent(this); |
| 352 | } |
| 353 | } else if (pmsg->message_id == MSG_ID_CONNECT) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 354 | RTC_DCHECK(nullptr != pmsg->pdata); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 355 | MessageAddress* data = static_cast<MessageAddress*>(pmsg->pdata); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 356 | if (listen_queue_ != nullptr) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 357 | listen_queue_->push_back(data->addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 358 | if (async_) { |
| 359 | SignalReadEvent(this); |
| 360 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 361 | } else if ((SOCK_STREAM == type_) && (CS_CONNECTING == state_)) { |
| 362 | CompleteConnect(data->addr, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 363 | } else { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 364 | RTC_LOG(LS_VERBOSE) << "Socket at " << local_addr_.ToString() |
| 365 | << " is not listening"; |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 366 | server_->Disconnect(data->addr); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 367 | } |
| 368 | delete data; |
| 369 | } else if (pmsg->message_id == MSG_ID_DISCONNECT) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 370 | RTC_DCHECK(SOCK_STREAM == type_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 371 | if (CS_CLOSED != state_) { |
| 372 | int error = (CS_CONNECTING == state_) ? ECONNREFUSED : 0; |
| 373 | state_ = CS_CLOSED; |
| 374 | remote_addr_.Clear(); |
| 375 | if (async_) { |
| 376 | SignalCloseEvent(this, error); |
| 377 | } |
| 378 | } |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 379 | } else if (pmsg->message_id == MSG_ID_SIGNALREADEVENT) { |
| 380 | if (!recv_buffer_.empty()) { |
| 381 | SignalReadEvent(this); |
| 382 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 383 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 384 | RTC_NOTREACHED(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | int VirtualSocket::InitiateConnect(const SocketAddress& addr, bool use_delay) { |
| 389 | if (!remote_addr_.IsNil()) { |
| 390 | error_ = (CS_CONNECTED == state_) ? EISCONN : EINPROGRESS; |
| 391 | return -1; |
| 392 | } |
| 393 | if (local_addr_.IsNil()) { |
| 394 | // If there's no local address set, grab a random one in the correct AF. |
| 395 | int result = 0; |
| 396 | if (addr.ipaddr().family() == AF_INET) { |
| 397 | result = Bind(SocketAddress("0.0.0.0", 0)); |
| 398 | } else if (addr.ipaddr().family() == AF_INET6) { |
| 399 | result = Bind(SocketAddress("::", 0)); |
| 400 | } |
| 401 | if (result != 0) { |
| 402 | return result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 405 | if (type_ == SOCK_DGRAM) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 406 | remote_addr_ = addr; |
| 407 | state_ = CS_CONNECTED; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 408 | } else { |
| 409 | int result = server_->Connect(this, addr, use_delay); |
| 410 | if (result != 0) { |
| 411 | error_ = EHOSTUNREACH; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 412 | return -1; |
| 413 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 414 | state_ = CS_CONNECTING; |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | void VirtualSocket::CompleteConnect(const SocketAddress& addr, bool notify) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 420 | RTC_DCHECK(CS_CONNECTING == state_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 421 | remote_addr_ = addr; |
| 422 | state_ = CS_CONNECTED; |
| 423 | server_->AddConnection(remote_addr_, local_addr_, this); |
| 424 | if (async_ && notify) { |
| 425 | SignalConnectEvent(this); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | int VirtualSocket::SendUdp(const void* pv, |
| 430 | size_t cb, |
| 431 | const SocketAddress& addr) { |
| 432 | // If we have not been assigned a local port, then get one. |
| 433 | if (local_addr_.IsNil()) { |
| 434 | local_addr_ = EmptySocketAddressWithFamily(addr.ipaddr().family()); |
| 435 | int result = server_->Bind(this, &local_addr_); |
| 436 | if (result != 0) { |
| 437 | local_addr_.Clear(); |
| 438 | error_ = EADDRINUSE; |
| 439 | return result; |
| 440 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 441 | } |
| 442 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 443 | // Send the data in a message to the appropriate socket. |
| 444 | return server_->SendUdp(this, static_cast<const char*>(pv), cb, addr); |
| 445 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 446 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 447 | int VirtualSocket::SendTcp(const void* pv, size_t cb) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 448 | size_t capacity = server_->send_buffer_capacity() - send_buffer_.size(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 449 | if (0 == capacity) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 450 | ready_to_send_ = false; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 451 | error_ = EWOULDBLOCK; |
| 452 | return -1; |
| 453 | } |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 454 | size_t consumed = std::min(cb, capacity); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 455 | const char* cpv = static_cast<const char*>(pv); |
| 456 | send_buffer_.insert(send_buffer_.end(), cpv, cpv + consumed); |
| 457 | server_->SendTcp(this); |
| 458 | return static_cast<int>(consumed); |
| 459 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 460 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 461 | void VirtualSocket::OnSocketServerReadyToSend() { |
| 462 | if (ready_to_send_) { |
| 463 | // This socket didn't encounter EWOULDBLOCK, so there's nothing to do. |
| 464 | return; |
| 465 | } |
| 466 | if (type_ == SOCK_DGRAM) { |
| 467 | ready_to_send_ = true; |
| 468 | SignalWriteEvent(this); |
| 469 | } else { |
| 470 | RTC_DCHECK(type_ == SOCK_STREAM); |
| 471 | // This will attempt to empty the full send buffer, and will fire |
| 472 | // SignalWriteEvent if successful. |
| 473 | server_->SendTcp(this); |
| 474 | } |
| 475 | } |
| 476 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 477 | void VirtualSocket::SetToBlocked() { |
| 478 | CritScope cs(&crit_); |
| 479 | ready_to_send_ = false; |
| 480 | error_ = EWOULDBLOCK; |
| 481 | } |
| 482 | |
| 483 | void VirtualSocket::UpdateRecv(size_t data_size) { |
| 484 | recv_buffer_size_ += data_size; |
| 485 | } |
| 486 | |
| 487 | void VirtualSocket::UpdateSend(size_t data_size) { |
| 488 | size_t new_buffer_size = send_buffer_.size() - data_size; |
| 489 | // Avoid undefined access beyond the last element of the vector. |
| 490 | // This only happens when new_buffer_size is 0. |
| 491 | if (data_size < send_buffer_.size()) { |
| 492 | // memmove is required for potentially overlapping source/destination. |
| 493 | memmove(&send_buffer_[0], &send_buffer_[data_size], new_buffer_size); |
| 494 | } |
| 495 | send_buffer_.resize(new_buffer_size); |
| 496 | } |
| 497 | |
| 498 | void VirtualSocket::MaybeSignalWriteEvent(size_t capacity) { |
| 499 | if (!ready_to_send_ && (send_buffer_.size() < capacity)) { |
| 500 | ready_to_send_ = true; |
| 501 | SignalWriteEvent(this); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | uint32_t VirtualSocket::AddPacket(int64_t cur_time, size_t packet_size) { |
| 506 | network_size_ += packet_size; |
| 507 | uint32_t send_delay = |
| 508 | server_->SendDelay(static_cast<uint32_t>(network_size_)); |
| 509 | |
| 510 | NetworkEntry entry; |
| 511 | entry.size = packet_size; |
| 512 | entry.done_time = cur_time + send_delay; |
| 513 | network_.push_back(entry); |
| 514 | |
| 515 | return send_delay; |
| 516 | } |
| 517 | |
| 518 | int64_t VirtualSocket::UpdateOrderedDelivery(int64_t ts) { |
| 519 | // Ensure that new packets arrive after previous ones |
| 520 | ts = std::max(ts, last_delivery_time_); |
| 521 | // A socket should not have both ordered and unordered delivery, so its last |
| 522 | // delivery time only needs to be updated when it has ordered delivery. |
| 523 | last_delivery_time_ = ts; |
| 524 | return ts; |
| 525 | } |
| 526 | |
| 527 | size_t VirtualSocket::PurgeNetworkPackets(int64_t cur_time) { |
| 528 | CritScope cs(&crit_); |
| 529 | |
| 530 | while (!network_.empty() && (network_.front().done_time <= cur_time)) { |
| 531 | RTC_DCHECK(network_size_ >= network_.front().size); |
| 532 | network_size_ -= network_.front().size; |
| 533 | network_.pop_front(); |
| 534 | } |
| 535 | return network_size_; |
| 536 | } |
| 537 | |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 538 | VirtualSocketServer::VirtualSocketServer() : VirtualSocketServer(nullptr) {} |
| 539 | |
Sebastian Jansson | d624c39 | 2019-04-17 10:36:03 +0200 | [diff] [blame] | 540 | VirtualSocketServer::VirtualSocketServer(ThreadProcessingFakeClock* fake_clock) |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 541 | : fake_clock_(fake_clock), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 542 | msg_queue_(nullptr), |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 543 | stop_on_idle_(false), |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 544 | next_ipv4_(kInitialNextIPv4), |
| 545 | next_ipv6_(kInitialNextIPv6), |
| 546 | next_port_(kFirstEphemeralPort), |
| 547 | bindings_(new AddressMap()), |
| 548 | connections_(new ConnectionMap()), |
| 549 | bandwidth_(0), |
| 550 | network_capacity_(kDefaultNetworkCapacity), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 551 | send_buffer_capacity_(kDefaultTcpBufferSize), |
| 552 | recv_buffer_capacity_(kDefaultTcpBufferSize), |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 553 | delay_mean_(0), |
| 554 | delay_stddev_(0), |
| 555 | delay_samples_(NUM_SAMPLES), |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 556 | drop_prob_(0.0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 557 | UpdateDelayDistribution(); |
| 558 | } |
| 559 | |
| 560 | VirtualSocketServer::~VirtualSocketServer() { |
| 561 | delete bindings_; |
| 562 | delete connections_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | IPAddress VirtualSocketServer::GetNextIP(int family) { |
| 566 | if (family == AF_INET) { |
| 567 | IPAddress next_ip(next_ipv4_); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 568 | next_ipv4_.s_addr = HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 569 | return next_ip; |
| 570 | } else if (family == AF_INET6) { |
| 571 | IPAddress next_ip(next_ipv6_); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 572 | uint32_t* as_ints = reinterpret_cast<uint32_t*>(&next_ipv6_.s6_addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 573 | as_ints[3] += 1; |
| 574 | return next_ip; |
| 575 | } |
| 576 | return IPAddress(); |
| 577 | } |
| 578 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 579 | uint16_t VirtualSocketServer::GetNextPort() { |
| 580 | uint16_t port = next_port_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 581 | if (next_port_ < kLastEphemeralPort) { |
| 582 | ++next_port_; |
| 583 | } else { |
| 584 | next_port_ = kFirstEphemeralPort; |
| 585 | } |
| 586 | return port; |
| 587 | } |
| 588 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 589 | void VirtualSocketServer::SetSendingBlocked(bool blocked) { |
| 590 | if (blocked == sending_blocked_) { |
| 591 | // Unchanged; nothing to do. |
| 592 | return; |
| 593 | } |
| 594 | sending_blocked_ = blocked; |
| 595 | if (!sending_blocked_) { |
| 596 | // Sending was blocked, but is now unblocked. This signal gives sockets a |
| 597 | // chance to fire SignalWriteEvent, and for TCP, send buffered data. |
| 598 | SignalReadyToSend(); |
| 599 | } |
| 600 | } |
| 601 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 602 | Socket* VirtualSocketServer::CreateSocket(int family, int type) { |
| 603 | return CreateSocketInternal(family, type); |
| 604 | } |
| 605 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 606 | AsyncSocket* VirtualSocketServer::CreateAsyncSocket(int family, int type) { |
| 607 | return CreateSocketInternal(family, type); |
| 608 | } |
| 609 | |
| 610 | VirtualSocket* VirtualSocketServer::CreateSocketInternal(int family, int type) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 611 | return new VirtualSocket(this, family, type, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Sebastian Jansson | 290de82 | 2020-01-09 14:20:23 +0100 | [diff] [blame] | 614 | void VirtualSocketServer::SetMessageQueue(Thread* msg_queue) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 615 | msg_queue_ = msg_queue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | bool VirtualSocketServer::Wait(int cmsWait, bool process_io) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 619 | RTC_DCHECK(msg_queue_ == Thread::Current()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 620 | if (stop_on_idle_ && Thread::Current()->empty()) { |
| 621 | return false; |
| 622 | } |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 623 | // Note: we don't need to do anything with |process_io| since we don't have |
| 624 | // any real I/O. Received packets come in the form of queued messages, so |
Sebastian Jansson | 290de82 | 2020-01-09 14:20:23 +0100 | [diff] [blame] | 625 | // Thread will ensure WakeUp is called if another thread sends a |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 626 | // packet. |
| 627 | wakeup_.Wait(cmsWait); |
| 628 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | void VirtualSocketServer::WakeUp() { |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 632 | wakeup_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 633 | } |
| 634 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 635 | void VirtualSocketServer::SetAlternativeLocalAddress( |
| 636 | const rtc::IPAddress& address, |
| 637 | const rtc::IPAddress& alternative) { |
| 638 | alternative_address_mapping_[address] = alternative; |
| 639 | } |
| 640 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 641 | bool VirtualSocketServer::ProcessMessagesUntilIdle() { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 642 | RTC_DCHECK(msg_queue_ == Thread::Current()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 643 | stop_on_idle_ = true; |
| 644 | while (!msg_queue_->empty()) { |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 645 | if (fake_clock_) { |
| 646 | // If using a fake clock, advance it in millisecond increments until the |
Bjorn Mellem | 6eb03b8 | 2017-06-13 15:07:41 -0700 | [diff] [blame] | 647 | // queue is empty. |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 648 | fake_clock_->AdvanceTime(webrtc::TimeDelta::Millis(1)); |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 649 | } else { |
| 650 | // Otherwise, run a normal message loop. |
| 651 | Message msg; |
| 652 | if (msg_queue_->Get(&msg, Thread::kForever)) { |
| 653 | msg_queue_->Dispatch(&msg); |
| 654 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | stop_on_idle_ = false; |
| 658 | return !msg_queue_->IsQuitting(); |
| 659 | } |
| 660 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 661 | void VirtualSocketServer::SetNextPortForTesting(uint16_t port) { |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 +0000 | [diff] [blame] | 662 | next_port_ = port; |
| 663 | } |
| 664 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 665 | bool VirtualSocketServer::CloseTcpConnections( |
| 666 | const SocketAddress& addr_local, |
| 667 | const SocketAddress& addr_remote) { |
| 668 | VirtualSocket* socket = LookupConnection(addr_local, addr_remote); |
| 669 | if (!socket) { |
| 670 | return false; |
| 671 | } |
| 672 | // Signal the close event on the local connection first. |
| 673 | socket->SignalCloseEvent(socket, 0); |
| 674 | |
| 675 | // Trigger the remote connection's close event. |
| 676 | socket->Close(); |
| 677 | |
| 678 | return true; |
| 679 | } |
| 680 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 681 | int VirtualSocketServer::Bind(VirtualSocket* socket, |
| 682 | const SocketAddress& addr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 683 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 684 | // Address must be completely specified at this point |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 685 | RTC_DCHECK(!IPIsUnspec(addr.ipaddr())); |
| 686 | RTC_DCHECK(addr.port() != 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 687 | |
| 688 | // Normalize the address (turns v6-mapped addresses into v4-addresses). |
| 689 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
| 690 | |
| 691 | AddressMap::value_type entry(normalized, socket); |
Niels Möller | d44532a | 2021-02-18 14:38:14 +0100 | [diff] [blame] | 692 | return bindings_->insert(entry).second ? 0 : -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | int VirtualSocketServer::Bind(VirtualSocket* socket, SocketAddress* addr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 696 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 697 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 698 | // Normalize the IP. |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 +0000 | [diff] [blame] | 699 | if (!IPIsUnspec(addr->ipaddr())) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 700 | addr->SetIP(addr->ipaddr().Normalized()); |
| 701 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 702 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 703 | } |
| 704 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 705 | // If the IP appears in |alternative_address_mapping_|, meaning the test has |
| 706 | // configured sockets bound to this IP to actually use another IP, replace |
| 707 | // the IP here. |
| 708 | auto alternative = alternative_address_mapping_.find(addr->ipaddr()); |
| 709 | if (alternative != alternative_address_mapping_.end()) { |
| 710 | addr->SetIP(alternative->second); |
| 711 | } |
| 712 | |
| 713 | // Assign a port if not assigned. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 714 | if (addr->port() == 0) { |
| 715 | for (int i = 0; i < kEphemeralPortCount; ++i) { |
| 716 | addr->SetPort(GetNextPort()); |
| 717 | if (bindings_->find(*addr) == bindings_->end()) { |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | return Bind(socket, *addr); |
| 724 | } |
| 725 | |
| 726 | VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 727 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 728 | AddressMap::iterator it = bindings_->find(normalized); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 729 | if (it != bindings_->end()) { |
| 730 | return it->second; |
| 731 | } |
| 732 | |
| 733 | IPAddress default_ip = GetDefaultRoute(addr.ipaddr().family()); |
| 734 | if (!IPIsUnspec(default_ip) && addr.ipaddr() == default_ip) { |
| 735 | // If we can't find a binding for the packet which is sent to the interface |
| 736 | // corresponding to the default route, it should match a binding with the |
| 737 | // correct port to the any address. |
| 738 | SocketAddress sock_addr = |
| 739 | EmptySocketAddressWithFamily(addr.ipaddr().family()); |
| 740 | sock_addr.SetPort(addr.port()); |
| 741 | return LookupBinding(sock_addr); |
| 742 | } |
| 743 | |
| 744 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | int VirtualSocketServer::Unbind(const SocketAddress& addr, |
| 748 | VirtualSocket* socket) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 749 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 750 | RTC_DCHECK((*bindings_)[normalized] == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 751 | bindings_->erase(bindings_->find(normalized)); |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | void VirtualSocketServer::AddConnection(const SocketAddress& local, |
| 756 | const SocketAddress& remote, |
| 757 | VirtualSocket* remote_socket) { |
| 758 | // Add this socket pair to our routing table. This will allow |
| 759 | // multiple clients to connect to the same server address. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 760 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 761 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 762 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 763 | connections_->insert(std::pair<SocketAddressPair, VirtualSocket*>( |
| 764 | address_pair, remote_socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | VirtualSocket* VirtualSocketServer::LookupConnection( |
| 768 | const SocketAddress& local, |
| 769 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 770 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 771 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 772 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 773 | ConnectionMap::iterator it = connections_->find(address_pair); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 774 | return (connections_->end() != it) ? it->second : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | void VirtualSocketServer::RemoveConnection(const SocketAddress& local, |
| 778 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 779 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 780 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 781 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 782 | connections_->erase(address_pair); |
| 783 | } |
| 784 | |
| 785 | static double Random() { |
| 786 | return static_cast<double>(rand()) / RAND_MAX; |
| 787 | } |
| 788 | |
| 789 | int VirtualSocketServer::Connect(VirtualSocket* socket, |
| 790 | const SocketAddress& remote_addr, |
| 791 | bool use_delay) { |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 792 | uint32_t delay = use_delay ? GetTransitDelay(socket) : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 793 | VirtualSocket* remote = LookupBinding(remote_addr); |
| 794 | if (!CanInteractWith(socket, remote)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 795 | RTC_LOG(LS_INFO) << "Address family mismatch between " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 796 | << socket->GetLocalAddress().ToString() << " and " |
| 797 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 798 | return -1; |
| 799 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 800 | if (remote != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 801 | SocketAddress addr = socket->GetLocalAddress(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 802 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 803 | new MessageAddress(addr)); |
| 804 | } else { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 805 | RTC_LOG(LS_INFO) << "No one listening at " << remote_addr.ToString(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 806 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 807 | } |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { |
| 812 | if (socket) { |
Taylor Brandstetter | 716d07a | 2016-06-27 14:07:41 -0700 | [diff] [blame] | 813 | // If we simulate packets being delayed, we should simulate the |
| 814 | // equivalent of a FIN being delayed as well. |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 815 | uint32_t delay = GetTransitDelay(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 816 | // Remove the mapping. |
Taylor Brandstetter | 716d07a | 2016-06-27 14:07:41 -0700 | [diff] [blame] | 817 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 818 | return true; |
| 819 | } |
| 820 | return false; |
| 821 | } |
| 822 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 823 | bool VirtualSocketServer::Disconnect(const SocketAddress& addr) { |
| 824 | return Disconnect(LookupBinding(addr)); |
| 825 | } |
| 826 | |
| 827 | bool VirtualSocketServer::Disconnect(const SocketAddress& local_addr, |
| 828 | const SocketAddress& remote_addr) { |
| 829 | // Disconnect remote socket, check if it is a child of a server socket. |
| 830 | VirtualSocket* socket = LookupConnection(local_addr, remote_addr); |
| 831 | if (!socket) { |
| 832 | // Not a server socket child, then see if it is bound. |
| 833 | // TODO(tbd): If this is indeed a server socket that has no |
| 834 | // children this will cause the server socket to be |
| 835 | // closed. This might lead to unexpected results, how to fix this? |
| 836 | socket = LookupBinding(remote_addr); |
| 837 | } |
| 838 | Disconnect(socket); |
| 839 | |
| 840 | // Remove mapping for both directions. |
| 841 | RemoveConnection(remote_addr, local_addr); |
| 842 | RemoveConnection(local_addr, remote_addr); |
| 843 | return socket != nullptr; |
| 844 | } |
| 845 | |
| 846 | void VirtualSocketServer::CancelConnects(VirtualSocket* socket) { |
| 847 | MessageList msgs; |
| 848 | if (msg_queue_) { |
| 849 | msg_queue_->Clear(socket, MSG_ID_CONNECT, &msgs); |
| 850 | } |
| 851 | for (MessageList::iterator it = msgs.begin(); it != msgs.end(); ++it) { |
| 852 | RTC_DCHECK(nullptr != it->pdata); |
| 853 | MessageAddress* data = static_cast<MessageAddress*>(it->pdata); |
| 854 | SocketAddress local_addr = socket->GetLocalAddress(); |
| 855 | // Lookup remote side. |
| 856 | VirtualSocket* socket = LookupConnection(local_addr, data->addr); |
| 857 | if (socket) { |
| 858 | // Server socket, remote side is a socket retreived by |
| 859 | // accept. Accepted sockets are not bound so we will not |
| 860 | // find it by looking in the bindings table. |
| 861 | Disconnect(socket); |
| 862 | RemoveConnection(local_addr, data->addr); |
| 863 | } else { |
| 864 | Disconnect(data->addr); |
| 865 | } |
| 866 | delete data; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | void VirtualSocketServer::Clear(VirtualSocket* socket) { |
| 871 | // Clear incoming packets and disconnect messages |
| 872 | if (msg_queue_) { |
| 873 | msg_queue_->Clear(socket); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | void VirtualSocketServer::ProcessOneMessage() { |
| 878 | Message msg; |
| 879 | msg_queue_->Get(&msg); |
| 880 | msg_queue_->Dispatch(&msg); |
| 881 | } |
| 882 | |
| 883 | void VirtualSocketServer::PostSignalReadEvent(VirtualSocket* socket) { |
| 884 | // Clear the message so it doesn't end up posted multiple times. |
| 885 | msg_queue_->Clear(socket, MSG_ID_SIGNALREADEVENT); |
| 886 | msg_queue_->Post(RTC_FROM_HERE, socket, MSG_ID_SIGNALREADEVENT); |
| 887 | } |
| 888 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 889 | int VirtualSocketServer::SendUdp(VirtualSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 890 | const char* data, |
| 891 | size_t data_size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 892 | const SocketAddress& remote_addr) { |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 893 | ++sent_packets_; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 894 | if (sending_blocked_) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 895 | socket->SetToBlocked(); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 896 | return -1; |
| 897 | } |
| 898 | |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 899 | if (data_size > largest_seen_udp_payload_) { |
| 900 | if (data_size > 1000) { |
| 901 | RTC_LOG(LS_VERBOSE) << "Largest UDP seen is " << data_size; |
| 902 | } |
| 903 | largest_seen_udp_payload_ = data_size; |
| 904 | } |
| 905 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 906 | // See if we want to drop this packet. |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 907 | if (data_size > max_udp_payload_) { |
| 908 | RTC_LOG(LS_VERBOSE) << "Dropping too large UDP payload of size " |
| 909 | << data_size << ", UDP payload limit is " |
| 910 | << max_udp_payload_; |
| 911 | // Return as if send was successful; packet disappears. |
| 912 | return data_size; |
| 913 | } |
| 914 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 915 | if (Random() < drop_prob_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 916 | RTC_LOG(LS_VERBOSE) << "Dropping packet: bad luck"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 917 | return static_cast<int>(data_size); |
| 918 | } |
| 919 | |
| 920 | VirtualSocket* recipient = LookupBinding(remote_addr); |
| 921 | if (!recipient) { |
| 922 | // Make a fake recipient for address family checking. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 923 | std::unique_ptr<VirtualSocket> dummy_socket( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 924 | CreateSocketInternal(AF_INET, SOCK_DGRAM)); |
| 925 | dummy_socket->SetLocalAddress(remote_addr); |
| 926 | if (!CanInteractWith(socket, dummy_socket.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 927 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 928 | << socket->GetLocalAddress().ToString() << " and " |
| 929 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 930 | return -1; |
| 931 | } |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 932 | RTC_LOG(LS_VERBOSE) << "No one listening at " << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 933 | return static_cast<int>(data_size); |
| 934 | } |
| 935 | |
| 936 | if (!CanInteractWith(socket, recipient)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 937 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 938 | << socket->GetLocalAddress().ToString() << " and " |
| 939 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 940 | return -1; |
| 941 | } |
| 942 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 943 | { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 944 | int64_t cur_time = TimeMillis(); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 945 | size_t network_size = socket->PurgeNetworkPackets(cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 946 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 947 | // Determine whether we have enough bandwidth to accept this packet. To do |
| 948 | // this, we need to update the send queue. Once we know it's current size, |
| 949 | // we know whether we can fit this packet. |
| 950 | // |
| 951 | // NOTE: There are better algorithms for maintaining such a queue (such as |
| 952 | // "Derivative Random Drop"); however, this algorithm is a more accurate |
| 953 | // simulation of what a normal network would do. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 954 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 955 | size_t packet_size = data_size + UDP_HEADER_SIZE; |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 956 | if (network_size + packet_size > network_capacity_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 957 | RTC_LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded"; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 958 | return static_cast<int>(data_size); |
| 959 | } |
| 960 | |
| 961 | AddPacketToNetwork(socket, recipient, cur_time, data, data_size, |
| 962 | UDP_HEADER_SIZE, false); |
| 963 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 964 | return static_cast<int>(data_size); |
| 965 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | void VirtualSocketServer::SendTcp(VirtualSocket* socket) { |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 969 | ++sent_packets_; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 970 | if (sending_blocked_) { |
| 971 | // Eventually the socket's buffer will fill and VirtualSocket::SendTcp will |
| 972 | // set EWOULDBLOCK. |
| 973 | return; |
| 974 | } |
| 975 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 976 | // TCP can't send more data than will fill up the receiver's buffer. |
| 977 | // We track the data that is in the buffer plus data in flight using the |
| 978 | // recipient's recv_buffer_size_. Anything beyond that must be stored in the |
| 979 | // sender's buffer. We will trigger the buffered data to be sent when data |
| 980 | // is read from the recv_buffer. |
| 981 | |
| 982 | // Lookup the local/remote pair in the connections table. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 983 | VirtualSocket* recipient = |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 984 | LookupConnection(socket->GetLocalAddress(), socket->GetRemoteAddress()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 985 | if (!recipient) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 986 | RTC_LOG(LS_VERBOSE) << "Sending data to no one."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 987 | return; |
| 988 | } |
| 989 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 990 | int64_t cur_time = TimeMillis(); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 991 | socket->PurgeNetworkPackets(cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 992 | |
| 993 | while (true) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 994 | size_t available = recv_buffer_capacity_ - recipient->recv_buffer_size(); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 995 | size_t max_data_size = |
| 996 | std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 997 | size_t data_size = std::min(socket->send_buffer_size(), max_data_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 998 | if (0 == data_size) |
| 999 | break; |
| 1000 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1001 | AddPacketToNetwork(socket, recipient, cur_time, socket->send_buffer_data(), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1002 | data_size, TCP_HEADER_SIZE, true); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1003 | recipient->UpdateRecv(data_size); |
| 1004 | socket->UpdateSend(data_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1007 | socket->MaybeSignalWriteEvent(send_buffer_capacity_); |
| 1008 | } |
| 1009 | |
| 1010 | void VirtualSocketServer::SendTcp(const SocketAddress& addr) { |
| 1011 | VirtualSocket* sender = LookupBinding(addr); |
| 1012 | RTC_DCHECK(nullptr != sender); |
| 1013 | SendTcp(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender, |
| 1017 | VirtualSocket* recipient, |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1018 | int64_t cur_time, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1019 | const char* data, |
| 1020 | size_t data_size, |
| 1021 | size_t header_size, |
| 1022 | bool ordered) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1023 | uint32_t send_delay = sender->AddPacket(cur_time, data_size + header_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Find the delay for crossing the many virtual hops of the network. |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1026 | uint32_t transit_delay = GetTransitDelay(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1027 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1028 | // When the incoming packet is from a binding of the any address, translate it |
| 1029 | // to the default route here such that the recipient will see the default |
| 1030 | // route. |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1031 | SocketAddress sender_addr = sender->GetLocalAddress(); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1032 | IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
| 1033 | if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
| 1034 | sender_addr.SetIP(default_ip); |
| 1035 | } |
| 1036 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1037 | // Post the packet as a message to be delivered (on our own thread) |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1038 | Packet* p = new Packet(data, data_size, sender_addr); |
| 1039 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1040 | int64_t ts = TimeAfter(send_delay + transit_delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1041 | if (ordered) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1042 | ts = sender->UpdateOrderedDelivery(ts); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1043 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1044 | msg_queue_->PostAt(RTC_FROM_HERE, ts, recipient, MSG_ID_PACKET, p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1047 | uint32_t VirtualSocketServer::SendDelay(uint32_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1048 | if (bandwidth_ == 0) |
| 1049 | return 0; |
| 1050 | else |
| 1051 | return 1000 * size / bandwidth_; |
| 1052 | } |
| 1053 | |
| 1054 | #if 0 |
| 1055 | void PrintFunction(std::vector<std::pair<double, double> >* f) { |
| 1056 | return; |
| 1057 | double sum = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1058 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1059 | std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl; |
| 1060 | sum += (*f)[i].second; |
| 1061 | } |
| 1062 | if (!f->empty()) { |
| 1063 | const double mean = sum / f->size(); |
| 1064 | double sum_sq_dev = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1065 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1066 | double dev = (*f)[i].second - mean; |
| 1067 | sum_sq_dev += dev * dev; |
| 1068 | } |
| 1069 | std::cout << "Mean = " << mean << " StdDev = " |
| 1070 | << sqrt(sum_sq_dev / f->size()) << std::endl; |
| 1071 | } |
| 1072 | } |
| 1073 | #endif // <unused> |
| 1074 | |
| 1075 | void VirtualSocketServer::UpdateDelayDistribution() { |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1076 | delay_dist_ = CreateDistribution(delay_mean_, delay_stddev_, delay_samples_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static double PI = 4 * atan(1.0); |
| 1080 | |
| 1081 | static double Normal(double x, double mean, double stddev) { |
| 1082 | double a = (x - mean) * (x - mean) / (2 * stddev * stddev); |
| 1083 | return exp(-a) / (stddev * sqrt(2 * PI)); |
| 1084 | } |
| 1085 | |
| 1086 | #if 0 // static unused gives a warning |
| 1087 | static double Pareto(double x, double min, double k) { |
| 1088 | if (x < min) |
| 1089 | return 0; |
| 1090 | else |
| 1091 | return k * std::pow(min, k) / std::pow(x, k+1); |
| 1092 | } |
| 1093 | #endif |
| 1094 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1095 | std::unique_ptr<VirtualSocketServer::Function> |
| 1096 | VirtualSocketServer::CreateDistribution(uint32_t mean, |
| 1097 | uint32_t stddev, |
| 1098 | uint32_t samples) { |
| 1099 | auto f = std::make_unique<Function>(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1100 | |
| 1101 | if (0 == stddev) { |
| 1102 | f->push_back(Point(mean, 1.0)); |
| 1103 | } else { |
| 1104 | double start = 0; |
| 1105 | if (mean >= 4 * static_cast<double>(stddev)) |
| 1106 | start = mean - 4 * static_cast<double>(stddev); |
| 1107 | double end = mean + 4 * static_cast<double>(stddev); |
| 1108 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1109 | for (uint32_t i = 0; i < samples; i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1110 | double x = start + (end - start) * i / (samples - 1); |
| 1111 | double y = Normal(x, mean, stddev); |
| 1112 | f->push_back(Point(x, y)); |
| 1113 | } |
| 1114 | } |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1115 | return Resample(Invert(Accumulate(std::move(f))), 0, 1, samples); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1118 | uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) { |
| 1119 | // Use the delay based on the address if it is set. |
| 1120 | auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr()); |
| 1121 | if (iter != delay_by_ip_.end()) { |
| 1122 | return static_cast<uint32_t>(iter->second); |
| 1123 | } |
| 1124 | // Otherwise, use the delay from the distribution distribution. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1125 | size_t index = rand() % delay_dist_->size(); |
| 1126 | double delay = (*delay_dist_)[index].second; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1127 | // RTC_LOG_F(LS_INFO) << "random[" << index << "] = " << delay; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1128 | return static_cast<uint32_t>(delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | struct FunctionDomainCmp { |
| 1132 | bool operator()(const VirtualSocketServer::Point& p1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1133 | const VirtualSocketServer::Point& p2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1134 | return p1.first < p2.first; |
| 1135 | } |
| 1136 | bool operator()(double v1, const VirtualSocketServer::Point& p2) { |
| 1137 | return v1 < p2.first; |
| 1138 | } |
| 1139 | bool operator()(const VirtualSocketServer::Point& p1, double v2) { |
| 1140 | return p1.first < v2; |
| 1141 | } |
| 1142 | }; |
| 1143 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1144 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate( |
| 1145 | std::unique_ptr<Function> f) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1146 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1147 | double v = 0; |
| 1148 | for (Function::size_type i = 0; i < f->size() - 1; ++i) { |
| 1149 | double dx = (*f)[i + 1].first - (*f)[i].first; |
| 1150 | double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2; |
| 1151 | (*f)[i].second = v; |
| 1152 | v = v + dx * avgy; |
| 1153 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1154 | (*f)[f->size() - 1].second = v; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1155 | return f; |
| 1156 | } |
| 1157 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1158 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert( |
| 1159 | std::unique_ptr<Function> f) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1160 | for (Function::size_type i = 0; i < f->size(); ++i) |
| 1161 | std::swap((*f)[i].first, (*f)[i].second); |
| 1162 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 1163 | absl::c_sort(*f, FunctionDomainCmp()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1164 | return f; |
| 1165 | } |
| 1166 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1167 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample( |
| 1168 | std::unique_ptr<Function> f, |
| 1169 | double x1, |
| 1170 | double x2, |
| 1171 | uint32_t samples) { |
| 1172 | auto g = std::make_unique<Function>(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1173 | |
| 1174 | for (size_t i = 0; i < samples; i++) { |
| 1175 | double x = x1 + (x2 - x1) * i / (samples - 1); |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1176 | double y = Evaluate(f.get(), x); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1177 | g->push_back(Point(x, y)); |
| 1178 | } |
| 1179 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1180 | return g; |
| 1181 | } |
| 1182 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1183 | double VirtualSocketServer::Evaluate(const Function* f, double x) { |
| 1184 | Function::const_iterator iter = |
| 1185 | absl::c_lower_bound(*f, x, FunctionDomainCmp()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1186 | if (iter == f->begin()) { |
| 1187 | return (*f)[0].second; |
| 1188 | } else if (iter == f->end()) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1189 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1190 | return (*f)[f->size() - 1].second; |
| 1191 | } else if (iter->first == x) { |
| 1192 | return iter->second; |
| 1193 | } else { |
| 1194 | double x1 = (iter - 1)->first; |
| 1195 | double y1 = (iter - 1)->second; |
| 1196 | double x2 = iter->first; |
| 1197 | double y2 = iter->second; |
| 1198 | return y1 + (y2 - y1) * (x - x1) / (x2 - x1); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | bool VirtualSocketServer::CanInteractWith(VirtualSocket* local, |
| 1203 | VirtualSocket* remote) { |
| 1204 | if (!local || !remote) { |
| 1205 | return false; |
| 1206 | } |
| 1207 | IPAddress local_ip = local->GetLocalAddress().ipaddr(); |
| 1208 | IPAddress remote_ip = remote->GetLocalAddress().ipaddr(); |
| 1209 | IPAddress local_normalized = local_ip.Normalized(); |
| 1210 | IPAddress remote_normalized = remote_ip.Normalized(); |
| 1211 | // Check if the addresses are the same family after Normalization (turns |
| 1212 | // mapped IPv6 address into IPv4 addresses). |
| 1213 | // This will stop unmapped V6 addresses from talking to mapped V6 addresses. |
| 1214 | if (local_normalized.family() == remote_normalized.family()) { |
| 1215 | return true; |
| 1216 | } |
| 1217 | |
| 1218 | // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY. |
| 1219 | int remote_v6_only = 0; |
| 1220 | remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only); |
| 1221 | if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) { |
| 1222 | return true; |
| 1223 | } |
| 1224 | // Same check, backwards. |
| 1225 | int local_v6_only = 0; |
| 1226 | local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only); |
| 1227 | if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) { |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | // Check to see if either socket was explicitly bound to IPv6-any. |
| 1232 | // These sockets can talk with anyone. |
| 1233 | if (local_ip.family() == AF_INET6 && local->was_any()) { |
| 1234 | return true; |
| 1235 | } |
| 1236 | if (remote_ip.family() == AF_INET6 && remote->was_any()) { |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1243 | IPAddress VirtualSocketServer::GetDefaultRoute(int family) { |
| 1244 | if (family == AF_INET) { |
| 1245 | return default_route_v4_; |
| 1246 | } |
| 1247 | if (family == AF_INET6) { |
| 1248 | return default_route_v6_; |
| 1249 | } |
| 1250 | return IPAddress(); |
| 1251 | } |
| 1252 | void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1253 | RTC_DCHECK(!IPIsAny(from_addr)); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1254 | if (from_addr.family() == AF_INET) { |
| 1255 | default_route_v4_ = from_addr; |
| 1256 | } else if (from_addr.family() == AF_INET6) { |
| 1257 | default_route_v6_ = from_addr; |
| 1258 | } |
| 1259 | } |
| 1260 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1261 | } // namespace rtc |