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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_FAKE_NETWORK_H_ |
| 12 | #define RTC_BASE_FAKE_NETWORK_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" |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 21 | #include "rtc_base/mdns_responder_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/message_handler.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/network.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/socket_address.h" |
| 25 | #include "rtc_base/string_encode.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 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 | |
Mirko Bonadei | 1916cbc | 2019-03-01 15:20:35 +0100 | [diff] [blame] | 68 | void StartUpdating() override { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 | |
Mirko Bonadei | 1916cbc | 2019-03-01 15:20:35 +0100 | [diff] [blame] | 80 | void StopUpdating() override { --start_count_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | |
| 82 | // MessageHandler interface. |
Mirko Bonadei | 1916cbc | 2019-03-01 15:20:35 +0100 | [diff] [blame] | 83 | void OnMessage(Message* msg) override { DoUpdateNetworks(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 84 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | using NetworkManagerBase::set_default_local_addresses; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 86 | using NetworkManagerBase::set_enumeration_permission; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 88 | // rtc::NetworkManager override. |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 89 | webrtc::MdnsResponderInterface* GetMdnsResponder() const override { |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 90 | return mdns_responder_.get(); |
| 91 | } |
| 92 | |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 93 | void set_mdns_responder( |
| 94 | std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder) { |
| 95 | mdns_responder_ = std::move(mdns_responder); |
Qingsi Wang | 1dac6d8 | 2018-12-12 15:28:47 -0800 | [diff] [blame] | 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 | |
Qingsi Wang | ecd3054 | 2019-05-22 14:34:56 -0700 | [diff] [blame] | 131 | std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 136 | #endif // RTC_BASE_FAKE_NETWORK_H_ |