blob: 5d36e3e1dec04d0f421404665ca01ec8d1d22106 [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) {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700615 RTC_DCHECK(msg_queue_ == Thread::Current());
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() {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700638 RTC_DCHECK(msg_queue_ == Thread::Current());
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) {
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700788 uint32_t delay = use_delay ? GetTransitDelay(socket) : 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000789 VirtualSocket* remote = LookupBinding(remote_addr);
790 if (!CanInteractWith(socket, remote)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100791 RTC_LOG(LS_INFO) << "Address family mismatch between "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200792 << socket->GetLocalAddress().ToString() << " and "
793 << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000794 return -1;
795 }
deadbeef37f5ecf2017-02-27 14:06:41 -0800796 if (remote != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000797 SocketAddress addr = socket->GetLocalAddress();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700798 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000799 new MessageAddress(addr));
800 } else {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200801 RTC_LOG(LS_INFO) << "No one listening at " << remote_addr.ToString();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700802 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000803 }
804 return 0;
805}
806
807bool VirtualSocketServer::Disconnect(VirtualSocket* socket) {
808 if (socket) {
Taylor Brandstetter716d07a2016-06-27 14:07:41 -0700809 // If we simulate packets being delayed, we should simulate the
810 // equivalent of a FIN being delayed as well.
Honghai Zhangc67e0f52016-09-19 16:57:37 -0700811 uint32_t delay = GetTransitDelay(socket);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000812 // Remove the mapping.
Taylor Brandstetter716d07a2016-06-27 14:07:41 -0700813 msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000814 return true;
815 }
816 return false;
817}
818
Niels Möllerc79bd432021-02-16 09:25:52 +0100819bool VirtualSocketServer::Disconnect(const SocketAddress& addr) {
820 return Disconnect(LookupBinding(addr));
821}
822
823bool VirtualSocketServer::Disconnect(const SocketAddress& local_addr,
824 const SocketAddress& remote_addr) {
825 // Disconnect remote socket, check if it is a child of a server socket.
826 VirtualSocket* socket = LookupConnection(local_addr, remote_addr);
827 if (!socket) {
828 // Not a server socket child, then see if it is bound.
829 // TODO(tbd): If this is indeed a server socket that has no
830 // children this will cause the server socket to be
831 // closed. This might lead to unexpected results, how to fix this?
832 socket = LookupBinding(remote_addr);
833 }
834 Disconnect(socket);
835
836 // Remove mapping for both directions.
837 RemoveConnection(remote_addr, local_addr);
838 RemoveConnection(local_addr, remote_addr);
839 return socket != nullptr;
840}
841
842void VirtualSocketServer::CancelConnects(VirtualSocket* socket) {
843 MessageList msgs;
844 if (msg_queue_) {
845 msg_queue_->Clear(socket, MSG_ID_CONNECT, &msgs);
846 }
847 for (MessageList::iterator it = msgs.begin(); it != msgs.end(); ++it) {
848 RTC_DCHECK(nullptr != it->pdata);
849 MessageAddress* data = static_cast<MessageAddress*>(it->pdata);
850 SocketAddress local_addr = socket->GetLocalAddress();
851 // Lookup remote side.
Mirko Bonadei54c90f22021-10-03 11:26:11 +0200852 VirtualSocket* lookup_socket = LookupConnection(local_addr, data->addr);
853 if (lookup_socket) {
Niels Möllerc79bd432021-02-16 09:25:52 +0100854 // Server socket, remote side is a socket retreived by
855 // accept. Accepted sockets are not bound so we will not
856 // find it by looking in the bindings table.
Mirko Bonadei54c90f22021-10-03 11:26:11 +0200857 Disconnect(lookup_socket);
Niels Möllerc79bd432021-02-16 09:25:52 +0100858 RemoveConnection(local_addr, data->addr);
859 } else {
860 Disconnect(data->addr);
861 }
862 delete data;
863 }
864}
865
866void VirtualSocketServer::Clear(VirtualSocket* socket) {
867 // Clear incoming packets and disconnect messages
868 if (msg_queue_) {
869 msg_queue_->Clear(socket);
870 }
871}
872
Niels Möllerc79bd432021-02-16 09:25:52 +0100873void VirtualSocketServer::PostSignalReadEvent(VirtualSocket* socket) {
874 // Clear the message so it doesn't end up posted multiple times.
875 msg_queue_->Clear(socket, MSG_ID_SIGNALREADEVENT);
876 msg_queue_->Post(RTC_FROM_HERE, socket, MSG_ID_SIGNALREADEVENT);
877}
878
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000879int VirtualSocketServer::SendUdp(VirtualSocket* socket,
Yves Gerey665174f2018-06-19 15:03:05 +0200880 const char* data,
881 size_t data_size,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000882 const SocketAddress& remote_addr) {
Florent Castellif94c0532021-11-16 13:29:53 +0100883 {
884 webrtc::MutexLock lock(&mutex_);
885 ++sent_packets_;
886 if (sending_blocked_) {
887 socket->SetToBlocked();
888 return -1;
Harald Alvestrand3d792e92021-03-10 07:29:28 +0000889 }
Harald Alvestrand3d792e92021-03-10 07:29:28 +0000890
Florent Castellif94c0532021-11-16 13:29:53 +0100891 // See if we want to drop this packet.
892 if (data_size > max_udp_payload_) {
893 RTC_LOG(LS_VERBOSE) << "Dropping too large UDP payload of size "
894 << data_size << ", UDP payload limit is "
895 << max_udp_payload_;
896 // Return as if send was successful; packet disappears.
897 return data_size;
898 }
Harald Alvestrand3d792e92021-03-10 07:29:28 +0000899
Florent Castellif94c0532021-11-16 13:29:53 +0100900 if (Random() < drop_prob_) {
901 RTC_LOG(LS_VERBOSE) << "Dropping packet: bad luck";
902 return static_cast<int>(data_size);
903 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000904 }
905
906 VirtualSocket* recipient = LookupBinding(remote_addr);
907 if (!recipient) {
908 // Make a fake recipient for address family checking.
jbauch555604a2016-04-26 03:13:22 -0700909 std::unique_ptr<VirtualSocket> dummy_socket(
Niels Möllerea423a52021-08-19 10:13:31 +0200910 CreateSocket(AF_INET, SOCK_DGRAM));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000911 dummy_socket->SetLocalAddress(remote_addr);
912 if (!CanInteractWith(socket, dummy_socket.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100913 RTC_LOG(LS_VERBOSE) << "Incompatible address families: "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200914 << socket->GetLocalAddress().ToString() << " and "
915 << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000916 return -1;
917 }
Jonas Olssonabbe8412018-04-03 13:40:05 +0200918 RTC_LOG(LS_VERBOSE) << "No one listening at " << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000919 return static_cast<int>(data_size);
920 }
921
922 if (!CanInteractWith(socket, recipient)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100923 RTC_LOG(LS_VERBOSE) << "Incompatible address families: "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200924 << socket->GetLocalAddress().ToString() << " and "
925 << remote_addr.ToString();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000926 return -1;
927 }
928
Taylor Brandstettere7536412016-09-09 13:16:15 -0700929 {
Taylor Brandstettere7536412016-09-09 13:16:15 -0700930 int64_t cur_time = TimeMillis();
Niels Möllerc79bd432021-02-16 09:25:52 +0100931 size_t network_size = socket->PurgeNetworkPackets(cur_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000932
Taylor Brandstettere7536412016-09-09 13:16:15 -0700933 // Determine whether we have enough bandwidth to accept this packet. To do
934 // this, we need to update the send queue. Once we know it's current size,
935 // we know whether we can fit this packet.
936 //
937 // NOTE: There are better algorithms for maintaining such a queue (such as
938 // "Derivative Random Drop"); however, this algorithm is a more accurate
939 // simulation of what a normal network would do.
Florent Castellif94c0532021-11-16 13:29:53 +0100940 {
941 webrtc::MutexLock lock(&mutex_);
942 size_t packet_size = data_size + UDP_HEADER_SIZE;
943 if (network_size + packet_size > network_capacity_) {
944 RTC_LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded";
945 return static_cast<int>(data_size);
946 }
Taylor Brandstettere7536412016-09-09 13:16:15 -0700947 }
948
949 AddPacketToNetwork(socket, recipient, cur_time, data, data_size,
950 UDP_HEADER_SIZE, false);
951
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000952 return static_cast<int>(data_size);
953 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000954}
955
956void VirtualSocketServer::SendTcp(VirtualSocket* socket) {
Florent Castellif94c0532021-11-16 13:29:53 +0100957 {
958 webrtc::MutexLock lock(&mutex_);
959 ++sent_packets_;
960 if (sending_blocked_) {
961 // Eventually the socket's buffer will fill and VirtualSocket::SendTcp
962 // will set EWOULDBLOCK.
963 return;
964 }
Taylor Brandstettere7536412016-09-09 13:16:15 -0700965 }
966
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000967 // TCP can't send more data than will fill up the receiver's buffer.
968 // We track the data that is in the buffer plus data in flight using the
969 // recipient's recv_buffer_size_. Anything beyond that must be stored in the
970 // sender's buffer. We will trigger the buffered data to be sent when data
971 // is read from the recv_buffer.
972
973 // Lookup the local/remote pair in the connections table.
Yves Gerey665174f2018-06-19 15:03:05 +0200974 VirtualSocket* recipient =
Niels Möllerc79bd432021-02-16 09:25:52 +0100975 LookupConnection(socket->GetLocalAddress(), socket->GetRemoteAddress());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000976 if (!recipient) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100977 RTC_LOG(LS_VERBOSE) << "Sending data to no one.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000978 return;
979 }
980
Honghai Zhang82d78622016-05-06 11:29:15 -0700981 int64_t cur_time = TimeMillis();
Niels Möllerc79bd432021-02-16 09:25:52 +0100982 socket->PurgeNetworkPackets(cur_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000983
984 while (true) {
Florent Castellif94c0532021-11-16 13:29:53 +0100985 size_t available = recv_buffer_capacity() - recipient->recv_buffer_size();
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000986 size_t max_data_size =
987 std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE);
Niels Möllerc79bd432021-02-16 09:25:52 +0100988 size_t data_size = std::min(socket->send_buffer_size(), max_data_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000989 if (0 == data_size)
990 break;
991
Niels Möllerc79bd432021-02-16 09:25:52 +0100992 AddPacketToNetwork(socket, recipient, cur_time, socket->send_buffer_data(),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000993 data_size, TCP_HEADER_SIZE, true);
Niels Möllerc79bd432021-02-16 09:25:52 +0100994 recipient->UpdateRecv(data_size);
995 socket->UpdateSend(data_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000996 }
997
Florent Castellif94c0532021-11-16 13:29:53 +0100998 socket->MaybeSignalWriteEvent(send_buffer_capacity());
Niels Möllerc79bd432021-02-16 09:25:52 +0100999}
1000
1001void VirtualSocketServer::SendTcp(const SocketAddress& addr) {
1002 VirtualSocket* sender = LookupBinding(addr);
1003 RTC_DCHECK(nullptr != sender);
1004 SendTcp(sender);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001005}
1006
1007void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender,
1008 VirtualSocket* recipient,
Honghai Zhang82d78622016-05-06 11:29:15 -07001009 int64_t cur_time,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001010 const char* data,
1011 size_t data_size,
1012 size_t header_size,
1013 bool ordered) {
Niels Möllerc79bd432021-02-16 09:25:52 +01001014 uint32_t send_delay = sender->AddPacket(cur_time, data_size + header_size);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001015
1016 // Find the delay for crossing the many virtual hops of the network.
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001017 uint32_t transit_delay = GetTransitDelay(sender);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001018
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001019 // When the incoming packet is from a binding of the any address, translate it
1020 // to the default route here such that the recipient will see the default
1021 // route.
Niels Möllerc79bd432021-02-16 09:25:52 +01001022 SocketAddress sender_addr = sender->GetLocalAddress();
Niels Möller84d15952021-09-01 10:50:34 +02001023 IPAddress default_ip = GetDefaultSourceAddress(sender_addr.ipaddr().family());
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001024 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) {
1025 sender_addr.SetIP(default_ip);
1026 }
1027
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001028 // Post the packet as a message to be delivered (on our own thread)
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001029 Packet* p = new Packet(data, data_size, sender_addr);
1030
Honghai Zhang82d78622016-05-06 11:29:15 -07001031 int64_t ts = TimeAfter(send_delay + transit_delay);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001032 if (ordered) {
Niels Möllerc79bd432021-02-16 09:25:52 +01001033 ts = sender->UpdateOrderedDelivery(ts);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001034 }
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001035 msg_queue_->PostAt(RTC_FROM_HERE, ts, recipient, MSG_ID_PACKET, p);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001036}
1037
Peter Boström0c4e06b2015-10-07 12:23:21 +02001038uint32_t VirtualSocketServer::SendDelay(uint32_t size) {
Florent Castellif94c0532021-11-16 13:29:53 +01001039 webrtc::MutexLock lock(&mutex_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001040 if (bandwidth_ == 0)
1041 return 0;
1042 else
1043 return 1000 * size / bandwidth_;
1044}
1045
1046#if 0
1047void PrintFunction(std::vector<std::pair<double, double> >* f) {
1048 return;
1049 double sum = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001050 for (uint32_t i = 0; i < f->size(); ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001051 std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl;
1052 sum += (*f)[i].second;
1053 }
1054 if (!f->empty()) {
1055 const double mean = sum / f->size();
1056 double sum_sq_dev = 0;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001057 for (uint32_t i = 0; i < f->size(); ++i) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001058 double dev = (*f)[i].second - mean;
1059 sum_sq_dev += dev * dev;
1060 }
1061 std::cout << "Mean = " << mean << " StdDev = "
1062 << sqrt(sum_sq_dev / f->size()) << std::endl;
1063 }
1064}
1065#endif // <unused>
1066
1067void VirtualSocketServer::UpdateDelayDistribution() {
Florent Castellif94c0532021-11-16 13:29:53 +01001068 webrtc::MutexLock lock(&mutex_);
Niels Möller983627c2021-02-09 15:12:28 +01001069 delay_dist_ = CreateDistribution(delay_mean_, delay_stddev_, delay_samples_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001070}
1071
1072static double PI = 4 * atan(1.0);
1073
1074static double Normal(double x, double mean, double stddev) {
1075 double a = (x - mean) * (x - mean) / (2 * stddev * stddev);
1076 return exp(-a) / (stddev * sqrt(2 * PI));
1077}
1078
1079#if 0 // static unused gives a warning
1080static double Pareto(double x, double min, double k) {
1081 if (x < min)
1082 return 0;
1083 else
1084 return k * std::pow(min, k) / std::pow(x, k+1);
1085}
1086#endif
1087
Niels Möller983627c2021-02-09 15:12:28 +01001088std::unique_ptr<VirtualSocketServer::Function>
1089VirtualSocketServer::CreateDistribution(uint32_t mean,
1090 uint32_t stddev,
1091 uint32_t samples) {
1092 auto f = std::make_unique<Function>();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001093
1094 if (0 == stddev) {
1095 f->push_back(Point(mean, 1.0));
1096 } else {
1097 double start = 0;
1098 if (mean >= 4 * static_cast<double>(stddev))
1099 start = mean - 4 * static_cast<double>(stddev);
1100 double end = mean + 4 * static_cast<double>(stddev);
1101
Peter Boström0c4e06b2015-10-07 12:23:21 +02001102 for (uint32_t i = 0; i < samples; i++) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001103 double x = start + (end - start) * i / (samples - 1);
1104 double y = Normal(x, mean, stddev);
1105 f->push_back(Point(x, y));
1106 }
1107 }
Niels Möller983627c2021-02-09 15:12:28 +01001108 return Resample(Invert(Accumulate(std::move(f))), 0, 1, samples);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001109}
1110
Honghai Zhangc67e0f52016-09-19 16:57:37 -07001111uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) {
1112 // Use the delay based on the address if it is set.
1113 auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr());
1114 if (iter != delay_by_ip_.end()) {
1115 return static_cast<uint32_t>(iter->second);
1116 }
1117 // Otherwise, use the delay from the distribution distribution.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001118 size_t index = rand() % delay_dist_->size();
1119 double delay = (*delay_dist_)[index].second;
Mirko Bonadei675513b2017-11-09 11:09:25 +01001120 // RTC_LOG_F(LS_INFO) << "random[" << index << "] = " << delay;
Peter Boström0c4e06b2015-10-07 12:23:21 +02001121 return static_cast<uint32_t>(delay);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001122}
1123
1124struct FunctionDomainCmp {
1125 bool operator()(const VirtualSocketServer::Point& p1,
Yves Gerey665174f2018-06-19 15:03:05 +02001126 const VirtualSocketServer::Point& p2) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001127 return p1.first < p2.first;
1128 }
1129 bool operator()(double v1, const VirtualSocketServer::Point& p2) {
1130 return v1 < p2.first;
1131 }
1132 bool operator()(const VirtualSocketServer::Point& p1, double v2) {
1133 return p1.first < v2;
1134 }
1135};
1136
Niels Möller983627c2021-02-09 15:12:28 +01001137std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate(
1138 std::unique_ptr<Function> f) {
Taylor Brandstettere7536412016-09-09 13:16:15 -07001139 RTC_DCHECK(f->size() >= 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001140 double v = 0;
1141 for (Function::size_type i = 0; i < f->size() - 1; ++i) {
1142 double dx = (*f)[i + 1].first - (*f)[i].first;
1143 double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2;
1144 (*f)[i].second = v;
1145 v = v + dx * avgy;
1146 }
Yves Gerey665174f2018-06-19 15:03:05 +02001147 (*f)[f->size() - 1].second = v;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001148 return f;
1149}
1150
Niels Möller983627c2021-02-09 15:12:28 +01001151std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert(
1152 std::unique_ptr<Function> f) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001153 for (Function::size_type i = 0; i < f->size(); ++i)
1154 std::swap((*f)[i].first, (*f)[i].second);
1155
Steve Anton2acd1632019-03-25 13:48:30 -07001156 absl::c_sort(*f, FunctionDomainCmp());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001157 return f;
1158}
1159
Niels Möller983627c2021-02-09 15:12:28 +01001160std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample(
1161 std::unique_ptr<Function> f,
1162 double x1,
1163 double x2,
1164 uint32_t samples) {
1165 auto g = std::make_unique<Function>();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001166
1167 for (size_t i = 0; i < samples; i++) {
1168 double x = x1 + (x2 - x1) * i / (samples - 1);
Niels Möller983627c2021-02-09 15:12:28 +01001169 double y = Evaluate(f.get(), x);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001170 g->push_back(Point(x, y));
1171 }
1172
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001173 return g;
1174}
1175
Niels Möller983627c2021-02-09 15:12:28 +01001176double VirtualSocketServer::Evaluate(const Function* f, double x) {
1177 Function::const_iterator iter =
1178 absl::c_lower_bound(*f, x, FunctionDomainCmp());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001179 if (iter == f->begin()) {
1180 return (*f)[0].second;
1181 } else if (iter == f->end()) {
Taylor Brandstettere7536412016-09-09 13:16:15 -07001182 RTC_DCHECK(f->size() >= 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001183 return (*f)[f->size() - 1].second;
1184 } else if (iter->first == x) {
1185 return iter->second;
1186 } else {
1187 double x1 = (iter - 1)->first;
1188 double y1 = (iter - 1)->second;
1189 double x2 = iter->first;
1190 double y2 = iter->second;
1191 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1192 }
1193}
1194
1195bool VirtualSocketServer::CanInteractWith(VirtualSocket* local,
1196 VirtualSocket* remote) {
1197 if (!local || !remote) {
1198 return false;
1199 }
1200 IPAddress local_ip = local->GetLocalAddress().ipaddr();
1201 IPAddress remote_ip = remote->GetLocalAddress().ipaddr();
1202 IPAddress local_normalized = local_ip.Normalized();
1203 IPAddress remote_normalized = remote_ip.Normalized();
1204 // Check if the addresses are the same family after Normalization (turns
1205 // mapped IPv6 address into IPv4 addresses).
1206 // This will stop unmapped V6 addresses from talking to mapped V6 addresses.
1207 if (local_normalized.family() == remote_normalized.family()) {
1208 return true;
1209 }
1210
1211 // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY.
1212 int remote_v6_only = 0;
1213 remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only);
1214 if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) {
1215 return true;
1216 }
1217 // Same check, backwards.
1218 int local_v6_only = 0;
1219 local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only);
1220 if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) {
1221 return true;
1222 }
1223
1224 // Check to see if either socket was explicitly bound to IPv6-any.
1225 // These sockets can talk with anyone.
1226 if (local_ip.family() == AF_INET6 && local->was_any()) {
1227 return true;
1228 }
1229 if (remote_ip.family() == AF_INET6 && remote->was_any()) {
1230 return true;
1231 }
1232
1233 return false;
1234}
1235
Niels Möller84d15952021-09-01 10:50:34 +02001236IPAddress VirtualSocketServer::GetDefaultSourceAddress(int family) {
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001237 if (family == AF_INET) {
Niels Möller84d15952021-09-01 10:50:34 +02001238 return default_source_address_v4_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001239 }
1240 if (family == AF_INET6) {
Niels Möller84d15952021-09-01 10:50:34 +02001241 return default_source_address_v6_;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001242 }
1243 return IPAddress();
1244}
Niels Möller84d15952021-09-01 10:50:34 +02001245void VirtualSocketServer::SetDefaultSourceAddress(const IPAddress& from_addr) {
henrikg91d6ede2015-09-17 00:24:34 -07001246 RTC_DCHECK(!IPIsAny(from_addr));
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001247 if (from_addr.family() == AF_INET) {
Niels Möller84d15952021-09-01 10:50:34 +02001248 default_source_address_v4_ = from_addr;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001249 } else if (from_addr.family() == AF_INET6) {
Niels Möller84d15952021-09-01 10:50:34 +02001250 default_source_address_v6_ = from_addr;
Guo-wei Shieh38f88932015-08-13 22:24:02 -07001251 }
1252}
1253
Florent Castellif94c0532021-11-16 13:29:53 +01001254void VirtualSocketServer::set_bandwidth(uint32_t bandwidth) {
1255 webrtc::MutexLock lock(&mutex_);
1256 bandwidth_ = bandwidth;
1257}
1258void VirtualSocketServer::set_network_capacity(uint32_t capacity) {
1259 webrtc::MutexLock lock(&mutex_);
1260 network_capacity_ = capacity;
1261}
1262
1263uint32_t VirtualSocketServer::send_buffer_capacity() const {
1264 webrtc::MutexLock lock(&mutex_);
1265 return send_buffer_capacity_;
1266}
1267void VirtualSocketServer::set_send_buffer_capacity(uint32_t capacity) {
1268 webrtc::MutexLock lock(&mutex_);
1269 send_buffer_capacity_ = capacity;
1270}
1271
1272uint32_t VirtualSocketServer::recv_buffer_capacity() const {
1273 webrtc::MutexLock lock(&mutex_);
1274 return recv_buffer_capacity_;
1275}
1276void VirtualSocketServer::set_recv_buffer_capacity(uint32_t capacity) {
1277 webrtc::MutexLock lock(&mutex_);
1278 recv_buffer_capacity_ = capacity;
1279}
1280
1281void VirtualSocketServer::set_delay_mean(uint32_t delay_mean) {
1282 webrtc::MutexLock lock(&mutex_);
1283 delay_mean_ = delay_mean;
1284}
1285void VirtualSocketServer::set_delay_stddev(uint32_t delay_stddev) {
1286 webrtc::MutexLock lock(&mutex_);
1287 delay_stddev_ = delay_stddev;
1288}
1289void VirtualSocketServer::set_delay_samples(uint32_t delay_samples) {
1290 webrtc::MutexLock lock(&mutex_);
1291 delay_samples_ = delay_samples;
1292}
1293
1294void VirtualSocketServer::set_drop_probability(double drop_prob) {
1295 RTC_DCHECK_GE(drop_prob, 0.0);
1296 RTC_DCHECK_LE(drop_prob, 1.0);
1297
1298 webrtc::MutexLock lock(&mutex_);
1299 drop_prob_ = drop_prob;
1300}
1301
1302void VirtualSocketServer::set_max_udp_payload(size_t payload_size) {
1303 webrtc::MutexLock lock(&mutex_);
1304 max_udp_payload_ = payload_size;
1305}
1306
1307uint32_t VirtualSocketServer::sent_packets() const {
1308 webrtc::MutexLock lock(&mutex_);
1309 return sent_packets_;
1310}
1311
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001312} // namespace rtc