blob: 8ed11646cde716fec45fd915b82f4cd809fce7c2 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/messagehandler.h"
20#include "rtc_base/network.h"
21#include "rtc_base/socketaddress.h"
22#include "rtc_base/stringencode.h"
23#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024
25namespace rtc {
26
27const int kFakeIPv4NetworkPrefixLength = 24;
28const int kFakeIPv6NetworkPrefixLength = 64;
29
30// Fake network manager that allows us to manually specify the IPs to use.
Yves Gerey665174f2018-06-19 15:03:05 +020031class FakeNetworkManager : public NetworkManagerBase, public MessageHandler {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 public:
33 FakeNetworkManager() {}
34
35 typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList;
36
37 void AddInterface(const SocketAddress& iface) {
38 // Ensure a unique name for the interface if its name is not given.
39 AddInterface(iface, "test" + rtc::ToString(next_index_++));
40 }
41
42 void AddInterface(const SocketAddress& iface, const std::string& if_name) {
43 AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN);
44 }
45
46 void AddInterface(const SocketAddress& iface,
47 const std::string& if_name,
48 AdapterType type) {
49 SocketAddress address(if_name, 0);
50 address.SetResolvedIP(iface.ipaddr());
51 ifaces_.push_back(std::make_pair(address, type));
52 DoUpdateNetworks();
53 }
54
55 void RemoveInterface(const SocketAddress& iface) {
Yves Gerey665174f2018-06-19 15:03:05 +020056 for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 if (it->first.EqualIPs(iface)) {
58 ifaces_.erase(it);
59 break;
60 }
61 }
62 DoUpdateNetworks();
63 }
64
65 virtual void StartUpdating() {
66 ++start_count_;
67 if (start_count_ == 1) {
68 sent_first_update_ = false;
69 rtc::Thread::Current()->Post(RTC_FROM_HERE, this);
70 } else {
71 if (sent_first_update_) {
72 SignalNetworksChanged();
73 }
74 }
75 }
76
77 virtual void StopUpdating() { --start_count_; }
78
79 // MessageHandler interface.
Yves Gerey665174f2018-06-19 15:03:05 +020080 virtual void OnMessage(Message* msg) { DoUpdateNetworks(); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081
82 using NetworkManagerBase::set_enumeration_permission;
83 using NetworkManagerBase::set_default_local_addresses;
84
85 private:
86 void DoUpdateNetworks() {
87 if (start_count_ == 0)
88 return;
89 std::vector<Network*> networks;
Yves Gerey665174f2018-06-19 15:03:05 +020090 for (IfaceList::iterator it = ifaces_.begin(); it != ifaces_.end(); ++it) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091 int prefix_length = 0;
92 if (it->first.ipaddr().family() == AF_INET) {
93 prefix_length = kFakeIPv4NetworkPrefixLength;
94 } else if (it->first.ipaddr().family() == AF_INET6) {
95 prefix_length = kFakeIPv6NetworkPrefixLength;
96 }
97 IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length);
98 std::unique_ptr<Network> net(new Network(it->first.hostname(),
99 it->first.hostname(), prefix,
100 prefix_length, it->second));
101 net->set_default_local_address_provider(this);
102 net->AddIP(it->first.ipaddr());
103 networks.push_back(net.release());
104 }
105 bool changed;
106 MergeNetworkList(networks, &changed);
107 if (changed || !sent_first_update_) {
108 SignalNetworksChanged();
109 sent_first_update_ = true;
110 }
111 }
112
113 IfaceList ifaces_;
114 int next_index_ = 0;
115 int start_count_ = 0;
116 bool sent_first_update_ = false;
117
118 IPAddress default_local_ipv4_address_;
119 IPAddress default_local_ipv6_address_;
120};
121
122} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // RTC_BASE_FAKENETWORK_H_