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" |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 +0000 | [diff] [blame^] | 21 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 +0000 | [diff] [blame^] | 23 | #include "rtc_base/event.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "rtc_base/physical_socket_server.h" |
| 27 | #include "rtc_base/socket_address_pair.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/time_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | |
| 31 | namespace rtc { |
| 32 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | const in_addr kInitialNextIPv4 = {{{0x01, 0, 0, 0}}}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | #else |
| 35 | // This value is entirely arbitrary, hence the lack of concern about endianness. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 36 | const in_addr kInitialNextIPv4 = {0x01000000}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | #endif |
| 38 | // Starts at ::2 so as to not cause confusion with ::1. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | const in6_addr kInitialNextIPv6 = { |
| 40 | {{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] | 41 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 42 | const uint16_t kFirstEphemeralPort = 49152; |
| 43 | const uint16_t kLastEphemeralPort = 65535; |
| 44 | const uint16_t kEphemeralPortCount = |
| 45 | kLastEphemeralPort - kFirstEphemeralPort + 1; |
| 46 | const uint32_t kDefaultNetworkCapacity = 64 * 1024; |
| 47 | const uint32_t kDefaultTcpBufferSize = 32 * 1024; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 49 | const uint32_t UDP_HEADER_SIZE = 28; // IP + UDP headers |
| 50 | const uint32_t TCP_HEADER_SIZE = 40; // IP + TCP headers |
| 51 | const uint32_t TCP_MSS = 1400; // Maximum segment size |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | |
| 53 | // Note: The current algorithm doesn't work for sample sizes smaller than this. |
| 54 | const int NUM_SAMPLES = 1000; |
| 55 | |
| 56 | enum { |
| 57 | MSG_ID_PACKET, |
| 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 | |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 97 | VirtualSocket::VirtualSocket(VirtualSocketServer* server, int family, int type) |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 98 | : server_(server), |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 99 | type_(type), |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 100 | state_(CS_CLOSED), |
| 101 | error_(0), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 102 | listen_queue_(nullptr), |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 103 | network_size_(0), |
| 104 | recv_buffer_size_(0), |
| 105 | bound_(false), |
| 106 | was_any_(false) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 107 | RTC_DCHECK((type_ == SOCK_DGRAM) || (type_ == SOCK_STREAM)); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 108 | server->SignalReadyToSend.connect(this, |
| 109 | &VirtualSocket::OnSocketServerReadyToSend); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | VirtualSocket::~VirtualSocket() { |
| 113 | Close(); |
| 114 | |
| 115 | for (RecvBuffer::iterator it = recv_buffer_.begin(); it != recv_buffer_.end(); |
| 116 | ++it) { |
| 117 | delete *it; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 119 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 121 | SocketAddress VirtualSocket::GetLocalAddress() const { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 122 | return local_addr_; |
| 123 | } |
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 | SocketAddress VirtualSocket::GetRemoteAddress() const { |
| 126 | return remote_addr_; |
| 127 | } |
| 128 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 129 | void VirtualSocket::SetLocalAddress(const SocketAddress& addr) { |
| 130 | local_addr_ = addr; |
| 131 | } |
| 132 | |
| 133 | int VirtualSocket::Bind(const SocketAddress& addr) { |
| 134 | if (!local_addr_.IsNil()) { |
| 135 | error_ = EINVAL; |
| 136 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | } |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 138 | local_addr_ = server_->AssignBindAddress(addr); |
| 139 | int result = server_->Bind(this, local_addr_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 140 | if (result != 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | local_addr_.Clear(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 142 | error_ = EADDRINUSE; |
| 143 | } else { |
| 144 | bound_ = true; |
| 145 | was_any_ = addr.IsAnyIP(); |
| 146 | } |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | int VirtualSocket::Connect(const SocketAddress& addr) { |
| 151 | return InitiateConnect(addr, true); |
| 152 | } |
| 153 | |
| 154 | int VirtualSocket::Close() { |
| 155 | if (!local_addr_.IsNil() && bound_) { |
| 156 | // Remove from the binding table. |
| 157 | server_->Unbind(local_addr_, this); |
| 158 | bound_ = false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | } |
| 160 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 161 | if (SOCK_STREAM == type_) { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 162 | webrtc::MutexLock lock(&mutex_); |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 163 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 164 | // Cancel pending sockets |
| 165 | if (listen_queue_) { |
| 166 | while (!listen_queue_->empty()) { |
| 167 | SocketAddress addr = listen_queue_->front(); |
| 168 | |
| 169 | // Disconnect listening socket. |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 170 | server_->Disconnect(addr); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 171 | listen_queue_->pop_front(); |
| 172 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 173 | listen_queue_ = nullptr; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 174 | } |
| 175 | // Disconnect stream sockets |
| 176 | if (CS_CONNECTED == state_) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 177 | server_->Disconnect(local_addr_, remote_addr_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 178 | } |
| 179 | // Cancel potential connects |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 180 | server_->CancelConnects(this); |
Tomas Gunnarsson | d966347 | 2020-11-21 16:20:23 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Clear incoming packets and disconnect messages |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 184 | server_->Clear(this); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 185 | |
| 186 | state_ = CS_CLOSED; |
| 187 | local_addr_.Clear(); |
| 188 | remote_addr_.Clear(); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | int VirtualSocket::Send(const void* pv, size_t cb) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 193 | if (CS_CONNECTED != state_) { |
| 194 | error_ = ENOTCONN; |
| 195 | return -1; |
| 196 | } |
| 197 | if (SOCK_DGRAM == type_) { |
| 198 | return SendUdp(pv, cb, remote_addr_); |
| 199 | } else { |
| 200 | return SendTcp(pv, cb); |
| 201 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 202 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 203 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 204 | int VirtualSocket::SendTo(const void* pv, |
| 205 | size_t cb, |
| 206 | const SocketAddress& addr) { |
| 207 | if (SOCK_DGRAM == type_) { |
| 208 | return SendUdp(pv, cb, addr); |
| 209 | } else { |
| 210 | if (CS_CONNECTED != state_) { |
| 211 | error_ = ENOTCONN; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 212 | return -1; |
| 213 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 214 | return SendTcp(pv, cb); |
| 215 | } |
| 216 | } |
| 217 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 218 | int VirtualSocket::Recv(void* pv, size_t cb, int64_t* timestamp) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 219 | SocketAddress addr; |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 220 | return RecvFrom(pv, cb, &addr, timestamp); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 223 | int VirtualSocket::RecvFrom(void* pv, |
| 224 | size_t cb, |
| 225 | SocketAddress* paddr, |
| 226 | int64_t* timestamp) { |
| 227 | if (timestamp) { |
| 228 | *timestamp = -1; |
| 229 | } |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 230 | |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 231 | webrtc::MutexLock lock(&mutex_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 232 | // If we don't have a packet, then either error or wait for one to arrive. |
| 233 | if (recv_buffer_.empty()) { |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 234 | error_ = EAGAIN; |
| 235 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 236 | } |
| 237 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 238 | // Return the packet at the front of the queue. |
| 239 | Packet* packet = recv_buffer_.front(); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 240 | size_t data_read = std::min(cb, packet->size()); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 241 | memcpy(pv, packet->data(), data_read); |
| 242 | *paddr = packet->from(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 243 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 244 | if (data_read < packet->size()) { |
| 245 | packet->Consume(data_read); |
| 246 | } else { |
| 247 | recv_buffer_.pop_front(); |
| 248 | delete packet; |
| 249 | } |
| 250 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 251 | // To behave like a real socket, SignalReadEvent should fire in the next |
| 252 | // message loop pass if there's still data buffered. |
| 253 | if (!recv_buffer_.empty()) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 254 | server_->PostSignalReadEvent(this); |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 255 | } |
| 256 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 257 | if (SOCK_STREAM == type_) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 258 | bool was_full = (recv_buffer_size_ == server_->recv_buffer_capacity()); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 259 | recv_buffer_size_ -= data_read; |
| 260 | if (was_full) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 261 | server_->SendTcp(remote_addr_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 262 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | return static_cast<int>(data_read); |
| 266 | } |
| 267 | |
| 268 | int VirtualSocket::Listen(int backlog) { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 269 | webrtc::MutexLock lock(&mutex_); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 270 | RTC_DCHECK(SOCK_STREAM == type_); |
| 271 | RTC_DCHECK(CS_CLOSED == state_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 272 | if (local_addr_.IsNil()) { |
| 273 | error_ = EINVAL; |
| 274 | return -1; |
| 275 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 276 | RTC_DCHECK(nullptr == listen_queue_); |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 277 | listen_queue_ = std::make_unique<ListenQueue>(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 278 | state_ = CS_CONNECTING; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | VirtualSocket* VirtualSocket::Accept(SocketAddress* paddr) { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 283 | webrtc::MutexLock lock(&mutex_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 284 | if (nullptr == listen_queue_) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 285 | error_ = EINVAL; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 286 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 287 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 288 | while (!listen_queue_->empty()) { |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 289 | VirtualSocket* socket = new VirtualSocket(server_, AF_INET, type_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 290 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 291 | // Set the new local address to the same as this server socket. |
| 292 | socket->SetLocalAddress(local_addr_); |
| 293 | // Sockets made from a socket that 'was Any' need to inherit that. |
| 294 | socket->set_was_any(was_any_); |
| 295 | SocketAddress remote_addr(listen_queue_->front()); |
| 296 | int result = socket->InitiateConnect(remote_addr, false); |
| 297 | listen_queue_->pop_front(); |
| 298 | if (result != 0) { |
| 299 | delete socket; |
| 300 | continue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 301 | } |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 302 | socket->CompleteConnect(remote_addr); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 303 | if (paddr) { |
| 304 | *paddr = remote_addr; |
| 305 | } |
| 306 | return socket; |
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 | error_ = EWOULDBLOCK; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 309 | return nullptr; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 310 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 311 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 312 | int VirtualSocket::GetError() const { |
| 313 | return error_; |
| 314 | } |
| 315 | |
| 316 | void VirtualSocket::SetError(int error) { |
| 317 | error_ = error; |
| 318 | } |
| 319 | |
| 320 | Socket::ConnState VirtualSocket::GetState() const { |
| 321 | return state_; |
| 322 | } |
| 323 | |
| 324 | int VirtualSocket::GetOption(Option opt, int* value) { |
| 325 | OptionsMap::const_iterator it = options_map_.find(opt); |
| 326 | if (it == options_map_.end()) { |
| 327 | return -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 328 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 329 | *value = it->second; |
| 330 | return 0; // 0 is success to emulate getsockopt() |
| 331 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 332 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 333 | int VirtualSocket::SetOption(Option opt, int value) { |
| 334 | options_map_[opt] = value; |
| 335 | return 0; // 0 is success to emulate setsockopt() |
| 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 | void VirtualSocket::OnMessage(Message* pmsg) { |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 339 | bool signal_read_event = false; |
| 340 | bool signal_close_event = false; |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 341 | bool signal_connect_event = false; |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 342 | int error_to_signal = 0; |
| 343 | { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 344 | webrtc::MutexLock lock(&mutex_); |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 345 | if (pmsg->message_id == MSG_ID_PACKET) { |
| 346 | RTC_DCHECK(nullptr != pmsg->pdata); |
| 347 | Packet* packet = static_cast<Packet*>(pmsg->pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 348 | |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 349 | recv_buffer_.push_back(packet); |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 350 | signal_read_event = true; |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 351 | } else if (pmsg->message_id == MSG_ID_CONNECT) { |
| 352 | RTC_DCHECK(nullptr != pmsg->pdata); |
| 353 | MessageAddress* data = static_cast<MessageAddress*>(pmsg->pdata); |
| 354 | if (listen_queue_ != nullptr) { |
| 355 | listen_queue_->push_back(data->addr); |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 356 | signal_read_event = true; |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 357 | } else if ((SOCK_STREAM == type_) && (CS_CONNECTING == state_)) { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 358 | CompleteConnect(data->addr); |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 359 | signal_connect_event = true; |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 360 | } else { |
| 361 | RTC_LOG(LS_VERBOSE) |
| 362 | << "Socket at " << local_addr_.ToString() << " is not listening"; |
| 363 | server_->Disconnect(data->addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 364 | } |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 365 | delete data; |
| 366 | } else if (pmsg->message_id == MSG_ID_DISCONNECT) { |
| 367 | RTC_DCHECK(SOCK_STREAM == type_); |
| 368 | if (CS_CLOSED != state_) { |
| 369 | error_to_signal = (CS_CONNECTING == state_) ? ECONNREFUSED : 0; |
| 370 | state_ = CS_CLOSED; |
| 371 | remote_addr_.Clear(); |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 372 | signal_close_event = true; |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 373 | } |
| 374 | } else if (pmsg->message_id == MSG_ID_SIGNALREADEVENT) { |
| 375 | signal_read_event = !recv_buffer_.empty(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 376 | } else { |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 377 | RTC_DCHECK_NOTREACHED(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 378 | } |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 379 | } |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 380 | // Signal events without holding `mutex_`, to avoid recursive locking, as well |
| 381 | // as issues with sigslot and lock order. |
Niels Möller | 257f81b | 2021-06-17 16:58:59 +0200 | [diff] [blame] | 382 | if (signal_read_event) { |
| 383 | SignalReadEvent(this); |
| 384 | } |
| 385 | if (signal_close_event) { |
| 386 | SignalCloseEvent(this, error_to_signal); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 387 | } |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 388 | if (signal_connect_event) { |
| 389 | SignalConnectEvent(this); |
| 390 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | int VirtualSocket::InitiateConnect(const SocketAddress& addr, bool use_delay) { |
| 394 | if (!remote_addr_.IsNil()) { |
| 395 | error_ = (CS_CONNECTED == state_) ? EISCONN : EINPROGRESS; |
| 396 | return -1; |
| 397 | } |
| 398 | if (local_addr_.IsNil()) { |
| 399 | // If there's no local address set, grab a random one in the correct AF. |
| 400 | int result = 0; |
| 401 | if (addr.ipaddr().family() == AF_INET) { |
| 402 | result = Bind(SocketAddress("0.0.0.0", 0)); |
| 403 | } else if (addr.ipaddr().family() == AF_INET6) { |
| 404 | result = Bind(SocketAddress("::", 0)); |
| 405 | } |
| 406 | if (result != 0) { |
| 407 | return result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 410 | if (type_ == SOCK_DGRAM) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 411 | remote_addr_ = addr; |
| 412 | state_ = CS_CONNECTED; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 413 | } else { |
| 414 | int result = server_->Connect(this, addr, use_delay); |
| 415 | if (result != 0) { |
| 416 | error_ = EHOSTUNREACH; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 417 | return -1; |
| 418 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 419 | state_ = CS_CONNECTING; |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 424 | void VirtualSocket::CompleteConnect(const SocketAddress& addr) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 425 | RTC_DCHECK(CS_CONNECTING == state_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 426 | remote_addr_ = addr; |
| 427 | state_ = CS_CONNECTED; |
| 428 | server_->AddConnection(remote_addr_, local_addr_, this); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | int VirtualSocket::SendUdp(const void* pv, |
| 432 | size_t cb, |
| 433 | const SocketAddress& addr) { |
| 434 | // If we have not been assigned a local port, then get one. |
| 435 | if (local_addr_.IsNil()) { |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 436 | local_addr_ = server_->AssignBindAddress( |
| 437 | EmptySocketAddressWithFamily(addr.ipaddr().family())); |
| 438 | int result = server_->Bind(this, local_addr_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 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() { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 481 | webrtc::MutexLock lock(&mutex_); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 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) { |
Niels Möller | c413c55 | 2021-06-22 10:03:14 +0200 | [diff] [blame] | 531 | webrtc::MutexLock lock(&mutex_); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 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) { |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 593 | { |
| 594 | webrtc::MutexLock lock(&mutex_); |
| 595 | if (blocked == sending_blocked_) { |
| 596 | // Unchanged; nothing to do. |
| 597 | return; |
| 598 | } |
| 599 | sending_blocked_ = blocked; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 600 | } |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 601 | if (!blocked) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 602 | // Sending was blocked, but is now unblocked. This signal gives sockets a |
| 603 | // chance to fire SignalWriteEvent, and for TCP, send buffered data. |
| 604 | SignalReadyToSend(); |
| 605 | } |
| 606 | } |
| 607 | |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 608 | VirtualSocket* VirtualSocketServer::CreateSocket(int family, int type) { |
| 609 | return new VirtualSocket(this, family, type); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Sebastian Jansson | 290de82 | 2020-01-09 14:20:23 +0100 | [diff] [blame] | 612 | void VirtualSocketServer::SetMessageQueue(Thread* msg_queue) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 613 | msg_queue_ = msg_queue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | bool VirtualSocketServer::Wait(int cmsWait, bool process_io) { |
Tommi | ccc9d97 | 2022-03-24 08:12:36 +0100 | [diff] [blame] | 617 | RTC_DCHECK_RUN_ON(msg_queue_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 618 | if (stop_on_idle_ && Thread::Current()->empty()) { |
| 619 | return false; |
| 620 | } |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 621 | // Note: we don't need to do anything with `process_io` since we don't have |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 622 | // 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] | 623 | // Thread will ensure WakeUp is called if another thread sends a |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 624 | // packet. |
Markus Handell | 2cfc1af | 2022-08-19 08:16:48 +0000 | [diff] [blame^] | 625 | wakeup_.Wait(cmsWait == kForever ? Event::kForever |
| 626 | : webrtc::TimeDelta::Millis(cmsWait)); |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 627 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void VirtualSocketServer::WakeUp() { |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 631 | wakeup_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 632 | } |
| 633 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 634 | void VirtualSocketServer::SetAlternativeLocalAddress( |
| 635 | const rtc::IPAddress& address, |
| 636 | const rtc::IPAddress& alternative) { |
| 637 | alternative_address_mapping_[address] = alternative; |
| 638 | } |
| 639 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 640 | bool VirtualSocketServer::ProcessMessagesUntilIdle() { |
Tommi | ccc9d97 | 2022-03-24 08:12:36 +0100 | [diff] [blame] | 641 | RTC_DCHECK_RUN_ON(msg_queue_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 642 | stop_on_idle_ = true; |
| 643 | while (!msg_queue_->empty()) { |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 644 | if (fake_clock_) { |
| 645 | // If using a fake clock, advance it in millisecond increments until the |
Bjorn Mellem | 6eb03b8 | 2017-06-13 15:07:41 -0700 | [diff] [blame] | 646 | // queue is empty. |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 647 | fake_clock_->AdvanceTime(webrtc::TimeDelta::Millis(1)); |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 648 | } else { |
| 649 | // Otherwise, run a normal message loop. |
| 650 | Message msg; |
| 651 | if (msg_queue_->Get(&msg, Thread::kForever)) { |
| 652 | msg_queue_->Dispatch(&msg); |
| 653 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | stop_on_idle_ = false; |
| 657 | return !msg_queue_->IsQuitting(); |
| 658 | } |
| 659 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 660 | void VirtualSocketServer::SetNextPortForTesting(uint16_t port) { |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 +0000 | [diff] [blame] | 661 | next_port_ = port; |
| 662 | } |
| 663 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 664 | bool VirtualSocketServer::CloseTcpConnections( |
| 665 | const SocketAddress& addr_local, |
| 666 | const SocketAddress& addr_remote) { |
| 667 | VirtualSocket* socket = LookupConnection(addr_local, addr_remote); |
| 668 | if (!socket) { |
| 669 | return false; |
| 670 | } |
| 671 | // Signal the close event on the local connection first. |
| 672 | socket->SignalCloseEvent(socket, 0); |
| 673 | |
| 674 | // Trigger the remote connection's close event. |
| 675 | socket->Close(); |
| 676 | |
| 677 | return true; |
| 678 | } |
| 679 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 680 | int VirtualSocketServer::Bind(VirtualSocket* socket, |
| 681 | const SocketAddress& addr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 682 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 683 | // Address must be completely specified at this point |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 684 | RTC_DCHECK(!IPIsUnspec(addr.ipaddr())); |
| 685 | RTC_DCHECK(addr.port() != 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 686 | |
| 687 | // Normalize the address (turns v6-mapped addresses into v4-addresses). |
| 688 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
| 689 | |
| 690 | AddressMap::value_type entry(normalized, socket); |
Niels Möller | d44532a | 2021-02-18 14:38:14 +0100 | [diff] [blame] | 691 | return bindings_->insert(entry).second ? 0 : -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 694 | SocketAddress VirtualSocketServer::AssignBindAddress( |
| 695 | const SocketAddress& app_addr) { |
| 696 | RTC_DCHECK(!IPIsUnspec(app_addr.ipaddr())); |
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. |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 699 | SocketAddress addr; |
| 700 | addr.SetIP(app_addr.ipaddr().Normalized()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 701 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 702 | // If the IP appears in `alternative_address_mapping_`, meaning the test has |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 703 | // configured sockets bound to this IP to actually use another IP, replace |
| 704 | // the IP here. |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 705 | auto alternative = alternative_address_mapping_.find(addr.ipaddr()); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 706 | if (alternative != alternative_address_mapping_.end()) { |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 707 | addr.SetIP(alternative->second); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 710 | if (app_addr.port() != 0) { |
| 711 | addr.SetPort(app_addr.port()); |
| 712 | } else { |
| 713 | // Assign a port. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 714 | for (int i = 0; i < kEphemeralPortCount; ++i) { |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 715 | addr.SetPort(GetNextPort()); |
| 716 | if (bindings_->find(addr) == bindings_->end()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Niels Möller | c2d8f1e | 2021-08-24 15:49:34 +0200 | [diff] [blame] | 722 | return addr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 726 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 727 | AddressMap::iterator it = bindings_->find(normalized); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 728 | if (it != bindings_->end()) { |
| 729 | return it->second; |
| 730 | } |
| 731 | |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 732 | IPAddress default_ip = GetDefaultSourceAddress(addr.ipaddr().family()); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 733 | if (!IPIsUnspec(default_ip) && addr.ipaddr() == default_ip) { |
| 734 | // If we can't find a binding for the packet which is sent to the interface |
| 735 | // corresponding to the default route, it should match a binding with the |
| 736 | // correct port to the any address. |
| 737 | SocketAddress sock_addr = |
| 738 | EmptySocketAddressWithFamily(addr.ipaddr().family()); |
| 739 | sock_addr.SetPort(addr.port()); |
| 740 | return LookupBinding(sock_addr); |
| 741 | } |
| 742 | |
| 743 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | int VirtualSocketServer::Unbind(const SocketAddress& addr, |
| 747 | VirtualSocket* socket) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 748 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 749 | RTC_DCHECK((*bindings_)[normalized] == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 750 | bindings_->erase(bindings_->find(normalized)); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | void VirtualSocketServer::AddConnection(const SocketAddress& local, |
| 755 | const SocketAddress& remote, |
| 756 | VirtualSocket* remote_socket) { |
| 757 | // Add this socket pair to our routing table. This will allow |
| 758 | // multiple clients to connect to the same server address. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 759 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 760 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 761 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 762 | connections_->insert(std::pair<SocketAddressPair, VirtualSocket*>( |
| 763 | address_pair, remote_socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | VirtualSocket* VirtualSocketServer::LookupConnection( |
| 767 | const SocketAddress& local, |
| 768 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 769 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 770 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 771 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 772 | ConnectionMap::iterator it = connections_->find(address_pair); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 773 | return (connections_->end() != it) ? it->second : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void VirtualSocketServer::RemoveConnection(const SocketAddress& local, |
| 777 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 778 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 779 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 780 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 781 | connections_->erase(address_pair); |
| 782 | } |
| 783 | |
| 784 | static double Random() { |
| 785 | return static_cast<double>(rand()) / RAND_MAX; |
| 786 | } |
| 787 | |
| 788 | int VirtualSocketServer::Connect(VirtualSocket* socket, |
| 789 | const SocketAddress& remote_addr, |
| 790 | bool use_delay) { |
Tommi | ccc9d97 | 2022-03-24 08:12:36 +0100 | [diff] [blame] | 791 | RTC_DCHECK(msg_queue_); |
| 792 | |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 793 | uint32_t delay = use_delay ? GetTransitDelay(socket) : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 794 | VirtualSocket* remote = LookupBinding(remote_addr); |
| 795 | if (!CanInteractWith(socket, remote)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 796 | RTC_LOG(LS_INFO) << "Address family mismatch between " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 797 | << socket->GetLocalAddress().ToString() << " and " |
| 798 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 799 | return -1; |
| 800 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 801 | if (remote != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 802 | SocketAddress addr = socket->GetLocalAddress(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 803 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 804 | new MessageAddress(addr)); |
| 805 | } else { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 806 | RTC_LOG(LS_INFO) << "No one listening at " << remote_addr.ToString(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 807 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 808 | } |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { |
Tommi | ccc9d97 | 2022-03-24 08:12:36 +0100 | [diff] [blame] | 813 | if (!socket || !msg_queue_) |
| 814 | return false; |
| 815 | |
| 816 | // If we simulate packets being delayed, we should simulate the |
| 817 | // equivalent of a FIN being delayed as well. |
| 818 | uint32_t delay = GetTransitDelay(socket); |
| 819 | // Remove the mapping. |
| 820 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
| 821 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 824 | bool VirtualSocketServer::Disconnect(const SocketAddress& addr) { |
| 825 | return Disconnect(LookupBinding(addr)); |
| 826 | } |
| 827 | |
| 828 | bool VirtualSocketServer::Disconnect(const SocketAddress& local_addr, |
| 829 | const SocketAddress& remote_addr) { |
| 830 | // Disconnect remote socket, check if it is a child of a server socket. |
| 831 | VirtualSocket* socket = LookupConnection(local_addr, remote_addr); |
| 832 | if (!socket) { |
| 833 | // Not a server socket child, then see if it is bound. |
| 834 | // TODO(tbd): If this is indeed a server socket that has no |
| 835 | // children this will cause the server socket to be |
| 836 | // closed. This might lead to unexpected results, how to fix this? |
| 837 | socket = LookupBinding(remote_addr); |
| 838 | } |
| 839 | Disconnect(socket); |
| 840 | |
| 841 | // Remove mapping for both directions. |
| 842 | RemoveConnection(remote_addr, local_addr); |
| 843 | RemoveConnection(local_addr, remote_addr); |
| 844 | return socket != nullptr; |
| 845 | } |
| 846 | |
| 847 | void VirtualSocketServer::CancelConnects(VirtualSocket* socket) { |
| 848 | MessageList msgs; |
| 849 | if (msg_queue_) { |
| 850 | msg_queue_->Clear(socket, MSG_ID_CONNECT, &msgs); |
| 851 | } |
| 852 | for (MessageList::iterator it = msgs.begin(); it != msgs.end(); ++it) { |
| 853 | RTC_DCHECK(nullptr != it->pdata); |
| 854 | MessageAddress* data = static_cast<MessageAddress*>(it->pdata); |
| 855 | SocketAddress local_addr = socket->GetLocalAddress(); |
| 856 | // Lookup remote side. |
Mirko Bonadei | 54c90f2 | 2021-10-03 11:26:11 +0200 | [diff] [blame] | 857 | VirtualSocket* lookup_socket = LookupConnection(local_addr, data->addr); |
| 858 | if (lookup_socket) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 859 | // Server socket, remote side is a socket retreived by |
| 860 | // accept. Accepted sockets are not bound so we will not |
| 861 | // find it by looking in the bindings table. |
Mirko Bonadei | 54c90f2 | 2021-10-03 11:26:11 +0200 | [diff] [blame] | 862 | Disconnect(lookup_socket); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 863 | RemoveConnection(local_addr, data->addr); |
| 864 | } else { |
| 865 | Disconnect(data->addr); |
| 866 | } |
| 867 | delete data; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | void VirtualSocketServer::Clear(VirtualSocket* socket) { |
| 872 | // Clear incoming packets and disconnect messages |
| 873 | if (msg_queue_) { |
| 874 | msg_queue_->Clear(socket); |
| 875 | } |
| 876 | } |
| 877 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 878 | void VirtualSocketServer::PostSignalReadEvent(VirtualSocket* socket) { |
Tommi | ccc9d97 | 2022-03-24 08:12:36 +0100 | [diff] [blame] | 879 | if (!msg_queue_) |
| 880 | return; |
| 881 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 882 | // Clear the message so it doesn't end up posted multiple times. |
| 883 | msg_queue_->Clear(socket, MSG_ID_SIGNALREADEVENT); |
| 884 | msg_queue_->Post(RTC_FROM_HERE, socket, MSG_ID_SIGNALREADEVENT); |
| 885 | } |
| 886 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 887 | int VirtualSocketServer::SendUdp(VirtualSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 888 | const char* data, |
| 889 | size_t data_size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 890 | const SocketAddress& remote_addr) { |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 891 | { |
| 892 | webrtc::MutexLock lock(&mutex_); |
| 893 | ++sent_packets_; |
| 894 | if (sending_blocked_) { |
| 895 | socket->SetToBlocked(); |
| 896 | return -1; |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 897 | } |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 898 | |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 899 | // See if we want to drop this packet. |
| 900 | if (data_size > max_udp_payload_) { |
| 901 | RTC_LOG(LS_VERBOSE) << "Dropping too large UDP payload of size " |
| 902 | << data_size << ", UDP payload limit is " |
| 903 | << max_udp_payload_; |
| 904 | // Return as if send was successful; packet disappears. |
| 905 | return data_size; |
| 906 | } |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 907 | |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 908 | if (Random() < drop_prob_) { |
| 909 | RTC_LOG(LS_VERBOSE) << "Dropping packet: bad luck"; |
| 910 | return static_cast<int>(data_size); |
| 911 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | VirtualSocket* recipient = LookupBinding(remote_addr); |
| 915 | if (!recipient) { |
| 916 | // Make a fake recipient for address family checking. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 917 | std::unique_ptr<VirtualSocket> dummy_socket( |
Niels Möller | ea423a5 | 2021-08-19 10:13:31 +0200 | [diff] [blame] | 918 | CreateSocket(AF_INET, SOCK_DGRAM)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 919 | dummy_socket->SetLocalAddress(remote_addr); |
| 920 | if (!CanInteractWith(socket, dummy_socket.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 921 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 922 | << socket->GetLocalAddress().ToString() << " and " |
| 923 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 924 | return -1; |
| 925 | } |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 926 | RTC_LOG(LS_VERBOSE) << "No one listening at " << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 927 | return static_cast<int>(data_size); |
| 928 | } |
| 929 | |
| 930 | if (!CanInteractWith(socket, recipient)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 931 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 932 | << socket->GetLocalAddress().ToString() << " and " |
| 933 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 934 | return -1; |
| 935 | } |
| 936 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 937 | { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 938 | int64_t cur_time = TimeMillis(); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 939 | size_t network_size = socket->PurgeNetworkPackets(cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 940 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 941 | // Determine whether we have enough bandwidth to accept this packet. To do |
| 942 | // this, we need to update the send queue. Once we know it's current size, |
| 943 | // we know whether we can fit this packet. |
| 944 | // |
| 945 | // NOTE: There are better algorithms for maintaining such a queue (such as |
| 946 | // "Derivative Random Drop"); however, this algorithm is a more accurate |
| 947 | // simulation of what a normal network would do. |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 948 | { |
| 949 | webrtc::MutexLock lock(&mutex_); |
| 950 | size_t packet_size = data_size + UDP_HEADER_SIZE; |
| 951 | if (network_size + packet_size > network_capacity_) { |
| 952 | RTC_LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded"; |
| 953 | return static_cast<int>(data_size); |
| 954 | } |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | AddPacketToNetwork(socket, recipient, cur_time, data, data_size, |
| 958 | UDP_HEADER_SIZE, false); |
| 959 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 960 | return static_cast<int>(data_size); |
| 961 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | void VirtualSocketServer::SendTcp(VirtualSocket* socket) { |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 965 | { |
| 966 | webrtc::MutexLock lock(&mutex_); |
| 967 | ++sent_packets_; |
| 968 | if (sending_blocked_) { |
| 969 | // Eventually the socket's buffer will fill and VirtualSocket::SendTcp |
| 970 | // will set EWOULDBLOCK. |
| 971 | return; |
| 972 | } |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 973 | } |
| 974 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 975 | // TCP can't send more data than will fill up the receiver's buffer. |
| 976 | // We track the data that is in the buffer plus data in flight using the |
| 977 | // recipient's recv_buffer_size_. Anything beyond that must be stored in the |
| 978 | // sender's buffer. We will trigger the buffered data to be sent when data |
| 979 | // is read from the recv_buffer. |
| 980 | |
| 981 | // Lookup the local/remote pair in the connections table. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 982 | VirtualSocket* recipient = |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 983 | LookupConnection(socket->GetLocalAddress(), socket->GetRemoteAddress()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 984 | if (!recipient) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 985 | RTC_LOG(LS_VERBOSE) << "Sending data to no one."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 986 | return; |
| 987 | } |
| 988 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 989 | int64_t cur_time = TimeMillis(); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 990 | socket->PurgeNetworkPackets(cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 991 | |
| 992 | while (true) { |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 993 | size_t available = recv_buffer_capacity() - recipient->recv_buffer_size(); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 994 | size_t max_data_size = |
| 995 | std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 996 | 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] | 997 | if (0 == data_size) |
| 998 | break; |
| 999 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1000 | AddPacketToNetwork(socket, recipient, cur_time, socket->send_buffer_data(), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1001 | data_size, TCP_HEADER_SIZE, true); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1002 | recipient->UpdateRecv(data_size); |
| 1003 | socket->UpdateSend(data_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 1006 | socket->MaybeSignalWriteEvent(send_buffer_capacity()); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | void VirtualSocketServer::SendTcp(const SocketAddress& addr) { |
| 1010 | VirtualSocket* sender = LookupBinding(addr); |
| 1011 | RTC_DCHECK(nullptr != sender); |
| 1012 | SendTcp(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender, |
| 1016 | VirtualSocket* recipient, |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1017 | int64_t cur_time, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1018 | const char* data, |
| 1019 | size_t data_size, |
| 1020 | size_t header_size, |
| 1021 | bool ordered) { |
Tommi | ccc9d97 | 2022-03-24 08:12:36 +0100 | [diff] [blame] | 1022 | RTC_DCHECK(msg_queue_); |
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(); |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1032 | IPAddress default_ip = GetDefaultSourceAddress(sender_addr.ipaddr().family()); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 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) { |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 1048 | webrtc::MutexLock lock(&mutex_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1049 | if (bandwidth_ == 0) |
| 1050 | return 0; |
| 1051 | else |
| 1052 | return 1000 * size / bandwidth_; |
| 1053 | } |
| 1054 | |
| 1055 | #if 0 |
| 1056 | void PrintFunction(std::vector<std::pair<double, double> >* f) { |
| 1057 | return; |
| 1058 | double sum = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1059 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1060 | std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl; |
| 1061 | sum += (*f)[i].second; |
| 1062 | } |
| 1063 | if (!f->empty()) { |
| 1064 | const double mean = sum / f->size(); |
| 1065 | double sum_sq_dev = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1066 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1067 | double dev = (*f)[i].second - mean; |
| 1068 | sum_sq_dev += dev * dev; |
| 1069 | } |
| 1070 | std::cout << "Mean = " << mean << " StdDev = " |
| 1071 | << sqrt(sum_sq_dev / f->size()) << std::endl; |
| 1072 | } |
| 1073 | } |
| 1074 | #endif // <unused> |
| 1075 | |
| 1076 | void VirtualSocketServer::UpdateDelayDistribution() { |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 1077 | webrtc::MutexLock lock(&mutex_); |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1078 | delay_dist_ = CreateDistribution(delay_mean_, delay_stddev_, delay_samples_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static double PI = 4 * atan(1.0); |
| 1082 | |
| 1083 | static double Normal(double x, double mean, double stddev) { |
| 1084 | double a = (x - mean) * (x - mean) / (2 * stddev * stddev); |
| 1085 | return exp(-a) / (stddev * sqrt(2 * PI)); |
| 1086 | } |
| 1087 | |
| 1088 | #if 0 // static unused gives a warning |
| 1089 | static double Pareto(double x, double min, double k) { |
| 1090 | if (x < min) |
| 1091 | return 0; |
| 1092 | else |
| 1093 | return k * std::pow(min, k) / std::pow(x, k+1); |
| 1094 | } |
| 1095 | #endif |
| 1096 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1097 | std::unique_ptr<VirtualSocketServer::Function> |
| 1098 | VirtualSocketServer::CreateDistribution(uint32_t mean, |
| 1099 | uint32_t stddev, |
| 1100 | uint32_t samples) { |
| 1101 | auto f = std::make_unique<Function>(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1102 | |
| 1103 | if (0 == stddev) { |
| 1104 | f->push_back(Point(mean, 1.0)); |
| 1105 | } else { |
| 1106 | double start = 0; |
| 1107 | if (mean >= 4 * static_cast<double>(stddev)) |
| 1108 | start = mean - 4 * static_cast<double>(stddev); |
| 1109 | double end = mean + 4 * static_cast<double>(stddev); |
| 1110 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1111 | for (uint32_t i = 0; i < samples; i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1112 | double x = start + (end - start) * i / (samples - 1); |
| 1113 | double y = Normal(x, mean, stddev); |
| 1114 | f->push_back(Point(x, y)); |
| 1115 | } |
| 1116 | } |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1117 | return Resample(Invert(Accumulate(std::move(f))), 0, 1, samples); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1120 | uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) { |
| 1121 | // Use the delay based on the address if it is set. |
| 1122 | auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr()); |
| 1123 | if (iter != delay_by_ip_.end()) { |
| 1124 | return static_cast<uint32_t>(iter->second); |
| 1125 | } |
| 1126 | // Otherwise, use the delay from the distribution distribution. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1127 | size_t index = rand() % delay_dist_->size(); |
| 1128 | double delay = (*delay_dist_)[index].second; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1129 | // RTC_LOG_F(LS_INFO) << "random[" << index << "] = " << delay; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1130 | return static_cast<uint32_t>(delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | struct FunctionDomainCmp { |
| 1134 | bool operator()(const VirtualSocketServer::Point& p1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1135 | const VirtualSocketServer::Point& p2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1136 | return p1.first < p2.first; |
| 1137 | } |
| 1138 | bool operator()(double v1, const VirtualSocketServer::Point& p2) { |
| 1139 | return v1 < p2.first; |
| 1140 | } |
| 1141 | bool operator()(const VirtualSocketServer::Point& p1, double v2) { |
| 1142 | return p1.first < v2; |
| 1143 | } |
| 1144 | }; |
| 1145 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1146 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate( |
| 1147 | std::unique_ptr<Function> f) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1148 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1149 | double v = 0; |
| 1150 | for (Function::size_type i = 0; i < f->size() - 1; ++i) { |
| 1151 | double dx = (*f)[i + 1].first - (*f)[i].first; |
| 1152 | double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2; |
| 1153 | (*f)[i].second = v; |
| 1154 | v = v + dx * avgy; |
| 1155 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1156 | (*f)[f->size() - 1].second = v; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1157 | return f; |
| 1158 | } |
| 1159 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1160 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert( |
| 1161 | std::unique_ptr<Function> f) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1162 | for (Function::size_type i = 0; i < f->size(); ++i) |
| 1163 | std::swap((*f)[i].first, (*f)[i].second); |
| 1164 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 1165 | absl::c_sort(*f, FunctionDomainCmp()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1166 | return f; |
| 1167 | } |
| 1168 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1169 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample( |
| 1170 | std::unique_ptr<Function> f, |
| 1171 | double x1, |
| 1172 | double x2, |
| 1173 | uint32_t samples) { |
| 1174 | auto g = std::make_unique<Function>(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1175 | |
| 1176 | for (size_t i = 0; i < samples; i++) { |
| 1177 | double x = x1 + (x2 - x1) * i / (samples - 1); |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1178 | double y = Evaluate(f.get(), x); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1179 | g->push_back(Point(x, y)); |
| 1180 | } |
| 1181 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1182 | return g; |
| 1183 | } |
| 1184 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1185 | double VirtualSocketServer::Evaluate(const Function* f, double x) { |
| 1186 | Function::const_iterator iter = |
| 1187 | absl::c_lower_bound(*f, x, FunctionDomainCmp()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1188 | if (iter == f->begin()) { |
| 1189 | return (*f)[0].second; |
| 1190 | } else if (iter == f->end()) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1191 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1192 | return (*f)[f->size() - 1].second; |
| 1193 | } else if (iter->first == x) { |
| 1194 | return iter->second; |
| 1195 | } else { |
| 1196 | double x1 = (iter - 1)->first; |
| 1197 | double y1 = (iter - 1)->second; |
| 1198 | double x2 = iter->first; |
| 1199 | double y2 = iter->second; |
| 1200 | return y1 + (y2 - y1) * (x - x1) / (x2 - x1); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | bool VirtualSocketServer::CanInteractWith(VirtualSocket* local, |
| 1205 | VirtualSocket* remote) { |
| 1206 | if (!local || !remote) { |
| 1207 | return false; |
| 1208 | } |
| 1209 | IPAddress local_ip = local->GetLocalAddress().ipaddr(); |
| 1210 | IPAddress remote_ip = remote->GetLocalAddress().ipaddr(); |
| 1211 | IPAddress local_normalized = local_ip.Normalized(); |
| 1212 | IPAddress remote_normalized = remote_ip.Normalized(); |
| 1213 | // Check if the addresses are the same family after Normalization (turns |
| 1214 | // mapped IPv6 address into IPv4 addresses). |
| 1215 | // This will stop unmapped V6 addresses from talking to mapped V6 addresses. |
| 1216 | if (local_normalized.family() == remote_normalized.family()) { |
| 1217 | return true; |
| 1218 | } |
| 1219 | |
| 1220 | // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY. |
| 1221 | int remote_v6_only = 0; |
| 1222 | remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only); |
| 1223 | if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) { |
| 1224 | return true; |
| 1225 | } |
| 1226 | // Same check, backwards. |
| 1227 | int local_v6_only = 0; |
| 1228 | local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only); |
| 1229 | if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) { |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
| 1233 | // Check to see if either socket was explicitly bound to IPv6-any. |
| 1234 | // These sockets can talk with anyone. |
| 1235 | if (local_ip.family() == AF_INET6 && local->was_any()) { |
| 1236 | return true; |
| 1237 | } |
| 1238 | if (remote_ip.family() == AF_INET6 && remote->was_any()) { |
| 1239 | return true; |
| 1240 | } |
| 1241 | |
| 1242 | return false; |
| 1243 | } |
| 1244 | |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1245 | IPAddress VirtualSocketServer::GetDefaultSourceAddress(int family) { |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1246 | if (family == AF_INET) { |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1247 | return default_source_address_v4_; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1248 | } |
| 1249 | if (family == AF_INET6) { |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1250 | return default_source_address_v6_; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1251 | } |
| 1252 | return IPAddress(); |
| 1253 | } |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1254 | void VirtualSocketServer::SetDefaultSourceAddress(const IPAddress& from_addr) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1255 | RTC_DCHECK(!IPIsAny(from_addr)); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1256 | if (from_addr.family() == AF_INET) { |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1257 | default_source_address_v4_ = from_addr; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1258 | } else if (from_addr.family() == AF_INET6) { |
Niels Möller | 84d1595 | 2021-09-01 10:50:34 +0200 | [diff] [blame] | 1259 | default_source_address_v6_ = from_addr; |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
Florent Castelli | f94c053 | 2021-11-16 13:29:53 +0100 | [diff] [blame] | 1263 | void VirtualSocketServer::set_bandwidth(uint32_t bandwidth) { |
| 1264 | webrtc::MutexLock lock(&mutex_); |
| 1265 | bandwidth_ = bandwidth; |
| 1266 | } |
| 1267 | void VirtualSocketServer::set_network_capacity(uint32_t capacity) { |
| 1268 | webrtc::MutexLock lock(&mutex_); |
| 1269 | network_capacity_ = capacity; |
| 1270 | } |
| 1271 | |
| 1272 | uint32_t VirtualSocketServer::send_buffer_capacity() const { |
| 1273 | webrtc::MutexLock lock(&mutex_); |
| 1274 | return send_buffer_capacity_; |
| 1275 | } |
| 1276 | void VirtualSocketServer::set_send_buffer_capacity(uint32_t capacity) { |
| 1277 | webrtc::MutexLock lock(&mutex_); |
| 1278 | send_buffer_capacity_ = capacity; |
| 1279 | } |
| 1280 | |
| 1281 | uint32_t VirtualSocketServer::recv_buffer_capacity() const { |
| 1282 | webrtc::MutexLock lock(&mutex_); |
| 1283 | return recv_buffer_capacity_; |
| 1284 | } |
| 1285 | void VirtualSocketServer::set_recv_buffer_capacity(uint32_t capacity) { |
| 1286 | webrtc::MutexLock lock(&mutex_); |
| 1287 | recv_buffer_capacity_ = capacity; |
| 1288 | } |
| 1289 | |
| 1290 | void VirtualSocketServer::set_delay_mean(uint32_t delay_mean) { |
| 1291 | webrtc::MutexLock lock(&mutex_); |
| 1292 | delay_mean_ = delay_mean; |
| 1293 | } |
| 1294 | void VirtualSocketServer::set_delay_stddev(uint32_t delay_stddev) { |
| 1295 | webrtc::MutexLock lock(&mutex_); |
| 1296 | delay_stddev_ = delay_stddev; |
| 1297 | } |
| 1298 | void VirtualSocketServer::set_delay_samples(uint32_t delay_samples) { |
| 1299 | webrtc::MutexLock lock(&mutex_); |
| 1300 | delay_samples_ = delay_samples; |
| 1301 | } |
| 1302 | |
| 1303 | void VirtualSocketServer::set_drop_probability(double drop_prob) { |
| 1304 | RTC_DCHECK_GE(drop_prob, 0.0); |
| 1305 | RTC_DCHECK_LE(drop_prob, 1.0); |
| 1306 | |
| 1307 | webrtc::MutexLock lock(&mutex_); |
| 1308 | drop_prob_ = drop_prob; |
| 1309 | } |
| 1310 | |
| 1311 | void VirtualSocketServer::set_max_udp_payload(size_t payload_size) { |
| 1312 | webrtc::MutexLock lock(&mutex_); |
| 1313 | max_udp_payload_ = payload_size; |
| 1314 | } |
| 1315 | |
| 1316 | uint32_t VirtualSocketServer::sent_packets() const { |
| 1317 | webrtc::MutexLock lock(&mutex_); |
| 1318 | return sent_packets_; |
| 1319 | } |
| 1320 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1321 | } // namespace rtc |