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" | |
24 #include "base/threading/thread_restrictions.h" | |
23 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
24 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
25 | 27 |
26 namespace net { | 28 namespace net { |
27 namespace internal { | 29 namespace internal { |
28 | 30 |
29 // Keeps track of network interface addresses using rtnetlink. Used by | 31 // Keeps track of network interface addresses using rtnetlink. Used by |
30 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. | 32 // NetworkChangeNotifier to provide signals to registered IPAddressObservers. |
31 class NET_EXPORT_PRIVATE AddressTrackerLinux : | 33 class NET_EXPORT_PRIVATE AddressTrackerLinux : |
32 public base::MessageLoopForIO::Watcher { | 34 public base::MessageLoopForIO::Watcher { |
33 public: | 35 public: |
34 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; | 36 typedef std::map<IPAddressNumber, struct ifaddrmsg> AddressMap; |
35 | 37 |
36 // Will run |address_callback| when the AddressMap changes, |link_callback| | 38 // Non-tracking version constructor: When AddressTrackerLinux is |
37 // when the list of online links changes, and |tunnel_callback| when the list | 39 // constructed without callbacks, it takes a snapshot of current |
38 // of online tunnels changes. | 40 // system configuration once Init() returns. The configuration is |
41 // available through GetOnlineLinks() and GetAddressMap (). Note | |
42 // that the thread creating |AddressTrackerLinux| must be the one | |
43 // calling Init(). Otherwise, the |thread_checker_| will fail the | |
44 // assert on CalledOnValidThread(). | |
45 AddressTrackerLinux(); | |
46 | |
47 // Tracking version constructor: it will run |address_callback| when | |
48 // the AddressMap changes, |link_callback| when the list of online | |
49 // links changes, and |tunnel_callback| when the list of online | |
50 // tunnels changes. | |
39 AddressTrackerLinux(const base::Closure& address_callback, | 51 AddressTrackerLinux(const base::Closure& address_callback, |
40 const base::Closure& link_callback, | 52 const base::Closure& link_callback, |
41 const base::Closure& tunnel_callback); | 53 const base::Closure& tunnel_callback); |
42 virtual ~AddressTrackerLinux(); | 54 virtual ~AddressTrackerLinux(); |
43 | 55 |
44 // Starts watching system configuration for changes. The current thread must | 56 // In tracking mode, it starts watching system configuration for |
45 // have a MessageLoopForIO. | 57 // changes. The current thread must have a MessageLoopForIO. For |
58 // non-tracking mode, the |AddressMap| and |online_links_| will be a | |
59 // one time snapshot of current system configuration after Init() | |
60 // returns. | |
46 void Init(); | 61 void Init(); |
47 | 62 |
48 AddressMap GetAddressMap() const; | 63 AddressMap GetAddressMap() const; |
64 base::hash_set<int> GetOnlineLinks() const; | |
49 | 65 |
50 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | 66 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
51 // Safe to call from any thread, but will block until Init() has completed. | 67 // Safe to call from any thread, but will block until Init() has completed. |
52 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | 68 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
53 | 69 |
54 private: | 70 private: |
55 friend class AddressTrackerLinuxTest; | 71 friend class AddressTrackerLinuxTest; |
56 | 72 |
73 // A private nested class to facilitate different locking behaviors | |
74 // depending on whether the |AddressTrackerLinux| is in tracking | |
75 // mode or not. Lock will only be taken in the tracking mode. | |
76 class AddressTrackerAutoLock { | |
pauljensen
2014/09/19 18:39:42
Please move all this code into address_tracker_lin
guoweis2
2014/09/19 18:48:30
Done.
| |
77 public: | |
78 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, base::Lock& lock) | |
79 : tracker_(tracker), lock_(lock) { | |
80 if (tracker_.tracking_) | |
81 lock_.Acquire(); | |
82 } | |
83 | |
84 ~AddressTrackerAutoLock() { | |
85 if (tracker_.tracking_) { | |
86 lock_.AssertAcquired(); | |
87 lock_.Release(); | |
88 } else { | |
89 DCHECK(tracker_.thread_checker_.CalledOnValidThread()); | |
pauljensen
2014/09/19 18:39:42
let's move this to the constructor so it's a littl
guoweis2
2014/09/19 18:48:30
Done.
| |
90 } | |
91 } | |
92 | |
93 private: | |
94 const AddressTrackerLinux& tracker_; | |
95 base::Lock& lock_; | |
96 DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock); | |
97 }; | |
98 | |
57 // A function that returns the name of an interface given the interface index | 99 // A function that returns the name of an interface given the interface index |
58 // in |interface_index|. | 100 // in |interface_index|. |
59 typedef const char* (*GetInterfaceNameFunction)(int interface_index); | 101 typedef const char* (*GetInterfaceNameFunction)(int interface_index); |
60 | 102 |
61 // Sets |*address_changed| to indicate whether |address_map_| changed and | 103 // Sets |*address_changed| to indicate whether |address_map_| changed and |
62 // sets |*link_changed| to indicate if |online_links_| changed and sets | 104 // sets |*link_changed| to indicate if |online_links_| changed and sets |
63 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a | 105 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a |
64 // tunnel interface while reading messages from |netlink_fd_|. | 106 // tunnel interface while reading messages from |netlink_fd_|. |
65 void ReadMessages(bool* address_changed, | 107 void ReadMessages(bool* address_changed, |
66 bool* link_changed, | 108 bool* link_changed, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 base::Closure link_callback_; | 140 base::Closure link_callback_; |
99 base::Closure tunnel_callback_; | 141 base::Closure tunnel_callback_; |
100 | 142 |
101 int netlink_fd_; | 143 int netlink_fd_; |
102 base::MessageLoopForIO::FileDescriptorWatcher watcher_; | 144 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
103 | 145 |
104 mutable base::Lock address_map_lock_; | 146 mutable base::Lock address_map_lock_; |
105 AddressMap address_map_; | 147 AddressMap address_map_; |
106 | 148 |
107 // Set of interface indices for links that are currently online. | 149 // Set of interface indices for links that are currently online. |
150 mutable base::Lock online_links_lock_; | |
108 base::hash_set<int> online_links_; | 151 base::hash_set<int> online_links_; |
109 | 152 |
110 base::Lock is_offline_lock_; | 153 base::Lock is_offline_lock_; |
111 bool is_offline_; | 154 bool is_offline_; |
112 bool is_offline_initialized_; | 155 bool is_offline_initialized_; |
113 base::ConditionVariable is_offline_initialized_cv_; | 156 base::ConditionVariable is_offline_initialized_cv_; |
157 bool tracking_; | |
158 | |
159 // A |ThreadChecker| to validate that in the non-tracking mode, the | |
pauljensen
2014/09/19 18:39:42
Eh how about something like "Used to verify single
guoweis2
2014/09/19 18:48:30
Done.
| |
160 // thread destructing a |AddressTrackerAutoLock| is the same as the | |
161 // one which created it. This is to catch any unexpected callback | |
162 // happening in the non-tracking mode. | |
163 base::ThreadChecker thread_checker_; | |
114 }; | 164 }; |
115 | 165 |
116 } // namespace internal | 166 } // namespace internal |
117 } // namespace net | 167 } // namespace net |
118 | 168 |
119 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 169 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
OLD | NEW |