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