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 VirtualSocket* socket = (bindings_->end() != it) ? it->second : NULL; |
666 if (socket || addr.ipaddr() != GetDefaultRoute(addr.ipaddr().family())) { | |
667 return socket; | |
668 } | |
pthatcher1
2015/08/07 21:28:19
I think this would be more clear as:
if (it != bi
guoweis_webrtc
2015/08/13 14:17:28
Yes, it is much better. Thanks.
| |
669 | |
670 // If we can't find a binding for the packet which is sent to the interface | |
671 // corresponding to the default route, it should match a binding with the | |
672 // correct port to the any address. | |
673 SocketAddress sock_addr = | |
674 EmptySocketAddressWithFamily(addr.ipaddr().family()); | |
675 sock_addr.SetPort(addr.port()); | |
676 return LookupBinding(sock_addr); | |
665 } | 677 } |
666 | 678 |
667 int VirtualSocketServer::Unbind(const SocketAddress& addr, | 679 int VirtualSocketServer::Unbind(const SocketAddress& addr, |
668 VirtualSocket* socket) { | 680 VirtualSocket* socket) { |
669 SocketAddress normalized(addr.ipaddr().Normalized(), | 681 SocketAddress normalized(addr.ipaddr().Normalized(), |
670 addr.port()); | 682 addr.port()); |
671 ASSERT((*bindings_)[normalized] == socket); | 683 ASSERT((*bindings_)[normalized] == socket); |
672 bindings_->erase(bindings_->find(normalized)); | 684 bindings_->erase(bindings_->find(normalized)); |
673 return 0; | 685 return 0; |
674 } | 686 } |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 entry.size = data_size + header_size; | 871 entry.size = data_size + header_size; |
860 | 872 |
861 sender->network_size_ += entry.size; | 873 sender->network_size_ += entry.size; |
862 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_)); | 874 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_)); |
863 entry.done_time = cur_time + send_delay; | 875 entry.done_time = cur_time + send_delay; |
864 sender->network_.push_back(entry); | 876 sender->network_.push_back(entry); |
865 | 877 |
866 // Find the delay for crossing the many virtual hops of the network. | 878 // Find the delay for crossing the many virtual hops of the network. |
867 uint32 transit_delay = GetRandomTransitDelay(); | 879 uint32 transit_delay = GetRandomTransitDelay(); |
868 | 880 |
881 // When the incoming packet is from a binding of the any address, translate it | |
882 // to the default route here such that the STUN server will see the default | |
883 // route. | |
884 SocketAddress addr = sender->local_addr_; | |
pthatcher1
2015/08/07 21:28:19
I think "sender_addr" would be more clear than jus
guoweis_webrtc
2015/08/13 14:17:28
Done.
| |
885 IPAddress default_ip = GetDefaultRoute(addr.ipaddr().family()); | |
886 if (addr.IsAnyIP() && !IPIsUnspec(default_ip)) { | |
887 addr.SetIP(default_ip); | |
pthatcher1
2015/08/07 21:28:19
Since default_ip isn't used outside this scope, wh
guoweis_webrtc
2015/08/13 14:17:28
I still want to check whether IPIsUnspec. Yes, Whe
| |
888 } | |
889 | |
869 // Post the packet as a message to be delivered (on our own thread) | 890 // Post the packet as a message to be delivered (on our own thread) |
870 Packet* p = new Packet(data, data_size, sender->local_addr_); | 891 Packet* p = new Packet(data, data_size, addr); |
892 | |
871 uint32 ts = TimeAfter(send_delay + transit_delay); | 893 uint32 ts = TimeAfter(send_delay + transit_delay); |
872 if (ordered) { | 894 if (ordered) { |
873 // Ensure that new packets arrive after previous ones | 895 // Ensure that new packets arrive after previous ones |
874 // TODO: consider ordering on a per-socket basis, since this | 896 // TODO: consider ordering on a per-socket basis, since this |
875 // introduces artifical delay. | 897 // introduces artifical delay. |
876 ts = TimeMax(ts, network_delay_); | 898 ts = TimeMax(ts, network_delay_); |
877 } | 899 } |
878 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); | 900 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); |
879 network_delay_ = TimeMax(ts, network_delay_); | 901 network_delay_ = TimeMax(ts, network_delay_); |
880 } | 902 } |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1073 if (local_ip.family() == AF_INET6 && local->was_any()) { | 1095 if (local_ip.family() == AF_INET6 && local->was_any()) { |
1074 return true; | 1096 return true; |
1075 } | 1097 } |
1076 if (remote_ip.family() == AF_INET6 && remote->was_any()) { | 1098 if (remote_ip.family() == AF_INET6 && remote->was_any()) { |
1077 return true; | 1099 return true; |
1078 } | 1100 } |
1079 | 1101 |
1080 return false; | 1102 return false; |
1081 } | 1103 } |
1082 | 1104 |
1105 IPAddress VirtualSocketServer::GetDefaultRoute(int family) { | |
1106 if (family == AF_INET) { | |
1107 return default_route_v4_; | |
1108 } | |
1109 if (family == AF_INET6) { | |
1110 return default_route_v6_; | |
1111 } | |
1112 return IPAddress(); | |
1113 } | |
1114 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) { | |
1115 DCHECK(!IPIsAny(from_addr)); | |
1116 if (from_addr.family() == AF_INET) { | |
1117 default_route_v4_ = from_addr; | |
1118 } else if (from_addr.family() == AF_INET6) { | |
1119 default_route_v6_ = from_addr; | |
1120 } | |
1121 } | |
1122 | |
1083 } // namespace rtc | 1123 } // namespace rtc |
OLD | NEW |