blob: 108e738468db6bf26a466116f2f3dc5d219019ab [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
11#ifndef WEBRTC_BASE_FAKENETWORK_H_
12#define WEBRTC_BASE_FAKENETWORK_H_
13
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <string>
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080016#include <utility>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include <vector>
18
19#include "webrtc/base/network.h"
20#include "webrtc/base/messagehandler.h"
21#include "webrtc/base/socketaddress.h"
22#include "webrtc/base/stringencode.h"
23#include "webrtc/base/thread.h"
24
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:
Taylor Brandstettere5835f52016-09-16 15:07:50 -070034 FakeNetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080036 typedef std::vector<std::pair<SocketAddress, AdapterType>> IfaceList;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037
38 void AddInterface(const SocketAddress& iface) {
honghaiz8c404fa2015-09-28 07:59:43 -070039 // 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) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080044 AddInterface(iface, if_name, ADAPTER_TYPE_UNKNOWN);
45 }
46
47 void AddInterface(const SocketAddress& iface,
48 const std::string& if_name,
49 AdapterType type) {
honghaiz8c404fa2015-09-28 07:59:43 -070050 SocketAddress address(if_name, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051 address.SetResolvedIP(iface.ipaddr());
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080052 ifaces_.push_back(std::make_pair(address, type));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053 DoUpdateNetworks();
54 }
55
56 void RemoveInterface(const SocketAddress& iface) {
57 for (IfaceList::iterator it = ifaces_.begin();
58 it != ifaces_.end(); ++it) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080059 if (it->first.EqualIPs(iface)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060 ifaces_.erase(it);
61 break;
62 }
63 }
64 DoUpdateNetworks();
65 }
66
67 virtual void StartUpdating() {
deadbeefcbecd352015-09-23 11:50:27 -070068 ++start_count_;
69 if (start_count_ == 1) {
70 sent_first_update_ = false;
Taylor Brandstettere5835f52016-09-16 15:07:50 -070071 rtc::Thread::Current()->Post(RTC_FROM_HERE, this);
deadbeefcbecd352015-09-23 11:50:27 -070072 } else {
73 if (sent_first_update_) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 SignalNetworksChanged();
deadbeefcbecd352015-09-23 11:50:27 -070075 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000077 }
78
deadbeefcbecd352015-09-23 11:50:27 -070079 virtual void StopUpdating() { --start_count_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080
81 // MessageHandler interface.
82 virtual void OnMessage(Message* msg) {
83 DoUpdateNetworks();
84 }
85
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070086 using NetworkManagerBase::set_enumeration_permission;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080087 using NetworkManagerBase::set_default_local_addresses;
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070088
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 private:
90 void DoUpdateNetworks() {
deadbeefcbecd352015-09-23 11:50:27 -070091 if (start_count_ == 0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 return;
93 std::vector<Network*> networks;
94 for (IfaceList::iterator it = ifaces_.begin();
95 it != ifaces_.end(); ++it) {
96 int prefix_length = 0;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080097 if (it->first.ipaddr().family() == AF_INET) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098 prefix_length = kFakeIPv4NetworkPrefixLength;
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080099 } else if (it->first.ipaddr().family() == AF_INET6) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 prefix_length = kFakeIPv6NetworkPrefixLength;
101 }
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800102 IPAddress prefix = TruncateIP(it->first.ipaddr(), prefix_length);
jbauch555604a2016-04-26 03:13:22 -0700103 std::unique_ptr<Network> net(new Network(it->first.hostname(),
104 it->first.hostname(), prefix,
105 prefix_length, it->second));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800106 net->set_default_local_address_provider(this);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800107 net->AddIP(it->first.ipaddr());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118 IfaceList ifaces_;
deadbeefcbecd352015-09-23 11:50:27 -0700119 int next_index_ = 0;
120 int start_count_ = 0;
121 bool sent_first_update_ = false;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800122
123 IPAddress default_local_ipv4_address_;
124 IPAddress default_local_ipv6_address_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125};
126
127} // namespace rtc
128
129#endif // WEBRTC_BASE_FAKENETWORK_H_