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