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 | |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 19 | #include "absl/memory/memory.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/fake_mdns_responder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/messagehandler.h" |
| 23 | #include "rtc_base/network.h" |
| 24 | #include "rtc_base/socketaddress.h" |
| 25 | #include "rtc_base/stringencode.h" |
| 26 | #include "rtc_base/thread.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | |
| 28 | namespace rtc { |
| 29 | |
| 30 | const int kFakeIPv4NetworkPrefixLength = 24; |
| 31 | const int kFakeIPv6NetworkPrefixLength = 64; |
| 32 | |
| 33 | // Fake network manager that allows us to manually specify the IPs to use. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 34 | class FakeNetworkManager : public NetworkManagerBase, public MessageHandler { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | public: |
| 36 | FakeNetworkManager() {} |
| 37 | |
| 38 | typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList; |
| 39 | |
| 40 | void AddInterface(const SocketAddress& iface) { |
| 41 | // Ensure a unique name for the interface if its name is not given. |
| 42 | AddInterface(iface, "test" + rtc::ToString(next_index_++)); |
| 43 | } |
| 44 | |
| 45 | void AddInterface(const SocketAddress& iface, const std::string& if_name) { |
| 46 | AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN); |
| 47 | } |
| 48 | |
| 49 | void AddInterface(const SocketAddress& iface, |
| 50 | const std::string& if_name, |
| 51 | AdapterType type) { |
| 52 | SocketAddress address(if_name, 0); |
| 53 | address.SetResolvedIP(iface.ipaddr()); |
| 54 | ifaces_.push_back(std::make_pair(address, type)); |
| 55 | DoUpdateNetworks(); |
| 56 | } |
| 57 | |
| 58 | void RemoveInterface(const SocketAddress& iface) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 59 | for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 60 | if (it->first.EqualIPs(iface)) { |
| 61 | ifaces_.erase(it); |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | DoUpdateNetworks(); |
| 66 | } |
| 67 | |
| 68 | virtual void StartUpdating() { |
| 69 | ++start_count_; |
| 70 | if (start_count_ == 1) { |
| 71 | sent_first_update_ = false; |
| 72 | rtc::Thread::Current()->Post(RTC_FROM_HERE, this); |
| 73 | } else { |
| 74 | if (sent_first_update_) { |
| 75 | SignalNetworksChanged(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | virtual void StopUpdating() { --start_count_; } |
| 81 | |
| 82 | // MessageHandler interface. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 83 | virtual void OnMessage(Message* msg) { DoUpdateNetworks(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 84 | |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 85 | void CreateMdnsResponder() { |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 86 | if (mdns_responder_ == nullptr) { |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 87 | mdns_responder_ = absl::make_unique<webrtc::FakeMdnsResponder>(); |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 91 | using NetworkManagerBase::set_enumeration_permission; |
| 92 | using NetworkManagerBase::set_default_local_addresses; |
| 93 | |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 94 | webrtc::MdnsResponderInterface* GetMdnsResponder() const override { |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 95 | return mdns_responder_.get(); |
| 96 | } |
| 97 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 98 | private: |
| 99 | void DoUpdateNetworks() { |
| 100 | if (start_count_ == 0) |
| 101 | return; |
| 102 | std::vector<Network*> networks; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 104 | int prefix_length = 0; |
| 105 | if (it->first.ipaddr().family() == AF_INET) { |
| 106 | prefix_length = kFakeIPv4NetworkPrefixLength; |
| 107 | } else if (it->first.ipaddr().family() == AF_INET6) { |
| 108 | prefix_length = kFakeIPv6NetworkPrefixLength; |
| 109 | } |
| 110 | IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length); |
| 111 | std::unique_ptr<Network> net(new Network(it->first.hostname(), |
| 112 | it->first.hostname(), prefix, |
| 113 | prefix_length, it->second)); |
| 114 | net->set_default_local_address_provider(this); |
| 115 | net->AddIP(it->first.ipaddr()); |
| 116 | networks.push_back(net.release()); |
| 117 | } |
| 118 | bool changed; |
| 119 | MergeNetworkList(networks, &changed); |
| 120 | if (changed || !sent_first_update_) { |
| 121 | SignalNetworksChanged(); |
| 122 | sent_first_update_ = true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | IfaceList ifaces_; |
| 127 | int next_index_ = 0; |
| 128 | int start_count_ = 0; |
| 129 | bool sent_first_update_ = false; |
| 130 | |
| 131 | IPAddress default_local_ipv4_address_; |
| 132 | IPAddress default_local_ipv6_address_; |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 133 | |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 134 | std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 138 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 139 | #endif // RTC_BASE_FAKENETWORK_H_ |