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