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 | AsyncSocket* VirtualSocketServer::CreateAsyncSocket(int family, int type) { |
| 618 | return CreateSocketInternal(family, type); |
| 619 | } |
| 620 | |
| 621 | VirtualSocket* VirtualSocketServer::CreateSocketInternal(int family, int type) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 622 | return new VirtualSocket(this, family, type, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Sebastian Jansson | 290de82 | 2020-01-09 14:20:23 +0100 | [diff] [blame] | 625 | void VirtualSocketServer::SetMessageQueue(Thread* msg_queue) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 626 | msg_queue_ = msg_queue; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | bool VirtualSocketServer::Wait(int cmsWait, bool process_io) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 630 | RTC_DCHECK(msg_queue_ == Thread::Current()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 631 | if (stop_on_idle_ && Thread::Current()->empty()) { |
| 632 | return false; |
| 633 | } |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame^] | 634 | // 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] | 635 | // 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] | 636 | // Thread will ensure WakeUp is called if another thread sends a |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 637 | // packet. |
| 638 | wakeup_.Wait(cmsWait); |
| 639 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void VirtualSocketServer::WakeUp() { |
deadbeef | 98e186c | 2017-05-16 18:00:06 -0700 | [diff] [blame] | 643 | wakeup_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 644 | } |
| 645 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 646 | void VirtualSocketServer::SetAlternativeLocalAddress( |
| 647 | const rtc::IPAddress& address, |
| 648 | const rtc::IPAddress& alternative) { |
| 649 | alternative_address_mapping_[address] = alternative; |
| 650 | } |
| 651 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 652 | bool VirtualSocketServer::ProcessMessagesUntilIdle() { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 653 | RTC_DCHECK(msg_queue_ == Thread::Current()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 654 | stop_on_idle_ = true; |
| 655 | while (!msg_queue_->empty()) { |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 656 | if (fake_clock_) { |
| 657 | // If using a fake clock, advance it in millisecond increments until the |
Bjorn Mellem | 6eb03b8 | 2017-06-13 15:07:41 -0700 | [diff] [blame] | 658 | // queue is empty. |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 659 | fake_clock_->AdvanceTime(webrtc::TimeDelta::Millis(1)); |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 660 | } else { |
| 661 | // Otherwise, run a normal message loop. |
| 662 | Message msg; |
| 663 | if (msg_queue_->Get(&msg, Thread::kForever)) { |
| 664 | msg_queue_->Dispatch(&msg); |
| 665 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | stop_on_idle_ = false; |
| 669 | return !msg_queue_->IsQuitting(); |
| 670 | } |
| 671 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 672 | void VirtualSocketServer::SetNextPortForTesting(uint16_t port) { |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 +0000 | [diff] [blame] | 673 | next_port_ = port; |
| 674 | } |
| 675 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 676 | bool VirtualSocketServer::CloseTcpConnections( |
| 677 | const SocketAddress& addr_local, |
| 678 | const SocketAddress& addr_remote) { |
| 679 | VirtualSocket* socket = LookupConnection(addr_local, addr_remote); |
| 680 | if (!socket) { |
| 681 | return false; |
| 682 | } |
| 683 | // Signal the close event on the local connection first. |
| 684 | socket->SignalCloseEvent(socket, 0); |
| 685 | |
| 686 | // Trigger the remote connection's close event. |
| 687 | socket->Close(); |
| 688 | |
| 689 | return true; |
| 690 | } |
| 691 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 692 | int VirtualSocketServer::Bind(VirtualSocket* socket, |
| 693 | const SocketAddress& addr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 694 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 695 | // Address must be completely specified at this point |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 696 | RTC_DCHECK(!IPIsUnspec(addr.ipaddr())); |
| 697 | RTC_DCHECK(addr.port() != 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 698 | |
| 699 | // Normalize the address (turns v6-mapped addresses into v4-addresses). |
| 700 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
| 701 | |
| 702 | AddressMap::value_type entry(normalized, socket); |
Niels Möller | d44532a | 2021-02-18 14:38:14 +0100 | [diff] [blame] | 703 | return bindings_->insert(entry).second ? 0 : -1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | int VirtualSocketServer::Bind(VirtualSocket* socket, SocketAddress* addr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 707 | RTC_DCHECK(nullptr != socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 708 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 709 | // Normalize the IP. |
guoweis@webrtc.org | d3b453b | 2015-02-14 00:43:41 +0000 | [diff] [blame] | 710 | if (!IPIsUnspec(addr->ipaddr())) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 711 | addr->SetIP(addr->ipaddr().Normalized()); |
| 712 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 713 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame^] | 716 | // If the IP appears in `alternative_address_mapping_`, meaning the test has |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 717 | // configured sockets bound to this IP to actually use another IP, replace |
| 718 | // the IP here. |
| 719 | auto alternative = alternative_address_mapping_.find(addr->ipaddr()); |
| 720 | if (alternative != alternative_address_mapping_.end()) { |
| 721 | addr->SetIP(alternative->second); |
| 722 | } |
| 723 | |
| 724 | // Assign a port if not assigned. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 725 | if (addr->port() == 0) { |
| 726 | for (int i = 0; i < kEphemeralPortCount; ++i) { |
| 727 | addr->SetPort(GetNextPort()); |
| 728 | if (bindings_->find(*addr) == bindings_->end()) { |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return Bind(socket, *addr); |
| 735 | } |
| 736 | |
| 737 | VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 738 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 739 | AddressMap::iterator it = bindings_->find(normalized); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 740 | if (it != bindings_->end()) { |
| 741 | return it->second; |
| 742 | } |
| 743 | |
| 744 | IPAddress default_ip = GetDefaultRoute(addr.ipaddr().family()); |
| 745 | if (!IPIsUnspec(default_ip) && addr.ipaddr() == default_ip) { |
| 746 | // If we can't find a binding for the packet which is sent to the interface |
| 747 | // corresponding to the default route, it should match a binding with the |
| 748 | // correct port to the any address. |
| 749 | SocketAddress sock_addr = |
| 750 | EmptySocketAddressWithFamily(addr.ipaddr().family()); |
| 751 | sock_addr.SetPort(addr.port()); |
| 752 | return LookupBinding(sock_addr); |
| 753 | } |
| 754 | |
| 755 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | int VirtualSocketServer::Unbind(const SocketAddress& addr, |
| 759 | VirtualSocket* socket) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 760 | SocketAddress normalized(addr.ipaddr().Normalized(), addr.port()); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 761 | RTC_DCHECK((*bindings_)[normalized] == socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 762 | bindings_->erase(bindings_->find(normalized)); |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | void VirtualSocketServer::AddConnection(const SocketAddress& local, |
| 767 | const SocketAddress& remote, |
| 768 | VirtualSocket* remote_socket) { |
| 769 | // Add this socket pair to our routing table. This will allow |
| 770 | // multiple clients to connect to the same server address. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 771 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 772 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 773 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 774 | connections_->insert(std::pair<SocketAddressPair, VirtualSocket*>( |
| 775 | address_pair, remote_socket)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | VirtualSocket* VirtualSocketServer::LookupConnection( |
| 779 | const SocketAddress& local, |
| 780 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 781 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 782 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 783 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 784 | ConnectionMap::iterator it = connections_->find(address_pair); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 785 | return (connections_->end() != it) ? it->second : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void VirtualSocketServer::RemoveConnection(const SocketAddress& local, |
| 789 | const SocketAddress& remote) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 790 | SocketAddress local_normalized(local.ipaddr().Normalized(), local.port()); |
| 791 | SocketAddress remote_normalized(remote.ipaddr().Normalized(), remote.port()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 792 | SocketAddressPair address_pair(local_normalized, remote_normalized); |
| 793 | connections_->erase(address_pair); |
| 794 | } |
| 795 | |
| 796 | static double Random() { |
| 797 | return static_cast<double>(rand()) / RAND_MAX; |
| 798 | } |
| 799 | |
| 800 | int VirtualSocketServer::Connect(VirtualSocket* socket, |
| 801 | const SocketAddress& remote_addr, |
| 802 | bool use_delay) { |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 803 | uint32_t delay = use_delay ? GetTransitDelay(socket) : 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 804 | VirtualSocket* remote = LookupBinding(remote_addr); |
| 805 | if (!CanInteractWith(socket, remote)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 806 | RTC_LOG(LS_INFO) << "Address family mismatch between " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 807 | << socket->GetLocalAddress().ToString() << " and " |
| 808 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 809 | return -1; |
| 810 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 811 | if (remote != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 812 | SocketAddress addr = socket->GetLocalAddress(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 813 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, remote, MSG_ID_CONNECT, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | new MessageAddress(addr)); |
| 815 | } else { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 816 | RTC_LOG(LS_INFO) << "No one listening at " << remote_addr.ToString(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 817 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 818 | } |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | bool VirtualSocketServer::Disconnect(VirtualSocket* socket) { |
| 823 | if (socket) { |
Taylor Brandstetter | 716d07a | 2016-06-27 14:07:41 -0700 | [diff] [blame] | 824 | // If we simulate packets being delayed, we should simulate the |
| 825 | // equivalent of a FIN being delayed as well. |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 826 | uint32_t delay = GetTransitDelay(socket); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 827 | // Remove the mapping. |
Taylor Brandstetter | 716d07a | 2016-06-27 14:07:41 -0700 | [diff] [blame] | 828 | msg_queue_->PostDelayed(RTC_FROM_HERE, delay, socket, MSG_ID_DISCONNECT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 829 | return true; |
| 830 | } |
| 831 | return false; |
| 832 | } |
| 833 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 834 | bool VirtualSocketServer::Disconnect(const SocketAddress& addr) { |
| 835 | return Disconnect(LookupBinding(addr)); |
| 836 | } |
| 837 | |
| 838 | bool VirtualSocketServer::Disconnect(const SocketAddress& local_addr, |
| 839 | const SocketAddress& remote_addr) { |
| 840 | // Disconnect remote socket, check if it is a child of a server socket. |
| 841 | VirtualSocket* socket = LookupConnection(local_addr, remote_addr); |
| 842 | if (!socket) { |
| 843 | // Not a server socket child, then see if it is bound. |
| 844 | // TODO(tbd): If this is indeed a server socket that has no |
| 845 | // children this will cause the server socket to be |
| 846 | // closed. This might lead to unexpected results, how to fix this? |
| 847 | socket = LookupBinding(remote_addr); |
| 848 | } |
| 849 | Disconnect(socket); |
| 850 | |
| 851 | // Remove mapping for both directions. |
| 852 | RemoveConnection(remote_addr, local_addr); |
| 853 | RemoveConnection(local_addr, remote_addr); |
| 854 | return socket != nullptr; |
| 855 | } |
| 856 | |
| 857 | void VirtualSocketServer::CancelConnects(VirtualSocket* socket) { |
| 858 | MessageList msgs; |
| 859 | if (msg_queue_) { |
| 860 | msg_queue_->Clear(socket, MSG_ID_CONNECT, &msgs); |
| 861 | } |
| 862 | for (MessageList::iterator it = msgs.begin(); it != msgs.end(); ++it) { |
| 863 | RTC_DCHECK(nullptr != it->pdata); |
| 864 | MessageAddress* data = static_cast<MessageAddress*>(it->pdata); |
| 865 | SocketAddress local_addr = socket->GetLocalAddress(); |
| 866 | // Lookup remote side. |
| 867 | VirtualSocket* socket = LookupConnection(local_addr, data->addr); |
| 868 | if (socket) { |
| 869 | // Server socket, remote side is a socket retreived by |
| 870 | // accept. Accepted sockets are not bound so we will not |
| 871 | // find it by looking in the bindings table. |
| 872 | Disconnect(socket); |
| 873 | RemoveConnection(local_addr, data->addr); |
| 874 | } else { |
| 875 | Disconnect(data->addr); |
| 876 | } |
| 877 | delete data; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void VirtualSocketServer::Clear(VirtualSocket* socket) { |
| 882 | // Clear incoming packets and disconnect messages |
| 883 | if (msg_queue_) { |
| 884 | msg_queue_->Clear(socket); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | void VirtualSocketServer::ProcessOneMessage() { |
| 889 | Message msg; |
| 890 | msg_queue_->Get(&msg); |
| 891 | msg_queue_->Dispatch(&msg); |
| 892 | } |
| 893 | |
| 894 | void VirtualSocketServer::PostSignalReadEvent(VirtualSocket* socket) { |
| 895 | // Clear the message so it doesn't end up posted multiple times. |
| 896 | msg_queue_->Clear(socket, MSG_ID_SIGNALREADEVENT); |
| 897 | msg_queue_->Post(RTC_FROM_HERE, socket, MSG_ID_SIGNALREADEVENT); |
| 898 | } |
| 899 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 900 | int VirtualSocketServer::SendUdp(VirtualSocket* socket, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 901 | const char* data, |
| 902 | size_t data_size, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 903 | const SocketAddress& remote_addr) { |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 904 | ++sent_packets_; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 905 | if (sending_blocked_) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 906 | socket->SetToBlocked(); |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 907 | return -1; |
| 908 | } |
| 909 | |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 910 | if (data_size > largest_seen_udp_payload_) { |
| 911 | if (data_size > 1000) { |
| 912 | RTC_LOG(LS_VERBOSE) << "Largest UDP seen is " << data_size; |
| 913 | } |
| 914 | largest_seen_udp_payload_ = data_size; |
| 915 | } |
| 916 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 917 | // See if we want to drop this packet. |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 +0000 | [diff] [blame] | 918 | if (data_size > max_udp_payload_) { |
| 919 | RTC_LOG(LS_VERBOSE) << "Dropping too large UDP payload of size " |
| 920 | << data_size << ", UDP payload limit is " |
| 921 | << max_udp_payload_; |
| 922 | // Return as if send was successful; packet disappears. |
| 923 | return data_size; |
| 924 | } |
| 925 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 926 | if (Random() < drop_prob_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 927 | RTC_LOG(LS_VERBOSE) << "Dropping packet: bad luck"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 928 | return static_cast<int>(data_size); |
| 929 | } |
| 930 | |
| 931 | VirtualSocket* recipient = LookupBinding(remote_addr); |
| 932 | if (!recipient) { |
| 933 | // Make a fake recipient for address family checking. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 934 | std::unique_ptr<VirtualSocket> dummy_socket( |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 935 | CreateSocketInternal(AF_INET, SOCK_DGRAM)); |
| 936 | dummy_socket->SetLocalAddress(remote_addr); |
| 937 | if (!CanInteractWith(socket, dummy_socket.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 938 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 939 | << socket->GetLocalAddress().ToString() << " and " |
| 940 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 941 | return -1; |
| 942 | } |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 943 | RTC_LOG(LS_VERBOSE) << "No one listening at " << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 944 | return static_cast<int>(data_size); |
| 945 | } |
| 946 | |
| 947 | if (!CanInteractWith(socket, recipient)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 948 | RTC_LOG(LS_VERBOSE) << "Incompatible address families: " |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 949 | << socket->GetLocalAddress().ToString() << " and " |
| 950 | << remote_addr.ToString(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 951 | return -1; |
| 952 | } |
| 953 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 954 | { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 955 | int64_t cur_time = TimeMillis(); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 956 | size_t network_size = socket->PurgeNetworkPackets(cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 957 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 958 | // Determine whether we have enough bandwidth to accept this packet. To do |
| 959 | // this, we need to update the send queue. Once we know it's current size, |
| 960 | // we know whether we can fit this packet. |
| 961 | // |
| 962 | // NOTE: There are better algorithms for maintaining such a queue (such as |
| 963 | // "Derivative Random Drop"); however, this algorithm is a more accurate |
| 964 | // simulation of what a normal network would do. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 965 | |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 966 | size_t packet_size = data_size + UDP_HEADER_SIZE; |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 967 | if (network_size + packet_size > network_capacity_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 968 | RTC_LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded"; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 969 | return static_cast<int>(data_size); |
| 970 | } |
| 971 | |
| 972 | AddPacketToNetwork(socket, recipient, cur_time, data, data_size, |
| 973 | UDP_HEADER_SIZE, false); |
| 974 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 975 | return static_cast<int>(data_size); |
| 976 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | void VirtualSocketServer::SendTcp(VirtualSocket* socket) { |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 980 | ++sent_packets_; |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 981 | if (sending_blocked_) { |
| 982 | // Eventually the socket's buffer will fill and VirtualSocket::SendTcp will |
| 983 | // set EWOULDBLOCK. |
| 984 | return; |
| 985 | } |
| 986 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 987 | // TCP can't send more data than will fill up the receiver's buffer. |
| 988 | // We track the data that is in the buffer plus data in flight using the |
| 989 | // recipient's recv_buffer_size_. Anything beyond that must be stored in the |
| 990 | // sender's buffer. We will trigger the buffered data to be sent when data |
| 991 | // is read from the recv_buffer. |
| 992 | |
| 993 | // Lookup the local/remote pair in the connections table. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 994 | VirtualSocket* recipient = |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 995 | LookupConnection(socket->GetLocalAddress(), socket->GetRemoteAddress()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 996 | if (!recipient) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 997 | RTC_LOG(LS_VERBOSE) << "Sending data to no one."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
| 1000 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1001 | int64_t cur_time = TimeMillis(); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1002 | socket->PurgeNetworkPackets(cur_time); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1003 | |
| 1004 | while (true) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1005 | size_t available = recv_buffer_capacity_ - recipient->recv_buffer_size(); |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 1006 | size_t max_data_size = |
| 1007 | std::min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1008 | 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] | 1009 | if (0 == data_size) |
| 1010 | break; |
| 1011 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1012 | AddPacketToNetwork(socket, recipient, cur_time, socket->send_buffer_data(), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | data_size, TCP_HEADER_SIZE, true); |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1014 | recipient->UpdateRecv(data_size); |
| 1015 | socket->UpdateSend(data_size); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1018 | socket->MaybeSignalWriteEvent(send_buffer_capacity_); |
| 1019 | } |
| 1020 | |
| 1021 | void VirtualSocketServer::SendTcp(const SocketAddress& addr) { |
| 1022 | VirtualSocket* sender = LookupBinding(addr); |
| 1023 | RTC_DCHECK(nullptr != sender); |
| 1024 | SendTcp(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender, |
| 1028 | VirtualSocket* recipient, |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1029 | int64_t cur_time, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1030 | const char* data, |
| 1031 | size_t data_size, |
| 1032 | size_t header_size, |
| 1033 | bool ordered) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1034 | 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] | 1035 | |
| 1036 | // Find the delay for crossing the many virtual hops of the network. |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1037 | uint32_t transit_delay = GetTransitDelay(sender); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1038 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1039 | // When the incoming packet is from a binding of the any address, translate it |
| 1040 | // to the default route here such that the recipient will see the default |
| 1041 | // route. |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1042 | SocketAddress sender_addr = sender->GetLocalAddress(); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1043 | IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
| 1044 | if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
| 1045 | sender_addr.SetIP(default_ip); |
| 1046 | } |
| 1047 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1048 | // 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] | 1049 | Packet* p = new Packet(data, data_size, sender_addr); |
| 1050 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1051 | int64_t ts = TimeAfter(send_delay + transit_delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1052 | if (ordered) { |
Niels Möller | c79bd43 | 2021-02-16 09:25:52 +0100 | [diff] [blame] | 1053 | ts = sender->UpdateOrderedDelivery(ts); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1054 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1055 | 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] | 1056 | } |
| 1057 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1058 | uint32_t VirtualSocketServer::SendDelay(uint32_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1059 | if (bandwidth_ == 0) |
| 1060 | return 0; |
| 1061 | else |
| 1062 | return 1000 * size / bandwidth_; |
| 1063 | } |
| 1064 | |
| 1065 | #if 0 |
| 1066 | void PrintFunction(std::vector<std::pair<double, double> >* f) { |
| 1067 | return; |
| 1068 | double sum = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1069 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1070 | std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl; |
| 1071 | sum += (*f)[i].second; |
| 1072 | } |
| 1073 | if (!f->empty()) { |
| 1074 | const double mean = sum / f->size(); |
| 1075 | double sum_sq_dev = 0; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1076 | for (uint32_t i = 0; i < f->size(); ++i) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1077 | double dev = (*f)[i].second - mean; |
| 1078 | sum_sq_dev += dev * dev; |
| 1079 | } |
| 1080 | std::cout << "Mean = " << mean << " StdDev = " |
| 1081 | << sqrt(sum_sq_dev / f->size()) << std::endl; |
| 1082 | } |
| 1083 | } |
| 1084 | #endif // <unused> |
| 1085 | |
| 1086 | void VirtualSocketServer::UpdateDelayDistribution() { |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1087 | delay_dist_ = CreateDistribution(delay_mean_, delay_stddev_, delay_samples_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | static double PI = 4 * atan(1.0); |
| 1091 | |
| 1092 | static double Normal(double x, double mean, double stddev) { |
| 1093 | double a = (x - mean) * (x - mean) / (2 * stddev * stddev); |
| 1094 | return exp(-a) / (stddev * sqrt(2 * PI)); |
| 1095 | } |
| 1096 | |
| 1097 | #if 0 // static unused gives a warning |
| 1098 | static double Pareto(double x, double min, double k) { |
| 1099 | if (x < min) |
| 1100 | return 0; |
| 1101 | else |
| 1102 | return k * std::pow(min, k) / std::pow(x, k+1); |
| 1103 | } |
| 1104 | #endif |
| 1105 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1106 | std::unique_ptr<VirtualSocketServer::Function> |
| 1107 | VirtualSocketServer::CreateDistribution(uint32_t mean, |
| 1108 | uint32_t stddev, |
| 1109 | uint32_t samples) { |
| 1110 | auto f = std::make_unique<Function>(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (0 == stddev) { |
| 1113 | f->push_back(Point(mean, 1.0)); |
| 1114 | } else { |
| 1115 | double start = 0; |
| 1116 | if (mean >= 4 * static_cast<double>(stddev)) |
| 1117 | start = mean - 4 * static_cast<double>(stddev); |
| 1118 | double end = mean + 4 * static_cast<double>(stddev); |
| 1119 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1120 | for (uint32_t i = 0; i < samples; i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1121 | double x = start + (end - start) * i / (samples - 1); |
| 1122 | double y = Normal(x, mean, stddev); |
| 1123 | f->push_back(Point(x, y)); |
| 1124 | } |
| 1125 | } |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1126 | return Resample(Invert(Accumulate(std::move(f))), 0, 1, samples); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Honghai Zhang | c67e0f5 | 2016-09-19 16:57:37 -0700 | [diff] [blame] | 1129 | uint32_t VirtualSocketServer::GetTransitDelay(Socket* socket) { |
| 1130 | // Use the delay based on the address if it is set. |
| 1131 | auto iter = delay_by_ip_.find(socket->GetLocalAddress().ipaddr()); |
| 1132 | if (iter != delay_by_ip_.end()) { |
| 1133 | return static_cast<uint32_t>(iter->second); |
| 1134 | } |
| 1135 | // Otherwise, use the delay from the distribution distribution. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1136 | size_t index = rand() % delay_dist_->size(); |
| 1137 | double delay = (*delay_dist_)[index].second; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1138 | // RTC_LOG_F(LS_INFO) << "random[" << index << "] = " << delay; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1139 | return static_cast<uint32_t>(delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | struct FunctionDomainCmp { |
| 1143 | bool operator()(const VirtualSocketServer::Point& p1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1144 | const VirtualSocketServer::Point& p2) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1145 | return p1.first < p2.first; |
| 1146 | } |
| 1147 | bool operator()(double v1, const VirtualSocketServer::Point& p2) { |
| 1148 | return v1 < p2.first; |
| 1149 | } |
| 1150 | bool operator()(const VirtualSocketServer::Point& p1, double v2) { |
| 1151 | return p1.first < v2; |
| 1152 | } |
| 1153 | }; |
| 1154 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1155 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Accumulate( |
| 1156 | std::unique_ptr<Function> f) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1157 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1158 | double v = 0; |
| 1159 | for (Function::size_type i = 0; i < f->size() - 1; ++i) { |
| 1160 | double dx = (*f)[i + 1].first - (*f)[i].first; |
| 1161 | double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2; |
| 1162 | (*f)[i].second = v; |
| 1163 | v = v + dx * avgy; |
| 1164 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1165 | (*f)[f->size() - 1].second = v; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1166 | return f; |
| 1167 | } |
| 1168 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1169 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Invert( |
| 1170 | std::unique_ptr<Function> f) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1171 | for (Function::size_type i = 0; i < f->size(); ++i) |
| 1172 | std::swap((*f)[i].first, (*f)[i].second); |
| 1173 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 1174 | absl::c_sort(*f, FunctionDomainCmp()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1175 | return f; |
| 1176 | } |
| 1177 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1178 | std::unique_ptr<VirtualSocketServer::Function> VirtualSocketServer::Resample( |
| 1179 | std::unique_ptr<Function> f, |
| 1180 | double x1, |
| 1181 | double x2, |
| 1182 | uint32_t samples) { |
| 1183 | auto g = std::make_unique<Function>(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1184 | |
| 1185 | for (size_t i = 0; i < samples; i++) { |
| 1186 | double x = x1 + (x2 - x1) * i / (samples - 1); |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1187 | double y = Evaluate(f.get(), x); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1188 | g->push_back(Point(x, y)); |
| 1189 | } |
| 1190 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1191 | return g; |
| 1192 | } |
| 1193 | |
Niels Möller | 983627c | 2021-02-09 15:12:28 +0100 | [diff] [blame] | 1194 | double VirtualSocketServer::Evaluate(const Function* f, double x) { |
| 1195 | Function::const_iterator iter = |
| 1196 | absl::c_lower_bound(*f, x, FunctionDomainCmp()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1197 | if (iter == f->begin()) { |
| 1198 | return (*f)[0].second; |
| 1199 | } else if (iter == f->end()) { |
Taylor Brandstetter | e753641 | 2016-09-09 13:16:15 -0700 | [diff] [blame] | 1200 | RTC_DCHECK(f->size() >= 1); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1201 | return (*f)[f->size() - 1].second; |
| 1202 | } else if (iter->first == x) { |
| 1203 | return iter->second; |
| 1204 | } else { |
| 1205 | double x1 = (iter - 1)->first; |
| 1206 | double y1 = (iter - 1)->second; |
| 1207 | double x2 = iter->first; |
| 1208 | double y2 = iter->second; |
| 1209 | return y1 + (y2 - y1) * (x - x1) / (x2 - x1); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | bool VirtualSocketServer::CanInteractWith(VirtualSocket* local, |
| 1214 | VirtualSocket* remote) { |
| 1215 | if (!local || !remote) { |
| 1216 | return false; |
| 1217 | } |
| 1218 | IPAddress local_ip = local->GetLocalAddress().ipaddr(); |
| 1219 | IPAddress remote_ip = remote->GetLocalAddress().ipaddr(); |
| 1220 | IPAddress local_normalized = local_ip.Normalized(); |
| 1221 | IPAddress remote_normalized = remote_ip.Normalized(); |
| 1222 | // Check if the addresses are the same family after Normalization (turns |
| 1223 | // mapped IPv6 address into IPv4 addresses). |
| 1224 | // This will stop unmapped V6 addresses from talking to mapped V6 addresses. |
| 1225 | if (local_normalized.family() == remote_normalized.family()) { |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY. |
| 1230 | int remote_v6_only = 0; |
| 1231 | remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only); |
| 1232 | if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) { |
| 1233 | return true; |
| 1234 | } |
| 1235 | // Same check, backwards. |
| 1236 | int local_v6_only = 0; |
| 1237 | local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only); |
| 1238 | if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) { |
| 1239 | return true; |
| 1240 | } |
| 1241 | |
| 1242 | // Check to see if either socket was explicitly bound to IPv6-any. |
| 1243 | // These sockets can talk with anyone. |
| 1244 | if (local_ip.family() == AF_INET6 && local->was_any()) { |
| 1245 | return true; |
| 1246 | } |
| 1247 | if (remote_ip.family() == AF_INET6 && remote->was_any()) { |
| 1248 | return true; |
| 1249 | } |
| 1250 | |
| 1251 | return false; |
| 1252 | } |
| 1253 | |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1254 | IPAddress VirtualSocketServer::GetDefaultRoute(int family) { |
| 1255 | if (family == AF_INET) { |
| 1256 | return default_route_v4_; |
| 1257 | } |
| 1258 | if (family == AF_INET6) { |
| 1259 | return default_route_v6_; |
| 1260 | } |
| 1261 | return IPAddress(); |
| 1262 | } |
| 1263 | void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 1264 | RTC_DCHECK(!IPIsAny(from_addr)); |
Guo-wei Shieh | 38f8893 | 2015-08-13 22:24:02 -0700 | [diff] [blame] | 1265 | if (from_addr.family() == AF_INET) { |
| 1266 | default_route_v4_ = from_addr; |
| 1267 | } else if (from_addr.family() == AF_INET6) { |
| 1268 | default_route_v6_ = from_addr; |
| 1269 | } |
| 1270 | } |
| 1271 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1272 | } // namespace rtc |