blob: c8cac0e95280fd2f746a2dc059037191701e73ad [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/virtualsocketserver.h"
29
30#include <errno.h>
31
32#include <algorithm>
33#include <cmath>
34#include <map>
35#include <vector>
36
37#include "talk/base/common.h"
38#include "talk/base/logging.h"
39#include "talk/base/physicalsocketserver.h"
40#include "talk/base/socketaddresspair.h"
41#include "talk/base/thread.h"
42#include "talk/base/timeutils.h"
43
44namespace talk_base {
45#ifdef WIN32
46const in_addr kInitialNextIPv4 = { {0x01, 0, 0, 0} };
47#else
48// This value is entirely arbitrary, hence the lack of concern about endianness.
49const in_addr kInitialNextIPv4 = { 0x01000000 };
50#endif
51// Starts at ::2 so as to not cause confusion with ::1.
52const in6_addr kInitialNextIPv6 = { { {
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
54 } } };
55
56const uint16 kFirstEphemeralPort = 49152;
57const uint16 kLastEphemeralPort = 65535;
58const uint16 kEphemeralPortCount = kLastEphemeralPort - kFirstEphemeralPort + 1;
59const uint32 kDefaultNetworkCapacity = 64 * 1024;
60const uint32 kDefaultTcpBufferSize = 32 * 1024;
61
62const uint32 UDP_HEADER_SIZE = 28; // IP + UDP headers
63const uint32 TCP_HEADER_SIZE = 40; // IP + TCP headers
64const uint32 TCP_MSS = 1400; // Maximum segment size
65
66// Note: The current algorithm doesn't work for sample sizes smaller than this.
67const int NUM_SAMPLES = 1000;
68
69enum {
70 MSG_ID_PACKET,
71 MSG_ID_CONNECT,
72 MSG_ID_DISCONNECT,
73};
74
75// Packets are passed between sockets as messages. We copy the data just like
76// the kernel does.
77class Packet : public MessageData {
78 public:
79 Packet(const char* data, size_t size, const SocketAddress& from)
80 : size_(size), consumed_(0), from_(from) {
81 ASSERT(NULL != data);
82 data_ = new char[size_];
83 std::memcpy(data_, data, size_);
84 }
85
86 virtual ~Packet() {
87 delete[] data_;
88 }
89
90 const char* data() const { return data_ + consumed_; }
91 size_t size() const { return size_ - consumed_; }
92 const SocketAddress& from() const { return from_; }
93
94 // Remove the first size bytes from the data.
95 void Consume(size_t size) {
96 ASSERT(size + consumed_ < size_);
97 consumed_ += size;
98 }
99
100 private:
101 char* data_;
102 size_t size_, consumed_;
103 SocketAddress from_;
104};
105
106struct MessageAddress : public MessageData {
107 explicit MessageAddress(const SocketAddress& a) : addr(a) { }
108 SocketAddress addr;
109};
110
111// Implements the socket interface using the virtual network. Packets are
112// passed as messages using the message queue of the socket server.
113class VirtualSocket : public AsyncSocket, public MessageHandler {
114 public:
115 VirtualSocket(VirtualSocketServer* server, int family, int type, bool async)
116 : server_(server), family_(family), type_(type), async_(async),
117 state_(CS_CLOSED), listen_queue_(NULL), write_enabled_(false),
118 network_size_(0), recv_buffer_size_(0), bound_(false), was_any_(false) {
119 ASSERT((type_ == SOCK_DGRAM) || (type_ == SOCK_STREAM));
120 ASSERT(async_ || (type_ != SOCK_STREAM)); // We only support async streams
121 }
122
123 virtual ~VirtualSocket() {
124 Close();
125
126 for (RecvBuffer::iterator it = recv_buffer_.begin();
127 it != recv_buffer_.end(); ++it) {
128 delete *it;
129 }
130 }
131
132 virtual SocketAddress GetLocalAddress() const {
133 return local_addr_;
134 }
135
136 virtual SocketAddress GetRemoteAddress() const {
137 return remote_addr_;
138 }
139
140 // Used by server sockets to set the local address without binding.
141 void SetLocalAddress(const SocketAddress& addr) {
142 local_addr_ = addr;
143 }
144
145 virtual int Bind(const SocketAddress& addr) {
146 if (!local_addr_.IsNil()) {
147 error_ = EINVAL;
148 return -1;
149 }
150 local_addr_ = addr;
151 int result = server_->Bind(this, &local_addr_);
152 if (result != 0) {
153 local_addr_.Clear();
154 error_ = EADDRINUSE;
155 } else {
156 bound_ = true;
157 was_any_ = addr.IsAnyIP();
158 }
159 return result;
160 }
161
162 virtual int Connect(const SocketAddress& addr) {
163 return InitiateConnect(addr, true);
164 }
165
166 virtual int Close() {
167 if (!local_addr_.IsNil() && bound_) {
168 // Remove from the binding table.
169 server_->Unbind(local_addr_, this);
170 bound_ = false;
171 }
172
173 if (SOCK_STREAM == type_) {
174 // Cancel pending sockets
175 if (listen_queue_) {
176 while (!listen_queue_->empty()) {
177 SocketAddress addr = listen_queue_->front();
178
179 // Disconnect listening socket.
180 server_->Disconnect(server_->LookupBinding(addr));
181 listen_queue_->pop_front();
182 }
183 delete listen_queue_;
184 listen_queue_ = NULL;
185 }
186 // Disconnect stream sockets
187 if (CS_CONNECTED == state_) {
188 // Disconnect remote socket, check if it is a child of a server socket.
189 VirtualSocket* socket =
190 server_->LookupConnection(local_addr_, remote_addr_);
191 if (!socket) {
192 // Not a server socket child, then see if it is bound.
193 // TODO: If this is indeed a server socket that has no
194 // children this will cause the server socket to be
195 // closed. This might lead to unexpected results, how to fix this?
196 socket = server_->LookupBinding(remote_addr_);
197 }
198 server_->Disconnect(socket);
199
200 // Remove mapping for both directions.
201 server_->RemoveConnection(remote_addr_, local_addr_);
202 server_->RemoveConnection(local_addr_, remote_addr_);
203 }
204 // Cancel potential connects
205 MessageList msgs;
206 if (server_->msg_queue_) {
207 server_->msg_queue_->Clear(this, MSG_ID_CONNECT, &msgs);
208 }
209 for (MessageList::iterator it = msgs.begin(); it != msgs.end(); ++it) {
210 ASSERT(NULL != it->pdata);
211 MessageAddress* data = static_cast<MessageAddress*>(it->pdata);
212
213 // Lookup remote side.
214 VirtualSocket* socket = server_->LookupConnection(local_addr_,
215 data->addr);
216 if (socket) {
217 // Server socket, remote side is a socket retreived by
218 // accept. Accepted sockets are not bound so we will not
219 // find it by looking in the bindings table.
220 server_->Disconnect(socket);
221 server_->RemoveConnection(local_addr_, data->addr);
222 } else {
223 server_->Disconnect(server_->LookupBinding(data->addr));
224 }
225 delete data;
226 }
227 // Clear incoming packets and disconnect messages
228 if (server_->msg_queue_) {
229 server_->msg_queue_->Clear(this);
230 }
231 }
232
233 state_ = CS_CLOSED;
234 local_addr_.Clear();
235 remote_addr_.Clear();
236 return 0;
237 }
238
239 virtual int Send(const void *pv, size_t cb) {
240 if (CS_CONNECTED != state_) {
241 error_ = ENOTCONN;
242 return -1;
243 }
244 if (SOCK_DGRAM == type_) {
245 return SendUdp(pv, cb, remote_addr_);
246 } else {
247 return SendTcp(pv, cb);
248 }
249 }
250
251 virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) {
252 if (SOCK_DGRAM == type_) {
253 return SendUdp(pv, cb, addr);
254 } else {
255 if (CS_CONNECTED != state_) {
256 error_ = ENOTCONN;
257 return -1;
258 }
259 return SendTcp(pv, cb);
260 }
261 }
262
263 virtual int Recv(void *pv, size_t cb) {
264 SocketAddress addr;
265 return RecvFrom(pv, cb, &addr);
266 }
267
268 virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) {
269 // If we don't have a packet, then either error or wait for one to arrive.
270 if (recv_buffer_.empty()) {
271 if (async_) {
272 error_ = EAGAIN;
273 return -1;
274 }
275 while (recv_buffer_.empty()) {
276 Message msg;
277 server_->msg_queue_->Get(&msg);
278 server_->msg_queue_->Dispatch(&msg);
279 }
280 }
281
282 // Return the packet at the front of the queue.
283 Packet* packet = recv_buffer_.front();
284 size_t data_read = _min(cb, packet->size());
285 std::memcpy(pv, packet->data(), data_read);
286 *paddr = packet->from();
287
288 if (data_read < packet->size()) {
289 packet->Consume(data_read);
290 } else {
291 recv_buffer_.pop_front();
292 delete packet;
293 }
294
295 if (SOCK_STREAM == type_) {
296 bool was_full = (recv_buffer_size_ == server_->recv_buffer_capacity_);
297 recv_buffer_size_ -= data_read;
298 if (was_full) {
299 VirtualSocket* sender = server_->LookupBinding(remote_addr_);
300 ASSERT(NULL != sender);
301 server_->SendTcp(sender);
302 }
303 }
304
305 return static_cast<int>(data_read);
306 }
307
308 virtual int Listen(int backlog) {
309 ASSERT(SOCK_STREAM == type_);
310 ASSERT(CS_CLOSED == state_);
311 if (local_addr_.IsNil()) {
312 error_ = EINVAL;
313 return -1;
314 }
315 ASSERT(NULL == listen_queue_);
316 listen_queue_ = new ListenQueue;
317 state_ = CS_CONNECTING;
318 return 0;
319 }
320
321 virtual VirtualSocket* Accept(SocketAddress *paddr) {
322 if (NULL == listen_queue_) {
323 error_ = EINVAL;
324 return NULL;
325 }
326 while (!listen_queue_->empty()) {
327 VirtualSocket* socket = new VirtualSocket(server_, AF_INET, type_,
328 async_);
329
330 // Set the new local address to the same as this server socket.
331 socket->SetLocalAddress(local_addr_);
332 // Sockets made from a socket that 'was Any' need to inherit that.
333 socket->set_was_any(was_any_);
334 SocketAddress remote_addr(listen_queue_->front());
335 int result = socket->InitiateConnect(remote_addr, false);
336 listen_queue_->pop_front();
337 if (result != 0) {
338 delete socket;
339 continue;
340 }
341 socket->CompleteConnect(remote_addr, false);
342 if (paddr) {
343 *paddr = remote_addr;
344 }
345 return socket;
346 }
347 error_ = EWOULDBLOCK;
348 return NULL;
349 }
350
351 virtual int GetError() const {
352 return error_;
353 }
354
355 virtual void SetError(int error) {
356 error_ = error;
357 }
358
359 virtual ConnState GetState() const {
360 return state_;
361 }
362
363 virtual int GetOption(Option opt, int* value) {
364 OptionsMap::const_iterator it = options_map_.find(opt);
365 if (it == options_map_.end()) {
366 return -1;
367 }
368 *value = it->second;
369 return 0; // 0 is success to emulate getsockopt()
370 }
371
372 virtual int SetOption(Option opt, int value) {
373 options_map_[opt] = value;
374 return 0; // 0 is success to emulate setsockopt()
375 }
376
377 virtual int EstimateMTU(uint16* mtu) {
378 if (CS_CONNECTED != state_)
379 return ENOTCONN;
380 else
381 return 65536;
382 }
383
384 void OnMessage(Message *pmsg) {
385 if (pmsg->message_id == MSG_ID_PACKET) {
386 //ASSERT(!local_addr_.IsAny());
387 ASSERT(NULL != pmsg->pdata);
388 Packet* packet = static_cast<Packet*>(pmsg->pdata);
389
390 recv_buffer_.push_back(packet);
391
392 if (async_) {
393 SignalReadEvent(this);
394 }
395 } else if (pmsg->message_id == MSG_ID_CONNECT) {
396 ASSERT(NULL != pmsg->pdata);
397 MessageAddress* data = static_cast<MessageAddress*>(pmsg->pdata);
398 if (listen_queue_ != NULL) {
399 listen_queue_->push_back(data->addr);
400 if (async_) {
401 SignalReadEvent(this);
402 }
403 } else if ((SOCK_STREAM == type_) && (CS_CONNECTING == state_)) {
404 CompleteConnect(data->addr, true);
405 } else {
406 LOG(LS_VERBOSE) << "Socket at " << local_addr_ << " is not listening";
407 server_->Disconnect(server_->LookupBinding(data->addr));
408 }
409 delete data;
410 } else if (pmsg->message_id == MSG_ID_DISCONNECT) {
411 ASSERT(SOCK_STREAM == type_);
412 if (CS_CLOSED != state_) {
413 int error = (CS_CONNECTING == state_) ? ECONNREFUSED : 0;
414 state_ = CS_CLOSED;
415 remote_addr_.Clear();
416 if (async_) {
417 SignalCloseEvent(this, error);
418 }
419 }
420 } else {
421 ASSERT(false);
422 }
423 }
424
425 bool was_any() { return was_any_; }
426 void set_was_any(bool was_any) { was_any_ = was_any; }
427
428 private:
429 struct NetworkEntry {
430 size_t size;
431 uint32 done_time;
432 };
433
434 typedef std::deque<SocketAddress> ListenQueue;
435 typedef std::deque<NetworkEntry> NetworkQueue;
436 typedef std::vector<char> SendBuffer;
437 typedef std::list<Packet*> RecvBuffer;
438 typedef std::map<Option, int> OptionsMap;
439
440 int InitiateConnect(const SocketAddress& addr, bool use_delay) {
441 if (!remote_addr_.IsNil()) {
442 error_ = (CS_CONNECTED == state_) ? EISCONN : EINPROGRESS;
443 return -1;
444 }
445 if (local_addr_.IsNil()) {
446 // If there's no local address set, grab a random one in the correct AF.
447 int result = 0;
448 if (addr.ipaddr().family() == AF_INET) {
449 result = Bind(SocketAddress("0.0.0.0", 0));
450 } else if (addr.ipaddr().family() == AF_INET6) {
451 result = Bind(SocketAddress("::", 0));
452 }
453 if (result != 0) {
454 return result;
455 }
456 }
457 if (type_ == SOCK_DGRAM) {
458 remote_addr_ = addr;
459 state_ = CS_CONNECTED;
460 } else {
461 int result = server_->Connect(this, addr, use_delay);
462 if (result != 0) {
463 error_ = EHOSTUNREACH;
464 return -1;
465 }
466 state_ = CS_CONNECTING;
467 }
468 return 0;
469 }
470
471 void CompleteConnect(const SocketAddress& addr, bool notify) {
472 ASSERT(CS_CONNECTING == state_);
473 remote_addr_ = addr;
474 state_ = CS_CONNECTED;
475 server_->AddConnection(remote_addr_, local_addr_, this);
476 if (async_ && notify) {
477 SignalConnectEvent(this);
478 }
479 }
480
481 int SendUdp(const void* pv, size_t cb, const SocketAddress& addr) {
482 // If we have not been assigned a local port, then get one.
483 if (local_addr_.IsNil()) {
484 local_addr_ = EmptySocketAddressWithFamily(addr.ipaddr().family());
485 int result = server_->Bind(this, &local_addr_);
486 if (result != 0) {
487 local_addr_.Clear();
488 error_ = EADDRINUSE;
489 return result;
490 }
491 }
492
493 // Send the data in a message to the appropriate socket.
494 return server_->SendUdp(this, static_cast<const char*>(pv), cb, addr);
495 }
496
497 int SendTcp(const void* pv, size_t cb) {
498 size_t capacity = server_->send_buffer_capacity_ - send_buffer_.size();
499 if (0 == capacity) {
500 write_enabled_ = true;
501 error_ = EWOULDBLOCK;
502 return -1;
503 }
504 size_t consumed = _min(cb, capacity);
505 const char* cpv = static_cast<const char*>(pv);
506 send_buffer_.insert(send_buffer_.end(), cpv, cpv + consumed);
507 server_->SendTcp(this);
508 return static_cast<int>(consumed);
509 }
510
511 VirtualSocketServer* server_;
512 int family_;
513 int type_;
514 bool async_;
515 ConnState state_;
516 int error_;
517 SocketAddress local_addr_;
518 SocketAddress remote_addr_;
519
520 // Pending sockets which can be Accepted
521 ListenQueue* listen_queue_;
522
523 // Data which tcp has buffered for sending
524 SendBuffer send_buffer_;
525 bool write_enabled_;
526
527 // Critical section to protect the recv_buffer and queue_
528 CriticalSection crit_;
529
530 // Network model that enforces bandwidth and capacity constraints
531 NetworkQueue network_;
532 size_t network_size_;
533
534 // Data which has been received from the network
535 RecvBuffer recv_buffer_;
536 // The amount of data which is in flight or in recv_buffer_
537 size_t recv_buffer_size_;
538
539 // Is this socket bound?
540 bool bound_;
541
542 // When we bind a socket to Any, VSS's Bind gives it another address. For
543 // dual-stack sockets, we want to distinguish between sockets that were
544 // explicitly given a particular address and sockets that had one picked
545 // for them by VSS.
546 bool was_any_;
547
548 // Store the options that are set
549 OptionsMap options_map_;
550
551 friend class VirtualSocketServer;
552};
553
554VirtualSocketServer::VirtualSocketServer(SocketServer* ss)
555 : server_(ss), server_owned_(false), msg_queue_(NULL), stop_on_idle_(false),
556 network_delay_(Time()), next_ipv4_(kInitialNextIPv4),
557 next_ipv6_(kInitialNextIPv6), next_port_(kFirstEphemeralPort),
558 bindings_(new AddressMap()), connections_(new ConnectionMap()),
559 bandwidth_(0), network_capacity_(kDefaultNetworkCapacity),
560 send_buffer_capacity_(kDefaultTcpBufferSize),
561 recv_buffer_capacity_(kDefaultTcpBufferSize),
562 delay_mean_(0), delay_stddev_(0), delay_samples_(NUM_SAMPLES),
563 delay_dist_(NULL), drop_prob_(0.0) {
564 if (!server_) {
565 server_ = new PhysicalSocketServer();
566 server_owned_ = true;
567 }
568 UpdateDelayDistribution();
569}
570
571VirtualSocketServer::~VirtualSocketServer() {
572 delete bindings_;
573 delete connections_;
574 delete delay_dist_;
575 if (server_owned_) {
576 delete server_;
577 }
578}
579
580IPAddress VirtualSocketServer::GetNextIP(int family) {
581 if (family == AF_INET) {
582 IPAddress next_ip(next_ipv4_);
583 next_ipv4_.s_addr =
584 HostToNetwork32(NetworkToHost32(next_ipv4_.s_addr) + 1);
585 return next_ip;
586 } else if (family == AF_INET6) {
587 IPAddress next_ip(next_ipv6_);
588 uint32* as_ints = reinterpret_cast<uint32*>(&next_ipv6_.s6_addr);
589 as_ints[3] += 1;
590 return next_ip;
591 }
592 return IPAddress();
593}
594
595uint16 VirtualSocketServer::GetNextPort() {
596 uint16 port = next_port_;
597 if (next_port_ < kLastEphemeralPort) {
598 ++next_port_;
599 } else {
600 next_port_ = kFirstEphemeralPort;
601 }
602 return port;
603}
604
605Socket* VirtualSocketServer::CreateSocket(int type) {
606 return CreateSocket(AF_INET, type);
607}
608
609Socket* VirtualSocketServer::CreateSocket(int family, int type) {
610 return CreateSocketInternal(family, type);
611}
612
613AsyncSocket* VirtualSocketServer::CreateAsyncSocket(int type) {
614 return CreateAsyncSocket(AF_INET, type);
615}
616
617AsyncSocket* VirtualSocketServer::CreateAsyncSocket(int family, int type) {
618 return CreateSocketInternal(family, type);
619}
620
621VirtualSocket* VirtualSocketServer::CreateSocketInternal(int family, int type) {
622 return new VirtualSocket(this, family, type, true);
623}
624
625void VirtualSocketServer::SetMessageQueue(MessageQueue* msg_queue) {
626 msg_queue_ = msg_queue;
627 if (msg_queue_) {
628 msg_queue_->SignalQueueDestroyed.connect(this,
629 &VirtualSocketServer::OnMessageQueueDestroyed);
630 }
631}
632
633bool VirtualSocketServer::Wait(int cmsWait, bool process_io) {
634 ASSERT(msg_queue_ == Thread::Current());
635 if (stop_on_idle_ && Thread::Current()->empty()) {
636 return false;
637 }
638 return socketserver()->Wait(cmsWait, process_io);
639}
640
641void VirtualSocketServer::WakeUp() {
642 socketserver()->WakeUp();
643}
644
645bool VirtualSocketServer::ProcessMessagesUntilIdle() {
646 ASSERT(msg_queue_ == Thread::Current());
647 stop_on_idle_ = true;
648 while (!msg_queue_->empty()) {
649 Message msg;
650 if (msg_queue_->Get(&msg, kForever)) {
651 msg_queue_->Dispatch(&msg);
652 }
653 }
654 stop_on_idle_ = false;
655 return !msg_queue_->IsQuitting();
656}
657
658int VirtualSocketServer::Bind(VirtualSocket* socket,
659 const SocketAddress& addr) {
660 ASSERT(NULL != socket);
661 // Address must be completely specified at this point
662 ASSERT(!IPIsUnspec(addr.ipaddr()));
663 ASSERT(addr.port() != 0);
664
665 // Normalize the address (turns v6-mapped addresses into v4-addresses).
666 SocketAddress normalized(addr.ipaddr().Normalized(), addr.port());
667
668 AddressMap::value_type entry(normalized, socket);
669 return bindings_->insert(entry).second ? 0 : -1;
670}
671
672int VirtualSocketServer::Bind(VirtualSocket* socket, SocketAddress* addr) {
673 ASSERT(NULL != socket);
674
675 if (IPIsAny(addr->ipaddr())) {
676 addr->SetIP(GetNextIP(addr->ipaddr().family()));
677 } else if (!IPIsUnspec(addr->ipaddr())) {
678 addr->SetIP(addr->ipaddr().Normalized());
679 } else {
680 ASSERT(false);
681 }
682
683 if (addr->port() == 0) {
684 for (int i = 0; i < kEphemeralPortCount; ++i) {
685 addr->SetPort(GetNextPort());
686 if (bindings_->find(*addr) == bindings_->end()) {
687 break;
688 }
689 }
690 }
691
692 return Bind(socket, *addr);
693}
694
695VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) {
696 SocketAddress normalized(addr.ipaddr().Normalized(),
697 addr.port());
698 AddressMap::iterator it = bindings_->find(normalized);
699 return (bindings_->end() != it) ? it->second : NULL;
700}
701
702int VirtualSocketServer::Unbind(const SocketAddress& addr,
703 VirtualSocket* socket) {
704 SocketAddress normalized(addr.ipaddr().Normalized(),
705 addr.port());
706 ASSERT((*bindings_)[normalized] == socket);
707 bindings_->erase(bindings_->find(normalized));
708 return 0;
709}
710
711void VirtualSocketServer::AddConnection(const SocketAddress& local,
712 const SocketAddress& remote,
713 VirtualSocket* remote_socket) {
714 // Add this socket pair to our routing table. This will allow
715 // multiple clients to connect to the same server address.
716 SocketAddress local_normalized(local.ipaddr().Normalized(),
717 local.port());
718 SocketAddress remote_normalized(remote.ipaddr().Normalized(),
719 remote.port());
720 SocketAddressPair address_pair(local_normalized, remote_normalized);
721 connections_->insert(std::pair<SocketAddressPair,
722 VirtualSocket*>(address_pair, remote_socket));
723}
724
725VirtualSocket* VirtualSocketServer::LookupConnection(
726 const SocketAddress& local,
727 const SocketAddress& remote) {
728 SocketAddress local_normalized(local.ipaddr().Normalized(),
729 local.port());
730 SocketAddress remote_normalized(remote.ipaddr().Normalized(),
731 remote.port());
732 SocketAddressPair address_pair(local_normalized, remote_normalized);
733 ConnectionMap::iterator it = connections_->find(address_pair);
734 return (connections_->end() != it) ? it->second : NULL;
735}
736
737void VirtualSocketServer::RemoveConnection(const SocketAddress& local,
738 const SocketAddress& remote) {
739 SocketAddress local_normalized(local.ipaddr().Normalized(),
740 local.port());
741 SocketAddress remote_normalized(remote.ipaddr().Normalized(),
742 remote.port());
743 SocketAddressPair address_pair(local_normalized, remote_normalized);
744 connections_->erase(address_pair);
745}
746
747static double Random() {
748 return static_cast<double>(rand()) / RAND_MAX;
749}
750
751int VirtualSocketServer::Connect(VirtualSocket* socket,
752 const SocketAddress& remote_addr,
753 bool use_delay) {
754 uint32 delay = use_delay ? GetRandomTransitDelay() : 0;
755 VirtualSocket* remote = LookupBinding(remote_addr);
756 if (!CanInteractWith(socket, remote)) {
757 LOG(LS_INFO) << "Address family mismatch between "
758 << socket->GetLocalAddress() << " and " << remote_addr;
759 return -1;
760 }
761 if (remote != NULL) {
762 SocketAddress addr = socket->GetLocalAddress();
763 msg_queue_->PostDelayed(delay, remote, MSG_ID_CONNECT,
764 new MessageAddress(addr));
765 } else {
766 LOG(LS_INFO) << "No one listening at " << remote_addr;
767 msg_queue_->PostDelayed(delay, socket, MSG_ID_DISCONNECT);
768 }
769 return 0;
770}
771
772bool VirtualSocketServer::Disconnect(VirtualSocket* socket) {
773 if (socket) {
774 // Remove the mapping.
775 msg_queue_->Post(socket, MSG_ID_DISCONNECT);
776 return true;
777 }
778 return false;
779}
780
781int VirtualSocketServer::SendUdp(VirtualSocket* socket,
782 const char* data, size_t data_size,
783 const SocketAddress& remote_addr) {
784 // See if we want to drop this packet.
785 if (Random() < drop_prob_) {
786 LOG(LS_VERBOSE) << "Dropping packet: bad luck";
787 return static_cast<int>(data_size);
788 }
789
790 VirtualSocket* recipient = LookupBinding(remote_addr);
791 if (!recipient) {
792 // Make a fake recipient for address family checking.
793 scoped_ptr<VirtualSocket> dummy_socket(
794 CreateSocketInternal(AF_INET, SOCK_DGRAM));
795 dummy_socket->SetLocalAddress(remote_addr);
796 if (!CanInteractWith(socket, dummy_socket.get())) {
797 LOG(LS_VERBOSE) << "Incompatible address families: "
798 << socket->GetLocalAddress() << " and " << remote_addr;
799 return -1;
800 }
801 LOG(LS_VERBOSE) << "No one listening at " << remote_addr;
802 return static_cast<int>(data_size);
803 }
804
805 if (!CanInteractWith(socket, recipient)) {
806 LOG(LS_VERBOSE) << "Incompatible address families: "
807 << socket->GetLocalAddress() << " and " << remote_addr;
808 return -1;
809 }
810
811 CritScope cs(&socket->crit_);
812
813 uint32 cur_time = Time();
814 PurgeNetworkPackets(socket, cur_time);
815
816 // Determine whether we have enough bandwidth to accept this packet. To do
817 // this, we need to update the send queue. Once we know it's current size,
818 // we know whether we can fit this packet.
819 //
820 // NOTE: There are better algorithms for maintaining such a queue (such as
821 // "Derivative Random Drop"); however, this algorithm is a more accurate
822 // simulation of what a normal network would do.
823
824 size_t packet_size = data_size + UDP_HEADER_SIZE;
825 if (socket->network_size_ + packet_size > network_capacity_) {
826 LOG(LS_VERBOSE) << "Dropping packet: network capacity exceeded";
827 return static_cast<int>(data_size);
828 }
829
830 AddPacketToNetwork(socket, recipient, cur_time, data, data_size,
831 UDP_HEADER_SIZE, false);
832
833 return static_cast<int>(data_size);
834}
835
836void VirtualSocketServer::SendTcp(VirtualSocket* socket) {
837 // TCP can't send more data than will fill up the receiver's buffer.
838 // We track the data that is in the buffer plus data in flight using the
839 // recipient's recv_buffer_size_. Anything beyond that must be stored in the
840 // sender's buffer. We will trigger the buffered data to be sent when data
841 // is read from the recv_buffer.
842
843 // Lookup the local/remote pair in the connections table.
844 VirtualSocket* recipient = LookupConnection(socket->local_addr_,
845 socket->remote_addr_);
846 if (!recipient) {
847 LOG(LS_VERBOSE) << "Sending data to no one.";
848 return;
849 }
850
851 CritScope cs(&socket->crit_);
852
853 uint32 cur_time = Time();
854 PurgeNetworkPackets(socket, cur_time);
855
856 while (true) {
857 size_t available = recv_buffer_capacity_ - recipient->recv_buffer_size_;
858 size_t max_data_size = _min<size_t>(available, TCP_MSS - TCP_HEADER_SIZE);
859 size_t data_size = _min(socket->send_buffer_.size(), max_data_size);
860 if (0 == data_size)
861 break;
862
863 AddPacketToNetwork(socket, recipient, cur_time, &socket->send_buffer_[0],
864 data_size, TCP_HEADER_SIZE, true);
865 recipient->recv_buffer_size_ += data_size;
866
867 size_t new_buffer_size = socket->send_buffer_.size() - data_size;
868 // Avoid undefined access beyond the last element of the vector.
869 // This only happens when new_buffer_size is 0.
870 if (data_size < socket->send_buffer_.size()) {
871 // memmove is required for potentially overlapping source/destination.
872 memmove(&socket->send_buffer_[0], &socket->send_buffer_[data_size],
873 new_buffer_size);
874 }
875 socket->send_buffer_.resize(new_buffer_size);
876 }
877
878 if (socket->write_enabled_
879 && (socket->send_buffer_.size() < send_buffer_capacity_)) {
880 socket->write_enabled_ = false;
881 socket->SignalWriteEvent(socket);
882 }
883}
884
885void VirtualSocketServer::AddPacketToNetwork(VirtualSocket* sender,
886 VirtualSocket* recipient,
887 uint32 cur_time,
888 const char* data,
889 size_t data_size,
890 size_t header_size,
891 bool ordered) {
892 VirtualSocket::NetworkEntry entry;
893 entry.size = data_size + header_size;
894
895 sender->network_size_ += entry.size;
896 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_));
897 entry.done_time = cur_time + send_delay;
898 sender->network_.push_back(entry);
899
900 // Find the delay for crossing the many virtual hops of the network.
901 uint32 transit_delay = GetRandomTransitDelay();
902
903 // Post the packet as a message to be delivered (on our own thread)
904 Packet* p = new Packet(data, data_size, sender->local_addr_);
905 uint32 ts = TimeAfter(send_delay + transit_delay);
906 if (ordered) {
907 // Ensure that new packets arrive after previous ones
908 // TODO: consider ordering on a per-socket basis, since this
909 // introduces artifical delay.
910 ts = TimeMax(ts, network_delay_);
911 }
912 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p);
913 network_delay_ = TimeMax(ts, network_delay_);
914}
915
916void VirtualSocketServer::PurgeNetworkPackets(VirtualSocket* socket,
917 uint32 cur_time) {
918 while (!socket->network_.empty() &&
919 (socket->network_.front().done_time <= cur_time)) {
920 ASSERT(socket->network_size_ >= socket->network_.front().size);
921 socket->network_size_ -= socket->network_.front().size;
922 socket->network_.pop_front();
923 }
924}
925
926uint32 VirtualSocketServer::SendDelay(uint32 size) {
927 if (bandwidth_ == 0)
928 return 0;
929 else
930 return 1000 * size / bandwidth_;
931}
932
933#if 0
934void PrintFunction(std::vector<std::pair<double, double> >* f) {
935 return;
936 double sum = 0;
937 for (uint32 i = 0; i < f->size(); ++i) {
938 std::cout << (*f)[i].first << '\t' << (*f)[i].second << std::endl;
939 sum += (*f)[i].second;
940 }
941 if (!f->empty()) {
942 const double mean = sum / f->size();
943 double sum_sq_dev = 0;
944 for (uint32 i = 0; i < f->size(); ++i) {
945 double dev = (*f)[i].second - mean;
946 sum_sq_dev += dev * dev;
947 }
948 std::cout << "Mean = " << mean << " StdDev = "
949 << sqrt(sum_sq_dev / f->size()) << std::endl;
950 }
951}
952#endif // <unused>
953
954void VirtualSocketServer::UpdateDelayDistribution() {
955 Function* dist = CreateDistribution(delay_mean_, delay_stddev_,
956 delay_samples_);
957 // We take a lock just to make sure we don't leak memory.
958 {
959 CritScope cs(&delay_crit_);
960 delete delay_dist_;
961 delay_dist_ = dist;
962 }
963}
964
965static double PI = 4 * std::atan(1.0);
966
967static double Normal(double x, double mean, double stddev) {
968 double a = (x - mean) * (x - mean) / (2 * stddev * stddev);
969 return std::exp(-a) / (stddev * sqrt(2 * PI));
970}
971
972#if 0 // static unused gives a warning
973static double Pareto(double x, double min, double k) {
974 if (x < min)
975 return 0;
976 else
977 return k * std::pow(min, k) / std::pow(x, k+1);
978}
979#endif
980
981VirtualSocketServer::Function* VirtualSocketServer::CreateDistribution(
982 uint32 mean, uint32 stddev, uint32 samples) {
983 Function* f = new Function();
984
985 if (0 == stddev) {
986 f->push_back(Point(mean, 1.0));
987 } else {
988 double start = 0;
989 if (mean >= 4 * static_cast<double>(stddev))
990 start = mean - 4 * static_cast<double>(stddev);
991 double end = mean + 4 * static_cast<double>(stddev);
992
993 for (uint32 i = 0; i < samples; i++) {
994 double x = start + (end - start) * i / (samples - 1);
995 double y = Normal(x, mean, stddev);
996 f->push_back(Point(x, y));
997 }
998 }
999 return Resample(Invert(Accumulate(f)), 0, 1, samples);
1000}
1001
1002uint32 VirtualSocketServer::GetRandomTransitDelay() {
1003 size_t index = rand() % delay_dist_->size();
1004 double delay = (*delay_dist_)[index].second;
1005 //LOG_F(LS_INFO) << "random[" << index << "] = " << delay;
1006 return static_cast<uint32>(delay);
1007}
1008
1009struct FunctionDomainCmp {
1010 bool operator()(const VirtualSocketServer::Point& p1,
1011 const VirtualSocketServer::Point& p2) {
1012 return p1.first < p2.first;
1013 }
1014 bool operator()(double v1, const VirtualSocketServer::Point& p2) {
1015 return v1 < p2.first;
1016 }
1017 bool operator()(const VirtualSocketServer::Point& p1, double v2) {
1018 return p1.first < v2;
1019 }
1020};
1021
1022VirtualSocketServer::Function* VirtualSocketServer::Accumulate(Function* f) {
1023 ASSERT(f->size() >= 1);
1024 double v = 0;
1025 for (Function::size_type i = 0; i < f->size() - 1; ++i) {
1026 double dx = (*f)[i + 1].first - (*f)[i].first;
1027 double avgy = ((*f)[i + 1].second + (*f)[i].second) / 2;
1028 (*f)[i].second = v;
1029 v = v + dx * avgy;
1030 }
1031 (*f)[f->size()-1].second = v;
1032 return f;
1033}
1034
1035VirtualSocketServer::Function* VirtualSocketServer::Invert(Function* f) {
1036 for (Function::size_type i = 0; i < f->size(); ++i)
1037 std::swap((*f)[i].first, (*f)[i].second);
1038
1039 std::sort(f->begin(), f->end(), FunctionDomainCmp());
1040 return f;
1041}
1042
1043VirtualSocketServer::Function* VirtualSocketServer::Resample(
1044 Function* f, double x1, double x2, uint32 samples) {
1045 Function* g = new Function();
1046
1047 for (size_t i = 0; i < samples; i++) {
1048 double x = x1 + (x2 - x1) * i / (samples - 1);
1049 double y = Evaluate(f, x);
1050 g->push_back(Point(x, y));
1051 }
1052
1053 delete f;
1054 return g;
1055}
1056
1057double VirtualSocketServer::Evaluate(Function* f, double x) {
1058 Function::iterator iter =
1059 std::lower_bound(f->begin(), f->end(), x, FunctionDomainCmp());
1060 if (iter == f->begin()) {
1061 return (*f)[0].second;
1062 } else if (iter == f->end()) {
1063 ASSERT(f->size() >= 1);
1064 return (*f)[f->size() - 1].second;
1065 } else if (iter->first == x) {
1066 return iter->second;
1067 } else {
1068 double x1 = (iter - 1)->first;
1069 double y1 = (iter - 1)->second;
1070 double x2 = iter->first;
1071 double y2 = iter->second;
1072 return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
1073 }
1074}
1075
1076bool VirtualSocketServer::CanInteractWith(VirtualSocket* local,
1077 VirtualSocket* remote) {
1078 if (!local || !remote) {
1079 return false;
1080 }
1081 IPAddress local_ip = local->GetLocalAddress().ipaddr();
1082 IPAddress remote_ip = remote->GetLocalAddress().ipaddr();
1083 IPAddress local_normalized = local_ip.Normalized();
1084 IPAddress remote_normalized = remote_ip.Normalized();
1085 // Check if the addresses are the same family after Normalization (turns
1086 // mapped IPv6 address into IPv4 addresses).
1087 // This will stop unmapped V6 addresses from talking to mapped V6 addresses.
1088 if (local_normalized.family() == remote_normalized.family()) {
1089 return true;
1090 }
1091
1092 // If ip1 is IPv4 and ip2 is :: and ip2 is not IPV6_V6ONLY.
1093 int remote_v6_only = 0;
1094 remote->GetOption(Socket::OPT_IPV6_V6ONLY, &remote_v6_only);
1095 if (local_ip.family() == AF_INET && !remote_v6_only && IPIsAny(remote_ip)) {
1096 return true;
1097 }
1098 // Same check, backwards.
1099 int local_v6_only = 0;
1100 local->GetOption(Socket::OPT_IPV6_V6ONLY, &local_v6_only);
1101 if (remote_ip.family() == AF_INET && !local_v6_only && IPIsAny(local_ip)) {
1102 return true;
1103 }
1104
1105 // Check to see if either socket was explicitly bound to IPv6-any.
1106 // These sockets can talk with anyone.
1107 if (local_ip.family() == AF_INET6 && local->was_any()) {
1108 return true;
1109 }
1110 if (remote_ip.family() == AF_INET6 && remote->was_any()) {
1111 return true;
1112 }
1113
1114 return false;
1115}
1116
1117} // namespace talk_base