| 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 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 net->set_id(next_available_network_id_++); | 285 net->set_id(next_available_network_id_++); |
| 286 // Also, we might have accumulated IPAddresses from the first | 286 // Also, we might have accumulated IPAddresses from the first |
| 287 // step, set it here. | 287 // step, set it here. |
| 288 net->SetIPs(kv.second.ips, true); | 288 net->SetIPs(kv.second.ips, true); |
| 289 *changed = true; | 289 *changed = true; |
| 290 } else { | 290 } else { |
| 291 // This network exists in the map already. Reset its IP addresses. | 291 // This network exists in the map already. Reset its IP addresses. |
| 292 Network* existing_net = existing->second; | 292 Network* existing_net = existing->second; |
| 293 *changed = existing_net->SetIPs(kv.second.ips, *changed); | 293 *changed = existing_net->SetIPs(kv.second.ips, *changed); |
| 294 merged_list.push_back(existing_net); | 294 merged_list.push_back(existing_net); |
| 295 if (net->type() != ADAPTER_TYPE_UNKNOWN && |
| 296 net->type() != existing_net->type()) { |
| 297 existing_net->set_type(net->type()); |
| 298 *changed = true; |
| 299 } |
| 295 // If the existing network was not active, networks have changed. | 300 // If the existing network was not active, networks have changed. |
| 296 if (!existing_net->active()) { | 301 if (!existing_net->active()) { |
| 297 *changed = true; | 302 *changed = true; |
| 298 } | 303 } |
| 299 ASSERT(net->active()); | 304 ASSERT(net->active()); |
| 300 if (existing_net != net) { | 305 if (existing_net != net) { |
| 301 delete net; | 306 delete net; |
| 302 } | 307 } |
| 303 } | 308 } |
| 304 } | 309 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 392 |
| 388 BasicNetworkManager::BasicNetworkManager() | 393 BasicNetworkManager::BasicNetworkManager() |
| 389 : thread_(NULL), sent_first_update_(false), start_count_(0), | 394 : thread_(NULL), sent_first_update_(false), start_count_(0), |
| 390 ignore_non_default_routes_(false) { | 395 ignore_non_default_routes_(false) { |
| 391 } | 396 } |
| 392 | 397 |
| 393 BasicNetworkManager::~BasicNetworkManager() { | 398 BasicNetworkManager::~BasicNetworkManager() { |
| 394 } | 399 } |
| 395 | 400 |
| 396 void BasicNetworkManager::OnNetworksChanged() { | 401 void BasicNetworkManager::OnNetworksChanged() { |
| 397 LOG(LS_VERBOSE) << "Network change was observed at the network manager"; | 402 LOG(LS_INFO) << "Network change was observed"; |
| 398 UpdateNetworksOnce(); | 403 UpdateNetworksOnce(); |
| 399 } | 404 } |
| 400 | 405 |
| 401 #if defined(__native_client__) | 406 #if defined(__native_client__) |
| 402 | 407 |
| 403 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 408 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 404 NetworkList* networks) const { | 409 NetworkList* networks) const { |
| 405 ASSERT(false); | 410 ASSERT(false); |
| 406 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; | 411 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; |
| 407 return false; | 412 return false; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 450 |
| 446 // Special case for IPv6 address. | 451 // Special case for IPv6 address. |
| 447 if (cursor->ifa_addr->sa_family == AF_INET6) { | 452 if (cursor->ifa_addr->sa_family == AF_INET6) { |
| 448 if (IsIgnoredIPv6(ip)) { | 453 if (IsIgnoredIPv6(ip)) { |
| 449 continue; | 454 continue; |
| 450 } | 455 } |
| 451 scope_id = | 456 scope_id = |
| 452 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id; | 457 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id; |
| 453 } | 458 } |
| 454 | 459 |
| 460 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
| 461 if (cursor->ifa_flags & IFF_LOOPBACK) { |
| 462 adapter_type = ADAPTER_TYPE_LOOPBACK; |
| 463 } else { |
| 464 adapter_type = GetAdapterTypeFromName(cursor->ifa_name); |
| 465 } |
| 455 int prefix_length = CountIPMaskBits(mask); | 466 int prefix_length = CountIPMaskBits(mask); |
| 456 prefix = TruncateIP(ip, prefix_length); | 467 prefix = TruncateIP(ip, prefix_length); |
| 457 std::string key = MakeNetworkKey(std::string(cursor->ifa_name), | 468 std::string key = MakeNetworkKey(std::string(cursor->ifa_name), |
| 458 prefix, prefix_length); | 469 prefix, prefix_length); |
| 459 auto existing_network = current_networks.find(key); | 470 auto iter = current_networks.find(key); |
| 460 if (existing_network == current_networks.end()) { | 471 if (iter == current_networks.end()) { |
| 461 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; | |
| 462 if (cursor->ifa_flags & IFF_LOOPBACK) { | |
| 463 adapter_type = ADAPTER_TYPE_LOOPBACK; | |
| 464 } else if (network_monitor_) { | |
| 465 adapter_type = network_monitor_->GetAdapterType(cursor->ifa_name); | |
| 466 } | |
| 467 #if defined(WEBRTC_IOS) | |
| 468 // Cell networks are pdp_ipN on iOS. | |
| 469 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { | |
| 470 adapter_type = ADAPTER_TYPE_CELLULAR; | |
| 471 } | |
| 472 #endif | |
| 473 // TODO(phoglund): Need to recognize other types as well. | 472 // TODO(phoglund): Need to recognize other types as well. |
| 474 std::unique_ptr<Network> network( | 473 std::unique_ptr<Network> network( |
| 475 new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length, | 474 new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length, |
| 476 adapter_type)); | 475 adapter_type)); |
| 477 network->set_default_local_address_provider(this); | 476 network->set_default_local_address_provider(this); |
| 478 network->set_scope_id(scope_id); | 477 network->set_scope_id(scope_id); |
| 479 network->AddIP(ip); | 478 network->AddIP(ip); |
| 480 network->set_ignored(IsIgnoredNetwork(*network)); | 479 network->set_ignored(IsIgnoredNetwork(*network)); |
| 481 if (include_ignored || !network->ignored()) { | 480 if (include_ignored || !network->ignored()) { |
| 482 current_networks[key] = network.get(); | 481 current_networks[key] = network.get(); |
| 483 networks->push_back(network.release()); | 482 networks->push_back(network.release()); |
| 484 } | 483 } |
| 485 } else { | 484 } else { |
| 486 (*existing_network).second->AddIP(ip); | 485 Network* existing_network = iter->second; |
| 486 existing_network->AddIP(ip); |
| 487 if (adapter_type != ADAPTER_TYPE_UNKNOWN) { |
| 488 existing_network->set_type(adapter_type); |
| 489 } |
| 487 } | 490 } |
| 488 } | 491 } |
| 489 } | 492 } |
| 490 | 493 |
| 491 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 494 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 492 NetworkList* networks) const { | 495 NetworkList* networks) const { |
| 493 struct ifaddrs* interfaces; | 496 struct ifaddrs* interfaces; |
| 494 int error = getifaddrs(&interfaces); | 497 int error = getifaddrs(&interfaces); |
| 495 if (error != 0) { | 498 if (error != 0) { |
| 496 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; | 499 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 780 } | 783 } |
| 781 case kSignalNetworksMessage: { | 784 case kSignalNetworksMessage: { |
| 782 SignalNetworksChanged(); | 785 SignalNetworksChanged(); |
| 783 break; | 786 break; |
| 784 } | 787 } |
| 785 default: | 788 default: |
| 786 ASSERT(false); | 789 ASSERT(false); |
| 787 } | 790 } |
| 788 } | 791 } |
| 789 | 792 |
| 793 AdapterType BasicNetworkManager::GetAdapterTypeFromName( |
| 794 const char* network_name) const { |
| 795 // If there is a network_monitor, use it to get the adapter type. |
| 796 // Otherwise, get the adapter type based on a few name matching rules. |
| 797 if (network_monitor_) { |
| 798 AdapterType type = network_monitor_->GetAdapterType(network_name); |
| 799 if (type != ADAPTER_TYPE_UNKNOWN) { |
| 800 return type; |
| 801 } |
| 802 } |
| 803 #if defined(WEBRTC_IOS) |
| 804 // Cell networks are pdp_ipN on iOS. |
| 805 if (strncmp(network_name, "pdp_ip", 6) == 0) { |
| 806 return ADAPTER_TYPE_CELLULAR; |
| 807 } |
| 808 #elif defined(WEBRTC_ANDROID) |
| 809 if (strncmp(network_name, "rmnet", 5) == 0 || |
| 810 strncmp(network_name, "v4-rmnet", 8) == 0) { |
| 811 return ADAPTER_TYPE_CELLULAR; |
| 812 } |
| 813 if (strncmp(network_name, "wlan", 4) == 0) { |
| 814 return ADAPTER_TYPE_WIFI; |
| 815 } |
| 816 #endif |
| 817 |
| 818 return ADAPTER_TYPE_UNKNOWN; |
| 819 } |
| 820 |
| 790 IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const { | 821 IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const { |
| 791 ASSERT(thread_ == Thread::Current()); | 822 ASSERT(thread_ == Thread::Current()); |
| 792 ASSERT(thread_->socketserver() != nullptr); | 823 ASSERT(thread_->socketserver() != nullptr); |
| 793 ASSERT(family == AF_INET || family == AF_INET6); | 824 ASSERT(family == AF_INET || family == AF_INET6); |
| 794 | 825 |
| 795 std::unique_ptr<AsyncSocket> socket( | 826 std::unique_ptr<AsyncSocket> socket( |
| 796 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); | 827 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); |
| 797 if (!socket) { | 828 if (!socket) { |
| 798 LOG_ERR(LERROR) << "Socket creation failed"; | 829 LOG_ERR(LERROR) << "Socket creation failed"; |
| 799 return IPAddress(); | 830 return IPAddress(); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 938 std::stringstream ss; | 969 std::stringstream ss; |
| 939 // Print out the first space-terminated token of the network desc, plus | 970 // Print out the first space-terminated token of the network desc, plus |
| 940 // the IP address. | 971 // the IP address. |
| 941 ss << "Net[" << description_.substr(0, description_.find(' ')) | 972 ss << "Net[" << description_.substr(0, description_.find(' ')) |
| 942 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 973 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
| 943 << ":" << AdapterTypeToString(type_) << "]"; | 974 << ":" << AdapterTypeToString(type_) << "]"; |
| 944 return ss.str(); | 975 return ss.str(); |
| 945 } | 976 } |
| 946 | 977 |
| 947 } // namespace rtc | 978 } // namespace rtc |
| OLD | NEW |