blob: 040b24205eff8e2cad23166e2a1bc3a7855f173e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_FAKE_NETWORK_H_
12#define RTC_BASE_FAKE_NETWORK_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <memory>
15#include <string>
16#include <utility>
17#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Qingsi Wang09619332018-09-12 22:51:55 -070019#include "absl/memory/memory.h"
20#include "rtc_base/checks.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070021#include "rtc_base/mdns_responder_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/message_handler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/network.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/socket_address.h"
25#include "rtc_base/string_encode.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027
28namespace rtc {
29
30const int kFakeIPv4NetworkPrefixLength = 24;
31const int kFakeIPv6NetworkPrefixLength = 64;
32
33// Fake network manager that allows us to manually specify the IPs to use.
Yves Gerey665174f2018-06-19 15:03:05 +020034class FakeNetworkManager : public NetworkManagerBase, public MessageHandler {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035 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 Gerey665174f2018-06-19 15:03:05 +020059 for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 if (it->first.EqualIPs(iface)) {
61 ifaces_.erase(it);
62 break;
63 }
64 }
65 DoUpdateNetworks();
66 }
67
Mirko Bonadei1916cbc2019-03-01 15:20:35 +010068 void StartUpdating() override {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069 ++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 Bonadei1916cbc2019-03-01 15:20:35 +010080 void StopUpdating() override { --start_count_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081
82 // MessageHandler interface.
Mirko Bonadei1916cbc2019-03-01 15:20:35 +010083 void OnMessage(Message* msg) override { DoUpdateNetworks(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085 using NetworkManagerBase::set_default_local_addresses;
Jonas Olssona4d87372019-07-05 19:08:33 +020086 using NetworkManagerBase::set_enumeration_permission;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087
Qingsi Wang1dac6d82018-12-12 15:28:47 -080088 // rtc::NetworkManager override.
Qingsi Wang7852d292018-10-31 11:17:07 -070089 webrtc::MdnsResponderInterface* GetMdnsResponder() const override {
Qingsi Wang09619332018-09-12 22:51:55 -070090 return mdns_responder_.get();
91 }
92
Qingsi Wangecd30542019-05-22 14:34:56 -070093 void set_mdns_responder(
94 std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder) {
95 mdns_responder_ = std::move(mdns_responder);
Qingsi Wang1dac6d82018-12-12 15:28:47 -080096 }
97
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098 private:
99 void DoUpdateNetworks() {
100 if (start_count_ == 0)
101 return;
102 std::vector<Network*> networks;
Yves Gerey665174f2018-06-19 15:03:05 +0200103 for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104 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 Wangecd30542019-05-22 14:34:56 -0700131 std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132};
133
134} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135
Steve Anton10542f22019-01-11 09:11:00 -0800136#endif // RTC_BASE_FAKE_NETWORK_H_