Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: webrtc/base/virtualsocketserver.cc

Issue 1274013002: Bug 4865: Enable connectivity when the remote peer is on public internet. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 if (addr.ipaddr() == GetDefaultRoute(addr.ipaddr().family())) {
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);
juberti1 2015/08/13 21:23:59 Is there any chance this could infinitely recurse
guoweis_webrtc 2015/08/14 05:24:25 If the addr is Unspec, yes, it'll be a infinite lo
677 }
678
679 return nullptr;
665 } 680 }
666 681
667 int VirtualSocketServer::Unbind(const SocketAddress& addr, 682 int VirtualSocketServer::Unbind(const SocketAddress& addr,
668 VirtualSocket* socket) { 683 VirtualSocket* socket) {
669 SocketAddress normalized(addr.ipaddr().Normalized(), 684 SocketAddress normalized(addr.ipaddr().Normalized(),
670 addr.port()); 685 addr.port());
671 ASSERT((*bindings_)[normalized] == socket); 686 ASSERT((*bindings_)[normalized] == socket);
672 bindings_->erase(bindings_->find(normalized)); 687 bindings_->erase(bindings_->find(normalized));
673 return 0; 688 return 0;
674 } 689 }
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 entry.size = data_size + header_size; 874 entry.size = data_size + header_size;
860 875
861 sender->network_size_ += entry.size; 876 sender->network_size_ += entry.size;
862 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_)); 877 uint32 send_delay = SendDelay(static_cast<uint32>(sender->network_size_));
863 entry.done_time = cur_time + send_delay; 878 entry.done_time = cur_time + send_delay;
864 sender->network_.push_back(entry); 879 sender->network_.push_back(entry);
865 880
866 // Find the delay for crossing the many virtual hops of the network. 881 // Find the delay for crossing the many virtual hops of the network.
867 uint32 transit_delay = GetRandomTransitDelay(); 882 uint32 transit_delay = GetRandomTransitDelay();
868 883
884 // When the incoming packet is from a binding of the any address, translate it
885 // to the default route here such that the STUN server will see the default
juberti1 2015/08/13 21:23:59 "STUN server" -> "recipient"
guoweis_webrtc 2015/08/14 05:24:25 Done.
886 // route.
887 SocketAddress sender_addr = sender->local_addr_;
888 IPAddress default_ip = GetDefaultRoute(sender_addr.ipaddr().family());
889 if (sender_addr.IsAnyIP() && !IPIsUnspec(default_ip)) {
890 sender_addr.SetIP(default_ip);
891 }
892
869 // Post the packet as a message to be delivered (on our own thread) 893 // Post the packet as a message to be delivered (on our own thread)
870 Packet* p = new Packet(data, data_size, sender->local_addr_); 894 Packet* p = new Packet(data, data_size, sender_addr);
895
871 uint32 ts = TimeAfter(send_delay + transit_delay); 896 uint32 ts = TimeAfter(send_delay + transit_delay);
872 if (ordered) { 897 if (ordered) {
873 // Ensure that new packets arrive after previous ones 898 // Ensure that new packets arrive after previous ones
874 // TODO: consider ordering on a per-socket basis, since this 899 // TODO: consider ordering on a per-socket basis, since this
875 // introduces artifical delay. 900 // introduces artifical delay.
876 ts = TimeMax(ts, network_delay_); 901 ts = TimeMax(ts, network_delay_);
877 } 902 }
878 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p); 903 msg_queue_->PostAt(ts, recipient, MSG_ID_PACKET, p);
879 network_delay_ = TimeMax(ts, network_delay_); 904 network_delay_ = TimeMax(ts, network_delay_);
880 } 905 }
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1073 if (local_ip.family() == AF_INET6 && local->was_any()) { 1098 if (local_ip.family() == AF_INET6 && local->was_any()) {
1074 return true; 1099 return true;
1075 } 1100 }
1076 if (remote_ip.family() == AF_INET6 && remote->was_any()) { 1101 if (remote_ip.family() == AF_INET6 && remote->was_any()) {
1077 return true; 1102 return true;
1078 } 1103 }
1079 1104
1080 return false; 1105 return false;
1081 } 1106 }
1082 1107
1108 IPAddress VirtualSocketServer::GetDefaultRoute(int family) {
1109 if (family == AF_INET) {
1110 return default_route_v4_;
1111 }
1112 if (family == AF_INET6) {
1113 return default_route_v6_;
1114 }
1115 return IPAddress();
1116 }
1117 void VirtualSocketServer::SetDefaultRoute(const IPAddress& from_addr) {
1118 DCHECK(!IPIsAny(from_addr));
1119 if (from_addr.family() == AF_INET) {
1120 default_route_v4_ = from_addr;
1121 } else if (from_addr.family() == AF_INET6) {
1122 default_route_v6_ = from_addr;
1123 }
1124 }
1125
1083 } // namespace rtc 1126 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698