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 protected: 131 #if defined(POSIX) 132 // Separated from CreateNetworks for tests. 133 void ConvertIfAddrs(ifaddrs* interfaces, 134 bool include_ignored, 135 NetworkList* networks) const; 136 #endif // defined(POSIX) 137 138 // Creates a network object for each network available on the machine. 139 bool CreateNetworks(bool include_ignored, NetworkList* networks) const; 140 141 // Determines if a network should be ignored. 142 static bool IsIgnoredNetwork(const Network& network); 143 144 private: 145 friend class NetworkTest; 146 147 void DoUpdateNetworks(); 148 149 Thread* thread_; 150 bool sent_first_update_; 151 int start_count_; 152 }; 153 154 // Represents a Unix-type network interface, with a name and single address. 155 class Network { 156 public: 157 Network() : prefix_(INADDR_ANY), scope_id_(0) {} 158 Network(const std::string& name, const std::string& description, 159 const IPAddress& prefix, int prefix_length); 160 161 // Returns the name of the interface this network is associated wtih. 162 const std::string& name() const { return name_; } 163 164 // Returns the OS-assigned name for this network. This is useful for 165 // debugging but should not be sent over the wire (for privacy reasons). 166 const std::string& description() const { return description_; } 167 168 // Returns the prefix for this network. 169 const IPAddress& prefix() const { return prefix_; } 170 // Returns the length, in bits, of this network's prefix. 171 int prefix_length() const { return prefix_length_; } 172 173 // Returns the Network's current idea of the 'best' IP it has. 174 // 'Best' currently means the first one added. 175 // TODO: We should be preferring temporary addresses. 176 // Returns an unset IP if this network has no active addresses. 177 IPAddress ip() const { 178 if (ips_.size() == 0) { 179 return IPAddress(); 180 } 181 return ips_.at(0); 182 } 183 // Adds an active IP address to this network. Does not check for duplicates. 184 void AddIP(const IPAddress& ip) { ips_.push_back(ip); } 185 186 // Sets the network's IP address list. Returns true if new IP addresses were 187 // detected. Passing true to already_changed skips this check. 188 bool SetIPs(const std::vector<IPAddress>& ips, bool already_changed); 189 // Get the list of IP Addresses associated with this network. 190 const std::vector<IPAddress>& GetIPs() { return ips_;} 191 // Clear the network's list of addresses. 192 void ClearIPs() { ips_.clear(); } 193 194 // Returns the scope-id of the network's address. 195 // Should only be relevant for link-local IPv6 addresses. 196 int scope_id() const { return scope_id_; } 197 void set_scope_id(int id) { scope_id_ = id; } 198 199 // Indicates whether this network should be ignored, perhaps because 200 // the IP is 0, or the interface is one we know is invalid. 201 bool ignored() const { return ignored_; } 202 void set_ignored(bool ignored) { ignored_ = ignored; } 203 204 // Debugging description of this network 205 std::string ToString() const; 206 207 private: 208 typedef std::vector<NetworkSession*> SessionList; 209 210 std::string name_; 211 std::string description_; 212 IPAddress prefix_; 213 int prefix_length_; 214 std::vector<IPAddress> ips_; 215 int scope_id_; 216 bool ignored_; 217 SessionList sessions_; 218 double uniform_numerator_; 219 double uniform_denominator_; 220 double exponential_numerator_; 221 double exponential_denominator_; 222 223 friend class NetworkManager; 224 }; 225 } // namespace talk_base 226 227 #endif // TALK_BASE_NETWORK_H_ 228