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 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 AddressMap GetAddressMap() const; | 59 AddressMap GetAddressMap() const; |
60 | 60 |
61 // Returns set of interface indicies for online interfaces. | 61 // Returns set of interface indicies for online interfaces. |
62 base::hash_set<int> GetOnlineLinks() const; | 62 base::hash_set<int> GetOnlineLinks() const; |
63 | 63 |
64 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). | 64 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
65 // Safe to call from any thread, but will block until Init() has completed. | 65 // Safe to call from any thread, but will block until Init() has completed. |
66 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); | 66 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
67 | 67 |
| 68 // 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 |
| 70 // 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 |
| 72 // with exclusively talking to the kernel and not the C library. |
| 73 static char* GetInterfaceName(int interface_index, char* buf); |
| 74 |
68 private: | 75 private: |
69 friend class AddressTrackerLinuxTest; | 76 friend class AddressTrackerLinuxTest; |
70 | 77 |
71 // In tracking mode, holds |lock| while alive. In non-tracking mode, | 78 // In tracking mode, holds |lock| while alive. In non-tracking mode, |
72 // enforces single-threaded access. | 79 // enforces single-threaded access. |
73 class AddressTrackerAutoLock { | 80 class AddressTrackerAutoLock { |
74 public: | 81 public: |
75 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, | 82 AddressTrackerAutoLock(const AddressTrackerLinux& tracker, |
76 base::Lock& lock); | 83 base::Lock& lock); |
77 ~AddressTrackerAutoLock(); | 84 ~AddressTrackerAutoLock(); |
78 | 85 |
79 private: | 86 private: |
80 const AddressTrackerLinux& tracker_; | 87 const AddressTrackerLinux& tracker_; |
81 base::Lock& lock_; | 88 base::Lock& lock_; |
82 DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock); | 89 DISALLOW_COPY_AND_ASSIGN(AddressTrackerAutoLock); |
83 }; | 90 }; |
84 | 91 |
85 // A function that returns the name of an interface given the interface index | 92 // A function that returns the name of an interface given the interface index |
86 // in |interface_index|. | 93 // in |interface_index|. |ifname| should be a buffer of size IFNAMSIZ. The |
87 typedef const char* (*GetInterfaceNameFunction)(int interface_index); | 94 // function should return a pointer to |ifname|. |
| 95 typedef char* (*GetInterfaceNameFunction)(int interface_index, char* ifname); |
88 | 96 |
89 // Sets |*address_changed| to indicate whether |address_map_| changed and | 97 // Sets |*address_changed| to indicate whether |address_map_| changed and |
90 // sets |*link_changed| to indicate if |online_links_| changed and sets | 98 // sets |*link_changed| to indicate if |online_links_| changed and sets |
91 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a | 99 // |*tunnel_changed| to indicate if |online_links_| changed with regards to a |
92 // tunnel interface while reading messages from |netlink_fd_|. | 100 // tunnel interface while reading messages from |netlink_fd_|. |
93 void ReadMessages(bool* address_changed, | 101 void ReadMessages(bool* address_changed, |
94 bool* link_changed, | 102 bool* link_changed, |
95 bool* tunnel_changed); | 103 bool* tunnel_changed); |
96 | 104 |
97 // Sets |*address_changed| to true if |address_map_| changed, sets | 105 // Sets |*address_changed| to true if |address_map_| changed, sets |
98 // |*link_changed| to true if |online_links_| changed, sets |*tunnel_changed| | 106 // |*link_changed| to true if |online_links_| changed, sets |*tunnel_changed| |
99 // to true if |online_links_| changed with regards to a tunnel interface while | 107 // to true if |online_links_| changed with regards to a tunnel interface while |
100 // reading the message from |buffer|. | 108 // reading the message from |buffer|. |
101 void HandleMessage(char* buffer, | 109 void HandleMessage(char* buffer, |
102 size_t length, | 110 size_t length, |
103 bool* address_changed, | 111 bool* address_changed, |
104 bool* link_changed, | 112 bool* link_changed, |
105 bool* tunnel_changed); | 113 bool* tunnel_changed); |
106 | 114 |
107 // Call when some part of initialization failed; forces online and unblocks. | 115 // Call when some part of initialization failed; forces online and unblocks. |
108 void AbortAndForceOnline(); | 116 void AbortAndForceOnline(); |
109 | 117 |
110 // MessageLoopForIO::Watcher: | 118 // MessageLoopForIO::Watcher: |
111 void OnFileCanReadWithoutBlocking(int fd) override; | 119 void OnFileCanReadWithoutBlocking(int fd) override; |
112 void OnFileCanWriteWithoutBlocking(int /* fd */) override; | 120 void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
113 | 121 |
114 // Close |netlink_fd_| | 122 // Close |netlink_fd_| |
115 void CloseSocket(); | 123 void CloseSocket(); |
116 | 124 |
117 // Does |msg| refer to a tunnel interface? | 125 // Does |interface_index| refer to a tunnel interface? |
118 bool IsTunnelInterface(const struct ifinfomsg* msg) const; | 126 bool IsTunnelInterface(int interface_index) const; |
| 127 |
| 128 // Updates current_connection_type_ based on the network list. |
| 129 void UpdateCurrentConnectionType(); |
119 | 130 |
120 // Gets the name of an interface given the interface index |interface_index|. | 131 // Gets the name of an interface given the interface index |interface_index|. |
121 // May return empty string if it fails but should not return NULL. This is | 132 // May return empty string if it fails but should not return NULL. This is |
122 // overridden by tests. | 133 // overridden by tests. |
123 GetInterfaceNameFunction get_interface_name_; | 134 GetInterfaceNameFunction get_interface_name_; |
124 | 135 |
125 base::Closure address_callback_; | 136 base::Closure address_callback_; |
126 base::Closure link_callback_; | 137 base::Closure link_callback_; |
127 base::Closure tunnel_callback_; | 138 base::Closure tunnel_callback_; |
128 | 139 |
129 int netlink_fd_; | 140 int netlink_fd_; |
130 base::MessageLoopForIO::FileDescriptorWatcher watcher_; | 141 base::MessageLoopForIO::FileDescriptorWatcher watcher_; |
131 | 142 |
132 mutable base::Lock address_map_lock_; | 143 mutable base::Lock address_map_lock_; |
133 AddressMap address_map_; | 144 AddressMap address_map_; |
134 | 145 |
135 // Set of interface indices for links that are currently online. | 146 // Set of interface indices for links that are currently online. |
136 mutable base::Lock online_links_lock_; | 147 mutable base::Lock online_links_lock_; |
137 base::hash_set<int> online_links_; | 148 base::hash_set<int> online_links_; |
138 | 149 |
139 base::Lock is_offline_lock_; | 150 base::Lock connection_type_lock_; |
140 bool is_offline_; | 151 bool connection_type_initialized_; |
141 bool is_offline_initialized_; | 152 base::ConditionVariable connection_type_initialized_cv_; |
142 base::ConditionVariable is_offline_initialized_cv_; | 153 NetworkChangeNotifier::ConnectionType current_connection_type_; |
143 bool tracking_; | 154 bool tracking_; |
144 | 155 |
145 // Used to verify single-threaded access in non-tracking mode. | 156 // Used to verify single-threaded access in non-tracking mode. |
146 base::ThreadChecker thread_checker_; | 157 base::ThreadChecker thread_checker_; |
147 }; | 158 }; |
148 | 159 |
149 } // namespace internal | 160 } // namespace internal |
150 } // namespace net | 161 } // namespace net |
151 | 162 |
152 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ | 163 #endif // NET_BASE_ADDRESS_TRACKER_LINUX_H_ |
OLD | NEW |