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