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