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