| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/virtualsocketserver.h" | 11 #include "webrtc/base/virtualsocketserver.h" |
| 12 | 12 |
| 13 #include <errno.h> | 13 #include <errno.h> |
| 14 #include <math.h> | 14 #include <math.h> |
| 15 | 15 |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 #include <map> | 17 #include <map> |
| 18 #include <vector> | 18 #include <vector> |
| 19 | 19 |
| 20 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/common.h" | 21 #include "webrtc/base/common.h" |
| 21 #include "webrtc/base/logging.h" | 22 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/physicalsocketserver.h" | 23 #include "webrtc/base/physicalsocketserver.h" |
| 23 #include "webrtc/base/socketaddresspair.h" | 24 #include "webrtc/base/socketaddresspair.h" |
| 24 #include "webrtc/base/thread.h" | 25 #include "webrtc/base/thread.h" |
| 25 #include "webrtc/base/timeutils.h" | 26 #include "webrtc/base/timeutils.h" |
| 26 | 27 |
| 27 namespace rtc { | 28 namespace rtc { |
| 28 #if defined(WEBRTC_WIN) | 29 #if defined(WEBRTC_WIN) |
| 29 const in_addr kInitialNextIPv4 = { {0x01, 0, 0, 0} }; | 30 const in_addr kInitialNextIPv4 = { {0x01, 0, 0, 0} }; |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 } | 655 } |
| 655 } | 656 } |
| 656 | 657 |
| 657 return Bind(socket, *addr); | 658 return Bind(socket, *addr); |
| 658 } | 659 } |
| 659 | 660 |
| 660 VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { | 661 VirtualSocket* VirtualSocketServer::LookupBinding(const SocketAddress& addr) { |
| 661 SocketAddress normalized(addr.ipaddr().Normalized(), | 662 SocketAddress normalized(addr.ipaddr().Normalized(), |
| 662 addr.port()); | 663 addr.port()); |
| 663 AddressMap::iterator it = bindings_->find(normalized); | 664 AddressMap::iterator it = bindings_->find(normalized); |
| 664 return (bindings_->end() != it) ? it->second : NULL; | 665 if (it != bindings_->end()) { |
| 666 return it->second; |
| 667 } |
| 668 |
| 669 IPAddress default_ip = GetDefaultRoute(addr.ipaddr().family()); |
| 670 if (!IPIsUnspec(default_ip) && addr.ipaddr() == default_ip) { |
| 671 // If we can't find a binding for the packet which is sent to the interface |
| 672 // corresponding to the default route, it should match a binding with the |
| 673 // correct port to the any address. |
| 674 SocketAddress sock_addr = |
| 675 EmptySocketAddressWithFamily(addr.ipaddr().family()); |
| 676 sock_addr.SetPort(addr.port()); |
| 677 return LookupBinding(sock_addr); |
| 678 } |
| 679 |
| 680 return nullptr; |
| 665 } | 681 } |
| 666 | 682 |
| 667 int VirtualSocketServer::Unbind(const SocketAddress& addr, | 683 int VirtualSocketServer::Unbind(const SocketAddress& addr, |
| 668 VirtualSocket* socket) { | 684 VirtualSocket* socket) { |
| 669 SocketAddress normalized(addr.ipaddr().Normalized(), | 685 SocketAddress normalized(addr.ipaddr().Normalized(), |
| 670 addr.port()); | 686 addr.port()); |
| 671 ASSERT((*bindings_)[normalized] == socket); | 687 ASSERT((*bindings_)[normalized] == socket); |
| 672 bindings_->erase(bindings_->find(normalized)); | 688 bindings_->erase(bindings_->find(normalized)); |
| 673 return 0; | 689 return 0; |
| 674 } | 690 } |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 entry.size = data_size + header_size; | 875 entry.size = data_size + header_size; |
| 860 | 876 |
| 861 sender->network_size_ += entry.size; | 877 sender->network_size_ += entry.size; |
| 862 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_)); | 878 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_)); |
| 863 entry.done_time = cur_time + send_delay; | 879 entry.done_time = cur_time + send_delay; |
| 864 sender->network_.push_back(entry); | 880 sender->network_.push_back(entry); |
| 865 | 881 |
| 866 // Find the delay for crossing the many virtual hops of the network. | 882 // Find the delay for crossing the many virtual hops of the network. |
| 867 uint32 transit_delay = GetRandomTransitDelay(); | 883 uint32 transit_delay = GetRandomTransitDelay(); |
| 868 | 884 |
| 885 // When the incoming packet is from a binding of the any address, translate it |
| 886 // to the default route here such that the recipient will see the default |
| 887 // route. |
| 888 SocketAddress sender_addr = sender->local_addr_; |
| 889 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family()); |
| 890 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) { |
| 891 sender_addr.SetIP(default_ip); |
| 892 } |
| 893 |
| 869 // Post the packet as a message to be delivered (on our own thread) | 894 // Post the packet as a message to be delivered (on our own thread) |
| 870 Packet* p = new Packet(data, data_size, sender->local_addr_); | 895 Packet* p = new Packet(data, data_size, sender_addr); |
| 896 |
| 871 uint32 ts = TimeAfter(send_delay + transit_delay); | 897 uint32 ts = TimeAfter(send_delay + transit_delay); |
| 872 if (ordered) { | 898 if (ordered) { |
| 873 // Ensure that new packets arrive after previous ones | 899 // Ensure that new packets arrive after previous ones |
| 874 // TODO: consider ordering on a per-socket basis, since this | 900 // TODO: consider ordering on a per-socket basis, since this |
| 875 // introduces artifical delay. | 901 // introduces artifical delay. |
| 876 ts = TimeMax(ts, network_delay_); | 902 ts = TimeMax(ts, network_delay_); |
| 877 } | 903 } |
| 878 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); | 904 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); |
| 879 network_delay_ = TimeMax(ts, network_delay_); | 905 network_delay_ = TimeMax(ts, network_delay_); |
| 880 } | 906 } |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1073 if (local_ip.family() == AF_INET6 && local->was_any()) { | 1099 if (local_ip.family() == AF_INET6 && local->was_any()) { |
| 1074 return true; | 1100 return true; |
| 1075 } | 1101 } |
| 1076 if (remote_ip.family() == AF_INET6 && remote->was_any()) { | 1102 if (remote_ip.family() == AF_INET6 && remote->was_any()) { |
| 1077 return true; | 1103 return true; |
| 1078 } | 1104 } |
| 1079 | 1105 |
| 1080 return false; | 1106 return false; |
| 1081 } | 1107 } |
| 1082 | 1108 |
| 1109 IPAddress VirtualSocketServer::GetDefaultRoute(int family) { |
| 1110 if (family == AF_INET) { |
| 1111 return default_route_v4_; |
| 1112 } |
| 1113 if (family == AF_INET6) { |
| 1114 return default_route_v6_; |
| 1115 } |
| 1116 return IPAddress(); |
| 1117 } |
| 1118 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { |
| 1119 DCHECK(!IPIsAny(from_addr)); |
| 1120 if (from_addr.family() == AF_INET) { |
| 1121 default_route_v4_ = from_addr; |
| 1122 } else if (from_addr.family() == AF_INET6) { |
| 1123 default_route_v6_ = from_addr; |
| 1124 } |
| 1125 } |
| 1126 |
| 1083 } // namespace rtc | 1127 } // namespace rtc |
| OLD | NEW |