blob: edf95157117437a4fd287ef1b2ba1b4db327e6fe [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/virtual_socket_server.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <errno.h>
14#include <math.h>
15
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include <map>
jbauch555604a2016-04-26 03:13:22 -070017#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include <vector>
19
Steve Anton2acd1632019-03-25 13:48:30 -070020#include "absl/algorithm/container.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/physical_socket_server.h"
25#include "rtc_base/socket_address_pair.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/time_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
29namespace rtc {
30#if defined(WEBRTC_WIN)
Yves Gerey665174f2018-06-19 15:03:05 +020031const in_addr kInitialNextIPv4 = {{{0x01, 0, 0, 0}}};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032#else
33// This value is entirely arbitrary, hence the lack of concern about endianness.
Yves Gerey665174f2018-06-19 15:03:05 +020034const in_addr kInitialNextIPv4 = {0x01000000};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035#endif
36// Starts at ::2 so as to not cause confusion with ::1.
Yves Gerey665174f2018-06-19 15:03:05 +020037const in6_addr kInitialNextIPv6 = {
38 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}}};
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039
Peter Boström0c4e06b2015-10-07 12:23:21 +020040const uint16_t kFirstEphemeralPort = 49152;
41const uint16_t kLastEphemeralPort = 65535;
42const uint16_t kEphemeralPortCount =
43 kLastEphemeralPort - kFirstEphemeralPort + 1;
44const uint32_t kDefaultNetworkCapacity = 64 * 1024;
45const uint32_t kDefaultTcpBufferSize = 32 * 1024;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046
Peter Boström0c4e06b2015-10-07 12:23:21 +020047const uint32_t UDP_HEADER_SIZE = 28; // IP + UDP headers
48const uint32_t TCP_HEADER_SIZE = 40; // IP + TCP headers
49const uint32_t TCP_MSS = 1400; // Maximum segment size
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51// Note: The current algorithm doesn't work for sample sizes smaller than this.
52const int NUM_SAMPLES = 1000;
53
54enum {
55 MSG_ID_PACKET,
56 MSG_ID_CONNECT,
57 MSG_ID_DISCONNECT,
deadbeefed3b9862017-06-02 10:33:16 -070058 MSG_ID_SIGNALREADEVENT,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059};
60
61// Packets are passed between sockets as messages. We copy the data just like
62// the kernel does.
63class Packet : public MessageData {
64 public:
65 Packet(const char* data, size_t size, const SocketAddress& from)
Yves Gerey665174f2018-06-19 15:03:05 +020066 : size_(size), consumed_(0), from_(from) {
deadbeef37f5ecf2017-02-27 14:06:41 -080067 RTC_DCHECK(nullptr != data);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 data_ = new char[size_];
69 memcpy(data_, data, size_);
70 }
71
Yves Gerey665174f2018-06-19 15:03:05 +020072 ~Packet() override { delete[] data_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073
74 const char* data() const { return data_ + consumed_; }
75 size_t size() const { return size_ - consumed_; }
76 const SocketAddress& from() const { return from_; }
77
78 // Remove the first size bytes from the data.
79 void Consume(size_t size) {
Taylor Brandstettere7536412016-09-09 13:16:15 -070080 RTC_DCHECK(size + consumed_ < size_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 consumed_ += size;
82 }
83
84 private:
85 char* data_;
86 size_t size_, consumed_;
87 SocketAddress from_;
88};
89
90struct MessageAddress : public MessageData {
Yves Gerey665174f2018-06-19 15:03:05 +020091 explicit MessageAddress(const SocketAddress& a) : addr(a) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 SocketAddress addr;
93};
94
Niels Möllerea423a52021-08-19 10:13:31 +020095VirtualSocket::VirtualSocket(VirtualSocketServer* server, int family, int type)
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +000096 : server_(server),
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +000097 type_(type),
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +000098 state_(CS_CLOSED),
99 error_(0),
deadbeef37f5ecf2017-02-27 14:06:41 -0800100 listen_queue_(nullptr),
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000101 network_size_(0),
102 recv_buffer_size_(0),
103 bound_(false),
104 was_any_(false) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700105 RTC_DCHECK((type_ == SOCK_DGRAM) || (type_ == SOCK_STREAM));
Taylor Brandstettere7536412016-09-09 13:16:15 -0700106 server->SignalReadyToSend.connect(this,
107 &VirtualSocket::OnSocketServerReadyToSend);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000108}
109
110VirtualSocket::~VirtualSocket() {
111 Close();
112
113 for (RecvBuffer::iterator it = recv_buffer_.begin(); it != recv_buffer_.end();
114 ++it) {
115 delete *it;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000117}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000119SocketAddress VirtualSocket::GetLocalAddress() const {
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000120 return local_addr_;
121}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000123SocketAddress VirtualSocket::GetRemoteAddress() const {
124 return remote_addr_;
125}
126
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000127void VirtualSocket::SetLocalAddress(const SocketAddress& addr) {
128 local_addr_ = addr;
129}
130
131int VirtualSocket::Bind(const SocketAddress& addr) {
132 if (!local_addr_.IsNil()) {
133 error_ = EINVAL;
134 return -1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 }
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200136 local_addr_ = server_->AssignBindAddress(addr);
137 int result = server_->Bind(this, local_addr_);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000138 if (result != 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 local_addr_.Clear();
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000140 error_ = EADDRINUSE;
141 } else {
142 bound_ = true;
143 was_any_ = addr.IsAnyIP();
144 }
145 return result;
146}
147
148int VirtualSocket::Connect(const SocketAddress& addr) {
149 return InitiateConnect(addr, true);
150}
151
152int VirtualSocket::Close() {
153 if (!local_addr_.IsNil() && bound_) {
154 // Remove from the binding table.
155 server_->Unbind(local_addr_, this);
156 bound_ = false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 }
158
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000159 if (SOCK_STREAM == type_) {
Niels Möllerc413c552021-06-22 10:03:14 +0200160 webrtc::MutexLock lock(&mutex_);
Niels Möller257f81b2021-06-17 16:58:59 +0200161
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000162 // Cancel pending sockets
163 if (listen_queue_) {
164 while (!listen_queue_->empty()) {
165 SocketAddress addr = listen_queue_->front();
166
167 // Disconnect listening socket.
Niels Möllerc79bd432021-02-16 09:25:52 +0100168 server_->Disconnect(addr);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000169 listen_queue_->pop_front();
170 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800171 listen_queue_ = nullptr;
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000172 }
173 // Disconnect stream sockets
174 if (CS_CONNECTED == state_) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100175 server_->Disconnect(local_addr_, remote_addr_);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000176 }
177 // Cancel potential connects
Niels Möllerc79bd432021-02-16 09:25:52 +0100178 server_->CancelConnects(this);
Tomas Gunnarssond9663472020-11-21 16:20:23 +0100179 }
180
181 // Clear incoming packets and disconnect messages
Niels Möllerc79bd432021-02-16 09:25:52 +0100182 server_->Clear(this);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000183
184 state_ = CS_CLOSED;
185 local_addr_.Clear();
186 remote_addr_.Clear();
187 return 0;
188}
189
190int VirtualSocket::Send(const void* pv, size_t cb) {
Yves Gerey665174f2018-06-19 15:03:05 +0200191 if (CS_CONNECTED != state_) {
192 error_ = ENOTCONN;
193 return -1;
194 }
195 if (SOCK_DGRAM == type_) {
196 return SendUdp(pv, cb, remote_addr_);
197 } else {
198 return SendTcp(pv, cb);
199 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000200}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000202int VirtualSocket::SendTo(const void* pv,
203 size_t cb,
204 const SocketAddress& addr) {
205 if (SOCK_DGRAM == type_) {
206 return SendUdp(pv, cb, addr);
207 } else {
208 if (CS_CONNECTED != state_) {
209 error_ = ENOTCONN;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210 return -1;
211 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000212 return SendTcp(pv, cb);
213 }
214}
215
Stefan Holmer9131efd2016-05-23 18:19:26 +0200216int VirtualSocket::Recv(void* pv, size_t cb, int64_t* timestamp) {
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000217 SocketAddress addr;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200218 return RecvFrom(pv, cb, &addr, timestamp);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000219}
220
Stefan Holmer9131efd2016-05-23 18:19:26 +0200221int VirtualSocket::RecvFrom(void* pv,
222 size_t cb,
223 SocketAddress* paddr,
224 int64_t* timestamp) {
225 if (timestamp) {
226 *timestamp = -1;
227 }
Niels Möller257f81b2021-06-17 16:58:59 +0200228
Niels Möllerc413c552021-06-22 10:03:14 +0200229 webrtc::MutexLock lock(&mutex_);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000230 // If we don't have a packet, then either error or wait for one to arrive.
231 if (recv_buffer_.empty()) {
Niels Möllerea423a52021-08-19 10:13:31 +0200232 error_ = EAGAIN;
233 return -1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234 }
235
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000236 // Return the packet at the front of the queue.
237 Packet* packet = recv_buffer_.front();
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000238 size_t data_read = std::min(cb, packet->size());
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000239 memcpy(pv, packet->data(), data_read);
240 *paddr = packet->from();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000242 if (data_read < packet->size()) {
243 packet->Consume(data_read);
244 } else {
245 recv_buffer_.pop_front();
246 delete packet;
247 }
248
deadbeefed3b9862017-06-02 10:33:16 -0700249 // To behave like a real socket, SignalReadEvent should fire in the next
250 // message loop pass if there's still data buffered.
251 if (!recv_buffer_.empty()) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100252 server_->PostSignalReadEvent(this);
deadbeefed3b9862017-06-02 10:33:16 -0700253 }
254
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000255 if (SOCK_STREAM == type_) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100256 bool was_full = (recv_buffer_size_ == server_->recv_buffer_capacity());
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000257 recv_buffer_size_ -= data_read;
258 if (was_full) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100259 server_->SendTcp(remote_addr_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000261 }
262
263 return static_cast<int>(data_read);
264}
265
266int VirtualSocket::Listen(int backlog) {
Niels Möllerc413c552021-06-22 10:03:14 +0200267 webrtc::MutexLock lock(&mutex_);
Taylor Brandstettere7536412016-09-09 13:16:15 -0700268 RTC_DCHECK(SOCK_STREAM == type_);
269 RTC_DCHECK(CS_CLOSED == state_);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000270 if (local_addr_.IsNil()) {
271 error_ = EINVAL;
272 return -1;
273 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800274 RTC_DCHECK(nullptr == listen_queue_);
Niels Möllerc413c552021-06-22 10:03:14 +0200275 listen_queue_ = std::make_unique<ListenQueue>();
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000276 state_ = CS_CONNECTING;
277 return 0;
278}
279
280VirtualSocket* VirtualSocket::Accept(SocketAddress* paddr) {
Niels Möllerc413c552021-06-22 10:03:14 +0200281 webrtc::MutexLock lock(&mutex_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800282 if (nullptr == listen_queue_) {
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000283 error_ = EINVAL;
deadbeef37f5ecf2017-02-27 14:06:41 -0800284 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000286 while (!listen_queue_->empty()) {
Niels Möllerea423a52021-08-19 10:13:31 +0200287 VirtualSocket* socket = new VirtualSocket(server_, AF_INET, type_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000289 // Set the new local address to the same as this server socket.
290 socket->SetLocalAddress(local_addr_);
291 // Sockets made from a socket that 'was Any' need to inherit that.
292 socket->set_was_any(was_any_);
293 SocketAddress remote_addr(listen_queue_->front());
294 int result = socket->InitiateConnect(remote_addr, false);
295 listen_queue_->pop_front();
296 if (result != 0) {
297 delete socket;
298 continue;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000299 }
Niels Möllerc413c552021-06-22 10:03:14 +0200300 socket->CompleteConnect(remote_addr);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000301 if (paddr) {
302 *paddr = remote_addr;
303 }
304 return socket;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000306 error_ = EWOULDBLOCK;
deadbeef37f5ecf2017-02-27 14:06:41 -0800307 return nullptr;
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000308}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000310int VirtualSocket::GetError() const {
311 return error_;
312}
313
314void VirtualSocket::SetError(int error) {
315 error_ = error;
316}
317
318Socket::ConnState VirtualSocket::GetState() const {
319 return state_;
320}
321
322int VirtualSocket::GetOption(Option opt, int* value) {
323 OptionsMap::const_iterator it = options_map_.find(opt);
324 if (it == options_map_.end()) {
325 return -1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000326 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000327 *value = it->second;
328 return 0; // 0 is success to emulate getsockopt()
329}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000330
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000331int VirtualSocket::SetOption(Option opt, int value) {
332 options_map_[opt] = value;
333 return 0; // 0 is success to emulate setsockopt()
334}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000335
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000336void VirtualSocket::OnMessage(Message* pmsg) {
Niels Möller257f81b2021-06-17 16:58:59 +0200337 bool signal_read_event = false;
338 bool signal_close_event = false;
Niels Möllerc413c552021-06-22 10:03:14 +0200339 bool signal_connect_event = false;
Niels Möller257f81b2021-06-17 16:58:59 +0200340 int error_to_signal = 0;
341 {
Niels Möllerc413c552021-06-22 10:03:14 +0200342 webrtc::MutexLock lock(&mutex_);
Niels Möller257f81b2021-06-17 16:58:59 +0200343 if (pmsg->message_id == MSG_ID_PACKET) {
344 RTC_DCHECK(nullptr != pmsg->pdata);
345 Packet* packet = static_cast<Packet*>(pmsg->pdata);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000346
Niels Möller257f81b2021-06-17 16:58:59 +0200347 recv_buffer_.push_back(packet);
Niels Möllerea423a52021-08-19 10:13:31 +0200348 signal_read_event = true;
Niels Möller257f81b2021-06-17 16:58:59 +0200349 } else if (pmsg->message_id == MSG_ID_CONNECT) {
350 RTC_DCHECK(nullptr != pmsg->pdata);
351 MessageAddress* data = static_cast<MessageAddress*>(pmsg->pdata);
352 if (listen_queue_ != nullptr) {
353 listen_queue_->push_back(data->addr);
Niels Möllerea423a52021-08-19 10:13:31 +0200354 signal_read_event = true;
Niels Möller257f81b2021-06-17 16:58:59 +0200355 } else if ((SOCK_STREAM == type_) && (CS_CONNECTING == state_)) {
Niels Möllerc413c552021-06-22 10:03:14 +0200356 CompleteConnect(data->addr);
Niels Möllerea423a52021-08-19 10:13:31 +0200357 signal_connect_event = true;
Niels Möller257f81b2021-06-17 16:58:59 +0200358 } else {
359 RTC_LOG(LS_VERBOSE)
360 << "Socket at " << local_addr_.ToString() << " is not listening";
361 server_->Disconnect(data->addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000362 }
Niels Möller257f81b2021-06-17 16:58:59 +0200363 delete data;
364 } else if (pmsg->message_id == MSG_ID_DISCONNECT) {
365 RTC_DCHECK(SOCK_STREAM == type_);
366 if (CS_CLOSED != state_) {
367 error_to_signal = (CS_CONNECTING == state_) ? ECONNREFUSED : 0;
368 state_ = CS_CLOSED;
369 remote_addr_.Clear();
Niels Möllerea423a52021-08-19 10:13:31 +0200370 signal_close_event = true;
Niels Möller257f81b2021-06-17 16:58:59 +0200371 }
372 } else if (pmsg->message_id == MSG_ID_SIGNALREADEVENT) {
373 signal_read_event = !recv_buffer_.empty();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374 } else {
Artem Titovd3251962021-11-15 16:57:07 +0100375 RTC_DCHECK_NOTREACHED();
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000376 }
Niels Möller257f81b2021-06-17 16:58:59 +0200377 }
Niels Möllerc413c552021-06-22 10:03:14 +0200378 // Signal events without holding `mutex_`, to avoid recursive locking, as well
379 // as issues with sigslot and lock order.
Niels Möller257f81b2021-06-17 16:58:59 +0200380 if (signal_read_event) {
381 SignalReadEvent(this);
382 }
383 if (signal_close_event) {
384 SignalCloseEvent(this, error_to_signal);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000385 }
Niels Möllerc413c552021-06-22 10:03:14 +0200386 if (signal_connect_event) {
387 SignalConnectEvent(this);
388 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000389}
390
391int 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.orgf0488722014-05-13 18:00:26 +0000406 }
407 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000408 if (type_ == SOCK_DGRAM) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000409 remote_addr_ = addr;
410 state_ = CS_CONNECTED;
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000411 } else {
412 int result = server_->Connect(this, addr, use_delay);
413 if (result != 0) {
414 error_ = EHOSTUNREACH;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000415 return -1;
416 }
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000417 state_ = CS_CONNECTING;
418 }
419 return 0;
420}
421
Niels Möllerc413c552021-06-22 10:03:14 +0200422void VirtualSocket::CompleteConnect(const SocketAddress& addr) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700423 RTC_DCHECK(CS_CONNECTING == state_);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000424 remote_addr_ = addr;
425 state_ = CS_CONNECTED;
426 server_->AddConnection(remote_addr_, local_addr_, this);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000427}
428
429int VirtualSocket::SendUdp(const void* pv,
430 size_t cb,
431 const SocketAddress& addr) {
432 // If we have not been assigned a local port, then get one.
433 if (local_addr_.IsNil()) {
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200434 local_addr_ = server_->AssignBindAddress(
435 EmptySocketAddressWithFamily(addr.ipaddr().family()));
436 int result = server_->Bind(this, local_addr_);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000437 if (result != 0) {
438 local_addr_.Clear();
439 error_ = EADDRINUSE;
440 return result;
441 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000442 }
443
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000444 // Send the data in a message to the appropriate socket.
445 return server_->SendUdp(this, static_cast<const char*>(pv), cb, addr);
446}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000447
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000448int VirtualSocket::SendTcp(const void* pv, size_t cb) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100449 size_t capacity = server_->send_buffer_capacity() - send_buffer_.size();
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000450 if (0 == capacity) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700451 ready_to_send_ = false;
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000452 error_ = EWOULDBLOCK;
453 return -1;
454 }
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000455 size_t consumed = std::min(cb, capacity);
guoweis@webrtc.org0eb6eec2014-12-17 22:03:33 +0000456 const char* cpv = static_cast<const char*>(pv);
457 send_buffer_.insert(send_buffer_.end(), cpv, cpv + consumed);
458 server_->SendTcp(this);
459 return static_cast<int>(consumed);
460}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000461
Taylor Brandstettere7536412016-09-09 13:16:15 -0700462void VirtualSocket::OnSocketServerReadyToSend() {
463 if (ready_to_send_) {
464 // This socket didn't encounter EWOULDBLOCK, so there's nothing to do.
465 return;
466 }
467 if (type_ == SOCK_DGRAM) {
468 ready_to_send_ = true;
469 SignalWriteEvent(this);
470 } else {
471 RTC_DCHECK(type_ == SOCK_STREAM);
472 // This will attempt to empty the full send buffer, and will fire
473 // SignalWriteEvent if successful.
474 server_->SendTcp(this);
475 }
476}
477
Niels Möllerc79bd432021-02-16 09:25:52 +0100478void VirtualSocket::SetToBlocked() {
Niels Möllerc413c552021-06-22 10:03:14 +0200479 webrtc::MutexLock lock(&mutex_);
Niels Möllerc79bd432021-02-16 09:25:52 +0100480 ready_to_send_ = false;
481 error_ = EWOULDBLOCK;
482}
483
484void VirtualSocket::UpdateRecv(size_t data_size) {
485 recv_buffer_size_ += data_size;
486}
487
488void VirtualSocket::UpdateSend(size_t data_size) {
489 size_t new_buffer_size = send_buffer_.size() - data_size;
490 // Avoid undefined access beyond the last element of the vector.
491 // This only happens when new_buffer_size is 0.
492 if (data_size < send_buffer_.size()) {
493 // memmove is required for potentially overlapping source/destination.
494 memmove(&send_buffer_[0], &send_buffer_[data_size], new_buffer_size);
495 }
496 send_buffer_.resize(new_buffer_size);
497}
498
499void VirtualSocket::MaybeSignalWriteEvent(size_t capacity) {
500 if (!ready_to_send_ && (send_buffer_.size() < capacity)) {
501 ready_to_send_ = true;
502 SignalWriteEvent(this);
503 }
504}
505
506uint32_t VirtualSocket::AddPacket(int64_t cur_time, size_t packet_size) {
507 network_size_ += packet_size;
508 uint32_t send_delay =
509 server_->SendDelay(static_cast<uint32_t>(network_size_));
510
511 NetworkEntry entry;
512 entry.size = packet_size;
513 entry.done_time = cur_time + send_delay;
514 network_.push_back(entry);
515
516 return send_delay;
517}
518
519int64_t VirtualSocket::UpdateOrderedDelivery(int64_t ts) {
520 // Ensure that new packets arrive after previous ones
521 ts = std::max(ts, last_delivery_time_);
522 // A socket should not have both ordered and unordered delivery, so its last
523 // delivery time only needs to be updated when it has ordered delivery.
524 last_delivery_time_ = ts;
525 return ts;
526}
527
528size_t VirtualSocket::PurgeNetworkPackets(int64_t cur_time) {
Niels Möllerc413c552021-06-22 10:03:14 +0200529 webrtc::MutexLock lock(&mutex_);
Niels Möllerc79bd432021-02-16 09:25:52 +0100530
531 while (!network_.empty() && (network_.front().done_time <= cur_time)) {
532 RTC_DCHECK(network_size_ >= network_.front().size);
533 network_size_ -= network_.front().size;
534 network_.pop_front();
535 }
536 return network_size_;
537}
538
deadbeef22e08142017-06-12 14:30:28 -0700539VirtualSocketServer::VirtualSocketServer() : VirtualSocketServer(nullptr) {}
540
Sebastian Janssond624c392019-04-17 10:36:03 +0200541VirtualSocketServer::VirtualSocketServer(ThreadProcessingFakeClock* fake_clock)
deadbeef22e08142017-06-12 14:30:28 -0700542 : fake_clock_(fake_clock),
deadbeef37f5ecf2017-02-27 14:06:41 -0800543 msg_queue_(nullptr),
Honghai Zhang82d78622016-05-06 11:29:15 -0700544 stop_on_idle_(false),
Honghai Zhang82d78622016-05-06 11:29:15 -0700545 next_ipv4_(kInitialNextIPv4),
546 next_ipv6_(kInitialNextIPv6),
547 next_port_(kFirstEphemeralPort),
548 bindings_(new AddressMap()),
549 connections_(new ConnectionMap()),
550 bandwidth_(0),
551 network_capacity_(kDefaultNetworkCapacity),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000552 send_buffer_capacity_(kDefaultTcpBufferSize),
553 recv_buffer_capacity_(kDefaultTcpBufferSize),
Honghai Zhang82d78622016-05-06 11:29:15 -0700554 delay_mean_(0),
555 delay_stddev_(0),
556 delay_samples_(NUM_SAMPLES),
Honghai Zhang82d78622016-05-06 11:29:15 -0700557 drop_prob_(0.0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558 UpdateDelayDistribution();
559}
560
561VirtualSocketServer::~VirtualSocketServer() {
562 delete bindings_;
563 delete connections_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000564}
565
566IPAddress VirtualSocketServer::GetNextIP(int family) {
567 if (family == AF_INET) {
568 IPAddress next_ip(next_ipv4_);
Yves Gerey665174f2018-06-19 15:03:05 +0200569 next_ipv4_.s_addr = HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000570 return next_ip;
571 } else if (family == AF_INET6) {
572 IPAddress next_ip(next_ipv6_);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200573 uint32_t* as_ints = reinterpret_cast<uint32_t*>(&next_ipv6_.s6_addr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000574 as_ints[3] += 1;
575 return next_ip;
576 }
577 return IPAddress();
578}
579
Peter Boström0c4e06b2015-10-07 12:23:21 +0200580uint16_t VirtualSocketServer::GetNextPort() {
581 uint16_t port = next_port_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000582 if (next_port_ < kLastEphemeralPort) {
583 ++next_port_;
584 } else {
585 next_port_ = kFirstEphemeralPort;
586 }
587 return port;
588}
589
Taylor Brandstettere7536412016-09-09 13:16:15 -0700590void VirtualSocketServer::SetSendingBlocked(bool blocked) {
Florent Castellif94c0532021-11-16 13:29:53 +0100591 {
592 webrtc::MutexLock lock(&mutex_);
593 if (blocked == sending_blocked_) {
594 // Unchanged; nothing to do.
595 return;
596 }
597 sending_blocked_ = blocked;
Taylor Brandstettere7536412016-09-09 13:16:15 -0700598 }
Florent Castellif94c0532021-11-16 13:29:53 +0100599 if (!blocked) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700600 // Sending was blocked, but is now unblocked. This signal gives sockets a
601 // chance to fire SignalWriteEvent, and for TCP, send buffered data.
602 SignalReadyToSend();
603 }
604}
605
Niels Möllerea423a52021-08-19 10:13:31 +0200606VirtualSocket* VirtualSocketServer::CreateSocket(int family, int type) {
607 return new VirtualSocket(this, family, type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000608}
609
Sebastian Jansson290de822020-01-09 14:20:23 +0100610void VirtualSocketServer::SetMessageQueue(Thread* msg_queue) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000611 msg_queue_ = msg_queue;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000612}
613
614bool VirtualSocketServer::Wait(int cmsWait, bool process_io) {
Tommiccc9d972022-03-24 08:12:36 +0100615 RTC_DCHECK_RUN_ON(msg_queue_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000616 if (stop_on_idle_ && Thread::Current()->empty()) {
617 return false;
618 }
Artem Titov96e3b992021-07-26 16:03:14 +0200619 // Note: we don't need to do anything with `process_io` since we don't have
deadbeef98e186c2017-05-16 18:00:06 -0700620 // any real I/O. Received packets come in the form of queued messages, so
Sebastian Jansson290de822020-01-09 14:20:23 +0100621 // Thread will ensure WakeUp is called if another thread sends a
deadbeef98e186c2017-05-16 18:00:06 -0700622 // packet.
623 wakeup_.Wait(cmsWait);
624 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000625}
626
627void VirtualSocketServer::WakeUp() {
deadbeef98e186c2017-05-16 18:00:06 -0700628 wakeup_.Set();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000629}
630
deadbeef5c3c1042017-08-04 15:01:57 -0700631void VirtualSocketServer::SetAlternativeLocalAddress(
632 const rtc::IPAddress& address,
633 const rtc::IPAddress& alternative) {
634 alternative_address_mapping_[address] = alternative;
635}
636
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000637bool VirtualSocketServer::ProcessMessagesUntilIdle() {
Tommiccc9d972022-03-24 08:12:36 +0100638 RTC_DCHECK_RUN_ON(msg_queue_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000639 stop_on_idle_ = true;
640 while (!msg_queue_->empty()) {
deadbeef22e08142017-06-12 14:30:28 -0700641 if (fake_clock_) {
642 // If using a fake clock, advance it in millisecond increments until the
Bjorn Mellem6eb03b82017-06-13 15:07:41 -0700643 // queue is empty.
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100644 fake_clock_->AdvanceTime(webrtc::TimeDelta::Millis(1));
deadbeef22e08142017-06-12 14:30:28 -0700645 } else {
646 // Otherwise, run a normal message loop.
647 Message msg;
648 if (msg_queue_->Get(&msg, Thread::kForever)) {
649 msg_queue_->Dispatch(&msg);
650 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000651 }
652 }
653 stop_on_idle_ = false;
654 return !msg_queue_->IsQuitting();
655}
656
Peter Boström0c4e06b2015-10-07 12:23:21 +0200657void VirtualSocketServer::SetNextPortForTesting(uint16_t port) {
jiayl@webrtc.org22406fc2014-09-09 15:44:05 +0000658 next_port_ = port;
659}
660
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700661bool VirtualSocketServer::CloseTcpConnections(
662 const SocketAddress& addr_local,
663 const SocketAddress& addr_remote) {
664 VirtualSocket* socket = LookupConnection(addr_local, addr_remote);
665 if (!socket) {
666 return false;
667 }
668 // Signal the close event on the local connection first.
669 socket->SignalCloseEvent(socket, 0);
670
671 // Trigger the remote connection's close event.
672 socket->Close();
673
674 return true;
675}
676
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000677int VirtualSocketServer::Bind(VirtualSocket* socket,
678 const SocketAddress& addr) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800679 RTC_DCHECK(nullptr != socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000680 // Address must be completely specified at this point
Taylor Brandstettere7536412016-09-09 13:16:15 -0700681 RTC_DCHECK(!IPIsUnspec(addr.ipaddr()));
682 RTC_DCHECK(addr.port() != 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000683
684 // Normalize the address (turns v6-mapped addresses into v4-addresses).
685 SocketAddress normalized(addr.ipaddr().Normalized(), addr.port());
686
687 AddressMap::value_type entry(normalized, socket);
Niels Möllerd44532a2021-02-18 14:38:14 +0100688 return bindings_->insert(entry).second ? 0 : -1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000689}
690
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200691SocketAddress VirtualSocketServer::AssignBindAddress(
692 const SocketAddress& app_addr) {
693 RTC_DCHECK(!IPIsUnspec(app_addr.ipaddr()));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000694
deadbeef5c3c1042017-08-04 15:01:57 -0700695 // Normalize the IP.
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200696 SocketAddress addr;
697 addr.SetIP(app_addr.ipaddr().Normalized());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000698
Artem Titov96e3b992021-07-26 16:03:14 +0200699 // If the IP appears in `alternative_address_mapping_`, meaning the test has
deadbeef5c3c1042017-08-04 15:01:57 -0700700 // configured sockets bound to this IP to actually use another IP, replace
701 // the IP here.
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200702 auto alternative = alternative_address_mapping_.find(addr.ipaddr());
deadbeef5c3c1042017-08-04 15:01:57 -0700703 if (alternative != alternative_address_mapping_.end()) {
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200704 addr.SetIP(alternative->second);
deadbeef5c3c1042017-08-04 15:01:57 -0700705 }
706
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200707 if (app_addr.port() != 0) {
708 addr.SetPort(app_addr.port());
709 } else {
710 // Assign a port.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000711 for (int i = 0; i < kEphemeralPortCount; ++i) {
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200712 addr.SetPort(GetNextPort());
713 if (bindings_->find(addr) == bindings_->end()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000714 break;
715 }
716 }
717 }
718
Niels Möllerc2d8f1e2021-08-24 15:49:34 +0200719 return addr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000720}
721
722VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) {
Yves Gerey665174f2018-06-19 15:03:05 +0200723 SocketAddress normalized(addr.ipaddr().Normalized(), addr.port());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000724 AddressMap::iterator it = bindings_->find(normalized);
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700725 if (it != bindings_->end()) {
726 return it->second;
727 }
728
Niels Möller84d15952021-09-01 10:50:34 +0200729 IPAddress default_ip = GetDefaultSourceAddress(addr.ipaddr().family());
Guo-wei Shieh38f88932015-08-13 22:24:02 -0700730 if (!IPIsUnspec(default_ip) && addr.ipaddr() == default_ip) {
731 // If we can't find a binding for the packet which is sent to the interface
732 // corresponding to the default route, it should match a binding with the
733 // correct port to the any address.
734 SocketAddress sock_addr =
735 EmptySocketAddressWithFamily(addr.ipaddr().family());
736 sock_addr.SetPort(addr.port());
737 return LookupBinding(sock_addr);
738 }
739
740 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000741}
742
743int VirtualSocketServer::Unbind(const SocketAddress& addr,
744 VirtualSocket* socket) {
Yves Gerey665174f2018-06-19 15:03:05 +0200745 SocketAddress normalized(addr.ipaddr().Normalized(), addr.port());
Taylor Brandstettere7536412016-09-09 13:16:15 -0700746 RTC_DCHECK((*bindings_)[normalized] == socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000747 bindings_->erase(bindings_->find(normalized));
748 return 0;
749}
750
751void VirtualSocketServer::AddConnection(const SocketAddress& local,
752 const SocketAddress& remote,
753 VirtualSocket* remote_socket) {
754 // Add this socket pair to our routing table. This will allow
755 // multiple clients to connect to the same server address.
Yves Gerey665174f2018-06-19 15:03:05 +0200756 SocketAddress local_normalized(local.ipaddr().Normalized(), local.port());
757 SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000758 SocketAddressPair address_pair(local_normalized, remote_normalized);
Yves Gerey665174f2018-06-19 15:03:05 +0200759 connections_->insert(std::pair<SocketAddressPair, VirtualSocket*>(
760 address_pair, remote_socket));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000761}
762
763VirtualSocket* VirtualSocketServer::LookupConnection(
764 const SocketAddress& local,
765 const SocketAddress& remote) {
Yves Gerey665174f2018-06-19 15:03:05 +0200766 SocketAddress local_normalized(local.ipaddr().Normalized(), local.port());
767 SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000768 SocketAddressPair address_pair(local_normalized, remote_normalized);
769 ConnectionMap::iterator it = connections_->find(address_pair);
deadbeef37f5ecf2017-02-27 14:06:41 -0800770 return (connections_->end() != it) ? it->second : nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000771}
772
773void VirtualSocketServer::RemoveConnection(const SocketAddress& local,
774 const SocketAddress& remote) {
Yves Gerey665174f2018-06-19 15:03:05 +0200775 SocketAddress local_normalized(local.ipaddr().Normalized(), local.port());
776 SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000777 SocketAddressPair address_pair(local_normalized, remote_normalized);
778 connections_->erase(address_pair);
779}
780
781static double Random() {
782 return static_cast<double>(rand()) / RAND_MAX;
783}
784
785int VirtualSocketServer::Connect(VirtualSocket* socket,
786 const SocketAddress& remote_addr,
787 bool use_delay) {
Tommiccc9d972022-03-24 08:12:36 +0100788 RTC_DCHECK(msg_queue_);
789
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700790 uint32_t delay = use_delay ? GetTransitDelay(socket) : 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000791 VirtualSocket* remote = LookupBinding(remote_addr);
792 if (!CanInteractWith(socket, remote)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100793 RTC_LOG(LS_INFO) << "Address family mismatch between "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200794 << socket->GetLocalAddress().ToString() << " and "
795 << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000796 return -1;
797 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800798 if (remote != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000799 SocketAddress addr = socket->GetLocalAddress();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700800 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000801 new MessageAddress(addr));
802 } else {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200803 RTC_LOG(LS_INFO) << "No one listening at " << remote_addr.ToString();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700804 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000805 }
806 return 0;
807}
808
809bool VirtualSocketServer::Disconnect(VirtualSocket* socket) {
Tommiccc9d972022-03-24 08:12:36 +0100810 if (!socket || !msg_queue_)
811 return false;
812
813 // If we simulate packets being delayed, we should simulate the
814 // equivalent of a FIN being delayed as well.
815 uint32_t delay = GetTransitDelay(socket);
816 // Remove the mapping.
817 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT);
818 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000819}
820
Niels Möllerc79bd432021-02-16 09:25:52 +0100821bool VirtualSocketServer::Disconnect(const SocketAddress& addr) {
822 return Disconnect(LookupBinding(addr));
823}
824
825bool VirtualSocketServer::Disconnect(const SocketAddress& local_addr,
826 const SocketAddress& remote_addr) {
827 // Disconnect remote socket, check if it is a child of a server socket.
828 VirtualSocket* socket = LookupConnection(local_addr, remote_addr);
829 if (!socket) {
830 // Not a server socket child, then see if it is bound.
831 // TODO(tbd): If this is indeed a server socket that has no
832 // children this will cause the server socket to be
833 // closed. This might lead to unexpected results, how to fix this?
834 socket = LookupBinding(remote_addr);
835 }
836 Disconnect(socket);
837
838 // Remove mapping for both directions.
839 RemoveConnection(remote_addr, local_addr);
840 RemoveConnection(local_addr, remote_addr);
841 return socket != nullptr;
842}
843
844void VirtualSocketServer::CancelConnects(VirtualSocket* socket) {
845 MessageList msgs;
846 if (msg_queue_) {
847 msg_queue_->Clear(socket, MSG_ID_CONNECT, &msgs);
848 }
849 for (MessageList::iterator it = msgs.begin(); it != msgs.end(); ++it) {
850 RTC_DCHECK(nullptr != it->pdata);
851 MessageAddress* data = static_cast<MessageAddress*>(it->pdata);
852 SocketAddress local_addr = socket->GetLocalAddress();
853 // Lookup remote side.
Mirko Bonadei54c90f22021-10-03 11:26:11 +0200854 VirtualSocket* lookup_socket = LookupConnection(local_addr, data->addr);
855 if (lookup_socket) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100856 // Server socket, remote side is a socket retreived by
857 // accept. Accepted sockets are not bound so we will not
858 // find it by looking in the bindings table.
Mirko Bonadei54c90f22021-10-03 11:26:11 +0200859 Disconnect(lookup_socket);
Niels Möllerc79bd432021-02-16 09:25:52 +0100860 RemoveConnection(local_addr, data->addr);
861 } else {
862 Disconnect(data->addr);
863 }
864 delete data;
865 }
866}
867
868void VirtualSocketServer::Clear(VirtualSocket* socket) {
869 // Clear incoming packets and disconnect messages
870 if (msg_queue_) {
871 msg_queue_->Clear(socket);
872 }
873}
874
Niels Möllerc79bd432021-02-16 09:25:52 +0100875void VirtualSocketServer::PostSignalReadEvent(VirtualSocket* socket) {
Tommiccc9d972022-03-24 08:12:36 +0100876 if (!msg_queue_)
877 return;
878
Niels Möllerc79bd432021-02-16 09:25:52 +0100879 // Clear the message so it doesn't end up posted multiple times.
880 msg_queue_->Clear(socket, MSG_ID_SIGNALREADEVENT);
881 msg_queue_->Post(RTC_FROM_HERE, socket, MSG_ID_SIGNALREADEVENT);
882}
883
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000884int VirtualSocketServer::SendUdp(VirtualSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200885 const char* data,
886 size_t data_size,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000887 const SocketAddress& remote_addr) {
Florent Castellif94c0532021-11-16 13:29:53 +0100888 {
889 webrtc::MutexLock lock(&mutex_);
890 ++sent_packets_;
891 if (sending_blocked_) {
892 socket->SetToBlocked();
893 return -1;
Harald Alvestrand3d792e92021-03-10 07:29:28 +0000894 }
Harald Alvestrand3d792e92021-03-10 07:29:28 +0000895
Florent Castellif94c0532021-11-16 13:29:53 +0100896 // See if we want to drop this packet.
897 if (data_size > max_udp_payload_) {
898 RTC_LOG(LS_VERBOSE) << "Dropping too large UDP payload of size "
899 << data_size << ", UDP payload limit is "
900 << max_udp_payload_;
901 // Return as if send was successful; packet disappears.
902 return data_size;
903 }
Harald Alvestrand3d792e92021-03-10 07:29:28 +0000904
Florent Castellif94c0532021-11-16 13:29:53 +0100905 if (Random() < drop_prob_) {
906 RTC_LOG(LS_VERBOSE) << "Dropping packet: bad luck";
907 return static_cast<int>(data_size);
908 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000909 }
910
911 VirtualSocket* recipient = LookupBinding(remote_addr);
912 if (!recipient) {
913 // Make a fake recipient for address family checking.
jbauch555604a2016-04-26 03:13:22 -0700914 std::unique_ptr<VirtualSocket> dummy_socket(
Niels Möllerea423a52021-08-19 10:13:31 +0200915 CreateSocket(AF_INET, SOCK_DGRAM));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000916 dummy_socket->SetLocalAddress(remote_addr);
917 if (!CanInteractWith(socket, dummy_socket.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100918 RTC_LOG(LS_VERBOSE) << "Incompatible address families: "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200919 << socket->GetLocalAddress().ToString() << " and "
920 << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000921 return -1;
922 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200923 RTC_LOG(LS_VERBOSE) << "No one listening at " << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000924 return static_cast<int>(data_size);
925 }
926
927 if (!CanInteractWith(socket, recipient)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100928 RTC_LOG(LS_VERBOSE) << "Incompatible address families: "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200929 << socket->GetLocalAddress().ToString() << " and "
930 << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000931 return -1;
932 }
933
Taylor Brandstettere7536412016-09-09 13:16:15 -0700934 {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700935 int64_t cur_time = TimeMillis();
Niels Möllerc79bd432021-02-16 09:25:52 +0100936 size_t network_size = socket->PurgeNetworkPackets(cur_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000937
Taylor Brandstettere7536412016-09-09 13:16:15 -0700938 // Determine whether we have enough bandwidth to accept this packet. To do
939 // this, we need to update the send queue. Once we know it's current size,
940 // we know whether we can fit this packet.
941 //
942 // NOTE: There are better algorithms for maintaining such a queue (such as
943 // "Derivative Random Drop"); however, this algorithm is a more accurate
944 // simulation of what a normal network would do.
Florent Castellif94c0532021-11-16 13:29:53 +0100945 {
946 webrtc::MutexLock lock(&mutex_);
947 size_t packet_size = data_size + UDP_HEADER_SIZE;
948 if (network_size + packet_size > network_capacity_) {
949 RTC_LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded";
950 return static_cast<int>(data_size);
951 }
Taylor Brandstettere7536412016-09-09 13:16:15 -0700952 }
953
954 AddPacketToNetwork(socket, recipient, cur_time, data, data_size,
955 UDP_HEADER_SIZE, false);
956
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000957 return static_cast<int>(data_size);
958 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000959}
960
961void VirtualSocketServer::SendTcp(VirtualSocket* socket) {
Florent Castellif94c0532021-11-16 13:29:53 +0100962 {
963 webrtc::MutexLock lock(&mutex_);
964 ++sent_packets_;
965 if (sending_blocked_) {
966 // Eventually the socket's buffer will fill and VirtualSocket::SendTcp
967 // will set EWOULDBLOCK.
968 return;
969 }
Taylor Brandstettere7536412016-09-09 13:16:15 -0700970 }
971
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000972 // TCP can't send more data than will fill up the receiver's buffer.
973 // We track the data that is in the buffer plus data in flight using the
974 // recipient's recv_buffer_size_. Anything beyond that must be stored in the
975 // sender's buffer. We will trigger the buffered data to be sent when data
976 // is read from the recv_buffer.
977
978 // Lookup the local/remote pair in the connections table.
Yves Gerey665174f2018-06-19 15:03:05 +0200979 VirtualSocket* recipient =
Niels Möllerc79bd432021-02-16 09:25:52 +0100980 LookupConnection(socket->GetLocalAddress(), socket->GetRemoteAddress());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000981 if (!recipient) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100982 RTC_LOG(LS_VERBOSE) << "Sending data to no one.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000983 return;
984 }
985
Honghai Zhang82d78622016-05-06 11:29:15 -0700986 int64_t cur_time = TimeMillis();
Niels Möllerc79bd432021-02-16 09:25:52 +0100987 socket->PurgeNetworkPackets(cur_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000988
989 while (true) {
Florent Castellif94c0532021-11-16 13:29:53 +0100990 size_t available = recv_buffer_capacity() - recipient->recv_buffer_size();
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000991 size_t max_data_size =
992 std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE);
Niels Möllerc79bd432021-02-16 09:25:52 +0100993 size_t data_size = std::min(socket->send_buffer_size(), max_data_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000994 if (0 == data_size)
995 break;
996
Niels Möllerc79bd432021-02-16 09:25:52 +0100997 AddPacketToNetwork(socket, recipient, cur_time, socket->send_buffer_data(),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000998 data_size, TCP_HEADER_SIZE, true);
Niels Möllerc79bd432021-02-16 09:25:52 +0100999 recipient->UpdateRecv(data_size);
1000 socket->UpdateSend(data_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001001 }
1002
Florent Castellif94c0532021-11-16 13:29:53 +01001003 socket->MaybeSignalWriteEvent(send_buffer_capacity());
Niels Möllerc79bd432021-02-16 09:25:52 +01001004}
1005
1006void VirtualSocketServer::SendTcp(const SocketAddress& addr) {
1007 VirtualSocket* sender = LookupBinding(addr);
1008 RTC_DCHECK(nullptr != sender);
1009 SendTcp(sender);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001010}
1011
1012void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender,
1013 VirtualSocket* recipient,
Honghai Zhang82d78622016-05-06 11:29:15 -07001014 int64_t cur_time,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001015 const char* data,
1016 size_t data_size,
1017 size_t header_size,
1018 bool ordered) {
Tommiccc9d972022-03-24 08:12:36 +01001019 RTC_DCHECK(msg_queue_);
Niels Möllerc79bd432021-02-16 09:25:52 +01001020 uint32_t send_delay = sender->AddPacket(cur_time, data_size + header_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001021
1022 // Find the delay for crossing the many virtual hops of the network.
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001023 uint32_t transit_delay = GetTransitDelay(sender);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001024
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001025 // When the incoming packet is from a binding of the any address, translate it
1026 // to the default route here such that the recipient will see the default
1027 // route.
Niels Möllerc79bd432021-02-16 09:25:52 +01001028 SocketAddress sender_addr = sender->GetLocalAddress();
Niels Möller84d15952021-09-01 10:50:34 +02001029 IPAddress default_ip = GetDefaultSourceAddress(sender_addr.ipaddr().family());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001030 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) {
1031 sender_addr.SetIP(default_ip);
1032 }
1033
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001034 // Post the packet as a message to be delivered (on our own thread)
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001035 Packet* p = new Packet(data, data_size, sender_addr);
1036
Honghai Zhang82d78622016-05-06 11:29:15 -07001037 int64_t ts = TimeAfter(send_delay + transit_delay);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001038 if (ordered) {
Niels Möllerc79bd432021-02-16 09:25:52 +01001039 ts = sender->UpdateOrderedDelivery(ts);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001040 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001041 msg_queue_->PostAt(RTC_FROM_HERE, ts, recipient, MSG_ID_PACKET, p);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001042}
1043
Peter Boström0c4e06b2015-10-07 12:23:21 +02001044uint32_t VirtualSocketServer::SendDelay(uint32_t size) {
Florent Castellif94c0532021-11-16 13:29:53 +01001045 webrtc::MutexLock lock(&mutex_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001046 if (bandwidth_ == 0)
1047 return 0;
1048 else
1049 return 1000 * size / bandwidth_;
1050}
1051
1052#if 0
1053void PrintFunction(std::vector<std::pair<double, double> >* f) {
1054 return;
1055 double sum = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001056 for (uint32_t i = 0; i < f->size(); ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001057 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öm0c4e06b2015-10-07 12:23:21 +02001063 for (uint32_t i = 0; i < f->size(); ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001064 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
1073void VirtualSocketServer::UpdateDelayDistribution() {
Florent Castellif94c0532021-11-16 13:29:53 +01001074 webrtc::MutexLock lock(&mutex_);
Niels Möller983627c2021-02-09 15:12:28 +01001075 delay_dist_ = CreateDistribution(delay_mean_, delay_stddev_, delay_samples_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001076}
1077
1078static double PI = 4 * atan(1.0);
1079
1080static double Normal(double x, double mean, double stddev) {
1081 double a = (x - mean) * (x - mean) / (2 * stddev * stddev);
1082 return exp(-a) / (stddev * sqrt(2 * PI));
1083}
1084
1085#if 0 // static unused gives a warning
1086static double Pareto(double x, double min, double k) {
1087 if (x < min)
1088 return 0;
1089 else
1090 return k * std::pow(min, k) / std::pow(x, k+1);
1091}
1092#endif
1093
Niels Möller983627c2021-02-09 15:12:28 +01001094std::unique_ptr<VirtualSocketServer::Function>
1095VirtualSocketServer::CreateDistribution(uint32_t mean,
1096 uint32_t stddev,
1097 uint32_t samples) {
1098 auto f = std::make_unique<Function>();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001099
1100 if (0 == stddev) {
1101 f->push_back(Point(mean, 1.0));
1102 } else {
1103 double start = 0;
1104 if (mean >= 4 * static_cast<double>(stddev))
1105 start = mean - 4 * static_cast<double>(stddev);
1106 double end = mean + 4 * static_cast<double>(stddev);
1107
Peter Boström0c4e06b2015-10-07 12:23:21 +02001108 for (uint32_t i = 0; i < samples; i++) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001109 double x = start + (end - start) * i / (samples - 1);
1110 double y = Normal(x, mean, stddev);
1111 f->push_back(Point(x, y));
1112 }
1113 }
Niels Möller983627c2021-02-09 15:12:28 +01001114 return Resample(Invert(Accumulate(std::move(f))), 0, 1, samples);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001115}
1116
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001117uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) {
1118 // Use the delay based on the address if it is set.
1119 auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr());
1120 if (iter != delay_by_ip_.end()) {
1121 return static_cast<uint32_t>(iter->second);
1122 }
1123 // Otherwise, use the delay from the distribution distribution.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001124 size_t index = rand() % delay_dist_->size();
1125 double delay = (*delay_dist_)[index].second;
Mirko Bonadei675513b2017-11-09 11:09:25 +01001126 // RTC_LOG_F(LS_INFO) << "random[" << index << "] = " << delay;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001127 return static_cast<uint32_t>(delay);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001128}
1129
1130struct FunctionDomainCmp {
1131 bool operator()(const VirtualSocketServer::Point& p1,
Yves Gerey665174f2018-06-19 15:03:05 +02001132 const VirtualSocketServer::Point& p2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001133 return p1.first < p2.first;
1134 }
1135 bool operator()(double v1, const VirtualSocketServer::Point& p2) {
1136 return v1 < p2.first;
1137 }
1138 bool operator()(const VirtualSocketServer::Point& p1, double v2) {
1139 return p1.first < v2;
1140 }
1141};
1142
Niels Möller983627c2021-02-09 15:12:28 +01001143std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate(
1144 std::unique_ptr<Function> f) {
Taylor Brandstettere7536412016-09-09 13:16:15 -07001145 RTC_DCHECK(f->size() >= 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001146 double v = 0;
1147 for (Function::size_type i = 0; i < f->size() - 1; ++i) {
1148 double dx = (*f)[i + 1].first - (*f)[i].first;
1149 double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2;
1150 (*f)[i].second = v;
1151 v = v + dx * avgy;
1152 }
Yves Gerey665174f2018-06-19 15:03:05 +02001153 (*f)[f->size() - 1].second = v;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001154 return f;
1155}
1156
Niels Möller983627c2021-02-09 15:12:28 +01001157std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert(
1158 std::unique_ptr<Function> f) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001159 for (Function::size_type i = 0; i < f->size(); ++i)
1160 std::swap((*f)[i].first, (*f)[i].second);
1161
Steve Anton2acd1632019-03-25 13:48:30 -07001162 absl::c_sort(*f, FunctionDomainCmp());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001163 return f;
1164}
1165
Niels Möller983627c2021-02-09 15:12:28 +01001166std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample(
1167 std::unique_ptr<Function> f,
1168 double x1,
1169 double x2,
1170 uint32_t samples) {
1171 auto g = std::make_unique<Function>();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001172
1173 for (size_t i = 0; i < samples; i++) {
1174 double x = x1 + (x2 - x1) * i / (samples - 1);
Niels Möller983627c2021-02-09 15:12:28 +01001175 double y = Evaluate(f.get(), x);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001176 g->push_back(Point(x, y));
1177 }
1178
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001179 return g;
1180}
1181
Niels Möller983627c2021-02-09 15:12:28 +01001182double VirtualSocketServer::Evaluate(const Function* f, double x) {
1183 Function::const_iterator iter =
1184 absl::c_lower_bound(*f, x, FunctionDomainCmp());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001185 if (iter == f->begin()) {
1186 return (*f)[0].second;
1187 } else if (iter == f->end()) {
Taylor Brandstettere7536412016-09-09 13:16:15 -07001188 RTC_DCHECK(f->size() >= 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001189 return (*f)[f->size() - 1].second;
1190 } else if (iter->first == x) {
1191 return iter->second;
1192 } else {
1193 double x1 = (iter - 1)->first;
1194 double y1 = (iter - 1)->second;
1195 double x2 = iter->first;
1196 double y2 = iter->second;
1197 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1198 }
1199}
1200
1201bool VirtualSocketServer::CanInteractWith(VirtualSocket* local,
1202 VirtualSocket* remote) {
1203 if (!local || !remote) {
1204 return false;
1205 }
1206 IPAddress local_ip = local->GetLocalAddress().ipaddr();
1207 IPAddress remote_ip = remote->GetLocalAddress().ipaddr();
1208 IPAddress local_normalized = local_ip.Normalized();
1209 IPAddress remote_normalized = remote_ip.Normalized();
1210 // Check if the addresses are the same family after Normalization (turns
1211 // mapped IPv6 address into IPv4 addresses).
1212 // This will stop unmapped V6 addresses from talking to mapped V6 addresses.
1213 if (local_normalized.family() == remote_normalized.family()) {
1214 return true;
1215 }
1216
1217 // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY.
1218 int remote_v6_only = 0;
1219 remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only);
1220 if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) {
1221 return true;
1222 }
1223 // Same check, backwards.
1224 int local_v6_only = 0;
1225 local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only);
1226 if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) {
1227 return true;
1228 }
1229
1230 // Check to see if either socket was explicitly bound to IPv6-any.
1231 // These sockets can talk with anyone.
1232 if (local_ip.family() == AF_INET6 && local->was_any()) {
1233 return true;
1234 }
1235 if (remote_ip.family() == AF_INET6 && remote->was_any()) {
1236 return true;
1237 }
1238
1239 return false;
1240}
1241
Niels Möller84d15952021-09-01 10:50:34 +02001242IPAddress VirtualSocketServer::GetDefaultSourceAddress(int family) {
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001243 if (family == AF_INET) {
Niels Möller84d15952021-09-01 10:50:34 +02001244 return default_source_address_v4_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001245 }
1246 if (family == AF_INET6) {
Niels Möller84d15952021-09-01 10:50:34 +02001247 return default_source_address_v6_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001248 }
1249 return IPAddress();
1250}
Niels Möller84d15952021-09-01 10:50:34 +02001251void VirtualSocketServer::SetDefaultSourceAddress(const IPAddress& from_addr) {
henrikg91d6ede2015-09-17 00:24:34 -07001252 RTC_DCHECK(!IPIsAny(from_addr));
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001253 if (from_addr.family() == AF_INET) {
Niels Möller84d15952021-09-01 10:50:34 +02001254 default_source_address_v4_ = from_addr;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001255 } else if (from_addr.family() == AF_INET6) {
Niels Möller84d15952021-09-01 10:50:34 +02001256 default_source_address_v6_ = from_addr;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001257 }
1258}
1259
Florent Castellif94c0532021-11-16 13:29:53 +01001260void VirtualSocketServer::set_bandwidth(uint32_t bandwidth) {
1261 webrtc::MutexLock lock(&mutex_);
1262 bandwidth_ = bandwidth;
1263}
1264void VirtualSocketServer::set_network_capacity(uint32_t capacity) {
1265 webrtc::MutexLock lock(&mutex_);
1266 network_capacity_ = capacity;
1267}
1268
1269uint32_t VirtualSocketServer::send_buffer_capacity() const {
1270 webrtc::MutexLock lock(&mutex_);
1271 return send_buffer_capacity_;
1272}
1273void VirtualSocketServer::set_send_buffer_capacity(uint32_t capacity) {
1274 webrtc::MutexLock lock(&mutex_);
1275 send_buffer_capacity_ = capacity;
1276}
1277
1278uint32_t VirtualSocketServer::recv_buffer_capacity() const {
1279 webrtc::MutexLock lock(&mutex_);
1280 return recv_buffer_capacity_;
1281}
1282void VirtualSocketServer::set_recv_buffer_capacity(uint32_t capacity) {
1283 webrtc::MutexLock lock(&mutex_);
1284 recv_buffer_capacity_ = capacity;
1285}
1286
1287void VirtualSocketServer::set_delay_mean(uint32_t delay_mean) {
1288 webrtc::MutexLock lock(&mutex_);
1289 delay_mean_ = delay_mean;
1290}
1291void VirtualSocketServer::set_delay_stddev(uint32_t delay_stddev) {
1292 webrtc::MutexLock lock(&mutex_);
1293 delay_stddev_ = delay_stddev;
1294}
1295void VirtualSocketServer::set_delay_samples(uint32_t delay_samples) {
1296 webrtc::MutexLock lock(&mutex_);
1297 delay_samples_ = delay_samples;
1298}
1299
1300void VirtualSocketServer::set_drop_probability(double drop_prob) {
1301 RTC_DCHECK_GE(drop_prob, 0.0);
1302 RTC_DCHECK_LE(drop_prob, 1.0);
1303
1304 webrtc::MutexLock lock(&mutex_);
1305 drop_prob_ = drop_prob;
1306}
1307
1308void VirtualSocketServer::set_max_udp_payload(size_t payload_size) {
1309 webrtc::MutexLock lock(&mutex_);
1310 max_udp_payload_ = payload_size;
1311}
1312
1313uint32_t VirtualSocketServer::sent_packets() const {
1314 webrtc::MutexLock lock(&mutex_);
1315 return sent_packets_;
1316}
1317
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001318} // namespace rtc