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 11 #ifndef RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ 12 #define RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ 13 14 #include <deque> 15 #include <map> 16 #include <vector> 17 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" 23 #include "rtc_base/checks.h" 24 #include "rtc_base/event.h" 25 #include "rtc_base/fake_clock.h" 26 #include "rtc_base/socket_server.h" 27 #include "rtc_base/synchronization/mutex.h" 28 29 namespace rtc { 30 31 class Packet; 32 class VirtualSocketServer; 33 class SocketAddressPair; 34 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<> { 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; 63 recv_buffer_size()64 size_t recv_buffer_size() const { return recv_buffer_size_; } send_buffer_size()65 size_t send_buffer_size() const { return send_buffer_.size(); } send_buffer_data()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 was_any()71 bool was_any() { return was_any_; } set_was_any(bool 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 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 93 private: 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 158 struct NetworkEntry { 159 size_t size; 160 int64_t done_time; 161 }; 162 163 typedef std::deque<NetworkEntry> NetworkQueue; 164 typedef std::vector<char> SendBuffer; 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 181 const scoped_refptr<SafetyBlock> safety_ = 182 make_ref_counted<SafetyBlock>(this); 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 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 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 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. 217 class VirtualSocketServer : public SocketServer { 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. 223 explicit VirtualSocketServer(ThreadProcessingFakeClock* fake_clock); 224 ~VirtualSocketServer() override; 225 226 VirtualSocketServer(const VirtualSocketServer&) = delete; 227 VirtualSocketServer& operator=(const VirtualSocketServer&) = delete; 228 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); 235 236 // Limits the network bandwidth (maximum bytes per second). Zero means that 237 // all sends occur instantly. Defaults to 0. 238 void set_bandwidth(uint32_t bandwidth) RTC_LOCKS_EXCLUDED(mutex_); 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. 242 void set_network_capacity(uint32_t capacity) RTC_LOCKS_EXCLUDED(mutex_); 243 244 // The amount of data which can be buffered by tcp on the sender's side 245 uint32_t send_buffer_capacity() const RTC_LOCKS_EXCLUDED(mutex_); 246 void set_send_buffer_capacity(uint32_t capacity) RTC_LOCKS_EXCLUDED(mutex_); 247 248 // The amount of data which can be buffered by tcp on the receiver's side 249 uint32_t recv_buffer_capacity() const RTC_LOCKS_EXCLUDED(mutex_); 250 void set_recv_buffer_capacity(uint32_t capacity) RTC_LOCKS_EXCLUDED(mutex_); 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. 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_); 258 259 // If the (transit) delay parameters are modified, this method should be 260 // called to recompute the new distribution. 261 void UpdateDelayDistribution() RTC_LOCKS_EXCLUDED(mutex_); 262 263 // Controls the (uniform) probability that any sent packet is dropped. This 264 // is separate from calculations to drop based on queue size. 265 void set_drop_probability(double drop_prob) RTC_LOCKS_EXCLUDED(mutex_); 266 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. 270 void set_max_udp_payload(size_t payload_size) RTC_LOCKS_EXCLUDED(mutex_); 271 272 // If `blocked` is true, subsequent attempts to send will result in -1 being 273 // returned, with the socket error set to EWOULDBLOCK. 274 // 275 // If this method is later called with `blocked` set to false, any sockets 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. 280 void SetSendingBlocked(bool blocked) RTC_LOCKS_EXCLUDED(mutex_); 281 282 // SocketFactory: 283 VirtualSocket* CreateSocket(int family, int type) override; 284 285 // SocketServer: 286 void SetMessageQueue(Thread* queue) override; 287 bool Wait(webrtc::TimeDelta max_wait_duration, bool process_io) override; 288 void WakeUp() override; 289 SetDelayOnAddress(const rtc::SocketAddress & address,int delay_ms)290 void SetDelayOnAddress(const rtc::SocketAddress& address, int delay_ms) { 291 delay_by_ip_[address.ipaddr()] = delay_ms; 292 } 293 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 304 typedef std::pair<double, double> Point; 305 typedef std::vector<Point> Function; 306 307 static std::unique_ptr<Function> CreateDistribution(uint32_t mean, 308 uint32_t stddev, 309 uint32_t samples); 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 324 // Number of packets that clients have attempted to send through this virtual 325 // socket server. Intended to be used for test assertions. 326 uint32_t sent_packets() const RTC_LOCKS_EXCLUDED(mutex_); 327 328 // Assign IP and Port if application's address is unspecified. Also apply 329 // `alternative_address_mapping_`. 330 SocketAddress AssignBindAddress(const SocketAddress& app_addr); 331 332 // Binds the given socket to the given (fully-defined) address. 333 int Bind(VirtualSocket* socket, const SocketAddress& addr); 334 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 342 // Connects the given socket to the socket at the given address 343 int Connect(VirtualSocket* socket, 344 const SocketAddress& remote_addr, 345 bool use_delay); 346 347 // Sends a disconnect message to the socket at the given address 348 bool Disconnect(VirtualSocket* socket); 349 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 357 // Sends the given packet to the socket at the given address (if one exists). 358 int SendUdp(VirtualSocket* socket, 359 const char* data, 360 size_t data_size, 361 const SocketAddress& remote_addr); 362 363 // Moves as much data as possible from the sender's buffer to the network 364 void SendTcp(VirtualSocket* socket) RTC_LOCKS_EXCLUDED(mutex_); 365 366 // Like above, but lookup sender by address. 367 void SendTcp(const SocketAddress& addr) RTC_LOCKS_EXCLUDED(mutex_); 368 369 // Computes the number of milliseconds required to send a packet of this size. 370 uint32_t SendDelay(uint32_t size) RTC_LOCKS_EXCLUDED(mutex_); 371 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: 383 friend VirtualSocket; 384 uint16_t GetNextPort(); 385 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 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 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 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); 415 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 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. 439 ThreadProcessingFakeClock* fake_clock_ = nullptr; 440 441 // Used to implement Wait/WakeUp. 442 Event wakeup_; 443 Thread* msg_queue_; 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 451 IPAddress default_source_address_v4_; 452 IPAddress default_source_address_v6_; 453 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_); 463 464 // Used for testing. 465 uint32_t sent_packets_ RTC_GUARDED_BY(mutex_) = 0; 466 467 std::map<rtc::IPAddress, int> delay_by_ip_; 468 std::map<rtc::IPAddress, rtc::IPAddress> alternative_address_mapping_; 469 std::unique_ptr<Function> delay_dist_; 470 471 double drop_prob_ RTC_GUARDED_BY(mutex_); 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. 475 size_t max_udp_payload_ RTC_GUARDED_BY(mutex_) = 65507; 476 477 bool sending_blocked_ RTC_GUARDED_BY(mutex_) = false; 478 }; 479 480 } // namespace rtc 481 482 #endif // RTC_BASE_VIRTUAL_SOCKET_SERVER_H_ 483