blob: fd4eb615bc738cf59b3db04dac8a10d4ea61d0e3 [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.
31class FakeNetworkManager : public NetworkManagerBase,
32 public MessageHandler {
33 public:
34 FakeNetworkManager() {}
35
36 typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList;
37
38 void AddInterface(const SocketAddress& iface) {
39 // Ensure a unique name for the interface if its name is not given.
40 AddInterface(iface, "test" + rtc::ToString(next_index_++));
41 }
42
43 void AddInterface(const SocketAddress& iface, const std::string& if_name) {
44 AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN);
45 }
46
47 void AddInterface(const SocketAddress& iface,
48 const std::string& if_name,
49 AdapterType type) {
50 SocketAddress address(if_name, 0);
51 address.SetResolvedIP(iface.ipaddr());
52 ifaces_.push_back(std::make_pair(address, type));
53 DoUpdateNetworks();
54 }
55
56 void RemoveInterface(const SocketAddress& iface) {
57 for (IfaceList::iterator it = ifaces_.begin();
58 it != ifaces_.end(); ++it) {
59 if (it->first.EqualIPs(iface)) {
60 ifaces_.erase(it);
61 break;
62 }
63 }
64 DoUpdateNetworks();
65 }
66
67 virtual void StartUpdating() {
68 ++start_count_;
69 if (start_count_ == 1) {
70 sent_first_update_ = false;
71 rtc::Thread::Current()->Post(RTC_FROM_HERE, this);
72 } else {
73 if (sent_first_update_) {
74 SignalNetworksChanged();
75 }
76 }
77 }
78
79 virtual void StopUpdating() { --start_count_; }
80
81 // MessageHandler interface.
82 virtual void OnMessage(Message* msg) {
83 DoUpdateNetworks();
84 }
85
86 using NetworkManagerBase::set_enumeration_permission;
87 using NetworkManagerBase::set_default_local_addresses;
88
89 private:
90 void DoUpdateNetworks() {
91 if (start_count_ == 0)
92 return;
93 std::vector<Network*> networks;
94 for (IfaceList::iterator it = ifaces_.begin();
95 it != ifaces_.end(); ++it) {
96 int prefix_length = 0;
97 if (it->first.ipaddr().family() == AF_INET) {
98 prefix_length = kFakeIPv4NetworkPrefixLength;
99 } else if (it->first.ipaddr().family() == AF_INET6) {
100 prefix_length = kFakeIPv6NetworkPrefixLength;
101 }
102 IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length);
103 std::unique_ptr<Network> net(new Network(it->first.hostname(),
104 it->first.hostname(), prefix,
105 prefix_length, it->second));
106 net->set_default_local_address_provider(this);
107 net->AddIP(it->first.ipaddr());
108 networks.push_back(net.release());
109 }
110 bool changed;
111 MergeNetworkList(networks, &changed);
112 if (changed || !sent_first_update_) {
113 SignalNetworksChanged();
114 sent_first_update_ = true;
115 }
116 }
117
118 IfaceList ifaces_;
119 int next_index_ = 0;
120 int start_count_ = 0;
121 bool sent_first_update_ = false;
122
123 IPAddress default_local_ipv4_address_;
124 IPAddress default_local_ipv6_address_;
125};
126
127} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // RTC_BASE_FAKENETWORK_H_