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