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 #include <stddef.h> | 13 #include <stddef.h> |
14 | 14 |
15 #include <map> | 15 #include <map> |
| 16 #include <unordered_set> |
16 | 17 |
17 #include "base/callback.h" | 18 #include "base/callback.h" |
18 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
19 #include "base/containers/hash_tables.h" | |
20 #include "base/macros.h" | 20 #include "base/macros.h" |
21 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
22 #include "base/synchronization/condition_variable.h" | 22 #include "base/synchronization/condition_variable.h" |
23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
24 #include "base/threading/thread_checker.h" | 24 #include "base/threading/thread_checker.h" |
25 #include "net/base/ip_address.h" | 25 #include "net/base/ip_address.h" |
26 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 namespace internal { | 29 namespace internal { |
(...skipping 14 matching lines...) Expand all Loading... |
44 // Tracking version constructor: it will run |address_callback| when | 44 // Tracking version constructor: it will run |address_callback| when |
45 // the AddressMap changes, |link_callback| when the list of online | 45 // the AddressMap changes, |link_callback| when the list of online |
46 // links changes, and |tunnel_callback| when the list of online | 46 // links changes, and |tunnel_callback| when the list of online |
47 // tunnels changes. | 47 // tunnels changes. |
48 // |ignored_interfaces| is the list of interfaces to ignore. Changes to an | 48 // |ignored_interfaces| is the list of interfaces to ignore. Changes to an |
49 // ignored interface will not cause any callback to be run. An ignored | 49 // ignored interface will not cause any callback to be run. An ignored |
50 // interface will not have entries in GetAddressMap() and GetOnlineLinks(). | 50 // interface will not have entries in GetAddressMap() and GetOnlineLinks(). |
51 // NOTE: Only ignore interfaces not used to connect to the internet. Adding | 51 // NOTE: Only ignore interfaces not used to connect to the internet. Adding |
52 // interfaces used to connect to the internet can cause critical network | 52 // interfaces used to connect to the internet can cause critical network |
53 // changed signals to be lost allowing incorrect stale state to persist. | 53 // changed signals to be lost allowing incorrect stale state to persist. |
54 AddressTrackerLinux(const base::Closure& address_callback, | 54 AddressTrackerLinux( |
55 const base::Closure& link_callback, | 55 const base::Closure& address_callback, |
56 const base::Closure& tunnel_callback, | 56 const base::Closure& link_callback, |
57 const base::hash_set<std::string>& ignored_interfaces); | 57 const base::Closure& tunnel_callback, |
| 58 const std::unordered_set<std::string>& ignored_interfaces); |
58 ~AddressTrackerLinux() override; | 59 ~AddressTrackerLinux() override; |
59 | 60 |
60 // In tracking mode, it starts watching the system configuration for | 61 // In tracking mode, it starts watching the system configuration for |
61 // changes. The current thread must have a MessageLoopForIO. In | 62 // changes. The current thread must have a MessageLoopForIO. In |
62 // non-tracking mode, once Init() returns, a snapshot of the system | 63 // non-tracking mode, once Init() returns, a snapshot of the system |
63 // configuration is available through GetOnlineLinks() and | 64 // configuration is available through GetOnlineLinks() and |
64 // GetAddressMap(). | 65 // GetAddressMap(). |
65 void Init(); | 66 void Init(); |
66 | 67 |
67 AddressMap GetAddressMap() const; | 68 AddressMap GetAddressMap() const; |
68 | 69 |
69 // Returns set of interface indicies for online interfaces. | 70 // Returns set of interface indicies for online interfaces. |
70 base::hash_set<int> GetOnlineLinks() const; | 71 std::unordered_set<int> GetOnlineLinks() const; |
71 | 72 |
72 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | 73 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
73 // Safe to call from any thread, but will block until Init() has completed. | 74 // Safe to call from any thread, but will block until Init() has completed. |
74 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | 75 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
75 | 76 |
76 // Returns the name for the interface with interface index |interface_index|. | 77 // Returns the name for the interface with interface index |interface_index|. |
77 // |buf| should be a pointer to an array of size IFNAMSIZ. The returned | 78 // |buf| should be a pointer to an array of size IFNAMSIZ. The returned |
78 // pointer will point to |buf|. This function acts like if_indextoname which | 79 // pointer will point to |buf|. This function acts like if_indextoname which |
79 // cannot be used as net/if.h cannot be mixed with linux/if.h. We'll stick | 80 // cannot be used as net/if.h cannot be mixed with linux/if.h. We'll stick |
80 // with exclusively talking to the kernel and not the C library. | 81 // with exclusively talking to the kernel and not the C library. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 base::Closure tunnel_callback_; | 154 base::Closure tunnel_callback_; |
154 | 155 |
155 int netlink_fd_; | 156 int netlink_fd_; |
156 base::MessageLoopForIO::FileDescriptorWatcher watcher_; | 157 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
157 | 158 |
158 mutable base::Lock address_map_lock_; | 159 mutable base::Lock address_map_lock_; |
159 AddressMap address_map_; | 160 AddressMap address_map_; |
160 | 161 |
161 // Set of interface indices for links that are currently online. | 162 // Set of interface indices for links that are currently online. |
162 mutable base::Lock online_links_lock_; | 163 mutable base::Lock online_links_lock_; |
163 base::hash_set<int> online_links_; | 164 std::unordered_set<int> online_links_; |
164 | 165 |
165 // Set of interface names that should be ignored. | 166 // Set of interface names that should be ignored. |
166 const base::hash_set<std::string> ignored_interfaces_; | 167 const std::unordered_set<std::string> ignored_interfaces_; |
167 | 168 |
168 base::Lock connection_type_lock_; | 169 base::Lock connection_type_lock_; |
169 bool connection_type_initialized_; | 170 bool connection_type_initialized_; |
170 base::ConditionVariable connection_type_initialized_cv_; | 171 base::ConditionVariable connection_type_initialized_cv_; |
171 NetworkChangeNotifier::ConnectionType current_connection_type_; | 172 NetworkChangeNotifier::ConnectionType current_connection_type_; |
172 bool tracking_; | 173 bool tracking_; |
173 int threads_waiting_for_connection_type_initialization_; | 174 int threads_waiting_for_connection_type_initialization_; |
174 | 175 |
175 // Used to verify single-threaded access in non-tracking mode. | 176 // Used to verify single-threaded access in non-tracking mode. |
176 base::ThreadChecker thread_checker_; | 177 base::ThreadChecker thread_checker_; |
177 }; | 178 }; |
178 | 179 |
179 } // namespace internal | 180 } // namespace internal |
180 } // namespace net | 181 } // namespace net |
181 | 182 |
182 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 183 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
OLD | NEW |