Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, 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_NETWORK_H_
     29 #define TALK_BASE_NETWORK_H_
     30 
     31 #include <deque>
     32 #include <map>
     33 #include <string>
     34 #include <vector>
     35 
     36 #include "talk/base/basictypes.h"
     37 #include "talk/base/ipaddress.h"
     38 #include "talk/base/messagehandler.h"
     39 #include "talk/base/sigslot.h"
     40 
     41 #if defined(POSIX)
     42 struct ifaddrs;
     43 #endif  // defined(POSIX)
     44 
     45 namespace talk_base {
     46 
     47 class Network;
     48 class NetworkSession;
     49 class Thread;
     50 
     51 // Generic network manager interface. It provides list of local
     52 // networks.
     53 class NetworkManager {
     54  public:
     55   typedef std::vector<Network*> NetworkList;
     56 
     57   NetworkManager();
     58   virtual ~NetworkManager();
     59 
     60   // Called when network list is updated.
     61   sigslot::signal0<> SignalNetworksChanged;
     62 
     63   // Indicates a failure when getting list of network interfaces.
     64   sigslot::signal0<> SignalError;
     65 
     66   // Start/Stop monitoring of network interfaces
     67   // list. SignalNetworksChanged or SignalError is emitted immidiately
     68   // after StartUpdating() is called. After that SignalNetworksChanged
     69   // is emitted wheneven list of networks changes.
     70   virtual void StartUpdating() = 0;
     71   virtual void StopUpdating() = 0;
     72 
     73   // Returns the current list of networks available on this machine.
     74   // UpdateNetworks() must be called before this method is called.
     75   // It makes sure that repeated calls return the same object for a
     76   // given network, so that quality is tracked appropriately. Does not
     77   // include ignored networks.
     78   virtual void GetNetworks(NetworkList* networks) const = 0;
     79 
     80   // Dumps a list of networks available to LS_INFO.
     81   virtual void DumpNetworks(bool include_ignored) {}
     82 };
     83 
     84 // Base class for NetworkManager implementations.
     85 class NetworkManagerBase : public NetworkManager {
     86  public:
     87   NetworkManagerBase();
     88   virtual ~NetworkManagerBase();
     89 
     90   virtual void GetNetworks(std::vector<Network*>* networks) const;
     91   bool ipv6_enabled() const { return ipv6_enabled_; }
     92   void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
     93 
     94  protected:
     95   typedef std::map<std::string, Network*> NetworkMap;
     96   // Updates |networks_| with the networks listed in |list|. If
     97   // |network_map_| already has a Network object for a network listed
     98   // in the |list| then it is reused. Accept ownership of the Network
     99   // objects in the |list|. |changed| will be set to true if there is
    100   // any change in the network list.
    101   void MergeNetworkList(const NetworkList& list, bool* changed);
    102 
    103  private:
    104   friend class NetworkTest;
    105   void DoUpdateNetworks();
    106 
    107   NetworkList networks_;
    108   NetworkMap networks_map_;
    109   bool ipv6_enabled_;
    110 };
    111 
    112 // Basic implementation of the NetworkManager interface that gets list
    113 // of networks using OS APIs.
    114 class BasicNetworkManager : public NetworkManagerBase,
    115                             public MessageHandler {
    116  public:
    117   BasicNetworkManager();
    118   virtual ~BasicNetworkManager();
    119 
    120   virtual void StartUpdating();
    121   virtual void StopUpdating();
    122 
    123   // Logs the available networks.
    124   virtual void DumpNetworks(bool include_ignored);
    125 
    126   // MessageHandler interface.
    127   virtual void OnMessage(Message* msg);
    128   bool started() { return start_count_ > 0; }
    129 
    130   // Sets the network ignore list, which is empty by default. Any network on
    131   // the ignore list will be filtered from network enumeration results.
    132   void set_network_ignore_list(const std::vector<std::string>& list) {
    133     network_ignore_list_ = list;
    134   }
    135 #if defined(ANDROID) || defined(LINUX)
    136   // Sets the flag for ignoring non-default routes.
    137   void set_ignore_non_default_routes(bool value) {
    138     ignore_non_default_routes_ = true;
    139   }
    140 #endif
    141 
    142  protected:
    143 #if defined(POSIX)
    144   // Separated from CreateNetworks for tests.
    145   void ConvertIfAddrs(ifaddrs* interfaces,
    146                       bool include_ignored,
    147                       NetworkList* networks) const;
    148 #endif  // defined(POSIX)
    149 
    150   // Creates a network object for each network available on the machine.
    151   bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
    152 
    153   // Determines if a network should be ignored.
    154   bool IsIgnoredNetwork(const Network& network) const;
    155 
    156  private:
    157   friend class NetworkTest;
    158 
    159   void DoUpdateNetworks();
    160 
    161   Thread* thread_;
    162   bool sent_first_update_;
    163   int start_count_;
    164   std::vector<std::string> network_ignore_list_;
    165   bool ignore_non_default_routes_;
    166 };
    167 
    168 // Represents a Unix-type network interface, with a name and single address.
    169 class Network {
    170  public:
    171   Network() : prefix_(INADDR_ANY), scope_id_(0) {}
    172   Network(const std::string& name, const std::string& description,
    173           const IPAddress& prefix, int prefix_length);
    174 
    175   // Returns the name of the interface this network is associated wtih.
    176   const std::string& name() const { return name_; }
    177 
    178   // Returns the OS-assigned name for this network. This is useful for
    179   // debugging but should not be sent over the wire (for privacy reasons).
    180   const std::string& description() const { return description_; }
    181 
    182   // Returns the prefix for this network.
    183   const IPAddress& prefix() const { return prefix_; }
    184   // Returns the length, in bits, of this network's prefix.
    185   int prefix_length() const { return prefix_length_; }
    186 
    187   // Returns the Network's current idea of the 'best' IP it has.
    188   // 'Best' currently means the first one added.
    189   // TODO: We should be preferring temporary addresses.
    190   // Returns an unset IP if this network has no active addresses.
    191   IPAddress ip() const {
    192     if (ips_.size() == 0) {
    193       return IPAddress();
    194     }
    195     return ips_.at(0);
    196   }
    197   // Adds an active IP address to this network. Does not check for duplicates.
    198   void AddIP(const IPAddress& ip) { ips_.push_back(ip); }
    199 
    200   // Sets the network's IP address list. Returns true if new IP addresses were
    201   // detected. Passing true to already_changed skips this check.
    202   bool SetIPs(const std::vector<IPAddress>& ips, bool already_changed);
    203   // Get the list of IP Addresses associated with this network.
    204   const std::vector<IPAddress>& GetIPs() { return ips_;}
    205   // Clear the network's list of addresses.
    206   void ClearIPs() { ips_.clear(); }
    207 
    208   // Returns the scope-id of the network's address.
    209   // Should only be relevant for link-local IPv6 addresses.
    210   int scope_id() const { return scope_id_; }
    211   void set_scope_id(int id) { scope_id_ = id; }
    212 
    213   // Indicates whether this network should be ignored, perhaps because
    214   // the IP is 0, or the interface is one we know is invalid.
    215   bool ignored() const { return ignored_; }
    216   void set_ignored(bool ignored) { ignored_ = ignored; }
    217 
    218   // Debugging description of this network
    219   std::string ToString() const;
    220 
    221  private:
    222   typedef std::vector<NetworkSession*> SessionList;
    223 
    224   std::string name_;
    225   std::string description_;
    226   IPAddress prefix_;
    227   int prefix_length_;
    228   std::vector<IPAddress> ips_;
    229   int scope_id_;
    230   bool ignored_;
    231   SessionList sessions_;
    232   double uniform_numerator_;
    233   double uniform_denominator_;
    234   double exponential_numerator_;
    235   double exponential_denominator_;
    236 
    237   friend class NetworkManager;
    238 };
    239 }  // namespace talk_base
    240 
    241 #endif  // TALK_BASE_NETWORK_H_
    242