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 #include <string> |
15 | 16 |
16 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
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/containers/hash_tables.h" |
20 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
21 #include "base/synchronization/condition_variable.h" | 22 #include "base/synchronization/condition_variable.h" |
22 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
23 #include "base/threading/thread_checker.h" | 24 #include "base/threading/thread_checker.h" |
24 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
26 | 27 |
27 namespace net { | 28 namespace net { |
28 namespace internal { | 29 namespace internal { |
29 | 30 |
30 // Keeps track of network interface addresses using rtnetlink. Used by | 31 // Keeps track of network interface addresses using rtnetlink. Used by |
31 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. | 32 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. |
32 class NET_EXPORT_PRIVATE AddressTrackerLinux : | 33 class NET_EXPORT_PRIVATE AddressTrackerLinux : |
33 public base::MessageLoopForIO::Watcher { | 34 public base::MessageLoopForIO::Watcher { |
34 public: | 35 public: |
35 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; | 36 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; |
36 | 37 |
| 38 // A function that returns the SSID of an interface given the interface name. |
| 39 typedef std::string (*GetInterfaceSSIDFunction)(const std::string& ifname); |
| 40 |
37 // Non-tracking version constructor: it takes a snapshot of the | 41 // Non-tracking version constructor: it takes a snapshot of the |
38 // current system configuration. Once Init() returns, the | 42 // current system configuration. Once Init() returns, the |
39 // configuration is available through GetOnlineLinks() and | 43 // configuration is available through GetOnlineLinks() and |
40 // GetAddressMap(). | 44 // GetAddressMap(). |
41 AddressTrackerLinux(); | 45 AddressTrackerLinux(); |
42 | 46 |
43 // Tracking version constructor: it will run |address_callback| when | 47 // Tracking version constructor: it will run |address_callback| when |
44 // the AddressMap changes, |link_callback| when the list of online | 48 // the AddressMap changes, |link_callback| when the list of online |
45 // links changes, and |tunnel_callback| when the list of online | 49 // links changes, and |tunnel_callback| when the list of online |
46 // tunnels changes. | 50 // tunnels changes. |
(...skipping 11 matching lines...) Expand all Loading... |
58 | 62 |
59 AddressMap GetAddressMap() const; | 63 AddressMap GetAddressMap() const; |
60 | 64 |
61 // Returns set of interface indicies for online interfaces. | 65 // Returns set of interface indicies for online interfaces. |
62 base::hash_set<int> GetOnlineLinks() const; | 66 base::hash_set<int> GetOnlineLinks() const; |
63 | 67 |
64 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | 68 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
65 // Safe to call from any thread, but will block until Init() has completed. | 69 // Safe to call from any thread, but will block until Init() has completed. |
66 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | 70 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
67 | 71 |
| 72 // Implementation of NetworkChangeNotifierLinux::GetCurrentWiFiSSID(). |
| 73 // Safe to call from any thread. Doesn't wait for initialization to complete, |
| 74 // so may return empty string. |
| 75 std::string GetCurrentWiFiSSID(); |
| 76 |
68 // Returns the name for the interface with interface index |interface_index|. | 77 // Returns the name for the interface with interface index |interface_index|. |
69 // |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 |
70 // 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 |
71 // 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 |
72 // with exclusively talking to the kernel and not the C library. | 81 // with exclusively talking to the kernel and not the C library. |
73 static char* GetInterfaceName(int interface_index, char* buf); | 82 static char* GetInterfaceName(int interface_index, char* buf); |
74 | 83 |
| 84 // Gets the current Wi-Fi SSID based on |interfaces|. Returns |
| 85 // empty string if there are no interfaces or if two interfaces have different |
| 86 // connection types. Otherwise returns the SSID of all interfaces if they have |
| 87 // the same SSID. This is adapted from |
| 88 // NetworkChangeNotifier::ConnectionTypeFromInterfaceList. |
| 89 static std::string ConnectionSSIDFromInterfaceList( |
| 90 const NetworkInterfaceList& interfaces, |
| 91 GetInterfaceSSIDFunction get_interface_ssid); |
| 92 |
75 private: | 93 private: |
76 friend class AddressTrackerLinuxTest; | 94 friend class AddressTrackerLinuxTest; |
77 | 95 |
78 // In tracking mode, holds |lock| while alive. In non-tracking mode, | 96 // In tracking mode, holds |lock| while alive. In non-tracking mode, |
79 // enforces single-threaded access. | 97 // enforces single-threaded access. |
80 class AddressTrackerAutoLock { | 98 class AddressTrackerAutoLock { |
81 public: | 99 public: |
82 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, | 100 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, |
83 base::Lock& lock); | 101 base::Lock& lock); |
84 ~AddressTrackerAutoLock(); | 102 ~AddressTrackerAutoLock(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // MessageLoopForIO::Watcher: | 136 // MessageLoopForIO::Watcher: |
119 void OnFileCanReadWithoutBlocking(int fd) override; | 137 void OnFileCanReadWithoutBlocking(int fd) override; |
120 void OnFileCanWriteWithoutBlocking(int /* fd */) override; | 138 void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
121 | 139 |
122 // Close |netlink_fd_| | 140 // Close |netlink_fd_| |
123 void CloseSocket(); | 141 void CloseSocket(); |
124 | 142 |
125 // Does |interface_index| refer to a tunnel interface? | 143 // Does |interface_index| refer to a tunnel interface? |
126 bool IsTunnelInterface(int interface_index) const; | 144 bool IsTunnelInterface(int interface_index) const; |
127 | 145 |
128 // Updates current_connection_type_ based on the network list. | 146 // Updates current_connection_type_ and current_wifi_ssid_ based on the |
129 void UpdateCurrentConnectionType(); | 147 // network list. |
| 148 void UpdateCurrentConnectionTypeAndSSID(); |
130 | 149 |
131 // Gets the name of an interface given the interface index |interface_index|. | 150 // Gets the name of an interface given the interface index |interface_index|. |
132 // May return empty string if it fails but should not return NULL. This is | 151 // May return empty string if it fails but should not return NULL. This is |
133 // overridden by tests. | 152 // overridden by tests. |
134 GetInterfaceNameFunction get_interface_name_; | 153 GetInterfaceNameFunction get_interface_name_; |
135 | 154 |
136 base::Closure address_callback_; | 155 base::Closure address_callback_; |
137 base::Closure link_callback_; | 156 base::Closure link_callback_; |
138 base::Closure tunnel_callback_; | 157 base::Closure tunnel_callback_; |
139 | 158 |
140 int netlink_fd_; | 159 int netlink_fd_; |
141 base::MessageLoopForIO::FileDescriptorWatcher watcher_; | 160 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
142 | 161 |
143 mutable base::Lock address_map_lock_; | 162 mutable base::Lock address_map_lock_; |
144 AddressMap address_map_; | 163 AddressMap address_map_; |
145 | 164 |
146 // Set of interface indices for links that are currently online. | 165 // Set of interface indices for links that are currently online. |
147 mutable base::Lock online_links_lock_; | 166 mutable base::Lock online_links_lock_; |
148 base::hash_set<int> online_links_; | 167 base::hash_set<int> online_links_; |
149 | 168 |
150 base::Lock connection_type_lock_; | 169 base::Lock connection_type_lock_; |
151 bool connection_type_initialized_; | 170 bool connection_type_initialized_; |
152 base::ConditionVariable connection_type_initialized_cv_; | 171 base::ConditionVariable connection_type_initialized_cv_; |
153 NetworkChangeNotifier::ConnectionType current_connection_type_; | 172 NetworkChangeNotifier::ConnectionType current_connection_type_; |
| 173 |
| 174 base::Lock wifi_ssid_lock_; |
| 175 std::string current_wifi_ssid_; |
| 176 |
154 bool tracking_; | 177 bool tracking_; |
155 | 178 |
156 // Used to verify single-threaded access in non-tracking mode. | 179 // Used to verify single-threaded access in non-tracking mode. |
157 base::ThreadChecker thread_checker_; | 180 base::ThreadChecker thread_checker_; |
158 }; | 181 }; |
159 | 182 |
160 } // namespace internal | 183 } // namespace internal |
161 } // namespace net | 184 } // namespace net |
162 | 185 |
163 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 186 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
OLD | NEW |