~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/base/network.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
namespace talk_base {
 
42
 
 
43
class Network;
 
44
class NetworkSession;
 
45
class Thread;
 
46
 
 
47
// Generic network manager interface. It provides list of local
 
48
// networks.
 
49
class NetworkManager {
 
50
 public:
 
51
  typedef std::vector<Network*> NetworkList;
 
52
 
 
53
  NetworkManager();
 
54
  virtual ~NetworkManager();
 
55
 
 
56
  // Called when network list is updated.
 
57
  sigslot::signal0<> SignalNetworksChanged;
 
58
 
 
59
  // Indicates a failure when getting list of network interfaces.
 
60
  sigslot::signal0<> SignalError;
 
61
 
 
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;
 
68
 
 
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;
 
75
 
 
76
  // Dumps a list of networks available to LS_INFO.
 
77
  virtual void DumpNetworks(bool include_ignored) {}
 
78
};
 
79
 
 
80
// Base class for NetworkManager implementations.
 
81
class NetworkManagerBase : public NetworkManager {
 
82
 public:
 
83
  NetworkManagerBase();
 
84
  virtual ~NetworkManagerBase();
 
85
 
 
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; }
 
89
 
 
90
 protected:
 
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);
 
98
 
 
99
 private:
 
100
  friend class NetworkTest;
 
101
  void DoUpdateNetworks();
 
102
 
 
103
  NetworkList networks_;
 
104
  NetworkMap networks_map_;
 
105
  bool ipv6_enabled_;
 
106
};
 
107
 
 
108
// Basic implementation of the NetworkManager interface that gets list
 
109
// of networks using OS APIs.
 
110
class BasicNetworkManager : public NetworkManagerBase,
 
111
                            public MessageHandler {
 
112
 public:
 
113
  BasicNetworkManager();
 
114
  virtual ~BasicNetworkManager();
 
115
 
 
116
  virtual void StartUpdating();
 
117
  virtual void StopUpdating();
 
118
 
 
119
  // Logs the available networks.
 
120
  virtual void DumpNetworks(bool include_ignored);
 
121
 
 
122
  // MessageHandler interface.
 
123
  virtual void OnMessage(Message* msg);
 
124
  bool started() { return start_count_ > 0; }
 
125
 
 
126
 protected:
 
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);
 
131
 
 
132
 private:
 
133
  friend class NetworkTest;
 
134
 
 
135
  void DoUpdateNetworks();
 
136
 
 
137
  Thread* thread_;
 
138
  bool sent_first_update_;
 
139
  int start_count_;
 
140
};
 
141
 
 
142
// Represents a Unix-type network interface, with a name and single address.
 
143
class Network {
 
144
 public:
 
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);
 
148
 
 
149
  // Returns the name of the interface this network is associated wtih.
 
150
  const std::string& name() const { return name_; }
 
151
 
 
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_; }
 
155
 
 
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_; }
 
160
 
 
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) {
 
167
      return IPAddress();
 
168
    }
 
169
    return ips_.at(0);
 
170
  }
 
171
  // Adds an active IP address to this network. Does not check for duplicates.
 
172
  void AddIP(const IPAddress& ip) { ips_.push_back(ip); }
 
173
 
 
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(); }
 
181
 
 
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; }
 
186
 
 
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; }
 
191
 
 
192
  // Debugging description of this network
 
193
  std::string ToString() const;
 
194
 
 
195
 private:
 
196
  typedef std::vector<NetworkSession*> SessionList;
 
197
 
 
198
  std::string name_;
 
199
  std::string description_;
 
200
  IPAddress prefix_;
 
201
  int prefix_length_;
 
202
  std::vector<IPAddress> ips_;
 
203
  int scope_id_;
 
204
  bool ignored_;
 
205
  SessionList sessions_;
 
206
  double uniform_numerator_;
 
207
  double uniform_denominator_;
 
208
  double exponential_numerator_;
 
209
  double exponential_denominator_;
 
210
 
 
211
  friend class NetworkManager;
 
212
};
 
213
}  // namespace talk_base
 
214
 
 
215
#endif  // TALK_BASE_NETWORK_H_