Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 5 #ifndef NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 6 #define NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| 7 | 7 |
| 8 #include <sys/socket.h> // Needed to include netlink. | 8 #include <sys/socket.h> // Needed to include netlink. |
| 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. | 9 // Mask superfluous definition of |struct net|. This is fixed in Linux 2.6.38. |
| 10 #define net net_kernel | 10 #define net net_kernel |
| 11 #include <linux/rtnetlink.h> | 11 #include <linux/rtnetlink.h> |
| 12 #undef net | 12 #undef net |
| 13 | 13 |
| 14 #include <map> | 14 #include <map> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/containers/hash_tables.h" | 19 #include "base/containers/hash_tables.h" |
| 20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/synchronization/condition_variable.h" | 21 #include "base/synchronization/condition_variable.h" |
| 22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 23 #include "base/threading/thread_checker.h" | |
| 23 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 24 #include "net/base/network_change_notifier.h" | 25 #include "net/base/network_change_notifier.h" |
| 25 | 26 |
| 26 namespace net { | 27 namespace net { |
| 27 namespace internal { | 28 namespace internal { |
| 28 | 29 |
| 29 // Keeps track of network interface addresses using rtnetlink. Used by | 30 // Keeps track of network interface addresses using rtnetlink. Used by |
| 30 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. | 31 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. |
| 31 class NET_EXPORT_PRIVATE AddressTrackerLinux : | 32 class NET_EXPORT_PRIVATE AddressTrackerLinux : |
| 32 public base::MessageLoopForIO::Watcher { | 33 public base::MessageLoopForIO::Watcher { |
| 33 public: | 34 public: |
| 34 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; | 35 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; |
| 35 | 36 |
| 36 // Will run |address_callback| when the AddressMap changes, |link_callback| | 37 // Non-tracking version constructor: it takes a snapshot of current |
| 37 // when the list of online links changes, and |tunnel_callback| when the list | 38 // system configuration once Init() returns. The configuration is |
|
pauljensen
2014/09/19 19:25:39
This is incorrect, the snapshot is not taken when
guoweis2
2014/09/19 22:12:55
Done.
| |
| 38 // of online tunnels changes. | 39 // available through GetOnlineLinks() and GetAddressMap(). |
| 40 AddressTrackerLinux(); | |
| 41 | |
| 42 // Tracking version constructor: it will run |address_callback| when | |
| 43 // the AddressMap changes, |link_callback| when the list of online | |
| 44 // links changes, and |tunnel_callback| when the list of online | |
| 45 // tunnels changes. | |
| 39 AddressTrackerLinux(const base::Closure& address_callback, | 46 AddressTrackerLinux(const base::Closure& address_callback, |
| 40 const base::Closure& link_callback, | 47 const base::Closure& link_callback, |
| 41 const base::Closure& tunnel_callback); | 48 const base::Closure& tunnel_callback); |
| 42 virtual ~AddressTrackerLinux(); | 49 virtual ~AddressTrackerLinux(); |
| 43 | 50 |
| 44 // Starts watching system configuration for changes. The current thread must | 51 // In tracking mode, it starts watching system configuration for |
|
pauljensen
2014/09/19 19:25:40
system->the system
guoweis2
2014/09/19 22:12:55
Done.
| |
| 45 // have a MessageLoopForIO. | 52 // changes. The current thread must have a MessageLoopForIO. For |
| 53 // non-tracking mode, the |AddressMap| and |online_links_| will be a | |
| 54 // one time snapshot of current system configuration after Init() | |
| 55 // returns. | |
|
pauljensen
2014/09/19 19:25:40
|AddressMap| is a type but |online_links_| is a pr
guoweis2
2014/09/19 22:12:55
Done.
| |
| 46 void Init(); | 56 void Init(); |
| 47 | 57 |
| 48 AddressMap GetAddressMap() const; | 58 AddressMap GetAddressMap() const; |
| 59 base::hash_set<int> GetOnlineLinks() const; | |
|
pauljensen
2014/09/19 19:25:39
This needs a comment, maybe "Returns set of interf
guoweis2
2014/09/19 22:12:55
Done.
| |
| 49 | 60 |
| 50 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | 61 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
| 51 // Safe to call from any thread, but will block until Init() has completed. | 62 // Safe to call from any thread, but will block until Init() has completed. |
| 52 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | 63 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
| 53 | 64 |
| 54 private: | 65 private: |
| 55 friend class AddressTrackerLinuxTest; | 66 friend class AddressTrackerLinuxTest; |
| 56 | 67 |
| 68 // In tracking mode, holds |lock| while alive. In non-tracking mode, | |
| 69 // enforces single-threaded access. | |
| 70 class AddressTrackerAutoLock { | |
| 71 public: | |
| 72 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, | |
| 73 base::Lock& lock); | |
| 74 ~AddressTrackerAutoLock(); | |
| 75 | |
| 76 private: | |
| 77 const AddressTrackerLinux& tracker_; | |
| 78 base::Lock& lock_; | |
| 79 DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock); | |
| 80 }; | |
| 81 | |
| 57 // A function that returns the name of an interface given the interface index | 82 // A function that returns the name of an interface given the interface index |
| 58 // in |interface_index|. | 83 // in |interface_index|. |
| 59 typedef const char* (*GetInterfaceNameFunction)(int interface_index); | 84 typedef const char* (*GetInterfaceNameFunction)(int interface_index); |
| 60 | 85 |
| 61 // Sets |*address_changed| to indicate whether |address_map_| changed and | 86 // Sets |*address_changed| to indicate whether |address_map_| changed and |
| 62 // sets |*link_changed| to indicate if |online_links_| changed and sets | 87 // sets |*link_changed| to indicate if |online_links_| changed and sets |
| 63 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a | 88 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a |
| 64 // tunnel interface while reading messages from |netlink_fd_|. | 89 // tunnel interface while reading messages from |netlink_fd_|. |
| 65 void ReadMessages(bool* address_changed, | 90 void ReadMessages(bool* address_changed, |
| 66 bool* link_changed, | 91 bool* link_changed, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 base::Closure link_callback_; | 123 base::Closure link_callback_; |
| 99 base::Closure tunnel_callback_; | 124 base::Closure tunnel_callback_; |
| 100 | 125 |
| 101 int netlink_fd_; | 126 int netlink_fd_; |
| 102 base::MessageLoopForIO::FileDescriptorWatcher watcher_; | 127 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
| 103 | 128 |
| 104 mutable base::Lock address_map_lock_; | 129 mutable base::Lock address_map_lock_; |
| 105 AddressMap address_map_; | 130 AddressMap address_map_; |
| 106 | 131 |
| 107 // Set of interface indices for links that are currently online. | 132 // Set of interface indices for links that are currently online. |
| 133 mutable base::Lock online_links_lock_; | |
| 108 base::hash_set<int> online_links_; | 134 base::hash_set<int> online_links_; |
| 109 | 135 |
| 110 base::Lock is_offline_lock_; | 136 base::Lock is_offline_lock_; |
| 111 bool is_offline_; | 137 bool is_offline_; |
| 112 bool is_offline_initialized_; | 138 bool is_offline_initialized_; |
| 113 base::ConditionVariable is_offline_initialized_cv_; | 139 base::ConditionVariable is_offline_initialized_cv_; |
| 140 bool tracking_; | |
| 141 | |
| 142 // A |ThreadChecker| used to verify single-threaded access in | |
|
pauljensen
2014/09/19 19:25:40
Remove "A |ThreadChecker|" as this is redundant wi
guoweis2
2014/09/19 22:12:55
Done.
| |
| 143 // non-tracking mode. | |
| 144 base::ThreadChecker thread_checker_; | |
| 114 }; | 145 }; |
| 115 | 146 |
| 116 } // namespace internal | 147 } // namespace internal |
| 117 } // namespace net | 148 } // namespace net |
| 118 | 149 |
| 119 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 150 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
| OLD | NEW |