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