henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ |
| 12 | #define RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <deque> |
| 15 | #include <map> |
Steve Anton | f417238 | 2020-01-27 23:45:02 | [diff] [blame] | 16 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 17 | |
Danil Chapovalov | babdaa86 | 2022-09-16 10:41:11 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
| 19 | #include "api/make_ref_counted.h" |
| 20 | #include "api/ref_counted_base.h" |
| 21 | #include "api/scoped_refptr.h" |
| 22 | #include "api/task_queue/task_queue_base.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "rtc_base/event.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 25 | #include "rtc_base/fake_clock.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "rtc_base/socket_server.h" |
Niels Möller | c413c55 | 2021-06-22 08:03:14 | [diff] [blame] | 27 | #include "rtc_base/synchronization/mutex.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 28 | |
| 29 | namespace rtc { |
| 30 | |
| 31 | class Packet; |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 32 | class VirtualSocketServer; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | class SocketAddressPair; |
| 34 | |
Danil Chapovalov | babdaa86 | 2022-09-16 10:41:11 | [diff] [blame] | 35 | // Implements the socket interface using the virtual network. Packets are |
| 36 | // passed in tasks using the thread of the socket server. |
| 37 | class VirtualSocket : public Socket, public sigslot::has_slots<> { |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 38 | public: |
| 39 | VirtualSocket(VirtualSocketServer* server, int family, int type); |
| 40 | ~VirtualSocket() override; |
| 41 | |
| 42 | SocketAddress GetLocalAddress() const override; |
| 43 | SocketAddress GetRemoteAddress() const override; |
| 44 | |
| 45 | int Bind(const SocketAddress& addr) override; |
| 46 | int Connect(const SocketAddress& addr) override; |
| 47 | int Close() override; |
| 48 | int Send(const void* pv, size_t cb) override; |
| 49 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
| 50 | int Recv(void* pv, size_t cb, int64_t* timestamp) override; |
| 51 | int RecvFrom(void* pv, |
| 52 | size_t cb, |
| 53 | SocketAddress* paddr, |
| 54 | int64_t* timestamp) override; |
| 55 | int Listen(int backlog) override; |
| 56 | VirtualSocket* Accept(SocketAddress* paddr) override; |
| 57 | |
| 58 | int GetError() const override; |
| 59 | void SetError(int error) override; |
| 60 | ConnState GetState() const override; |
| 61 | int GetOption(Option opt, int* value) override; |
| 62 | int SetOption(Option opt, int value) override; |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 63 | |
| 64 | size_t recv_buffer_size() const { return recv_buffer_size_; } |
| 65 | size_t send_buffer_size() const { return send_buffer_.size(); } |
| 66 | const char* send_buffer_data() const { return send_buffer_.data(); } |
| 67 | |
| 68 | // Used by server sockets to set the local address without binding. |
| 69 | void SetLocalAddress(const SocketAddress& addr); |
| 70 | |
| 71 | bool was_any() { return was_any_; } |
| 72 | void set_was_any(bool was_any) { was_any_ = was_any; } |
| 73 | |
| 74 | void SetToBlocked(); |
| 75 | |
| 76 | void UpdateRecv(size_t data_size); |
| 77 | void UpdateSend(size_t data_size); |
| 78 | |
| 79 | void MaybeSignalWriteEvent(size_t capacity); |
| 80 | |
| 81 | // Adds a packet to be sent. Returns delay, based on network_size_. |
| 82 | uint32_t AddPacket(int64_t cur_time, size_t packet_size); |
| 83 | |
| 84 | int64_t UpdateOrderedDelivery(int64_t ts); |
| 85 | |
| 86 | // Removes stale packets from the network. Returns current size. |
| 87 | size_t PurgeNetworkPackets(int64_t cur_time); |
| 88 | |
Danil Chapovalov | babdaa86 | 2022-09-16 10:41:11 | [diff] [blame] | 89 | void PostPacket(webrtc::TimeDelta delay, std::unique_ptr<Packet> packet); |
| 90 | void PostConnect(webrtc::TimeDelta delay, const SocketAddress& remote_addr); |
| 91 | void PostDisconnect(webrtc::TimeDelta delay); |
| 92 | |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 93 | private: |
Danil Chapovalov | babdaa86 | 2022-09-16 10:41:11 | [diff] [blame] | 94 | // Struct shared with pending tasks that may outlive VirtualSocket. |
| 95 | class SafetyBlock : public RefCountedNonVirtual<SafetyBlock> { |
| 96 | public: |
| 97 | explicit SafetyBlock(VirtualSocket* socket); |
| 98 | SafetyBlock(const SafetyBlock&) = delete; |
| 99 | SafetyBlock& operator=(const SafetyBlock&) = delete; |
| 100 | ~SafetyBlock(); |
| 101 | |
| 102 | // Prohibits posted delayed task to access owning VirtualSocket and |
| 103 | // cleanups members protected by the `mutex`. |
| 104 | void SetNotAlive(); |
| 105 | bool IsAlive(); |
| 106 | |
| 107 | // Copies up to `size` bytes into buffer from the next received packet |
| 108 | // and fills `addr` with remote address of that received packet. |
| 109 | // Returns number of bytes copied or negative value on failure. |
| 110 | int RecvFrom(void* buffer, size_t size, SocketAddress& addr); |
| 111 | |
| 112 | void Listen(); |
| 113 | |
| 114 | struct AcceptResult { |
| 115 | int error = 0; |
| 116 | std::unique_ptr<VirtualSocket> socket; |
| 117 | SocketAddress remote_addr; |
| 118 | }; |
| 119 | AcceptResult Accept(); |
| 120 | |
| 121 | bool AddPacket(std::unique_ptr<Packet> packet); |
| 122 | void PostConnect(webrtc::TimeDelta delay, const SocketAddress& remote_addr); |
| 123 | |
| 124 | private: |
| 125 | enum class Signal { kNone, kReadEvent, kConnectEvent }; |
| 126 | // `PostConnect` rely on the fact that std::list iterators are not |
| 127 | // invalidated on any changes to other elements in the container. |
| 128 | using PostedConnects = std::list<SocketAddress>; |
| 129 | |
| 130 | void PostSignalReadEvent() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 131 | void MaybeSignalReadEvent(); |
| 132 | Signal Connect(PostedConnects::iterator remote_addr_it); |
| 133 | |
| 134 | webrtc::Mutex mutex_; |
| 135 | VirtualSocket& socket_; |
| 136 | bool alive_ RTC_GUARDED_BY(mutex_) = true; |
| 137 | // Flag indicating if async Task to signal SignalReadEvent is posted. |
| 138 | // To avoid posting multiple such tasks. |
| 139 | bool pending_read_signal_event_ RTC_GUARDED_BY(mutex_) = false; |
| 140 | |
| 141 | // Members below do not need to outlive VirtualSocket, but are used by the |
| 142 | // posted tasks. Keeping them in the VirtualSocket confuses thread |
| 143 | // annotations because they can't detect that locked mutex is the same mutex |
| 144 | // this members are guarded by. |
| 145 | |
| 146 | // Addresses of the sockets for potential connect. For each address there |
| 147 | // is a posted task that should finilze the connect. |
| 148 | PostedConnects posted_connects_ RTC_GUARDED_BY(mutex_); |
| 149 | |
| 150 | // Data which has been received from the network |
| 151 | std::list<std::unique_ptr<Packet>> recv_buffer_ RTC_GUARDED_BY(mutex_); |
| 152 | |
| 153 | // Pending sockets which can be Accepted |
| 154 | absl::optional<std::deque<SocketAddress>> listen_queue_ |
| 155 | RTC_GUARDED_BY(mutex_); |
| 156 | }; |
| 157 | |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 158 | struct NetworkEntry { |
| 159 | size_t size; |
| 160 | int64_t done_time; |
| 161 | }; |
| 162 | |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 163 | typedef std::deque<NetworkEntry> NetworkQueue; |
| 164 | typedef std::vector<char> SendBuffer; |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 165 | typedef std::map<Option, int> OptionsMap; |
| 166 | |
| 167 | int InitiateConnect(const SocketAddress& addr, bool use_delay); |
| 168 | void CompleteConnect(const SocketAddress& addr); |
| 169 | int SendUdp(const void* pv, size_t cb, const SocketAddress& addr); |
| 170 | int SendTcp(const void* pv, size_t cb); |
| 171 | |
| 172 | void OnSocketServerReadyToSend(); |
| 173 | |
| 174 | VirtualSocketServer* const server_; |
| 175 | const int type_; |
| 176 | ConnState state_; |
| 177 | int error_; |
| 178 | SocketAddress local_addr_; |
| 179 | SocketAddress remote_addr_; |
| 180 | |
Danil Chapovalov | babdaa86 | 2022-09-16 10:41:11 | [diff] [blame] | 181 | const scoped_refptr<SafetyBlock> safety_ = |
| 182 | make_ref_counted<SafetyBlock>(this); |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 183 | |
| 184 | // Data which tcp has buffered for sending |
| 185 | SendBuffer send_buffer_; |
| 186 | // Set to false if the last attempt to send resulted in EWOULDBLOCK. |
| 187 | // Set back to true when the socket can send again. |
| 188 | bool ready_to_send_ = true; |
| 189 | |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 190 | // Network model that enforces bandwidth and capacity constraints |
| 191 | NetworkQueue network_; |
| 192 | size_t network_size_; |
| 193 | // The scheduled delivery time of the last packet sent on this socket. |
| 194 | // It is used to ensure ordered delivery of packets sent on this socket. |
| 195 | int64_t last_delivery_time_ = 0; |
| 196 | |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 197 | // The amount of data which is in flight or in recv_buffer_ |
| 198 | size_t recv_buffer_size_; |
| 199 | |
| 200 | // Is this socket bound? |
| 201 | bool bound_; |
| 202 | |
| 203 | // When we bind a socket to Any, VSS's Bind gives it another address. For |
| 204 | // dual-stack sockets, we want to distinguish between sockets that were |
| 205 | // explicitly given a particular address and sockets that had one picked |
| 206 | // for them by VSS. |
| 207 | bool was_any_; |
| 208 | |
| 209 | // Store the options that are set |
| 210 | OptionsMap options_map_; |
| 211 | }; |
| 212 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 213 | // Simulates a network in the same manner as a loopback interface. The |
| 214 | // interface can create as many addresses as you want. All of the sockets |
| 215 | // created by this network will be able to communicate with one another, unless |
| 216 | // they are bound to addresses from incompatible families. |
Niels Möller | 9bd2457 | 2021-04-19 10:18:27 | [diff] [blame] | 217 | class VirtualSocketServer : public SocketServer { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 218 | public: |
| 219 | VirtualSocketServer(); |
| 220 | // This constructor needs to be used if the test uses a fake clock and |
| 221 | // ProcessMessagesUntilIdle, since ProcessMessagesUntilIdle needs a way of |
| 222 | // advancing time. |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 223 | explicit VirtualSocketServer(ThreadProcessingFakeClock* fake_clock); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 224 | ~VirtualSocketServer() override; |
| 225 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 226 | VirtualSocketServer(const VirtualSocketServer&) = delete; |
| 227 | VirtualSocketServer& operator=(const VirtualSocketServer&) = delete; |
| 228 | |
Niels Möller | 84d1595 | 2021-09-01 08:50:34 | [diff] [blame] | 229 | // The default source address specifies which local address to use when a |
| 230 | // socket is bound to the 'any' address, e.g. 0.0.0.0. (If not set, the 'any' |
| 231 | // address is used as the source address on outgoing virtual packets, exposed |
| 232 | // to recipient's RecvFrom). |
| 233 | IPAddress GetDefaultSourceAddress(int family); |
| 234 | void SetDefaultSourceAddress(const IPAddress& from_addr); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 235 | |
| 236 | // Limits the network bandwidth (maximum bytes per second). Zero means that |
| 237 | // all sends occur instantly. Defaults to 0. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 238 | void set_bandwidth(uint32_t bandwidth) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 239 | |
| 240 | // Limits the amount of data which can be in flight on the network without |
| 241 | // packet loss (on a per sender basis). Defaults to 64 KB. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 242 | void set_network_capacity(uint32_t capacity) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 243 | |
| 244 | // The amount of data which can be buffered by tcp on the sender's side |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 245 | uint32_t send_buffer_capacity() const RTC_LOCKS_EXCLUDED(mutex_); |
| 246 | void set_send_buffer_capacity(uint32_t capacity) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 247 | |
| 248 | // The amount of data which can be buffered by tcp on the receiver's side |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 249 | uint32_t recv_buffer_capacity() const RTC_LOCKS_EXCLUDED(mutex_); |
| 250 | void set_recv_buffer_capacity(uint32_t capacity) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 251 | |
| 252 | // Controls the (transit) delay for packets sent in the network. This does |
| 253 | // not inclue the time required to sit in the send queue. Both of these |
| 254 | // values are measured in milliseconds. Defaults to no delay. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 255 | void set_delay_mean(uint32_t delay_mean) RTC_LOCKS_EXCLUDED(mutex_); |
| 256 | void set_delay_stddev(uint32_t delay_stddev) RTC_LOCKS_EXCLUDED(mutex_); |
| 257 | void set_delay_samples(uint32_t delay_samples) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 258 | |
| 259 | // If the (transit) delay parameters are modified, this method should be |
| 260 | // called to recompute the new distribution. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 261 | void UpdateDelayDistribution() RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 262 | |
| 263 | // Controls the (uniform) probability that any sent packet is dropped. This |
| 264 | // is separate from calculations to drop based on queue size. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 265 | void set_drop_probability(double drop_prob) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 266 | |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 | [diff] [blame] | 267 | // Controls the maximum UDP payload for the networks simulated |
| 268 | // by this server. Any UDP payload sent that is larger than this will |
| 269 | // be dropped. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 270 | void set_max_udp_payload(size_t payload_size) RTC_LOCKS_EXCLUDED(mutex_); |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 | [diff] [blame] | 271 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 272 | // If `blocked` is true, subsequent attempts to send will result in -1 being |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 273 | // returned, with the socket error set to EWOULDBLOCK. |
| 274 | // |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 275 | // If this method is later called with `blocked` set to false, any sockets |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 276 | // that previously failed to send with EWOULDBLOCK will emit SignalWriteEvent. |
| 277 | // |
| 278 | // This can be used to simulate the send buffer on a network interface being |
| 279 | // full, and test functionality related to EWOULDBLOCK/SignalWriteEvent. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 280 | void SetSendingBlocked(bool blocked) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 281 | |
| 282 | // SocketFactory: |
Niels Möller | ea423a5 | 2021-08-19 08:13:31 | [diff] [blame] | 283 | VirtualSocket* CreateSocket(int family, int type) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 284 | |
| 285 | // SocketServer: |
Sebastian Jansson | 290de82 | 2020-01-09 13:20:23 | [diff] [blame] | 286 | void SetMessageQueue(Thread* queue) override; |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 287 | bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 288 | void WakeUp() override; |
| 289 | |
| 290 | void SetDelayOnAddress(const rtc::SocketAddress& address, int delay_ms) { |
| 291 | delay_by_ip_[address.ipaddr()] = delay_ms; |
| 292 | } |
| 293 | |
deadbeef | 5c3c104 | 2017-08-04 22:01:57 | [diff] [blame] | 294 | // Used by TurnPortTest and TcpPortTest (for example), to mimic a case where |
| 295 | // a proxy returns the local host address instead of the original one the |
| 296 | // port was bound against. Please see WebRTC issue 3927 for more detail. |
| 297 | // |
| 298 | // If SetAlternativeLocalAddress(A, B) is called, then when something |
| 299 | // attempts to bind a socket to address A, it will get a socket bound to |
| 300 | // address B instead. |
| 301 | void SetAlternativeLocalAddress(const rtc::IPAddress& address, |
| 302 | const rtc::IPAddress& alternative); |
| 303 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 304 | typedef std::pair<double, double> Point; |
| 305 | typedef std::vector<Point> Function; |
| 306 | |
Niels Möller | 983627c | 2021-02-09 14:12:28 | [diff] [blame] | 307 | static std::unique_ptr<Function> CreateDistribution(uint32_t mean, |
| 308 | uint32_t stddev, |
| 309 | uint32_t samples); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 310 | |
| 311 | // Similar to Thread::ProcessMessages, but it only processes messages until |
| 312 | // there are no immediate messages or pending network traffic. Returns false |
| 313 | // if Thread::Stop() was called. |
| 314 | bool ProcessMessagesUntilIdle(); |
| 315 | |
| 316 | // Sets the next port number to use for testing. |
| 317 | void SetNextPortForTesting(uint16_t port); |
| 318 | |
| 319 | // Close a pair of Tcp connections by addresses. Both connections will have |
| 320 | // its own OnClose invoked. |
| 321 | bool CloseTcpConnections(const SocketAddress& addr_local, |
| 322 | const SocketAddress& addr_remote); |
| 323 | |
Taylor Brandstetter | 389a97c | 2018-01-04 00:26:06 | [diff] [blame] | 324 | // Number of packets that clients have attempted to send through this virtual |
| 325 | // socket server. Intended to be used for test assertions. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 326 | uint32_t sent_packets() const RTC_LOCKS_EXCLUDED(mutex_); |
Taylor Brandstetter | 389a97c | 2018-01-04 00:26:06 | [diff] [blame] | 327 | |
Niels Möller | c2d8f1e | 2021-08-24 13:49:34 | [diff] [blame] | 328 | // Assign IP and Port if application's address is unspecified. Also apply |
| 329 | // `alternative_address_mapping_`. |
| 330 | SocketAddress AssignBindAddress(const SocketAddress& app_addr); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 331 | |
| 332 | // Binds the given socket to the given (fully-defined) address. |
| 333 | int Bind(VirtualSocket* socket, const SocketAddress& addr); |
| 334 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 335 | int Unbind(const SocketAddress& addr, VirtualSocket* socket); |
| 336 | |
| 337 | // Adds a mapping between this socket pair and the socket. |
| 338 | void AddConnection(const SocketAddress& client, |
| 339 | const SocketAddress& server, |
| 340 | VirtualSocket* socket); |
| 341 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 342 | // Connects the given socket to the socket at the given address |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 343 | int Connect(VirtualSocket* socket, |
| 344 | const SocketAddress& remote_addr, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 345 | bool use_delay); |
| 346 | |
| 347 | // Sends a disconnect message to the socket at the given address |
| 348 | bool Disconnect(VirtualSocket* socket); |
| 349 | |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 350 | // Lookup address, and disconnect corresponding socket. |
| 351 | bool Disconnect(const SocketAddress& addr); |
| 352 | |
| 353 | // Lookup connection, close corresponding socket. |
| 354 | bool Disconnect(const SocketAddress& local_addr, |
| 355 | const SocketAddress& remote_addr); |
| 356 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 357 | // Sends the given packet to the socket at the given address (if one exists). |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 358 | int SendUdp(VirtualSocket* socket, |
| 359 | const char* data, |
| 360 | size_t data_size, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 361 | const SocketAddress& remote_addr); |
| 362 | |
| 363 | // Moves as much data as possible from the sender's buffer to the network |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 364 | void SendTcp(VirtualSocket* socket) RTC_LOCKS_EXCLUDED(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 365 | |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 366 | // Like above, but lookup sender by address. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 367 | void SendTcp(const SocketAddress& addr) RTC_LOCKS_EXCLUDED(mutex_); |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 368 | |
| 369 | // Computes the number of milliseconds required to send a packet of this size. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 370 | uint32_t SendDelay(uint32_t size) RTC_LOCKS_EXCLUDED(mutex_); |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 371 | |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 372 | // Sending was previously blocked, but now isn't. |
| 373 | sigslot::signal0<> SignalReadyToSend; |
| 374 | |
| 375 | protected: |
| 376 | // Returns a new IP not used before in this network. |
| 377 | IPAddress GetNextIP(int family); |
| 378 | |
| 379 | // Find the socket bound to the given address |
| 380 | VirtualSocket* LookupBinding(const SocketAddress& addr); |
| 381 | |
| 382 | private: |
Danil Chapovalov | babdaa86 | 2022-09-16 10:41:11 | [diff] [blame] | 383 | friend VirtualSocket; |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 384 | uint16_t GetNextPort(); |
| 385 | |
Niels Möller | c79bd43 | 2021-02-16 08:25:52 | [diff] [blame] | 386 | // Find the socket pair corresponding to this server address. |
| 387 | VirtualSocket* LookupConnection(const SocketAddress& client, |
| 388 | const SocketAddress& server); |
| 389 | |
| 390 | void RemoveConnection(const SocketAddress& client, |
| 391 | const SocketAddress& server); |
| 392 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 393 | // Places a packet on the network. |
| 394 | void AddPacketToNetwork(VirtualSocket* socket, |
| 395 | VirtualSocket* recipient, |
| 396 | int64_t cur_time, |
| 397 | const char* data, |
| 398 | size_t data_size, |
| 399 | size_t header_size, |
| 400 | bool ordered); |
| 401 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 402 | // If the delay has been set for the address of the socket, returns the set |
| 403 | // delay. Otherwise, returns a random transit delay chosen from the |
| 404 | // appropriate distribution. |
| 405 | uint32_t GetTransitDelay(Socket* socket); |
| 406 | |
Niels Möller | 983627c | 2021-02-09 14:12:28 | [diff] [blame] | 407 | // Basic operations on functions. |
| 408 | static std::unique_ptr<Function> Accumulate(std::unique_ptr<Function> f); |
| 409 | static std::unique_ptr<Function> Invert(std::unique_ptr<Function> f); |
| 410 | static std::unique_ptr<Function> Resample(std::unique_ptr<Function> f, |
| 411 | double x1, |
| 412 | double x2, |
| 413 | uint32_t samples); |
| 414 | static double Evaluate(const Function* f, double x); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 415 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 416 | // Determine if two sockets should be able to communicate. |
| 417 | // We don't (currently) specify an address family for sockets; instead, |
| 418 | // the currently bound address is used to infer the address family. |
| 419 | // Any socket that is not explicitly bound to an IPv4 address is assumed to be |
| 420 | // dual-stack capable. |
| 421 | // This function tests if two addresses can communicate, as well as the |
| 422 | // sockets to which they may be bound (the addresses may or may not yet be |
| 423 | // bound to the sockets). |
| 424 | // First the addresses are tested (after normalization): |
| 425 | // If both have the same family, then communication is OK. |
| 426 | // If only one is IPv4 then false, unless the other is bound to ::. |
| 427 | // This applies even if the IPv4 address is 0.0.0.0. |
| 428 | // The socket arguments are optional; the sockets are checked to see if they |
| 429 | // were explicitly bound to IPv6-any ('::'), and if so communication is |
| 430 | // permitted. |
| 431 | // NB: This scheme doesn't permit non-dualstack IPv6 sockets. |
| 432 | static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote); |
| 433 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 434 | typedef std::map<SocketAddress, VirtualSocket*> AddressMap; |
| 435 | typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap; |
| 436 | |
| 437 | // May be null if the test doesn't use a fake clock, or it does but doesn't |
| 438 | // use ProcessMessagesUntilIdle. |
Sebastian Jansson | d624c39 | 2019-04-17 08:36:03 | [diff] [blame] | 439 | ThreadProcessingFakeClock* fake_clock_ = nullptr; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 440 | |
| 441 | // Used to implement Wait/WakeUp. |
| 442 | Event wakeup_; |
Sebastian Jansson | 290de82 | 2020-01-09 13:20:23 | [diff] [blame] | 443 | Thread* msg_queue_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 444 | bool stop_on_idle_; |
| 445 | in_addr next_ipv4_; |
| 446 | in6_addr next_ipv6_; |
| 447 | uint16_t next_port_; |
| 448 | AddressMap* bindings_; |
| 449 | ConnectionMap* connections_; |
| 450 | |
Niels Möller | 84d1595 | 2021-09-01 08:50:34 | [diff] [blame] | 451 | IPAddress default_source_address_v4_; |
| 452 | IPAddress default_source_address_v6_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 453 | |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 454 | mutable webrtc::Mutex mutex_; |
| 455 | |
| 456 | uint32_t bandwidth_ RTC_GUARDED_BY(mutex_); |
| 457 | uint32_t network_capacity_ RTC_GUARDED_BY(mutex_); |
| 458 | uint32_t send_buffer_capacity_ RTC_GUARDED_BY(mutex_); |
| 459 | uint32_t recv_buffer_capacity_ RTC_GUARDED_BY(mutex_); |
| 460 | uint32_t delay_mean_ RTC_GUARDED_BY(mutex_); |
| 461 | uint32_t delay_stddev_ RTC_GUARDED_BY(mutex_); |
| 462 | uint32_t delay_samples_ RTC_GUARDED_BY(mutex_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 463 | |
Taylor Brandstetter | 389a97c | 2018-01-04 00:26:06 | [diff] [blame] | 464 | // Used for testing. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 465 | uint32_t sent_packets_ RTC_GUARDED_BY(mutex_) = 0; |
Taylor Brandstetter | 389a97c | 2018-01-04 00:26:06 | [diff] [blame] | 466 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 467 | std::map<rtc::IPAddress, int> delay_by_ip_; |
deadbeef | 5c3c104 | 2017-08-04 22:01:57 | [diff] [blame] | 468 | std::map<rtc::IPAddress, rtc::IPAddress> alternative_address_mapping_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 469 | std::unique_ptr<Function> delay_dist_; |
| 470 | |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 471 | double drop_prob_ RTC_GUARDED_BY(mutex_); |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 | [diff] [blame] | 472 | // The largest UDP payload permitted on this virtual socket server. |
| 473 | // The default is the max size of IPv4 fragmented UDP packet payload: |
| 474 | // 65535 bytes - 8 bytes UDP header - 20 bytes IP header. |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 475 | size_t max_udp_payload_ RTC_GUARDED_BY(mutex_) = 65507; |
Harald Alvestrand | 3d792e9 | 2021-03-10 07:29:28 | [diff] [blame] | 476 | |
Florent Castelli | f94c053 | 2021-11-16 12:29:53 | [diff] [blame] | 477 | bool sending_blocked_ RTC_GUARDED_BY(mutex_) = false; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 478 | }; |
| 479 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 480 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 481 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 482 | #endif // RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ |