You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.7 KiB

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