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"
47
// Generic network manager interface. It provides list of local
49
class NetworkManager {
51
typedef std::vector<Network*> NetworkList;
54
virtual ~NetworkManager();
56
// Called when network list is updated.
57
sigslot::signal0<> SignalNetworksChanged;
59
// Indicates a failure when getting list of network interfaces.
60
sigslot::signal0<> SignalError;
62
// Start/Stop monitoring of network interfaces
63
// list. SignalNetworksChanged or SignalError is emitted immidiately
64
// after StartUpdating() is called. After that SignalNetworksChanged
65
// is emitted wheneven list of networks changes.
66
virtual void StartUpdating() = 0;
67
virtual void StopUpdating() = 0;
69
// Returns the current list of networks available on this machine.
70
// UpdateNetworks() must be called before this method is called.
71
// It makes sure that repeated calls return the same object for a
72
// given network, so that quality is tracked appropriately. Does not
73
// include ignored networks.
74
virtual void GetNetworks(NetworkList* networks) const = 0;
76
// Dumps a list of networks available to LS_INFO.
77
virtual void DumpNetworks(bool include_ignored) {}
80
// Base class for NetworkManager implementations.
81
class NetworkManagerBase : public NetworkManager {
84
virtual ~NetworkManagerBase();
86
virtual void GetNetworks(std::vector<Network*>* networks) const;
87
bool ipv6_enabled() const { return ipv6_enabled_; }
88
void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
91
typedef std::map<std::string, Network*> NetworkMap;
92
// Updates |networks_| with the networks listed in |list|. If
93
// |network_map_| already has a Network object for a network listed
94
// in the |list| then it is reused. Accept ownership of the Network
95
// objects in the |list|. |changed| will be set to true if there is
96
// any change in the network list.
97
void MergeNetworkList(const NetworkList& list, bool* changed);
100
friend class NetworkTest;
101
void DoUpdateNetworks();
103
NetworkList networks_;
104
NetworkMap networks_map_;
108
// Basic implementation of the NetworkManager interface that gets list
109
// of networks using OS APIs.
110
class BasicNetworkManager : public NetworkManagerBase,
111
public MessageHandler {
113
BasicNetworkManager();
114
virtual ~BasicNetworkManager();
116
virtual void StartUpdating();
117
virtual void StopUpdating();
119
// Logs the available networks.
120
virtual void DumpNetworks(bool include_ignored);
122
// MessageHandler interface.
123
virtual void OnMessage(Message* msg);
124
bool started() { return start_count_ > 0; }
127
// Creates a network object for each network available on the machine.
128
bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
129
// Determines if a network should be ignored.
130
static bool IsIgnoredNetwork(const Network& network);
133
friend class NetworkTest;
135
void DoUpdateNetworks();
138
bool sent_first_update_;
142
// Represents a Unix-type network interface, with a name and single address.
145
Network() : prefix_(INADDR_ANY), scope_id_(0) {}
146
Network(const std::string& name, const std::string& description,
147
const IPAddress& prefix, int prefix_length);
149
// Returns the name of the interface this network is associated wtih.
150
const std::string& name() const { return name_; }
152
// Returns the OS-assigned name for this network. This is useful for
153
// debugging but should not be sent over the wire (for privacy reasons).
154
const std::string& description() const { return description_; }
156
// Returns the prefix for this network.
157
const IPAddress& prefix() const { return prefix_; }
158
// Returns the length, in bits, of this network's prefix.
159
int prefix_length() const { return prefix_length_; }
161
// Returns the Network's current idea of the 'best' IP it has.
162
// 'Best' currently means the first one added.
163
// TODO: We should be preferring temporary addresses.
164
// Returns an unset IP if this network has no active addresses.
165
IPAddress ip() const {
166
if (ips_.size() == 0) {
171
// Adds an active IP address to this network. Does not check for duplicates.
172
void AddIP(const IPAddress& ip) { ips_.push_back(ip); }
174
// Sets the network's IP address list. Returns true if new IP addresses were
175
// detected. Passing true to already_changed skips this check.
176
bool SetIPs(const std::vector<IPAddress>& ips, bool already_changed);
177
// Get the list of IP Addresses associated with this network.
178
const std::vector<IPAddress>& GetIPs() { return ips_;}
179
// Clear the network's list of addresses.
180
void ClearIPs() { ips_.clear(); }
182
// Returns the scope-id of the network's address.
183
// Should only be relevant for link-local IPv6 addresses.
184
int scope_id() const { return scope_id_; }
185
void set_scope_id(int id) { scope_id_ = id; }
187
// Indicates whether this network should be ignored, perhaps because
188
// the IP is 0, or the interface is one we know is invalid.
189
bool ignored() const { return ignored_; }
190
void set_ignored(bool ignored) { ignored_ = ignored; }
192
// Debugging description of this network
193
std::string ToString() const;
196
typedef std::vector<NetworkSession*> SessionList;
199
std::string description_;
202
std::vector<IPAddress> ips_;
205
SessionList sessions_;
206
double uniform_numerator_;
207
double uniform_denominator_;
208
double exponential_numerator_;
209
double exponential_denominator_;
211
friend class NetworkManager;
213
} // namespace talk_base
215
#endif // TALK_BASE_NETWORK_H_