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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 #if defined(WEBRTC_WIN) | 25 #if defined(WEBRTC_WIN) |
26 #include "webrtc/base/win32.h" | 26 #include "webrtc/base/win32.h" |
27 #include <Iphlpapi.h> | 27 #include <Iphlpapi.h> |
28 #elif !defined(__native_client__) | 28 #elif !defined(__native_client__) |
29 #include "webrtc/base/ifaddrs_converter.h" | 29 #include "webrtc/base/ifaddrs_converter.h" |
30 #endif | 30 #endif |
31 | 31 |
32 #include <stdio.h> | 32 #include <stdio.h> |
33 | 33 |
34 #include <algorithm> | 34 #include <algorithm> |
| 35 #include <memory> |
35 | 36 |
36 #include "webrtc/base/logging.h" | 37 #include "webrtc/base/logging.h" |
37 #include "webrtc/base/networkmonitor.h" | 38 #include "webrtc/base/networkmonitor.h" |
38 #include "webrtc/base/scoped_ptr.h" | |
39 #include "webrtc/base/socket.h" // includes something that makes windows happy | 39 #include "webrtc/base/socket.h" // includes something that makes windows happy |
40 #include "webrtc/base/stream.h" | 40 #include "webrtc/base/stream.h" |
41 #include "webrtc/base/stringencode.h" | 41 #include "webrtc/base/stringencode.h" |
42 #include "webrtc/base/thread.h" | 42 #include "webrtc/base/thread.h" |
43 | 43 |
44 namespace rtc { | 44 namespace rtc { |
45 namespace { | 45 namespace { |
46 | 46 |
47 // Turning on IPv6 could make many IPv6 interfaces available for connectivity | 47 // Turning on IPv6 could make many IPv6 interfaces available for connectivity |
48 // check and delay the call setup time. kMaxIPv6Networks is the default upper | 48 // check and delay the call setup time. kMaxIPv6Networks is the default upper |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } else if (network_monitor_) { | 464 } else if (network_monitor_) { |
465 adapter_type = network_monitor_->GetAdapterType(cursor->ifa_name); | 465 adapter_type = network_monitor_->GetAdapterType(cursor->ifa_name); |
466 } | 466 } |
467 #if defined(WEBRTC_IOS) | 467 #if defined(WEBRTC_IOS) |
468 // Cell networks are pdp_ipN on iOS. | 468 // Cell networks are pdp_ipN on iOS. |
469 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { | 469 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { |
470 adapter_type = ADAPTER_TYPE_CELLULAR; | 470 adapter_type = ADAPTER_TYPE_CELLULAR; |
471 } | 471 } |
472 #endif | 472 #endif |
473 // TODO(phoglund): Need to recognize other types as well. | 473 // TODO(phoglund): Need to recognize other types as well. |
474 scoped_ptr<Network> network(new Network(cursor->ifa_name, | 474 std::unique_ptr<Network> network( |
475 cursor->ifa_name, prefix, | 475 new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length, |
476 prefix_length, adapter_type)); | 476 adapter_type)); |
477 network->set_default_local_address_provider(this); | 477 network->set_default_local_address_provider(this); |
478 network->set_scope_id(scope_id); | 478 network->set_scope_id(scope_id); |
479 network->AddIP(ip); | 479 network->AddIP(ip); |
480 network->set_ignored(IsIgnoredNetwork(*network)); | 480 network->set_ignored(IsIgnoredNetwork(*network)); |
481 if (include_ignored || !network->ignored()) { | 481 if (include_ignored || !network->ignored()) { |
482 current_networks[key] = network.get(); | 482 current_networks[key] = network.get(); |
483 networks->push_back(network.release()); | 483 networks->push_back(network.release()); |
484 } | 484 } |
485 } else { | 485 } else { |
486 (*existing_network).second->AddIP(ip); | 486 (*existing_network).second->AddIP(ip); |
487 } | 487 } |
488 } | 488 } |
489 } | 489 } |
490 | 490 |
491 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 491 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
492 NetworkList* networks) const { | 492 NetworkList* networks) const { |
493 struct ifaddrs* interfaces; | 493 struct ifaddrs* interfaces; |
494 int error = getifaddrs(&interfaces); | 494 int error = getifaddrs(&interfaces); |
495 if (error != 0) { | 495 if (error != 0) { |
496 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; | 496 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; |
497 return false; | 497 return false; |
498 } | 498 } |
499 | 499 |
500 rtc::scoped_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter()); | 500 std::unique_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter()); |
501 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored, | 501 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored, |
502 networks); | 502 networks); |
503 | 503 |
504 freeifaddrs(interfaces); | 504 freeifaddrs(interfaces); |
505 return true; | 505 return true; |
506 } | 506 } |
507 | 507 |
508 #elif defined(WEBRTC_WIN) | 508 #elif defined(WEBRTC_WIN) |
509 | 509 |
510 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, | 510 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
546 } | 546 } |
547 *prefix = best_prefix; | 547 *prefix = best_prefix; |
548 return best_length; | 548 return best_length; |
549 } | 549 } |
550 | 550 |
551 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 551 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
552 NetworkList* networks) const { | 552 NetworkList* networks) const { |
553 NetworkMap current_networks; | 553 NetworkMap current_networks; |
554 // MSDN recommends a 15KB buffer for the first try at GetAdaptersAddresses. | 554 // MSDN recommends a 15KB buffer for the first try at GetAdaptersAddresses. |
555 size_t buffer_size = 16384; | 555 size_t buffer_size = 16384; |
556 scoped_ptr<char[]> adapter_info(new char[buffer_size]); | 556 std::unique_ptr<char[]> adapter_info(new char[buffer_size]); |
557 PIP_ADAPTER_ADDRESSES adapter_addrs = | 557 PIP_ADAPTER_ADDRESSES adapter_addrs = |
558 reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get()); | 558 reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get()); |
559 int adapter_flags = (GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST | | 559 int adapter_flags = (GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST | |
560 GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_INCLUDE_PREFIX); | 560 GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_INCLUDE_PREFIX); |
561 int ret = 0; | 561 int ret = 0; |
562 do { | 562 do { |
563 adapter_info.reset(new char[buffer_size]); | 563 adapter_info.reset(new char[buffer_size]); |
564 adapter_addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get()); | 564 adapter_addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get()); |
565 ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags, | 565 ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags, |
566 0, adapter_addrs, | 566 0, adapter_addrs, |
(...skipping 15 matching lines...) Expand all Loading... |
582 #endif | 582 #endif |
583 description = ToUtf8(adapter_addrs->Description, | 583 description = ToUtf8(adapter_addrs->Description, |
584 wcslen(adapter_addrs->Description)); | 584 wcslen(adapter_addrs->Description)); |
585 for (; address; address = address->Next) { | 585 for (; address; address = address->Next) { |
586 #if defined(NDEBUG) | 586 #if defined(NDEBUG) |
587 name = rtc::ToString(count); | 587 name = rtc::ToString(count); |
588 #endif | 588 #endif |
589 | 589 |
590 IPAddress ip; | 590 IPAddress ip; |
591 int scope_id = 0; | 591 int scope_id = 0; |
592 scoped_ptr<Network> network; | 592 std::unique_ptr<Network> network; |
593 switch (address->Address.lpSockaddr->sa_family) { | 593 switch (address->Address.lpSockaddr->sa_family) { |
594 case AF_INET: { | 594 case AF_INET: { |
595 sockaddr_in* v4_addr = | 595 sockaddr_in* v4_addr = |
596 reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr); | 596 reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr); |
597 ip = IPAddress(v4_addr->sin_addr); | 597 ip = IPAddress(v4_addr->sin_addr); |
598 break; | 598 break; |
599 } | 599 } |
600 case AF_INET6: { | 600 case AF_INET6: { |
601 if (ipv6_enabled()) { | 601 if (ipv6_enabled()) { |
602 sockaddr_in6* v6_addr = | 602 sockaddr_in6* v6_addr = |
(...skipping 18 matching lines...) Expand all Loading... |
621 IPAddress prefix; | 621 IPAddress prefix; |
622 int prefix_length = GetPrefix(prefixlist, ip, &prefix); | 622 int prefix_length = GetPrefix(prefixlist, ip, &prefix); |
623 std::string key = MakeNetworkKey(name, prefix, prefix_length); | 623 std::string key = MakeNetworkKey(name, prefix, prefix_length); |
624 auto existing_network = current_networks.find(key); | 624 auto existing_network = current_networks.find(key); |
625 if (existing_network == current_networks.end()) { | 625 if (existing_network == current_networks.end()) { |
626 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; | 626 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
627 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | 627 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { |
628 // TODO(phoglund): Need to recognize other types as well. | 628 // TODO(phoglund): Need to recognize other types as well. |
629 adapter_type = ADAPTER_TYPE_LOOPBACK; | 629 adapter_type = ADAPTER_TYPE_LOOPBACK; |
630 } | 630 } |
631 scoped_ptr<Network> network(new Network(name, description, prefix, | 631 std::unique_ptr<Network> network(new Network( |
632 prefix_length, adapter_type)); | 632 name, description, prefix, prefix_length, adapter_type)); |
633 network->set_default_local_address_provider(this); | 633 network->set_default_local_address_provider(this); |
634 network->set_scope_id(scope_id); | 634 network->set_scope_id(scope_id); |
635 network->AddIP(ip); | 635 network->AddIP(ip); |
636 bool ignored = IsIgnoredNetwork(*network); | 636 bool ignored = IsIgnoredNetwork(*network); |
637 network->set_ignored(ignored); | 637 network->set_ignored(ignored); |
638 if (include_ignored || !network->ignored()) { | 638 if (include_ignored || !network->ignored()) { |
639 current_networks[key] = network.get(); | 639 current_networks[key] = network.get(); |
640 networks->push_back(network.release()); | 640 networks->push_back(network.release()); |
641 } | 641 } |
642 } else { | 642 } else { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 default: | 785 default: |
786 ASSERT(false); | 786 ASSERT(false); |
787 } | 787 } |
788 } | 788 } |
789 | 789 |
790 IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const { | 790 IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const { |
791 ASSERT(thread_ == Thread::Current()); | 791 ASSERT(thread_ == Thread::Current()); |
792 ASSERT(thread_->socketserver() != nullptr); | 792 ASSERT(thread_->socketserver() != nullptr); |
793 ASSERT(family == AF_INET || family == AF_INET6); | 793 ASSERT(family == AF_INET || family == AF_INET6); |
794 | 794 |
795 scoped_ptr<AsyncSocket> socket( | 795 std::unique_ptr<AsyncSocket> socket( |
796 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); | 796 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); |
797 if (!socket) { | 797 if (!socket) { |
798 LOG_ERR(LERROR) << "Socket creation failed"; | 798 LOG_ERR(LERROR) << "Socket creation failed"; |
799 return IPAddress(); | 799 return IPAddress(); |
800 } | 800 } |
801 | 801 |
802 if (socket->Connect(SocketAddress( | 802 if (socket->Connect(SocketAddress( |
803 family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort)) < | 803 family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort)) < |
804 0) { | 804 0) { |
805 LOG(LS_INFO) << "Connect failed with " << socket->GetError(); | 805 LOG(LS_INFO) << "Connect failed with " << socket->GetError(); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
938 std::stringstream ss; | 938 std::stringstream ss; |
939 // Print out the first space-terminated token of the network desc, plus | 939 // Print out the first space-terminated token of the network desc, plus |
940 // the IP address. | 940 // the IP address. |
941 ss << "Net[" << description_.substr(0, description_.find(' ')) | 941 ss << "Net[" << description_.substr(0, description_.find(' ')) |
942 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 942 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
943 << ":" << AdapterTypeToString(type_) << "]"; | 943 << ":" << AdapterTypeToString(type_) << "]"; |
944 return ss.str(); | 944 return ss.str(); |
945 } | 945 } |
946 | 946 |
947 } // namespace rtc | 947 } // namespace rtc |
OLD | NEW |