henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 17:11:00 | [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 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 19 | #include "absl/memory/memory.h" |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [diff] [blame] | 20 | #include "rtc_base/mdns_responder_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "rtc_base/network.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "rtc_base/socket_address.h" |
| 23 | #include "rtc_base/string_encode.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 24 | #include "rtc_base/thread.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 25 | |
| 26 | namespace rtc { |
| 27 | |
| 28 | const int kFakeIPv4NetworkPrefixLength = 24; |
| 29 | const int kFakeIPv6NetworkPrefixLength = 64; |
| 30 | |
| 31 | // Fake network manager that allows us to manually specify the IPs to use. |
Danil Chapovalov | 6f8b4cd | 2022-09-02 15:27:23 | [diff] [blame] | 32 | class FakeNetworkManager : public NetworkManagerBase { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | public: |
| 34 | FakeNetworkManager() {} |
| 35 | |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 36 | struct Iface { |
| 37 | SocketAddress socket_address; |
| 38 | AdapterType adapter_type; |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 39 | std::optional<AdapterType> underlying_vpn_adapter_type; |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 40 | }; |
| 41 | typedef std::vector<Iface> IfaceList; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 42 | |
| 43 | void AddInterface(const SocketAddress& iface) { |
| 44 | // Ensure a unique name for the interface if its name is not given. |
| 45 | AddInterface(iface, "test" + rtc::ToString(next_index_++)); |
| 46 | } |
| 47 | |
Ali Tofigh | d389078 | 2022-04-29 12:02:22 | [diff] [blame] | 48 | void AddInterface(const SocketAddress& iface, absl::string_view if_name) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 49 | AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN); |
| 50 | } |
| 51 | |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 52 | void AddInterface( |
| 53 | const SocketAddress& iface, |
Ali Tofigh | d389078 | 2022-04-29 12:02:22 | [diff] [blame] | 54 | absl::string_view if_name, |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 55 | AdapterType type, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 56 | std::optional<AdapterType> underlying_vpn_adapter_type = std::nullopt) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 57 | SocketAddress address(if_name, 0); |
| 58 | address.SetResolvedIP(iface.ipaddr()); |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 59 | ifaces_.push_back({address, type, underlying_vpn_adapter_type}); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 60 | DoUpdateNetworks(); |
| 61 | } |
| 62 | |
| 63 | void RemoveInterface(const SocketAddress& iface) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 64 | for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) { |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 65 | if (it->socket_address.EqualIPs(iface)) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 66 | ifaces_.erase(it); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | DoUpdateNetworks(); |
| 71 | } |
| 72 | |
Mirko Bonadei | 1916cbc | 2019-03-01 14:20:35 | [diff] [blame] | 73 | void StartUpdating() override { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 74 | ++start_count_; |
| 75 | if (start_count_ == 1) { |
| 76 | sent_first_update_ = false; |
Danil Chapovalov | 6f8b4cd | 2022-09-02 15:27:23 | [diff] [blame] | 77 | Thread::Current()->PostTask([this] { DoUpdateNetworks(); }); |
| 78 | } else if (sent_first_update_) { |
| 79 | Thread::Current()->PostTask([this] { SignalNetworksChanged(); }); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Mirko Bonadei | 1916cbc | 2019-03-01 14:20:35 | [diff] [blame] | 83 | void StopUpdating() override { --start_count_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 84 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 85 | using NetworkManagerBase::set_default_local_addresses; |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 86 | using NetworkManagerBase::set_enumeration_permission; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 87 | |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 88 | // rtc::NetworkManager override. |
Qingsi Wang | 7852d29 | 2018-10-31 18:17:07 | [diff] [blame] | 89 | webrtc::MdnsResponderInterface* GetMdnsResponder() const override { |
Qingsi Wang | 0961933 | 2018-09-13 05:51:55 | [diff] [blame] | 90 | return mdns_responder_.get(); |
| 91 | } |
| 92 | |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [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 23:28:47 | [diff] [blame] | 96 | } |
| 97 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 98 | private: |
| 99 | void DoUpdateNetworks() { |
| 100 | if (start_count_ == 0) |
| 101 | return; |
Niels Möller | d959f3a | 2022-04-19 09:29:19 | [diff] [blame] | 102 | std::vector<std::unique_ptr<Network>> networks; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 103 | for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 104 | int prefix_length = 0; |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 105 | if (it->socket_address.ipaddr().family() == AF_INET) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 106 | prefix_length = kFakeIPv4NetworkPrefixLength; |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 107 | } else if (it->socket_address.ipaddr().family() == AF_INET6) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 108 | prefix_length = kFakeIPv6NetworkPrefixLength; |
| 109 | } |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 110 | IPAddress prefix = TruncateIP(it->socket_address.ipaddr(), prefix_length); |
Niels Möller | d959f3a | 2022-04-19 09:29:19 | [diff] [blame] | 111 | auto net = std::make_unique<Network>( |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 112 | it->socket_address.hostname(), it->socket_address.hostname(), prefix, |
Sameer Vijaykar | df7df19 | 2023-04-24 09:05:44 | [diff] [blame] | 113 | prefix_length, it->adapter_type); |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 114 | if (it->underlying_vpn_adapter_type.has_value()) { |
| 115 | net->set_underlying_type_for_vpn(*it->underlying_vpn_adapter_type); |
| 116 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 117 | net->set_default_local_address_provider(this); |
Jonas Oreland | c8fa1ee | 2021-08-25 06:58:04 | [diff] [blame] | 118 | net->AddIP(it->socket_address.ipaddr()); |
Niels Möller | d959f3a | 2022-04-19 09:29:19 | [diff] [blame] | 119 | networks.push_back(std::move(net)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 120 | } |
| 121 | bool changed; |
Niels Möller | d959f3a | 2022-04-19 09:29:19 | [diff] [blame] | 122 | MergeNetworkList(std::move(networks), &changed); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 123 | if (changed || !sent_first_update_) { |
| 124 | SignalNetworksChanged(); |
| 125 | sent_first_update_ = true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | IfaceList ifaces_; |
| 130 | int next_index_ = 0; |
| 131 | int start_count_ = 0; |
| 132 | bool sent_first_update_ = false; |
| 133 | |
Qingsi Wang | ecd3054 | 2019-05-22 21:34:56 | [diff] [blame] | 134 | std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 138 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 139 | #endif // RTC_BASE_FAKE_NETWORK_H_ |