Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2009 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      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.
     15  *
     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.
     26  */
     27 
     28 #ifndef TALK_BASE_FAKENETWORK_H_
     29 #define TALK_BASE_FAKENETWORK_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     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"
     39 
     40 namespace talk_base {
     41 
     42 const int kFakeIPv4NetworkPrefixLength = 24;
     43 const int kFakeIPv6NetworkPrefixLength = 64;
     44 
     45 // Fake network manager that allows us to manually specify the IPs to use.
     46 class FakeNetworkManager : public NetworkManagerBase,
     47                            public MessageHandler {
     48  public:
     49   FakeNetworkManager()
     50       : thread_(Thread::Current()),
     51         next_index_(0),
     52         started_(false),
     53         sent_first_update_(false) {
     54   }
     55 
     56   typedef std::vector<SocketAddress> IfaceList;
     57 
     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);
     63     DoUpdateNetworks();
     64   }
     65 
     66   void RemoveInterface(const SocketAddress& iface) {
     67     for (IfaceList::iterator it = ifaces_.begin();
     68          it != ifaces_.end(); ++it) {
     69       if (it->EqualIPs(iface)) {
     70         ifaces_.erase(it);
     71         break;
     72       }
     73     }
     74     DoUpdateNetworks();
     75   }
     76 
     77   virtual void StartUpdating() {
     78     if (started_) {
     79       if (sent_first_update_)
     80         SignalNetworksChanged();
     81       return;
     82     }
     83 
     84     started_ = true;
     85     sent_first_update_ = false;
     86     thread_->Post(this);
     87   }
     88 
     89   virtual void StopUpdating() {
     90     started_ = false;
     91   }
     92 
     93   // MessageHandler interface.
     94   virtual void OnMessage(Message* msg) {
     95     DoUpdateNetworks();
     96   }
     97 
     98  private:
     99   void DoUpdateNetworks() {
    100     if (!started_)
    101       return;
    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;
    110       }
    111       IPAddress prefix = TruncateIP(it->ipaddr(), prefix_length);
    112       scoped_ptr<Network> net(new Network(it->hostname(),
    113                                           it->hostname(),
    114                                           prefix,
    115                                           prefix_length));
    116       net->AddIP(it->ipaddr());
    117       networks.push_back(net.release());
    118     }
    119     bool changed;
    120     MergeNetworkList(networks, &changed);
    121     if (changed || !sent_first_update_) {
    122       SignalNetworksChanged();
    123       sent_first_update_ = true;
    124     }
    125   }
    126 
    127   Thread* thread_;
    128   IfaceList ifaces_;
    129   int next_index_;
    130   bool started_;
    131   bool sent_first_update_;
    132 };
    133 
    134 }  // namespace talk_base
    135 
    136 #endif  // TALK_BASE_FAKENETWORK_H_
    137