henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_FAKENETWORK_H_ |
| 12 | #define RTC_BASE_FAKENETWORK_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/messagehandler.h" |
| 20 | #include "rtc_base/network.h" |
| 21 | #include "rtc_base/socketaddress.h" |
| 22 | #include "rtc_base/stringencode.h" |
| 23 | #include "rtc_base/thread.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | |
| 25 | namespace rtc { |
| 26 | |
| 27 | const int kFakeIPv4NetworkPrefixLength = 24; |
| 28 | const int kFakeIPv6NetworkPrefixLength = 64; |
| 29 | |
| 30 | // Fake network manager that allows us to manually specify the IPs to use. |
| 31 | class FakeNetworkManager : public NetworkManagerBase, |
| 32 | public MessageHandler { |
| 33 | public: |
| 34 | FakeNetworkManager() {} |
| 35 | |
| 36 | typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList; |
| 37 | |
| 38 | void AddInterface(const SocketAddress& iface) { |
| 39 | // Ensure a unique name for the interface if its name is not given. |
| 40 | AddInterface(iface, "test" + rtc::ToString(next_index_++)); |
| 41 | } |
| 42 | |
| 43 | void AddInterface(const SocketAddress& iface, const std::string& if_name) { |
| 44 | AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN); |
| 45 | } |
| 46 | |
| 47 | void AddInterface(const SocketAddress& iface, |
| 48 | const std::string& if_name, |
| 49 | AdapterType type) { |
| 50 | SocketAddress address(if_name, 0); |
| 51 | address.SetResolvedIP(iface.ipaddr()); |
| 52 | ifaces_.push_back(std::make_pair(address, type)); |
| 53 | DoUpdateNetworks(); |
| 54 | } |
| 55 | |
| 56 | void RemoveInterface(const SocketAddress& iface) { |
| 57 | for (IfaceList::iterator it = ifaces_.begin(); |
| 58 | it != ifaces_.end(); ++it) { |
| 59 | if (it->first.EqualIPs(iface)) { |
| 60 | ifaces_.erase(it); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | DoUpdateNetworks(); |
| 65 | } |
| 66 | |
| 67 | virtual void StartUpdating() { |
| 68 | ++start_count_; |
| 69 | if (start_count_ == 1) { |
| 70 | sent_first_update_ = false; |
| 71 | rtc::Thread::Current()->Post(RTC_FROM_HERE, this); |
| 72 | } else { |
| 73 | if (sent_first_update_) { |
| 74 | SignalNetworksChanged(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | virtual void StopUpdating() { --start_count_; } |
| 80 | |
| 81 | // MessageHandler interface. |
| 82 | virtual void OnMessage(Message* msg) { |
| 83 | DoUpdateNetworks(); |
| 84 | } |
| 85 | |
| 86 | using NetworkManagerBase::set_enumeration_permission; |
| 87 | using NetworkManagerBase::set_default_local_addresses; |
| 88 | |
| 89 | private: |
| 90 | void DoUpdateNetworks() { |
| 91 | if (start_count_ == 0) |
| 92 | return; |
| 93 | std::vector<Network*> networks; |
| 94 | for (IfaceList::iterator it = ifaces_.begin(); |
| 95 | it != ifaces_.end(); ++it) { |
| 96 | int prefix_length = 0; |
| 97 | if (it->first.ipaddr().family() == AF_INET) { |
| 98 | prefix_length = kFakeIPv4NetworkPrefixLength; |
| 99 | } else if (it->first.ipaddr().family() == AF_INET6) { |
| 100 | prefix_length = kFakeIPv6NetworkPrefixLength; |
| 101 | } |
| 102 | IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length); |
| 103 | std::unique_ptr<Network> net(new Network(it->first.hostname(), |
| 104 | it->first.hostname(), prefix, |
| 105 | prefix_length, it->second)); |
| 106 | net->set_default_local_address_provider(this); |
| 107 | net->AddIP(it->first.ipaddr()); |
| 108 | networks.push_back(net.release()); |
| 109 | } |
| 110 | bool changed; |
| 111 | MergeNetworkList(networks, &changed); |
| 112 | if (changed || !sent_first_update_) { |
| 113 | SignalNetworksChanged(); |
| 114 | sent_first_update_ = true; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | IfaceList ifaces_; |
| 119 | int next_index_ = 0; |
| 120 | int start_count_ = 0; |
| 121 | bool sent_first_update_ = false; |
| 122 | |
| 123 | IPAddress default_local_ipv4_address_; |
| 124 | IPAddress default_local_ipv6_address_; |
| 125 | }; |
| 126 | |
| 127 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 129 | #endif // RTC_BASE_FAKENETWORK_H_ |