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