henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/virtualsocketserver.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
| 13 | #include <errno.h> |
| 14 | #include <math.h> |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <map> |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 18 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/fakeclock.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/physicalsocketserver.h" |
| 25 | #include "rtc_base/socketaddresspair.h" |
| 26 | #include "rtc_base/thread.h" |
| 27 | #include "rtc_base/timeutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 28 | |
| 29 | namespace rtc { |
| 30 | #if defined(WEBRTC_WIN) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 31 | const in_addr kInitialNextIPv4 = {{{0x01, 0, 0, 0}}}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 32 | #else |
| 33 | // This value is entirely arbitrary, hence the lack of concern about endianness. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 34 | const in_addr kInitialNextIPv4 = {0x01000000}; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 35 | #endif |
| 36 | // Starts at ::2 so as to not cause confusion with ::1. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [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 | [diff] [blame] | 39 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [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 | [diff] [blame] | 46 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [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 | [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 | [diff] [blame] | 56 | MSG_ID_ADDRESS_BOUND, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 57 | MSG_ID_CONNECT, |
| 58 | MSG_ID_DISCONNECT, |
deadbeef | ed3b986 | 2017-06-02 17:33:16 | [diff] [blame] | 59 | MSG_ID_SIGNALREADEVENT, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 13:03:05 | [diff] [blame] | 67 | : size_(size), consumed_(0), from_(from) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 68 | RTC_DCHECK(nullptr != data); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 69 | data_ = new char[size_]; |
| 70 | memcpy(data_, data, size_); |
| 71 | } |
| 72 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 73 | ~Packet() override { delete[] data_; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 20:16:15 | [diff] [blame] | 81 | RTC_DCHECK(size + consumed_ < size_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 13:03:05 | [diff] [blame] | 92 | explicit MessageAddress(const SocketAddress& a) : addr(a) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 93 | SocketAddress addr; |
| 94 | }; |
| 95 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 101 | type_(type), |
| 102 | async_(async), |
| 103 | state_(CS_CLOSED), |
| 104 | error_(0), |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 105 | listen_queue_(nullptr), |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 106 | network_size_(0), |
| 107 | recv_buffer_size_(0), |
| 108 | bound_(false), |
| 109 | was_any_(false) { |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [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 | [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 | [diff] [blame] | 123 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 124 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 125 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 126 | SocketAddress VirtualSocket::GetLocalAddress() const { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 127 | return local_addr_; |
| 128 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 129 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 130 | SocketAddress VirtualSocket::GetRemoteAddress() const { |
| 131 | return remote_addr_; |
| 132 | } |
| 133 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 142 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 146 | local_addr_.Clear(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [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 21:17:27 | [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 | [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 | [diff] [blame] | 167 | } |
| 168 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 22:06:41 | [diff] [blame] | 180 | listen_queue_ = nullptr; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 22:06:41 | [diff] [blame] | 206 | RTC_DCHECK(nullptr != it->pdata); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 13:03:05 | [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 | [diff] [blame] | 245 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 246 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 255 | return -1; |
| 256 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 257 | return SendTcp(pv, cb); |
| 258 | } |
| 259 | } |
| 260 | |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 261 | int VirtualSocket::Recv(void* pv, size_t cb, int64_t* timestamp) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 262 | SocketAddress addr; |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [diff] [blame] | 263 | return RecvFrom(pv, cb, &addr, timestamp); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 264 | } |
| 265 | |
Stefan Holmer | 9131efd | 2016-05-23 16:19:26 | [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 | [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 | [diff] [blame] | 284 | } |
| 285 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 288 | size_t data_read = std::min(cb, packet->size()); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 289 | memcpy(pv, packet->data(), data_read); |
| 290 | *paddr = packet->from(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 291 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 17:33:16 | [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 | [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 22:06:41 | [diff] [blame] | 312 | RTC_DCHECK(nullptr != sender); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 313 | server_->SendTcp(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 314 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 20:16:15 | [diff] [blame] | 321 | RTC_DCHECK(SOCK_STREAM == type_); |
| 322 | RTC_DCHECK(CS_CLOSED == state_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 323 | if (local_addr_.IsNil()) { |
| 324 | error_ = EINVAL; |
| 325 | return -1; |
| 326 | } |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 327 | RTC_DCHECK(nullptr == listen_queue_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 22:06:41 | [diff] [blame] | 334 | if (nullptr == listen_queue_) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 335 | error_ = EINVAL; |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 336 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 337 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 340 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 351 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 357 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 358 | error_ = EWOULDBLOCK; |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 359 | return nullptr; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 360 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 361 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 378 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 382 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 387 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 388 | void VirtualSocket::OnMessage(Message* pmsg) { |
| 389 | if (pmsg->message_id == MSG_ID_PACKET) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 390 | RTC_DCHECK(nullptr != pmsg->pdata); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 391 | Packet* packet = static_cast<Packet*>(pmsg->pdata); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 392 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 22:06:41 | [diff] [blame] | 399 | RTC_DCHECK(nullptr != pmsg->pdata); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 400 | MessageAddress* data = static_cast<MessageAddress*>(pmsg->pdata); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 401 | if (listen_queue_ != nullptr) { |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 402 | listen_queue_->push_back(data->addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 403 | if (async_) { |
| 404 | SignalReadEvent(this); |
| 405 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 408 | } else { |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [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 | [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 20:16:15 | [diff] [blame] | 415 | RTC_DCHECK(SOCK_STREAM == type_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 424 | } else if (pmsg->message_id == MSG_ID_ADDRESS_BOUND) { |
| 425 | SignalAddressReady(this, GetLocalAddress()); |
deadbeef | ed3b986 | 2017-06-02 17:33:16 | [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 | [diff] [blame] | 430 | } else { |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 431 | RTC_NOTREACHED(); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 450 | } |
| 451 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 452 | if (type_ == SOCK_DGRAM) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 453 | remote_addr_ = addr; |
| 454 | state_ = CS_CONNECTED; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 459 | return -1; |
| 460 | } |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 20:16:15 | [diff] [blame] | 467 | RTC_DCHECK(CS_CONNECTING == state_); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 488 | } |
| 489 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 493 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 20:16:15 | [diff] [blame] | 497 | ready_to_send_ = false; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [diff] [blame] | 498 | error_ = EWOULDBLOCK; |
| 499 | return -1; |
| 500 | } |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 | [diff] [blame] | 501 | size_t consumed = std::min(cb, capacity); |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 | [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 | [diff] [blame] | 507 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [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 21:30:28 | [diff] [blame] | 524 | VirtualSocketServer::VirtualSocketServer() : VirtualSocketServer(nullptr) {} |
| 525 | |
| 526 | VirtualSocketServer::VirtualSocketServer(FakeClock* fake_clock) |
| 527 | : fake_clock_(fake_clock), |
| 528 | wakeup_(/*manual_reset=*/false, /*initially_signaled=*/false), |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 529 | msg_queue_(nullptr), |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 530 | stop_on_idle_(false), |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 531 | next_ipv4_(kInitialNextIPv4), |
| 532 | next_ipv6_(kInitialNextIPv6), |
| 533 | next_port_(kFirstEphemeralPort), |
| 534 | bindings_(new AddressMap()), |
| 535 | connections_(new ConnectionMap()), |
| 536 | bandwidth_(0), |
| 537 | network_capacity_(kDefaultNetworkCapacity), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 538 | send_buffer_capacity_(kDefaultTcpBufferSize), |
| 539 | recv_buffer_capacity_(kDefaultTcpBufferSize), |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 540 | delay_mean_(0), |
| 541 | delay_stddev_(0), |
| 542 | delay_samples_(NUM_SAMPLES), |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 543 | drop_prob_(0.0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 544 | UpdateDelayDistribution(); |
| 545 | } |
| 546 | |
| 547 | VirtualSocketServer::~VirtualSocketServer() { |
| 548 | delete bindings_; |
| 549 | delete connections_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | IPAddress VirtualSocketServer::GetNextIP(int family) { |
| 553 | if (family == AF_INET) { |
| 554 | IPAddress next_ip(next_ipv4_); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 555 | next_ipv4_.s_addr = HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 556 | return next_ip; |
| 557 | } else if (family == AF_INET6) { |
| 558 | IPAddress next_ip(next_ipv6_); |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 559 | uint32_t* as_ints = reinterpret_cast<uint32_t*>(&next_ipv6_.s6_addr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 560 | as_ints[3] += 1; |
| 561 | return next_ip; |
| 562 | } |
| 563 | return IPAddress(); |
| 564 | } |
| 565 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 566 | uint16_t VirtualSocketServer::GetNextPort() { |
| 567 | uint16_t port = next_port_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 568 | if (next_port_ < kLastEphemeralPort) { |
| 569 | ++next_port_; |
| 570 | } else { |
| 571 | next_port_ = kFirstEphemeralPort; |
| 572 | } |
| 573 | return port; |
| 574 | } |
| 575 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 576 | void VirtualSocketServer::SetSendingBlocked(bool blocked) { |
| 577 | if (blocked == sending_blocked_) { |
| 578 | // Unchanged; nothing to do. |
| 579 | return; |
| 580 | } |
| 581 | sending_blocked_ = blocked; |
| 582 | if (!sending_blocked_) { |
| 583 | // Sending was blocked, but is now unblocked. This signal gives sockets a |
| 584 | // chance to fire SignalWriteEvent, and for TCP, send buffered data. |
| 585 | SignalReadyToSend(); |
| 586 | } |
| 587 | } |
| 588 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 589 | Socket* VirtualSocketServer::CreateSocket(int family, int type) { |
| 590 | return CreateSocketInternal(family, type); |
| 591 | } |
| 592 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 593 | AsyncSocket* VirtualSocketServer::CreateAsyncSocket(int family, int type) { |
| 594 | return CreateSocketInternal(family, type); |
| 595 | } |
| 596 | |
| 597 | VirtualSocket* VirtualSocketServer::CreateSocketInternal(int family, int type) { |
tommi | 5ce1a2a | 2016-05-14 10:19:31 | [diff] [blame] | 598 | VirtualSocket* socket = new VirtualSocket(this, family, type, true); |
| 599 | SignalSocketCreated(socket); |
| 600 | return socket; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void VirtualSocketServer::SetMessageQueue(MessageQueue* msg_queue) { |
| 604 | msg_queue_ = msg_queue; |
| 605 | if (msg_queue_) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 606 | msg_queue_->SignalQueueDestroyed.connect( |
| 607 | this, &VirtualSocketServer::OnMessageQueueDestroyed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | bool VirtualSocketServer::Wait(int cmsWait, bool process_io) { |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 612 | RTC_DCHECK(msg_queue_ == Thread::Current()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 613 | if (stop_on_idle_ && Thread::Current()->empty()) { |
| 614 | return false; |
| 615 | } |
deadbeef | 98e186c | 2017-05-17 01:00:06 | [diff] [blame] | 616 | // Note: we don't need to do anything with |process_io| since we don't have |
| 617 | // any real I/O. Received packets come in the form of queued messages, so |
| 618 | // MessageQueue will ensure WakeUp is called if another thread sends a |
| 619 | // packet. |
| 620 | wakeup_.Wait(cmsWait); |
| 621 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void VirtualSocketServer::WakeUp() { |
deadbeef | 98e186c | 2017-05-17 01:00:06 | [diff] [blame] | 625 | wakeup_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 626 | } |
| 627 | |
deadbeef | 5c3c104 | 2017-08-04 22:01:57 | [diff] [blame] | 628 | void VirtualSocketServer::SetAlternativeLocalAddress( |
| 629 | const rtc::IPAddress& address, |
| 630 | const rtc::IPAddress& alternative) { |
| 631 | alternative_address_mapping_[address] = alternative; |
| 632 | } |
| 633 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 634 | bool VirtualSocketServer::ProcessMessagesUntilIdle() { |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 635 | RTC_DCHECK(msg_queue_ == Thread::Current()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 636 | stop_on_idle_ = true; |
| 637 | while (!msg_queue_->empty()) { |
deadbeef | 22e0814 | 2017-06-12 21:30:28 | [diff] [blame] | 638 | if (fake_clock_) { |
| 639 | // If using a fake clock, advance it in millisecond increments until the |
Bjorn Mellem | 6eb03b8 | 2017-06-13 22:07:41 | [diff] [blame] | 640 | // queue is empty. |
Sebastian Jansson | 5f83cf0 | 2018-05-08 12:52:22 | [diff] [blame] | 641 | fake_clock_->AdvanceTime(webrtc::TimeDelta::ms(1)); |
deadbeef | 22e0814 | 2017-06-12 21:30:28 | [diff] [blame] | 642 | } else { |
| 643 | // Otherwise, run a normal message loop. |
| 644 | Message msg; |
| 645 | if (msg_queue_->Get(&msg, Thread::kForever)) { |
| 646 | msg_queue_->Dispatch(&msg); |
| 647 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | stop_on_idle_ = false; |
| 651 | return !msg_queue_->IsQuitting(); |
| 652 | } |
| 653 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 654 | void VirtualSocketServer::SetNextPortForTesting(uint16_t port) { |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 | [diff] [blame] | 655 | next_port_ = port; |
| 656 | } |
| 657 | |
Guo-wei Shieh | be508a1 | 2015-04-06 19:48:47 | [diff] [blame] | 658 | bool VirtualSocketServer::CloseTcpConnections( |
| 659 | const SocketAddress& addr_local, |
| 660 | const SocketAddress& addr_remote) { |
| 661 | VirtualSocket* socket = LookupConnection(addr_local, addr_remote); |
| 662 | if (!socket) { |
| 663 | return false; |
| 664 | } |
| 665 | // Signal the close event on the local connection first. |
| 666 | socket->SignalCloseEvent(socket, 0); |
| 667 | |
| 668 | // Trigger the remote connection's close event. |
| 669 | socket->Close(); |
| 670 | |
| 671 | return true; |
| 672 | } |
| 673 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 674 | int VirtualSocketServer::Bind(VirtualSocket* socket, |
| 675 | const SocketAddress& addr) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 676 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 677 | // Address must be completely specified at this point |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 678 | RTC_DCHECK(!IPIsUnspec(addr.ipaddr())); |
| 679 | RTC_DCHECK(addr.port() != 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 680 | |
| 681 | // Normalize the address (turns v6-mapped addresses into v4-addresses). |
| 682 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
| 683 | |
| 684 | AddressMap::value_type entry(normalized, socket); |
| 685 | return bindings_->insert(entry).second ? 0 : -1; |
| 686 | } |
| 687 | |
| 688 | int VirtualSocketServer::Bind(VirtualSocket* socket, SocketAddress* addr) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 689 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 690 | |
deadbeef | 5c3c104 | 2017-08-04 22:01:57 | [diff] [blame] | 691 | // Normalize the IP. |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 | [diff] [blame] | 692 | if (!IPIsUnspec(addr->ipaddr())) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 693 | addr->SetIP(addr->ipaddr().Normalized()); |
| 694 | } else { |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 695 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 696 | } |
| 697 | |
deadbeef | 5c3c104 | 2017-08-04 22:01:57 | [diff] [blame] | 698 | // If the IP appears in |alternative_address_mapping_|, meaning the test has |
| 699 | // configured sockets bound to this IP to actually use another IP, replace |
| 700 | // the IP here. |
| 701 | auto alternative = alternative_address_mapping_.find(addr->ipaddr()); |
| 702 | if (alternative != alternative_address_mapping_.end()) { |
| 703 | addr->SetIP(alternative->second); |
| 704 | } |
| 705 | |
| 706 | // Assign a port if not assigned. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 707 | if (addr->port() == 0) { |
| 708 | for (int i = 0; i < kEphemeralPortCount; ++i) { |
| 709 | addr->SetPort(GetNextPort()); |
| 710 | if (bindings_->find(*addr) == bindings_->end()) { |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | return Bind(socket, *addr); |
| 717 | } |
| 718 | |
| 719 | VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 720 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 721 | AddressMap::iterator it = bindings_->find(normalized); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 722 | if (it != bindings_->end()) { |
| 723 | return it->second; |
| 724 | } |
| 725 | |
| 726 | IPAddress default_ip = GetDefaultRoute(addr.ipaddr().family()); |
| 727 | if (!IPIsUnspec(default_ip) && addr.ipaddr() == default_ip) { |
| 728 | // If we can't find a binding for the packet which is sent to the interface |
| 729 | // corresponding to the default route, it should match a binding with the |
| 730 | // correct port to the any address. |
| 731 | SocketAddress sock_addr = |
| 732 | EmptySocketAddressWithFamily(addr.ipaddr().family()); |
| 733 | sock_addr.SetPort(addr.port()); |
| 734 | return LookupBinding(sock_addr); |
| 735 | } |
| 736 | |
| 737 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | int VirtualSocketServer::Unbind(const SocketAddress& addr, |
| 741 | VirtualSocket* socket) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 742 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 743 | RTC_DCHECK((*bindings_)[normalized] == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 744 | bindings_->erase(bindings_->find(normalized)); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | void VirtualSocketServer::AddConnection(const SocketAddress& local, |
| 749 | const SocketAddress& remote, |
| 750 | VirtualSocket* remote_socket) { |
| 751 | // Add this socket pair to our routing table. This will allow |
| 752 | // multiple clients to connect to the same server address. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 753 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 754 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 755 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 756 | connections_->insert(std::pair<SocketAddressPair, VirtualSocket*>( |
| 757 | address_pair, remote_socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | VirtualSocket* VirtualSocketServer::LookupConnection( |
| 761 | const SocketAddress& local, |
| 762 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 763 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 764 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 765 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 766 | ConnectionMap::iterator it = connections_->find(address_pair); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 767 | return (connections_->end() != it) ? it->second : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void VirtualSocketServer::RemoveConnection(const SocketAddress& local, |
| 771 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 772 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 773 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 774 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 775 | connections_->erase(address_pair); |
| 776 | } |
| 777 | |
| 778 | static double Random() { |
| 779 | return static_cast<double>(rand()) / RAND_MAX; |
| 780 | } |
| 781 | |
| 782 | int VirtualSocketServer::Connect(VirtualSocket* socket, |
| 783 | const SocketAddress& remote_addr, |
| 784 | bool use_delay) { |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 785 | uint32_t delay = use_delay ? GetTransitDelay(socket) : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 786 | VirtualSocket* remote = LookupBinding(remote_addr); |
| 787 | if (!CanInteractWith(socket, remote)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 788 | RTC_LOG(LS_INFO) << "Address family mismatch between " |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 789 | << socket->GetLocalAddress().ToString() << " and " |
| 790 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 791 | return -1; |
| 792 | } |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 793 | if (remote != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 794 | SocketAddress addr = socket->GetLocalAddress(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 795 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 796 | new MessageAddress(addr)); |
| 797 | } else { |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 798 | RTC_LOG(LS_INFO) << "No one listening at " << remote_addr.ToString(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 799 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 800 | } |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { |
| 805 | if (socket) { |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 806 | // If we simulate packets being delayed, we should simulate the |
| 807 | // equivalent of a FIN being delayed as well. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 808 | uint32_t delay = GetTransitDelay(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 809 | // Remove the mapping. |
Taylor Brandstetter | 716d07a | 2016-06-27 21:07:41 | [diff] [blame] | 810 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 811 | return true; |
| 812 | } |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | int VirtualSocketServer::SendUdp(VirtualSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 817 | const char* data, |
| 818 | size_t data_size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 819 | const SocketAddress& remote_addr) { |
Taylor Brandstetter | 389a97c | 2018-01-04 00:26:06 | [diff] [blame] | 820 | ++sent_packets_; |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 821 | if (sending_blocked_) { |
| 822 | CritScope cs(&socket->crit_); |
| 823 | socket->ready_to_send_ = false; |
| 824 | socket->error_ = EWOULDBLOCK; |
| 825 | return -1; |
| 826 | } |
| 827 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 828 | // See if we want to drop this packet. |
| 829 | if (Random() < drop_prob_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 830 | RTC_LOG(LS_VERBOSE) << "Dropping packet: bad luck"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 831 | return static_cast<int>(data_size); |
| 832 | } |
| 833 | |
| 834 | VirtualSocket* recipient = LookupBinding(remote_addr); |
| 835 | if (!recipient) { |
| 836 | // Make a fake recipient for address family checking. |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 837 | std::unique_ptr<VirtualSocket> dummy_socket( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 838 | CreateSocketInternal(AF_INET, SOCK_DGRAM)); |
| 839 | dummy_socket->SetLocalAddress(remote_addr); |
| 840 | if (!CanInteractWith(socket, dummy_socket.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 841 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 842 | << socket->GetLocalAddress().ToString() << " and " |
| 843 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 844 | return -1; |
| 845 | } |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 846 | RTC_LOG(LS_VERBOSE) << "No one listening at " << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 847 | return static_cast<int>(data_size); |
| 848 | } |
| 849 | |
| 850 | if (!CanInteractWith(socket, recipient)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 851 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 11:40:05 | [diff] [blame] | 852 | << socket->GetLocalAddress().ToString() << " and " |
| 853 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 854 | return -1; |
| 855 | } |
| 856 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 857 | { |
| 858 | CritScope cs(&socket->crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 859 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 860 | int64_t cur_time = TimeMillis(); |
| 861 | PurgeNetworkPackets(socket, cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 862 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 863 | // Determine whether we have enough bandwidth to accept this packet. To do |
| 864 | // this, we need to update the send queue. Once we know it's current size, |
| 865 | // we know whether we can fit this packet. |
| 866 | // |
| 867 | // NOTE: There are better algorithms for maintaining such a queue (such as |
| 868 | // "Derivative Random Drop"); however, this algorithm is a more accurate |
| 869 | // simulation of what a normal network would do. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 870 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 871 | size_t packet_size = data_size + UDP_HEADER_SIZE; |
| 872 | if (socket->network_size_ + packet_size > network_capacity_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 873 | RTC_LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded"; |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 874 | return static_cast<int>(data_size); |
| 875 | } |
| 876 | |
| 877 | AddPacketToNetwork(socket, recipient, cur_time, data, data_size, |
| 878 | UDP_HEADER_SIZE, false); |
| 879 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 880 | return static_cast<int>(data_size); |
| 881 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | void VirtualSocketServer::SendTcp(VirtualSocket* socket) { |
Taylor Brandstetter | 389a97c | 2018-01-04 00:26:06 | [diff] [blame] | 885 | ++sent_packets_; |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 886 | if (sending_blocked_) { |
| 887 | // Eventually the socket's buffer will fill and VirtualSocket::SendTcp will |
| 888 | // set EWOULDBLOCK. |
| 889 | return; |
| 890 | } |
| 891 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 892 | // TCP can't send more data than will fill up the receiver's buffer. |
| 893 | // We track the data that is in the buffer plus data in flight using the |
| 894 | // recipient's recv_buffer_size_. Anything beyond that must be stored in the |
| 895 | // sender's buffer. We will trigger the buffered data to be sent when data |
| 896 | // is read from the recv_buffer. |
| 897 | |
| 898 | // Lookup the local/remote pair in the connections table. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 899 | VirtualSocket* recipient = |
| 900 | LookupConnection(socket->local_addr_, socket->remote_addr_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 901 | if (!recipient) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 902 | RTC_LOG(LS_VERBOSE) << "Sending data to no one."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 903 | return; |
| 904 | } |
| 905 | |
| 906 | CritScope cs(&socket->crit_); |
| 907 | |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 908 | int64_t cur_time = TimeMillis(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 909 | PurgeNetworkPackets(socket, cur_time); |
| 910 | |
| 911 | while (true) { |
| 912 | size_t available = recv_buffer_capacity_ - recipient->recv_buffer_size_; |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 | [diff] [blame] | 913 | size_t max_data_size = |
| 914 | std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE); |
| 915 | size_t data_size = std::min(socket->send_buffer_.size(), max_data_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 916 | if (0 == data_size) |
| 917 | break; |
| 918 | |
| 919 | AddPacketToNetwork(socket, recipient, cur_time, &socket->send_buffer_[0], |
| 920 | data_size, TCP_HEADER_SIZE, true); |
| 921 | recipient->recv_buffer_size_ += data_size; |
| 922 | |
| 923 | size_t new_buffer_size = socket->send_buffer_.size() - data_size; |
| 924 | // Avoid undefined access beyond the last element of the vector. |
| 925 | // This only happens when new_buffer_size is 0. |
| 926 | if (data_size < socket->send_buffer_.size()) { |
| 927 | // memmove is required for potentially overlapping source/destination. |
| 928 | memmove(&socket->send_buffer_[0], &socket->send_buffer_[data_size], |
| 929 | new_buffer_size); |
| 930 | } |
| 931 | socket->send_buffer_.resize(new_buffer_size); |
| 932 | } |
| 933 | |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 934 | if (!socket->ready_to_send_ && |
| 935 | (socket->send_buffer_.size() < send_buffer_capacity_)) { |
| 936 | socket->ready_to_send_ = true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 937 | socket->SignalWriteEvent(socket); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender, |
| 942 | VirtualSocket* recipient, |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 943 | int64_t cur_time, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 944 | const char* data, |
| 945 | size_t data_size, |
| 946 | size_t header_size, |
| 947 | bool ordered) { |
| 948 | VirtualSocket::NetworkEntry entry; |
| 949 | entry.size = data_size + header_size; |
| 950 | |
| 951 | sender->network_size_ += entry.size; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 952 | uint32_t send_delay = SendDelay(static_cast<uint32_t>(sender->network_size_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 953 | entry.done_time = cur_time + send_delay; |
| 954 | sender->network_.push_back(entry); |
| 955 | |
| 956 | // Find the delay for crossing the many virtual hops of the network. |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 957 | uint32_t transit_delay = GetTransitDelay(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 958 | |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 959 | // When the incoming packet is from a binding of the any address, translate it |
| 960 | // to the default route here such that the recipient will see the default |
| 961 | // route. |
| 962 | SocketAddress sender_addr = sender->local_addr_; |
| 963 | IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
| 964 | if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
| 965 | sender_addr.SetIP(default_ip); |
| 966 | } |
| 967 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 968 | // Post the packet as a message to be delivered (on our own thread) |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 969 | Packet* p = new Packet(data, data_size, sender_addr); |
| 970 | |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 971 | int64_t ts = TimeAfter(send_delay + transit_delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 972 | if (ordered) { |
| 973 | // Ensure that new packets arrive after previous ones |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 974 | ts = std::max(ts, sender->last_delivery_time_); |
| 975 | // A socket should not have both ordered and unordered delivery, so its last |
| 976 | // delivery time only needs to be updated when it has ordered delivery. |
| 977 | sender->last_delivery_time_ = ts; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 978 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 21:17:27 | [diff] [blame] | 979 | msg_queue_->PostAt(RTC_FROM_HERE, ts, recipient, MSG_ID_PACKET, p); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | void VirtualSocketServer::PurgeNetworkPackets(VirtualSocket* socket, |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 983 | int64_t cur_time) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 984 | while (!socket->network_.empty() && |
| 985 | (socket->network_.front().done_time <= cur_time)) { |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 986 | RTC_DCHECK(socket->network_size_ >= socket->network_.front().size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 987 | socket->network_size_ -= socket->network_.front().size; |
| 988 | socket->network_.pop_front(); |
| 989 | } |
| 990 | } |
| 991 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 992 | uint32_t VirtualSocketServer::SendDelay(uint32_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 993 | if (bandwidth_ == 0) |
| 994 | return 0; |
| 995 | else |
| 996 | return 1000 * size / bandwidth_; |
| 997 | } |
| 998 | |
| 999 | #if 0 |
| 1000 | void PrintFunction(std::vector<std::pair<double, double> >* f) { |
| 1001 | return; |
| 1002 | double sum = 0; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 1003 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1004 | std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl; |
| 1005 | sum += (*f)[i].second; |
| 1006 | } |
| 1007 | if (!f->empty()) { |
| 1008 | const double mean = sum / f->size(); |
| 1009 | double sum_sq_dev = 0; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 1010 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1011 | double dev = (*f)[i].second - mean; |
| 1012 | sum_sq_dev += dev * dev; |
| 1013 | } |
| 1014 | std::cout << "Mean = " << mean << " StdDev = " |
| 1015 | << sqrt(sum_sq_dev / f->size()) << std::endl; |
| 1016 | } |
| 1017 | } |
| 1018 | #endif // <unused> |
| 1019 | |
| 1020 | void VirtualSocketServer::UpdateDelayDistribution() { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1021 | Function* dist = |
| 1022 | CreateDistribution(delay_mean_, delay_stddev_, delay_samples_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1023 | // We take a lock just to make sure we don't leak memory. |
| 1024 | { |
| 1025 | CritScope cs(&delay_crit_); |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1026 | delay_dist_.reset(dist); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static double PI = 4 * atan(1.0); |
| 1031 | |
| 1032 | static double Normal(double x, double mean, double stddev) { |
| 1033 | double a = (x - mean) * (x - mean) / (2 * stddev * stddev); |
| 1034 | return exp(-a) / (stddev * sqrt(2 * PI)); |
| 1035 | } |
| 1036 | |
| 1037 | #if 0 // static unused gives a warning |
| 1038 | static double Pareto(double x, double min, double k) { |
| 1039 | if (x < min) |
| 1040 | return 0; |
| 1041 | else |
| 1042 | return k * std::pow(min, k) / std::pow(x, k+1); |
| 1043 | } |
| 1044 | #endif |
| 1045 | |
| 1046 | VirtualSocketServer::Function* VirtualSocketServer::CreateDistribution( |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 1047 | uint32_t mean, |
| 1048 | uint32_t stddev, |
| 1049 | uint32_t samples) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1050 | Function* f = new Function(); |
| 1051 | |
| 1052 | if (0 == stddev) { |
| 1053 | f->push_back(Point(mean, 1.0)); |
| 1054 | } else { |
| 1055 | double start = 0; |
| 1056 | if (mean >= 4 * static_cast<double>(stddev)) |
| 1057 | start = mean - 4 * static_cast<double>(stddev); |
| 1058 | double end = mean + 4 * static_cast<double>(stddev); |
| 1059 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 1060 | for (uint32_t i = 0; i < samples; i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1061 | double x = start + (end - start) * i / (samples - 1); |
| 1062 | double y = Normal(x, mean, stddev); |
| 1063 | f->push_back(Point(x, y)); |
| 1064 | } |
| 1065 | } |
| 1066 | return Resample(Invert(Accumulate(f)), 0, 1, samples); |
| 1067 | } |
| 1068 | |
Honghai Zhang | c67e0f5 | 2016-09-19 23:57:37 | [diff] [blame] | 1069 | uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) { |
| 1070 | // Use the delay based on the address if it is set. |
| 1071 | auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr()); |
| 1072 | if (iter != delay_by_ip_.end()) { |
| 1073 | return static_cast<uint32_t>(iter->second); |
| 1074 | } |
| 1075 | // Otherwise, use the delay from the distribution distribution. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1076 | size_t index = rand() % delay_dist_->size(); |
| 1077 | double delay = (*delay_dist_)[index].second; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1078 | // RTC_LOG_F(LS_INFO) << "random[" << index << "] = " << delay; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 1079 | return static_cast<uint32_t>(delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | struct FunctionDomainCmp { |
| 1083 | bool operator()(const VirtualSocketServer::Point& p1, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1084 | const VirtualSocketServer::Point& p2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1085 | return p1.first < p2.first; |
| 1086 | } |
| 1087 | bool operator()(double v1, const VirtualSocketServer::Point& p2) { |
| 1088 | return v1 < p2.first; |
| 1089 | } |
| 1090 | bool operator()(const VirtualSocketServer::Point& p1, double v2) { |
| 1091 | return p1.first < v2; |
| 1092 | } |
| 1093 | }; |
| 1094 | |
| 1095 | VirtualSocketServer::Function* VirtualSocketServer::Accumulate(Function* f) { |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 1096 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1097 | double v = 0; |
| 1098 | for (Function::size_type i = 0; i < f->size() - 1; ++i) { |
| 1099 | double dx = (*f)[i + 1].first - (*f)[i].first; |
| 1100 | double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2; |
| 1101 | (*f)[i].second = v; |
| 1102 | v = v + dx * avgy; |
| 1103 | } |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1104 | (*f)[f->size() - 1].second = v; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1105 | return f; |
| 1106 | } |
| 1107 | |
| 1108 | VirtualSocketServer::Function* VirtualSocketServer::Invert(Function* f) { |
| 1109 | for (Function::size_type i = 0; i < f->size(); ++i) |
| 1110 | std::swap((*f)[i].first, (*f)[i].second); |
| 1111 | |
| 1112 | std::sort(f->begin(), f->end(), FunctionDomainCmp()); |
| 1113 | return f; |
| 1114 | } |
| 1115 | |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 1116 | VirtualSocketServer::Function* VirtualSocketServer::Resample(Function* f, |
| 1117 | double x1, |
| 1118 | double x2, |
| 1119 | uint32_t samples) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1120 | Function* g = new Function(); |
| 1121 | |
| 1122 | for (size_t i = 0; i < samples; i++) { |
| 1123 | double x = x1 + (x2 - x1) * i / (samples - 1); |
| 1124 | double y = Evaluate(f, x); |
| 1125 | g->push_back(Point(x, y)); |
| 1126 | } |
| 1127 | |
| 1128 | delete f; |
| 1129 | return g; |
| 1130 | } |
| 1131 | |
| 1132 | double VirtualSocketServer::Evaluate(Function* f, double x) { |
| 1133 | Function::iterator iter = |
| 1134 | std::lower_bound(f->begin(), f->end(), x, FunctionDomainCmp()); |
| 1135 | if (iter == f->begin()) { |
| 1136 | return (*f)[0].second; |
| 1137 | } else if (iter == f->end()) { |
Taylor Brandstetter | e753641 | 2016-09-09 20:16:15 | [diff] [blame] | 1138 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1139 | return (*f)[f->size() - 1].second; |
| 1140 | } else if (iter->first == x) { |
| 1141 | return iter->second; |
| 1142 | } else { |
| 1143 | double x1 = (iter - 1)->first; |
| 1144 | double y1 = (iter - 1)->second; |
| 1145 | double x2 = iter->first; |
| 1146 | double y2 = iter->second; |
| 1147 | return y1 + (y2 - y1) * (x - x1) / (x2 - x1); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | bool VirtualSocketServer::CanInteractWith(VirtualSocket* local, |
| 1152 | VirtualSocket* remote) { |
| 1153 | if (!local || !remote) { |
| 1154 | return false; |
| 1155 | } |
| 1156 | IPAddress local_ip = local->GetLocalAddress().ipaddr(); |
| 1157 | IPAddress remote_ip = remote->GetLocalAddress().ipaddr(); |
| 1158 | IPAddress local_normalized = local_ip.Normalized(); |
| 1159 | IPAddress remote_normalized = remote_ip.Normalized(); |
| 1160 | // Check if the addresses are the same family after Normalization (turns |
| 1161 | // mapped IPv6 address into IPv4 addresses). |
| 1162 | // This will stop unmapped V6 addresses from talking to mapped V6 addresses. |
| 1163 | if (local_normalized.family() == remote_normalized.family()) { |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY. |
| 1168 | int remote_v6_only = 0; |
| 1169 | remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only); |
| 1170 | if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) { |
| 1171 | return true; |
| 1172 | } |
| 1173 | // Same check, backwards. |
| 1174 | int local_v6_only = 0; |
| 1175 | local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only); |
| 1176 | if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) { |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | // Check to see if either socket was explicitly bound to IPv6-any. |
| 1181 | // These sockets can talk with anyone. |
| 1182 | if (local_ip.family() == AF_INET6 && local->was_any()) { |
| 1183 | return true; |
| 1184 | } |
| 1185 | if (remote_ip.family() == AF_INET6 && remote->was_any()) { |
| 1186 | return true; |
| 1187 | } |
| 1188 | |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1192 | IPAddress VirtualSocketServer::GetDefaultRoute(int family) { |
| 1193 | if (family == AF_INET) { |
| 1194 | return default_route_v4_; |
| 1195 | } |
| 1196 | if (family == AF_INET6) { |
| 1197 | return default_route_v6_; |
| 1198 | } |
| 1199 | return IPAddress(); |
| 1200 | } |
| 1201 | void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 1202 | RTC_DCHECK(!IPIsAny(from_addr)); |
Guo-wei Shieh | 38f8893 | 2015-08-14 05:24:02 | [diff] [blame] | 1203 | if (from_addr.family() == AF_INET) { |
| 1204 | default_route_v4_ = from_addr; |
| 1205 | } else if (from_addr.family() == AF_INET6) { |
| 1206 | default_route_v6_ = from_addr; |
| 1207 | } |
| 1208 | } |
| 1209 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1210 | } // namespace rtc |