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 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 case ADAPTER_TYPE_VPN: | 122 case ADAPTER_TYPE_VPN: |
123 return "VPN"; | 123 return "VPN"; |
124 case ADAPTER_TYPE_LOOPBACK: | 124 case ADAPTER_TYPE_LOOPBACK: |
125 return "Loopback"; | 125 return "Loopback"; |
126 default: | 126 default: |
127 RTC_DCHECK(false) << "Invalid type " << type; | 127 RTC_DCHECK(false) << "Invalid type " << type; |
128 return std::string(); | 128 return std::string(); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
| 132 AdapterType FromNetworkType(NetworkType network_type) { |
| 133 switch (network_type) { |
| 134 case NETWORK_UNKNOWN: |
| 135 return ADAPTER_TYPE_UNKNOWN; |
| 136 case NETWORK_ETHERNET: |
| 137 return ADAPTER_TYPE_ETHERNET; |
| 138 case NETWORK_WIFI: |
| 139 return ADAPTER_TYPE_WIFI; |
| 140 case NETWORK_4G: |
| 141 case NETWORK_3G: |
| 142 case NETWORK_2G: |
| 143 return ADAPTER_TYPE_CELLULAR; |
| 144 case NETWORK_BLUETOOTH: |
| 145 case NETWORK_NONE: |
| 146 return ADAPTER_TYPE_LOOPBACK; |
| 147 default: |
| 148 RTC_DCHECK(false) << "Invalid network type " << network_type; |
| 149 return ADAPTER_TYPE_UNKNOWN; |
| 150 } |
| 151 } |
| 152 |
132 bool IsIgnoredIPv6(const IPAddress& ip) { | 153 bool IsIgnoredIPv6(const IPAddress& ip) { |
133 if (ip.family() != AF_INET6) { | 154 if (ip.family() != AF_INET6) { |
134 return false; | 155 return false; |
135 } | 156 } |
136 | 157 |
137 // Link-local addresses require scope id to be bound successfully. | 158 // Link-local addresses require scope id to be bound successfully. |
138 // However, our IPAddress structure doesn't carry that so the | 159 // However, our IPAddress structure doesn't carry that so the |
139 // information is lost and causes binding failure. | 160 // information is lost and causes binding failure. |
140 if (IPIsLinkLocal(ip)) { | 161 if (IPIsLinkLocal(ip)) { |
141 return true; | 162 return true; |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 if (family == AF_INET && !default_local_ipv4_address_.IsNil()) { | 369 if (family == AF_INET && !default_local_ipv4_address_.IsNil()) { |
349 *ipaddr = default_local_ipv4_address_; | 370 *ipaddr = default_local_ipv4_address_; |
350 return true; | 371 return true; |
351 } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) { | 372 } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) { |
352 *ipaddr = default_local_ipv6_address_; | 373 *ipaddr = default_local_ipv6_address_; |
353 return true; | 374 return true; |
354 } | 375 } |
355 return false; | 376 return false; |
356 } | 377 } |
357 | 378 |
| 379 void BasicNetworkManager::GetAndUpdateNetworkInfos() { |
| 380 std::vector<NetworkInformation> network_infos; |
| 381 LOG(LS_INFO) << "start to get network info"; |
| 382 if (network_monitor_->GetAllNetworkInfos(&network_infos)) { |
| 383 LOG(LS_INFO) << "done to get network info"; |
| 384 network_infos_.clear(); |
| 385 for (NetworkInformation& network_info : network_infos) { |
| 386 network_infos_[network_info.interface_name] = network_info; |
| 387 } |
| 388 for (auto kv : networks_map()) { |
| 389 UpdateNetworkInfo(kv.second); |
| 390 } |
| 391 LOG(LS_INFO) << "done to update network info"; |
| 392 } |
| 393 } |
| 394 |
| 395 void BasicNetworkManager::UpdateNetworkInfo(Network* network) const { |
| 396 auto it = network_infos_.find(network->name()); |
| 397 if (it != network_infos_.end()) { |
| 398 const NetworkInformation& network_info = it->second; |
| 399 network->set_handle(network_info.handle); |
| 400 network->set_type(FromNetworkType(network_info.type)); |
| 401 LOG(LS_INFO) << "Set network " << network->ToString() |
| 402 << " handle " << network->handle() |
| 403 << " type " << network->type(); |
| 404 } |
| 405 } |
| 406 |
| 407 |
358 BasicNetworkManager::BasicNetworkManager() | 408 BasicNetworkManager::BasicNetworkManager() |
359 : thread_(NULL), sent_first_update_(false), start_count_(0), | 409 : thread_(NULL), sent_first_update_(false), start_count_(0), |
360 network_ignore_mask_(kDefaultNetworkIgnoreMask), | 410 network_ignore_mask_(kDefaultNetworkIgnoreMask), |
361 ignore_non_default_routes_(false) { | 411 ignore_non_default_routes_(false) { |
362 } | 412 } |
363 | 413 |
364 BasicNetworkManager::~BasicNetworkManager() { | 414 BasicNetworkManager::~BasicNetworkManager() { |
365 } | 415 } |
366 | 416 |
367 void BasicNetworkManager::OnNetworksChanged() { | 417 void BasicNetworkManager::OnNetworksChanged() { |
368 LOG(LS_VERBOSE) << "Network change was observed at the network manager"; | 418 LOG(LS_VERBOSE) << "Network change was observed at the network manager"; |
| 419 std::vector<NetworkInformation> network_infos; |
| 420 |
369 UpdateNetworksOnce(); | 421 UpdateNetworksOnce(); |
370 } | 422 } |
371 | 423 |
| 424 void BasicNetworkManager::OnNetworkAvailable(const NetworkInformation& network_i
nfo) { |
| 425 LOG(LS_VERBOSE) << "Network available at network manager: " << network_info.in
terface_name; |
| 426 ASSERT(Thread::Current() == thread_); |
| 427 network_infos_[network_info.interface_name] = network_info; |
| 428 // Only need to update one. |
| 429 for (auto kv : networks_map()) { |
| 430 UpdateNetworkInfo(kv.second); |
| 431 } |
| 432 } |
| 433 |
372 #if defined(__native_client__) | 434 #if defined(__native_client__) |
373 | 435 |
374 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 436 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
375 NetworkList* networks) const { | 437 NetworkList* networks) const { |
376 ASSERT(false); | 438 ASSERT(false); |
377 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; | 439 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; |
378 return false; | 440 return false; |
379 } | 441 } |
380 | 442 |
381 #elif defined(WEBRTC_POSIX) | 443 #elif defined(WEBRTC_POSIX) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 } | 504 } |
443 #endif | 505 #endif |
444 // TODO(phoglund): Need to recognize other types as well. | 506 // TODO(phoglund): Need to recognize other types as well. |
445 scoped_ptr<Network> network(new Network(cursor->ifa_name, | 507 scoped_ptr<Network> network(new Network(cursor->ifa_name, |
446 cursor->ifa_name, prefix, | 508 cursor->ifa_name, prefix, |
447 prefix_length, adapter_type)); | 509 prefix_length, adapter_type)); |
448 network->set_default_local_address_provider(this); | 510 network->set_default_local_address_provider(this); |
449 network->set_scope_id(scope_id); | 511 network->set_scope_id(scope_id); |
450 network->AddIP(ip); | 512 network->AddIP(ip); |
451 network->set_ignored(IsIgnoredNetwork(*network)); | 513 network->set_ignored(IsIgnoredNetwork(*network)); |
| 514 UpdateNetworkInfo(network.get()); |
452 if (include_ignored || !network->ignored()) { | 515 if (include_ignored || !network->ignored()) { |
453 networks->push_back(network.release()); | 516 networks->push_back(network.release()); |
454 } | 517 } |
455 } else { | 518 } else { |
456 (*existing_network).second->AddIP(ip); | 519 (*existing_network).second->AddIP(ip); |
457 } | 520 } |
458 } | 521 } |
459 } | 522 } |
460 | 523 |
461 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 524 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 | 783 |
721 void BasicNetworkManager::StartNetworkMonitor() { | 784 void BasicNetworkManager::StartNetworkMonitor() { |
722 NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory(); | 785 NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory(); |
723 if (factory == nullptr) { | 786 if (factory == nullptr) { |
724 return; | 787 return; |
725 } | 788 } |
726 network_monitor_.reset(factory->CreateNetworkMonitor()); | 789 network_monitor_.reset(factory->CreateNetworkMonitor()); |
727 if (!network_monitor_) { | 790 if (!network_monitor_) { |
728 return; | 791 return; |
729 } | 792 } |
| 793 |
730 network_monitor_->SignalNetworksChanged.connect( | 794 network_monitor_->SignalNetworksChanged.connect( |
731 this, &BasicNetworkManager::OnNetworksChanged); | 795 this, &BasicNetworkManager::OnNetworksChanged); |
| 796 network_monitor_->SignalNetworkAvailable.connect( |
| 797 this, &BasicNetworkManager::OnNetworkAvailable); |
732 network_monitor_->Start(); | 798 network_monitor_->Start(); |
| 799 |
| 800 GetAndUpdateNetworkInfos(); |
733 } | 801 } |
734 | 802 |
735 void BasicNetworkManager::StopNetworkMonitor() { | 803 void BasicNetworkManager::StopNetworkMonitor() { |
736 if (!network_monitor_) { | 804 if (!network_monitor_) { |
737 return; | 805 return; |
738 } | 806 } |
739 network_monitor_->Stop(); | 807 network_monitor_->Stop(); |
740 network_monitor_.reset(); | 808 network_monitor_.reset(); |
741 } | 809 } |
742 | 810 |
| 811 |
743 void BasicNetworkManager::OnMessage(Message* msg) { | 812 void BasicNetworkManager::OnMessage(Message* msg) { |
744 switch (msg->message_id) { | 813 switch (msg->message_id) { |
745 case kUpdateNetworksMessage: { | 814 case kUpdateNetworksMessage: { |
746 UpdateNetworksContinually(); | 815 UpdateNetworksContinually(); |
747 break; | 816 break; |
748 } | 817 } |
749 case kSignalNetworksMessage: { | 818 case kSignalNetworksMessage: { |
750 SignalNetworksChanged(); | 819 SignalNetworksChanged(); |
751 break; | 820 break; |
752 } | 821 } |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 std::stringstream ss; | 980 std::stringstream ss; |
912 // Print out the first space-terminated token of the network desc, plus | 981 // Print out the first space-terminated token of the network desc, plus |
913 // the IP address. | 982 // the IP address. |
914 ss << "Net[" << description_.substr(0, description_.find(' ')) | 983 ss << "Net[" << description_.substr(0, description_.find(' ')) |
915 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 984 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
916 << ":" << AdapterTypeToString(type_) << "]"; | 985 << ":" << AdapterTypeToString(type_) << "]"; |
917 return ss.str(); | 986 return ss.str(); |
918 } | 987 } |
919 | 988 |
920 } // namespace rtc | 989 } // namespace rtc |
OLD | NEW |