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