3 * Copyright 2009 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_FAKENETWORK_H_
29 #define TALK_BASE_FAKENETWORK_H_
34 #include "talk/base/network.h"
35 #include "talk/base/messagehandler.h"
36 #include "talk/base/socketaddress.h"
37 #include "talk/base/stringencode.h"
38 #include "talk/base/thread.h"
42 const int kFakeIPv4NetworkPrefixLength = 24;
43 const int kFakeIPv6NetworkPrefixLength = 64;
45 // Fake network manager that allows us to manually specify the IPs to use.
46 class FakeNetworkManager : public NetworkManagerBase,
47 public MessageHandler {
50 : thread_(Thread::Current()),
53 sent_first_update_(false) {
56 typedef std::vector<SocketAddress> IfaceList;
58 void AddInterface(const SocketAddress& iface) {
59 // ensure a unique name for the interface
60 SocketAddress address("test" + talk_base::ToString(next_index_++), 0);
61 address.SetResolvedIP(iface.ipaddr());
62 ifaces_.push_back(address);
66 void RemoveInterface(const SocketAddress& iface) {
67 for (IfaceList::iterator it = ifaces_.begin();
68 it != ifaces_.end(); ++it) {
69 if (it->EqualIPs(iface)) {
77 virtual void StartUpdating() {
79 if (sent_first_update_)
80 SignalNetworksChanged();
85 sent_first_update_ = false;
89 virtual void StopUpdating() {
93 // MessageHandler interface.
94 virtual void OnMessage(Message* msg) {
99 void DoUpdateNetworks() {
102 std::vector<Network*> networks;
103 for (IfaceList::iterator it = ifaces_.begin();
104 it != ifaces_.end(); ++it) {
105 int prefix_length = 0;
106 if (it->ipaddr().family() == AF_INET) {
107 prefix_length = kFakeIPv4NetworkPrefixLength;
108 } else if (it->ipaddr().family() == AF_INET6) {
109 prefix_length = kFakeIPv6NetworkPrefixLength;
111 IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length);
112 scoped_ptr<Network> net(new Network(it->hostname(),
116 net->AddIP(it->ipaddr());
117 networks.push_back(net.release());
120 MergeNetworkList(networks, &changed);
121 if (changed || !sent_first_update_) {
122 SignalNetworksChanged();
123 sent_first_update_ = true;
131 bool sent_first_update_;
134 } // namespace talk_base
136 #endif // TALK_BASE_FAKENETWORK_H_