3 * Copyright 2004--2005, 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_NETWORK_H_
29 #define TALK_BASE_NETWORK_H_
36 #include "talk/base/basictypes.h"
37 #include "talk/base/ipaddress.h"
38 #include "talk/base/messagehandler.h"
39 #include "talk/base/sigslot.h"
43 #endif // defined(POSIX)
51 // Generic network manager interface. It provides list of local
53 class NetworkManager {
55 typedef std::vector<Network*> NetworkList;
58 virtual ~NetworkManager();
60 // Called when network list is updated.
61 sigslot::signal0<> SignalNetworksChanged;
63 // Indicates a failure when getting list of network interfaces.
64 sigslot::signal0<> SignalError;
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;
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;
80 // Dumps a list of networks available to LS_INFO.
81 virtual void DumpNetworks(bool include_ignored) {}
84 // Base class for NetworkManager implementations.
85 class NetworkManagerBase : public NetworkManager {
88 virtual ~NetworkManagerBase();
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; }
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);
104 friend class NetworkTest;
105 void DoUpdateNetworks();
107 NetworkList networks_;
108 NetworkMap networks_map_;
112 // Basic implementation of the NetworkManager interface that gets list
113 // of networks using OS APIs.
114 class BasicNetworkManager : public NetworkManagerBase,
115 public MessageHandler {
117 BasicNetworkManager();
118 virtual ~BasicNetworkManager();
120 virtual void StartUpdating();
121 virtual void StopUpdating();
123 // Logs the available networks.
124 virtual void DumpNetworks(bool include_ignored);
126 // MessageHandler interface.
127 virtual void OnMessage(Message* msg);
128 bool started() { return start_count_ > 0; }
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;
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;
144 // Separated from CreateNetworks for tests.
145 void ConvertIfAddrs(ifaddrs* interfaces,
146 bool include_ignored,
147 NetworkList* networks) const;
148 #endif // defined(POSIX)
150 // Creates a network object for each network available on the machine.
151 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
153 // Determines if a network should be ignored.
154 bool IsIgnoredNetwork(const Network& network) const;
157 friend class NetworkTest;
159 void DoUpdateNetworks();
162 bool sent_first_update_;
164 std::vector<std::string> network_ignore_list_;
165 bool ignore_non_default_routes_;
168 // Represents a Unix-type network interface, with a name and single address.
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);
175 // Returns the name of the interface this network is associated wtih.
176 const std::string& name() const { return name_; }
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_; }
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_; }
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) {
197 // Adds an active IP address to this network. Does not check for duplicates.
198 void AddIP(const IPAddress& ip) { ips_.push_back(ip); }
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(); }
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; }
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; }
218 // Debugging description of this network
219 std::string ToString() const;
222 typedef std::vector<NetworkSession*> SessionList;
225 std::string description_;
228 std::vector<IPAddress> ips_;
231 SessionList sessions_;
232 double uniform_numerator_;
233 double uniform_denominator_;
234 double exponential_numerator_;
235 double exponential_denominator_;
237 friend class NetworkManager;
239 } // namespace talk_base
241 #endif // TALK_BASE_NETWORK_H_