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