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