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 | |
| 11 | #ifndef WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |
| 12 | #define WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |
| 13 | |
| 14 | #include <assert.h> |
| 15 | |
| 16 | #include <deque> |
| 17 | #include <map> |
| 18 | |
| 19 | #include "webrtc/base/messagequeue.h" |
| 20 | #include "webrtc/base/socketserver.h" |
| 21 | |
| 22 | namespace rtc { |
| 23 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 24 | class Packet; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | class VirtualSocket; |
| 26 | class SocketAddressPair; |
| 27 | |
| 28 | // Simulates a network in the same manner as a loopback interface. The |
| 29 | // interface can create as many addresses as you want. All of the sockets |
| 30 | // created by this network will be able to communicate with one another, unless |
| 31 | // they are bound to addresses from incompatible families. |
| 32 | class VirtualSocketServer : public SocketServer, public sigslot::has_slots<> { |
| 33 | public: |
| 34 | // TODO: Add "owned" parameter. |
| 35 | // If "owned" is set, the supplied socketserver will be deleted later. |
| 36 | explicit VirtualSocketServer(SocketServer* ss); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 37 | ~VirtualSocketServer() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | |
| 39 | SocketServer* socketserver() { return server_; } |
| 40 | |
| 41 | // Limits the network bandwidth (maximum bytes per second). Zero means that |
| 42 | // all sends occur instantly. Defaults to 0. |
| 43 | uint32 bandwidth() const { return bandwidth_; } |
| 44 | void set_bandwidth(uint32 bandwidth) { bandwidth_ = bandwidth; } |
| 45 | |
| 46 | // Limits the amount of data which can be in flight on the network without |
| 47 | // packet loss (on a per sender basis). Defaults to 64 KB. |
| 48 | uint32 network_capacity() const { return network_capacity_; } |
| 49 | void set_network_capacity(uint32 capacity) { |
| 50 | network_capacity_ = capacity; |
| 51 | } |
| 52 | |
| 53 | // The amount of data which can be buffered by tcp on the sender's side |
| 54 | uint32 send_buffer_capacity() const { return send_buffer_capacity_; } |
| 55 | void set_send_buffer_capacity(uint32 capacity) { |
| 56 | send_buffer_capacity_ = capacity; |
| 57 | } |
| 58 | |
| 59 | // The amount of data which can be buffered by tcp on the receiver's side |
| 60 | uint32 recv_buffer_capacity() const { return recv_buffer_capacity_; } |
| 61 | void set_recv_buffer_capacity(uint32 capacity) { |
| 62 | recv_buffer_capacity_ = capacity; |
| 63 | } |
| 64 | |
| 65 | // Controls the (transit) delay for packets sent in the network. This does |
| 66 | // not inclue the time required to sit in the send queue. Both of these |
| 67 | // values are measured in milliseconds. Defaults to no delay. |
| 68 | uint32 delay_mean() const { return delay_mean_; } |
| 69 | uint32 delay_stddev() const { return delay_stddev_; } |
| 70 | uint32 delay_samples() const { return delay_samples_; } |
| 71 | void set_delay_mean(uint32 delay_mean) { delay_mean_ = delay_mean; } |
| 72 | void set_delay_stddev(uint32 delay_stddev) { |
| 73 | delay_stddev_ = delay_stddev; |
| 74 | } |
| 75 | void set_delay_samples(uint32 delay_samples) { |
| 76 | delay_samples_ = delay_samples; |
| 77 | } |
| 78 | |
| 79 | // If the (transit) delay parameters are modified, this method should be |
| 80 | // called to recompute the new distribution. |
| 81 | void UpdateDelayDistribution(); |
| 82 | |
| 83 | // Controls the (uniform) probability that any sent packet is dropped. This |
| 84 | // is separate from calculations to drop based on queue size. |
| 85 | double drop_probability() { return drop_prob_; } |
| 86 | void set_drop_probability(double drop_prob) { |
| 87 | assert((0 <= drop_prob) && (drop_prob <= 1)); |
| 88 | drop_prob_ = drop_prob; |
| 89 | } |
| 90 | |
| 91 | // SocketFactory: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 92 | Socket* CreateSocket(int type) override; |
| 93 | Socket* CreateSocket(int family, int type) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 95 | AsyncSocket* CreateAsyncSocket(int type) override; |
| 96 | AsyncSocket* CreateAsyncSocket(int family, int type) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 97 | |
| 98 | // SocketServer: |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 99 | void SetMessageQueue(MessageQueue* queue) override; |
| 100 | bool Wait(int cms, bool process_io) override; |
| 101 | void WakeUp() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | |
| 103 | typedef std::pair<double, double> Point; |
| 104 | typedef std::vector<Point> Function; |
| 105 | |
| 106 | static Function* CreateDistribution(uint32 mean, uint32 stddev, |
| 107 | uint32 samples); |
| 108 | |
| 109 | // Similar to Thread::ProcessMessages, but it only processes messages until |
| 110 | // there are no immediate messages or pending network traffic. Returns false |
| 111 | // if Thread::Stop() was called. |
| 112 | bool ProcessMessagesUntilIdle(); |
| 113 | |
jiayl@webrtc.org | 22406fc | 2014-09-09 15:44:05 +0000 | [diff] [blame] | 114 | // Sets the next port number to use for testing. |
| 115 | void SetNextPortForTesting(uint16 port); |
| 116 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 117 | // Close a pair of Tcp connections by addresses. Both connections will have |
| 118 | // its own OnClose invoked. |
| 119 | bool CloseTcpConnections(const SocketAddress& addr_local, |
| 120 | const SocketAddress& addr_remote); |
| 121 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | protected: |
| 123 | // Returns a new IP not used before in this network. |
| 124 | IPAddress GetNextIP(int family); |
| 125 | uint16 GetNextPort(); |
| 126 | |
| 127 | VirtualSocket* CreateSocketInternal(int family, int type); |
| 128 | |
| 129 | // Binds the given socket to addr, assigning and IP and Port if necessary |
| 130 | int Bind(VirtualSocket* socket, SocketAddress* addr); |
| 131 | |
| 132 | // Binds the given socket to the given (fully-defined) address. |
| 133 | int Bind(VirtualSocket* socket, const SocketAddress& addr); |
| 134 | |
| 135 | // Find the socket bound to the given address |
| 136 | VirtualSocket* LookupBinding(const SocketAddress& addr); |
| 137 | |
| 138 | int Unbind(const SocketAddress& addr, VirtualSocket* socket); |
| 139 | |
| 140 | // Adds a mapping between this socket pair and the socket. |
| 141 | void AddConnection(const SocketAddress& client, |
| 142 | const SocketAddress& server, |
| 143 | VirtualSocket* socket); |
| 144 | |
| 145 | // Find the socket pair corresponding to this server address. |
| 146 | VirtualSocket* LookupConnection(const SocketAddress& client, |
| 147 | const SocketAddress& server); |
| 148 | |
| 149 | void RemoveConnection(const SocketAddress& client, |
| 150 | const SocketAddress& server); |
| 151 | |
| 152 | // Connects the given socket to the socket at the given address |
| 153 | int Connect(VirtualSocket* socket, const SocketAddress& remote_addr, |
| 154 | bool use_delay); |
| 155 | |
| 156 | // Sends a disconnect message to the socket at the given address |
| 157 | bool Disconnect(VirtualSocket* socket); |
| 158 | |
| 159 | // Sends the given packet to the socket at the given address (if one exists). |
| 160 | int SendUdp(VirtualSocket* socket, const char* data, size_t data_size, |
| 161 | const SocketAddress& remote_addr); |
| 162 | |
| 163 | // Moves as much data as possible from the sender's buffer to the network |
| 164 | void SendTcp(VirtualSocket* socket); |
| 165 | |
| 166 | // Places a packet on the network. |
| 167 | void AddPacketToNetwork(VirtualSocket* socket, VirtualSocket* recipient, |
| 168 | uint32 cur_time, const char* data, size_t data_size, |
| 169 | size_t header_size, bool ordered); |
| 170 | |
| 171 | // Removes stale packets from the network |
| 172 | void PurgeNetworkPackets(VirtualSocket* socket, uint32 cur_time); |
| 173 | |
| 174 | // Computes the number of milliseconds required to send a packet of this size. |
| 175 | uint32 SendDelay(uint32 size); |
| 176 | |
| 177 | // Returns a random transit delay chosen from the appropriate distribution. |
| 178 | uint32 GetRandomTransitDelay(); |
| 179 | |
| 180 | // Basic operations on functions. Those that return a function also take |
| 181 | // ownership of the function given (and hence, may modify or delete it). |
| 182 | static Function* Accumulate(Function* f); |
| 183 | static Function* Invert(Function* f); |
| 184 | static Function* Resample(Function* f, double x1, double x2, uint32 samples); |
| 185 | static double Evaluate(Function* f, double x); |
| 186 | |
| 187 | // NULL out our message queue if it goes away. Necessary in the case where |
| 188 | // our lifetime is greater than that of the thread we are using, since we |
| 189 | // try to send Close messages for all connected sockets when we shutdown. |
| 190 | void OnMessageQueueDestroyed() { msg_queue_ = NULL; } |
| 191 | |
| 192 | // Determine if two sockets should be able to communicate. |
| 193 | // We don't (currently) specify an address family for sockets; instead, |
| 194 | // the currently bound address is used to infer the address family. |
| 195 | // Any socket that is not explicitly bound to an IPv4 address is assumed to be |
| 196 | // dual-stack capable. |
| 197 | // This function tests if two addresses can communicate, as well as the |
| 198 | // sockets to which they may be bound (the addresses may or may not yet be |
| 199 | // bound to the sockets). |
| 200 | // First the addresses are tested (after normalization): |
| 201 | // If both have the same family, then communication is OK. |
| 202 | // If only one is IPv4 then false, unless the other is bound to ::. |
| 203 | // This applies even if the IPv4 address is 0.0.0.0. |
| 204 | // The socket arguments are optional; the sockets are checked to see if they |
| 205 | // were explicitly bound to IPv6-any ('::'), and if so communication is |
| 206 | // permitted. |
| 207 | // NB: This scheme doesn't permit non-dualstack IPv6 sockets. |
| 208 | static bool CanInteractWith(VirtualSocket* local, VirtualSocket* remote); |
| 209 | |
| 210 | private: |
| 211 | friend class VirtualSocket; |
| 212 | |
| 213 | typedef std::map<SocketAddress, VirtualSocket*> AddressMap; |
| 214 | typedef std::map<SocketAddressPair, VirtualSocket*> ConnectionMap; |
| 215 | |
| 216 | SocketServer* server_; |
| 217 | bool server_owned_; |
| 218 | MessageQueue* msg_queue_; |
| 219 | bool stop_on_idle_; |
| 220 | uint32 network_delay_; |
| 221 | in_addr next_ipv4_; |
| 222 | in6_addr next_ipv6_; |
| 223 | uint16 next_port_; |
| 224 | AddressMap* bindings_; |
| 225 | ConnectionMap* connections_; |
| 226 | |
| 227 | uint32 bandwidth_; |
| 228 | uint32 network_capacity_; |
| 229 | uint32 send_buffer_capacity_; |
| 230 | uint32 recv_buffer_capacity_; |
| 231 | uint32 delay_mean_; |
| 232 | uint32 delay_stddev_; |
| 233 | uint32 delay_samples_; |
| 234 | Function* delay_dist_; |
| 235 | CriticalSection delay_crit_; |
| 236 | |
| 237 | double drop_prob_; |
Thiago Farina | ae0f0ee | 2015-04-04 23:56:53 +0000 | [diff] [blame] | 238 | DISALLOW_COPY_AND_ASSIGN(VirtualSocketServer); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 241 | // Implements the socket interface using the virtual network. Packets are |
| 242 | // passed as messages using the message queue of the socket server. |
| 243 | class VirtualSocket : public AsyncSocket, public MessageHandler { |
| 244 | public: |
| 245 | VirtualSocket(VirtualSocketServer* server, int family, int type, bool async); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 246 | ~VirtualSocket() override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 247 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 248 | SocketAddress GetLocalAddress() const override; |
| 249 | SocketAddress GetRemoteAddress() const override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 250 | |
| 251 | // Used by server sockets to set the local address without binding. |
| 252 | void SetLocalAddress(const SocketAddress& addr); |
| 253 | |
guoweis@webrtc.org | 4fba293 | 2014-12-18 04:45:05 +0000 | [diff] [blame] | 254 | // Used by TurnPortTest to mimic a case where proxy returns local host address |
| 255 | // instead of the original one TurnPort was bound against. Please see WebRTC |
| 256 | // issue 3927 for more detail. |
| 257 | void SetAlternativeLocalAddress(const SocketAddress& addr); |
| 258 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 259 | int Bind(const SocketAddress& addr) override; |
| 260 | int Connect(const SocketAddress& addr) override; |
| 261 | int Close() override; |
| 262 | int Send(const void* pv, size_t cb) override; |
| 263 | int SendTo(const void* pv, size_t cb, const SocketAddress& addr) override; |
| 264 | int Recv(void* pv, size_t cb) override; |
| 265 | int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) override; |
| 266 | int Listen(int backlog) override; |
| 267 | VirtualSocket* Accept(SocketAddress* paddr) override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 268 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 269 | int GetError() const override; |
| 270 | void SetError(int error) override; |
| 271 | ConnState GetState() const override; |
| 272 | int GetOption(Option opt, int* value) override; |
| 273 | int SetOption(Option opt, int value) override; |
| 274 | int EstimateMTU(uint16* mtu) override; |
| 275 | void OnMessage(Message* pmsg) override; |
guoweis@webrtc.org | 0eb6eec | 2014-12-17 22:03:33 +0000 | [diff] [blame] | 276 | |
| 277 | bool was_any() { return was_any_; } |
| 278 | void set_was_any(bool was_any) { was_any_ = was_any; } |
| 279 | |
| 280 | // For testing purpose only. Fired when client socket is bound to an address. |
| 281 | sigslot::signal2<VirtualSocket*, const SocketAddress&> SignalAddressReady; |
| 282 | |
| 283 | private: |
| 284 | struct NetworkEntry { |
| 285 | size_t size; |
| 286 | uint32 done_time; |
| 287 | }; |
| 288 | |
| 289 | typedef std::deque<SocketAddress> ListenQueue; |
| 290 | typedef std::deque<NetworkEntry> NetworkQueue; |
| 291 | typedef std::vector<char> SendBuffer; |
| 292 | typedef std::list<Packet*> RecvBuffer; |
| 293 | typedef std::map<Option, int> OptionsMap; |
| 294 | |
| 295 | int InitiateConnect(const SocketAddress& addr, bool use_delay); |
| 296 | void CompleteConnect(const SocketAddress& addr, bool notify); |
| 297 | int SendUdp(const void* pv, size_t cb, const SocketAddress& addr); |
| 298 | int SendTcp(const void* pv, size_t cb); |
| 299 | |
| 300 | VirtualSocketServer* server_; |
| 301 | int family_; |
| 302 | int type_; |
| 303 | bool async_; |
| 304 | ConnState state_; |
| 305 | int error_; |
| 306 | SocketAddress local_addr_; |
| 307 | SocketAddress alternative_local_addr_; |
| 308 | SocketAddress remote_addr_; |
| 309 | |
| 310 | // Pending sockets which can be Accepted |
| 311 | ListenQueue* listen_queue_; |
| 312 | |
| 313 | // Data which tcp has buffered for sending |
| 314 | SendBuffer send_buffer_; |
| 315 | bool write_enabled_; |
| 316 | |
| 317 | // Critical section to protect the recv_buffer and queue_ |
| 318 | CriticalSection crit_; |
| 319 | |
| 320 | // Network model that enforces bandwidth and capacity constraints |
| 321 | NetworkQueue network_; |
| 322 | size_t network_size_; |
| 323 | |
| 324 | // Data which has been received from the network |
| 325 | RecvBuffer recv_buffer_; |
| 326 | // The amount of data which is in flight or in recv_buffer_ |
| 327 | size_t recv_buffer_size_; |
| 328 | |
| 329 | // Is this socket bound? |
| 330 | bool bound_; |
| 331 | |
| 332 | // When we bind a socket to Any, VSS's Bind gives it another address. For |
| 333 | // dual-stack sockets, we want to distinguish between sockets that were |
| 334 | // explicitly given a particular address and sockets that had one picked |
| 335 | // for them by VSS. |
| 336 | bool was_any_; |
| 337 | |
| 338 | // Store the options that are set |
| 339 | OptionsMap options_map_; |
| 340 | |
| 341 | friend class VirtualSocketServer; |
| 342 | }; |
| 343 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 344 | } // namespace rtc |
| 345 | |
| 346 | #endif // WEBRTC_BASE_VIRTUALSOCKETSERVER_H_ |