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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 | 235 |
236 void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, | 236 void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, |
237 bool* changed) { | 237 bool* changed) { |
238 NetworkManager::Stats stats; | 238 NetworkManager::Stats stats; |
239 MergeNetworkList(new_networks, changed, &stats); | 239 MergeNetworkList(new_networks, changed, &stats); |
240 } | 240 } |
241 | 241 |
242 void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, | 242 void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, |
243 bool* changed, | 243 bool* changed, |
244 NetworkManager::Stats* stats) { | 244 NetworkManager::Stats* stats) { |
245 *changed = false; | |
245 // AddressList in this map will track IP addresses for all Networks | 246 // AddressList in this map will track IP addresses for all Networks |
246 // with the same key. | 247 // with the same key. |
247 std::map<std::string, AddressList> consolidated_address_list; | 248 std::map<std::string, AddressList> consolidated_address_list; |
248 NetworkList list(new_networks); | 249 NetworkList list(new_networks); |
249 | |
250 // Result of Network merge. Element in this list should have unique key. | |
251 NetworkList merged_list; | |
252 std::sort(list.begin(), list.end(), CompareNetworks); | 250 std::sort(list.begin(), list.end(), CompareNetworks); |
253 | |
254 *changed = false; | |
255 | |
256 if (networks_.size() != list.size()) | |
257 *changed = true; | |
258 | |
259 // First, build a set of network-keys to the ipaddresses. | 251 // First, build a set of network-keys to the ipaddresses. |
260 for (Network* network : list) { | 252 for (Network* network : list) { |
261 bool might_add_to_merged_list = false; | 253 bool might_add_to_merged_list = false; |
262 std::string key = MakeNetworkKey(network->name(), | 254 std::string key = MakeNetworkKey(network->name(), |
263 network->prefix(), | 255 network->prefix(), |
264 network->prefix_length()); | 256 network->prefix_length()); |
265 if (consolidated_address_list.find(key) == | 257 if (consolidated_address_list.find(key) == |
266 consolidated_address_list.end()) { | 258 consolidated_address_list.end()) { |
267 AddressList addrlist; | 259 AddressList addrlist; |
268 addrlist.net = network; | 260 addrlist.net = network; |
(...skipping 11 matching lines...) Expand all Loading... | |
280 if (current_list.ips[0].family() == AF_INET) { | 272 if (current_list.ips[0].family() == AF_INET) { |
281 stats->ipv4_network_count++; | 273 stats->ipv4_network_count++; |
282 } else { | 274 } else { |
283 ASSERT(current_list.ips[0].family() == AF_INET6); | 275 ASSERT(current_list.ips[0].family() == AF_INET6); |
284 stats->ipv6_network_count++; | 276 stats->ipv6_network_count++; |
285 } | 277 } |
286 } | 278 } |
287 } | 279 } |
288 | 280 |
289 // Next, look for existing network objects to re-use. | 281 // Next, look for existing network objects to re-use. |
282 // Result of Network merge. Element in this list should have unique key. | |
283 NetworkList merged_list; | |
290 for (const auto& kv : consolidated_address_list) { | 284 for (const auto& kv : consolidated_address_list) { |
291 const std::string& key = kv.first; | 285 const std::string& key = kv.first; |
292 Network* net = kv.second.net; | 286 Network* net = kv.second.net; |
293 auto existing = networks_map_.find(key); | 287 auto existing = networks_map_.find(key); |
294 if (existing == networks_map_.end()) { | 288 if (existing == networks_map_.end()) { |
295 // This network is new. Place it in the network map. | 289 // This network is new. Place it in the network map. |
296 merged_list.push_back(net); | 290 merged_list.push_back(net); |
297 networks_map_[key] = net; | 291 networks_map_[key] = net; |
298 // Also, we might have accumulated IPAddresses from the first | 292 // Also, we might have accumulated IPAddresses from the first |
299 // step, set it here. | 293 // step, set it here. |
300 net->SetIPs(kv.second.ips, true); | 294 net->SetIPs(kv.second.ips, true); |
301 *changed = true; | 295 *changed = true; |
302 } else { | 296 } else { |
303 // This network exists in the map already. Reset its IP addresses. | 297 // This network exists in the map already. Reset its IP addresses. |
304 *changed = existing->second->SetIPs(kv.second.ips, *changed); | 298 Network* existing_net = existing->second; |
305 merged_list.push_back(existing->second); | 299 *changed = existing_net->SetIPs(kv.second.ips, *changed); |
306 if (existing->second != net) { | 300 merged_list.push_back(existing_net); |
301 // If the existing network was not active, networks have changed. | |
302 if (!existing_net->active()) { | |
303 *changed = true; | |
304 } | |
305 ASSERT(net->active()); | |
306 if (existing_net != net) { | |
307 delete net; | 307 delete net; |
308 } | 308 } |
309 } | 309 } |
310 } | 310 } |
311 networks_ = merged_list; | 311 // It may still happen that the merged list is a subset of |networks_|. |
312 // To detect this change, we compare their sizes. | |
313 if (merged_list.size() != networks_.size()) { | |
314 *changed = true; | |
315 } | |
312 | 316 |
313 // If the network lists changes, we resort it. | 317 // If the network list changes, we re-assign |networks_| to the merged list |
318 // and re-sort it. | |
314 if (*changed) { | 319 if (*changed) { |
320 networks_ = merged_list; | |
321 // Reset the active states of all networks. | |
322 for (const auto& kv : networks_map_) { | |
323 kv.second->set_active(false); | |
324 } | |
325 for (Network* network : networks_) { | |
326 network->set_active(true); | |
327 } | |
315 std::sort(networks_.begin(), networks_.end(), SortNetworks); | 328 std::sort(networks_.begin(), networks_.end(), SortNetworks); |
316 // Now network interfaces are sorted, we should set the preference value | 329 // Now network interfaces are sorted, we should set the preference value |
317 // for each of the interfaces we are planning to use. | 330 // for each of the interfaces we are planning to use. |
318 // Preference order of network interfaces might have changed from previous | 331 // Preference order of network interfaces might have changed from previous |
319 // sorting due to addition of higher preference network interface. | 332 // sorting due to addition of higher preference network interface. |
320 // Since we have already sorted the network interfaces based on our | 333 // Since we have already sorted the network interfaces based on our |
321 // requirements, we will just assign a preference value starting with 127, | 334 // requirements, we will just assign a preference value starting with 127, |
322 // in decreasing order. | 335 // in decreasing order. |
323 int pref = kHighestNetworkPreference; | 336 int pref = kHighestNetworkPreference; |
324 for (Network* network : networks_) { | 337 for (Network* network : networks_) { |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 #endif | 456 #endif |
444 // TODO(phoglund): Need to recognize other types as well. | 457 // TODO(phoglund): Need to recognize other types as well. |
445 scoped_ptr<Network> network(new Network(cursor->ifa_name, | 458 scoped_ptr<Network> network(new Network(cursor->ifa_name, |
446 cursor->ifa_name, prefix, | 459 cursor->ifa_name, prefix, |
447 prefix_length, adapter_type)); | 460 prefix_length, adapter_type)); |
448 network->set_default_local_address_provider(this); | 461 network->set_default_local_address_provider(this); |
449 network->set_scope_id(scope_id); | 462 network->set_scope_id(scope_id); |
450 network->AddIP(ip); | 463 network->AddIP(ip); |
451 network->set_ignored(IsIgnoredNetwork(*network)); | 464 network->set_ignored(IsIgnoredNetwork(*network)); |
452 if (include_ignored || !network->ignored()) { | 465 if (include_ignored || !network->ignored()) { |
466 current_networks[key] = network.get(); | |
pthatcher1
2015/12/15 08:13:09
OK, I understand this now. But it makes me think
honghaiz3
2015/12/15 20:04:53
I have a test there already.
Note that this does
| |
453 networks->push_back(network.release()); | 467 networks->push_back(network.release()); |
454 } | 468 } |
455 } else { | 469 } else { |
456 (*existing_network).second->AddIP(ip); | 470 (*existing_network).second->AddIP(ip); |
457 } | 471 } |
458 } | 472 } |
459 } | 473 } |
460 | 474 |
461 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 475 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
462 NetworkList* networks) const { | 476 NetworkList* networks) const { |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
597 adapter_type = ADAPTER_TYPE_LOOPBACK; | 611 adapter_type = ADAPTER_TYPE_LOOPBACK; |
598 } | 612 } |
599 scoped_ptr<Network> network(new Network(name, description, prefix, | 613 scoped_ptr<Network> network(new Network(name, description, prefix, |
600 prefix_length, adapter_type)); | 614 prefix_length, adapter_type)); |
601 network->set_default_local_address_provider(this); | 615 network->set_default_local_address_provider(this); |
602 network->set_scope_id(scope_id); | 616 network->set_scope_id(scope_id); |
603 network->AddIP(ip); | 617 network->AddIP(ip); |
604 bool ignored = IsIgnoredNetwork(*network); | 618 bool ignored = IsIgnoredNetwork(*network); |
605 network->set_ignored(ignored); | 619 network->set_ignored(ignored); |
606 if (include_ignored || !network->ignored()) { | 620 if (include_ignored || !network->ignored()) { |
621 current_networks[key] = network.get(); | |
607 networks->push_back(network.release()); | 622 networks->push_back(network.release()); |
608 } | 623 } |
609 } else { | 624 } else { |
610 (*existing_network).second->AddIP(ip); | 625 (*existing_network).second->AddIP(ip); |
611 } | 626 } |
612 } | 627 } |
613 // Count is per-adapter - all 'Networks' created from the same | 628 // Count is per-adapter - all 'Networks' created from the same |
614 // adapter need to have the same name. | 629 // adapter need to have the same name. |
615 ++count; | 630 ++count; |
616 } | 631 } |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
794 sent_first_update_ = true; | 809 sent_first_update_ = true; |
795 } | 810 } |
796 } | 811 } |
797 } | 812 } |
798 | 813 |
799 void BasicNetworkManager::UpdateNetworksContinually() { | 814 void BasicNetworkManager::UpdateNetworksContinually() { |
800 UpdateNetworksOnce(); | 815 UpdateNetworksOnce(); |
801 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage); | 816 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage); |
802 } | 817 } |
803 | 818 |
804 void BasicNetworkManager::DumpNetworks(bool include_ignored) { | 819 void BasicNetworkManager::DumpNetworks() { |
805 NetworkList list; | 820 NetworkList list; |
806 CreateNetworks(include_ignored, &list); | 821 GetNetworks(&list); |
807 LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:"; | 822 LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:"; |
808 for (const Network* network : list) { | 823 for (const Network* network : list) { |
809 if (!network->ignored() || include_ignored) { | 824 LOG(LS_INFO) << network->ToString() << ": " << network->description() |
810 LOG(LS_INFO) << network->ToString() << ": " | 825 << ", active ? " << network->active() |
811 << network->description() | 826 << ((network->ignored()) ? ", Ignored" : ""); |
812 << ((network->ignored()) ? ", Ignored" : ""); | |
813 } | |
814 } | |
815 // Release the network list created previously. | |
816 // Do this in a seperated for loop for better readability. | |
817 for (Network* network : list) { | |
818 delete network; | |
819 } | 827 } |
820 } | 828 } |
821 | 829 |
822 Network::Network(const std::string& name, | 830 Network::Network(const std::string& name, |
823 const std::string& desc, | 831 const std::string& desc, |
824 const IPAddress& prefix, | 832 const IPAddress& prefix, |
825 int prefix_length) | 833 int prefix_length) |
826 : name_(name), | 834 : name_(name), |
827 description_(desc), | 835 description_(desc), |
828 prefix_(prefix), | 836 prefix_(prefix), |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
911 std::stringstream ss; | 919 std::stringstream ss; |
912 // Print out the first space-terminated token of the network desc, plus | 920 // Print out the first space-terminated token of the network desc, plus |
913 // the IP address. | 921 // the IP address. |
914 ss << "Net[" << description_.substr(0, description_.find(' ')) | 922 ss << "Net[" << description_.substr(0, description_.find(' ')) |
915 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 923 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
916 << ":" << AdapterTypeToString(type_) << "]"; | 924 << ":" << AdapterTypeToString(type_) << "]"; |
917 return ss.str(); | 925 return ss.str(); |
918 } | 926 } |
919 | 927 |
920 } // namespace rtc | 928 } // namespace rtc |
OLD | NEW |