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