xref: /aosp_15_r20/external/webrtc/rtc_base/fake_network.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef RTC_BASE_FAKE_NETWORK_H_
12 #define RTC_BASE_FAKE_NETWORK_H_
13 
14 #include <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "absl/memory/memory.h"
20 #include "rtc_base/mdns_responder_interface.h"
21 #include "rtc_base/network.h"
22 #include "rtc_base/socket_address.h"
23 #include "rtc_base/string_encode.h"
24 #include "rtc_base/thread.h"
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.
32 class FakeNetworkManager : public NetworkManagerBase {
33  public:
FakeNetworkManager()34   FakeNetworkManager() {}
35 
36   struct Iface {
37     SocketAddress socket_address;
38     AdapterType adapter_type;
39     absl::optional<AdapterType> underlying_vpn_adapter_type;
40   };
41   typedef std::vector<Iface> IfaceList;
42 
AddInterface(const SocketAddress & iface)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 
AddInterface(const SocketAddress & iface,absl::string_view if_name)48   void AddInterface(const SocketAddress& iface, absl::string_view if_name) {
49     AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN);
50   }
51 
52   void AddInterface(
53       const SocketAddress& iface,
54       absl::string_view if_name,
55       AdapterType type,
56       absl::optional<AdapterType> underlying_vpn_adapter_type = absl::nullopt) {
57     SocketAddress address(if_name, 0);
58     address.SetResolvedIP(iface.ipaddr());
59     ifaces_.push_back({address, type, underlying_vpn_adapter_type});
60     DoUpdateNetworks();
61   }
62 
RemoveInterface(const SocketAddress & iface)63   void RemoveInterface(const SocketAddress& iface) {
64     for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
65       if (it->socket_address.EqualIPs(iface)) {
66         ifaces_.erase(it);
67         break;
68       }
69     }
70     DoUpdateNetworks();
71   }
72 
StartUpdating()73   void StartUpdating() override {
74     ++start_count_;
75     if (start_count_ == 1) {
76       sent_first_update_ = false;
77       Thread::Current()->PostTask([this] { DoUpdateNetworks(); });
78     } else if (sent_first_update_) {
79       Thread::Current()->PostTask([this] { SignalNetworksChanged(); });
80     }
81   }
82 
StopUpdating()83   void StopUpdating() override { --start_count_; }
84 
85   using NetworkManagerBase::set_default_local_addresses;
86   using NetworkManagerBase::set_enumeration_permission;
87 
88   // rtc::NetworkManager override.
GetMdnsResponder()89   webrtc::MdnsResponderInterface* GetMdnsResponder() const override {
90     return mdns_responder_.get();
91   }
92 
set_mdns_responder(std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder)93   void set_mdns_responder(
94       std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder) {
95     mdns_responder_ = std::move(mdns_responder);
96   }
97 
98  private:
DoUpdateNetworks()99   void DoUpdateNetworks() {
100     if (start_count_ == 0)
101       return;
102     std::vector<std::unique_ptr<Network>> networks;
103     for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
104       int prefix_length = 0;
105       if (it->socket_address.ipaddr().family() == AF_INET) {
106         prefix_length = kFakeIPv4NetworkPrefixLength;
107       } else if (it->socket_address.ipaddr().family() == AF_INET6) {
108         prefix_length = kFakeIPv6NetworkPrefixLength;
109       }
110       IPAddress prefix = TruncateIP(it->socket_address.ipaddr(), prefix_length);
111       auto net = std::make_unique<Network>(
112           it->socket_address.hostname(), it->socket_address.hostname(), prefix,
113           prefix_length, it->adapter_type, /*field_trials=*/nullptr);
114       if (it->underlying_vpn_adapter_type.has_value()) {
115         net->set_underlying_type_for_vpn(*it->underlying_vpn_adapter_type);
116       }
117       net->set_default_local_address_provider(this);
118       net->AddIP(it->socket_address.ipaddr());
119       networks.push_back(std::move(net));
120     }
121     bool changed;
122     MergeNetworkList(std::move(networks), &changed);
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 
134   std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder_;
135 };
136 
137 }  // namespace rtc
138 
139 #endif  // RTC_BASE_FAKE_NETWORK_H_
140