blob: f3de83b4ef22aff3ef111b929358a671b75b05d0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_FAKENETWORK_H_
12#define RTC_BASE_FAKENETWORK_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"
21#include "rtc_base/fake_mdns_responder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#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 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
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 Gerey665174f2018-06-19 15:03:05 +020083 virtual void OnMessage(Message* msg) { DoUpdateNetworks(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
Qingsi Wang7852d292018-10-31 11:17:07 -070085 void CreateMdnsResponder() {
Qingsi Wang09619332018-09-12 22:51:55 -070086 if (mdns_responder_ == nullptr) {
Qingsi Wang7852d292018-10-31 11:17:07 -070087 mdns_responder_ = absl::make_unique<webrtc::FakeMdnsResponder>();
Qingsi Wang09619332018-09-12 22:51:55 -070088 }
89 }
90
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091 using NetworkManagerBase::set_enumeration_permission;
92 using NetworkManagerBase::set_default_local_addresses;
93
Qingsi Wang7852d292018-10-31 11:17:07 -070094 webrtc::MdnsResponderInterface* GetMdnsResponder() const override {
Qingsi Wang09619332018-09-12 22:51:55 -070095 return mdns_responder_.get();
96 }
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
131 IPAddress default_local_ipv4_address_;
132 IPAddress default_local_ipv6_address_;
Qingsi Wang09619332018-09-12 22:51:55 -0700133
Qingsi Wang7852d292018-10-31 11:17:07 -0700134 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135};
136
137} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200139#endif // RTC_BASE_FAKENETWORK_H_