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 #include "net/base/address_tracker_linux.h" | 5 #include "net/base/address_tracker_linux.h" |
6 | 6 |
7 #include <arpa/inet.h> | |
7 #include <errno.h> | 8 #include <errno.h> |
8 #include <linux/if.h> | 9 #include <linux/if.h> |
9 #include <sys/ioctl.h> | 10 #include <sys/ioctl.h> |
10 | 11 |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
15 #include "net/base/ip_endpoint.h" | |
16 #include "net/base/net_util_linux.h" | |
pauljensen
2015/01/15 15:05:10
net_util_linux.h#8 says it's not for general purpo
derekjchow1
2015/01/15 20:32:51
Done.
We use GetInterfaceName as an argument to G
| |
17 #include "net/base/net_util_posix.h" | |
14 | 18 |
15 namespace net { | 19 namespace net { |
16 namespace internal { | 20 namespace internal { |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 // Retrieves address from NETLINK address message. | 24 // Retrieves address from NETLINK address message. |
21 // Sets |really_deprecated| for IPv6 addresses with preferred lifetimes of 0. | 25 // Sets |really_deprecated| for IPv6 addresses with preferred lifetimes of 0. |
22 bool GetAddress(const struct nlmsghdr* header, | 26 bool GetAddress(const struct nlmsghdr* header, |
23 IPAddressNumber* out, | 27 IPAddressNumber* out, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 } | 73 } |
70 } | 74 } |
71 if (local) | 75 if (local) |
72 address = local; | 76 address = local; |
73 if (!address) | 77 if (!address) |
74 return false; | 78 return false; |
75 out->assign(address, address + address_length); | 79 out->assign(address, address + address_length); |
76 return true; | 80 return true; |
77 } | 81 } |
78 | 82 |
79 // Returns the name for the interface with interface index |interface_index|. | |
80 // The return value points to a function-scoped static so it may be changed by | |
81 // subsequent calls. This function could be replaced with if_indextoname() but | |
82 // net/if.h cannot be mixed with linux/if.h so we'll stick with exclusively | |
83 // talking to the kernel and not the C library. | |
84 const char* GetInterfaceName(int interface_index) { | |
85 int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0); | |
86 if (ioctl_socket < 0) | |
87 return ""; | |
88 static struct ifreq ifr; | |
89 memset(&ifr, 0, sizeof(ifr)); | |
90 ifr.ifr_ifindex = interface_index; | |
91 int rv = ioctl(ioctl_socket, SIOCGIFNAME, &ifr); | |
92 close(ioctl_socket); | |
93 if (rv != 0) | |
94 return ""; | |
95 return ifr.ifr_name; | |
96 } | |
97 | |
98 } // namespace | 83 } // namespace |
99 | 84 |
100 AddressTrackerLinux::AddressTrackerLinux() | 85 AddressTrackerLinux::AddressTrackerLinux() |
101 : get_interface_name_(GetInterfaceName), | 86 : get_interface_name_(GetInterfaceName), |
102 address_callback_(base::Bind(&base::DoNothing)), | 87 address_callback_(base::Bind(&base::DoNothing)), |
103 link_callback_(base::Bind(&base::DoNothing)), | 88 link_callback_(base::Bind(&base::DoNothing)), |
104 tunnel_callback_(base::Bind(&base::DoNothing)), | 89 tunnel_callback_(base::Bind(&base::DoNothing)), |
105 netlink_fd_(-1), | 90 netlink_fd_(-1), |
106 is_offline_(true), | 91 connection_type_initialized_(false), |
107 is_offline_initialized_(false), | 92 connection_type_initialized_cv_(&connection_type_lock_), |
108 is_offline_initialized_cv_(&is_offline_lock_), | 93 current_connection_type_(NetworkChangeNotifier::CONNECTION_NONE), |
109 tracking_(false) { | 94 tracking_(false) { |
110 } | 95 } |
111 | 96 |
112 AddressTrackerLinux::AddressTrackerLinux(const base::Closure& address_callback, | 97 AddressTrackerLinux::AddressTrackerLinux(const base::Closure& address_callback, |
113 const base::Closure& link_callback, | 98 const base::Closure& link_callback, |
114 const base::Closure& tunnel_callback) | 99 const base::Closure& tunnel_callback) |
115 : get_interface_name_(GetInterfaceName), | 100 : get_interface_name_(GetInterfaceName), |
116 address_callback_(address_callback), | 101 address_callback_(address_callback), |
117 link_callback_(link_callback), | 102 link_callback_(link_callback), |
118 tunnel_callback_(tunnel_callback), | 103 tunnel_callback_(tunnel_callback), |
119 netlink_fd_(-1), | 104 netlink_fd_(-1), |
120 is_offline_(true), | 105 connection_type_initialized_(false), |
121 is_offline_initialized_(false), | 106 connection_type_initialized_cv_(&connection_type_lock_), |
122 is_offline_initialized_cv_(&is_offline_lock_), | 107 current_connection_type_(NetworkChangeNotifier::CONNECTION_NONE), |
123 tracking_(true) { | 108 tracking_(true) { |
124 DCHECK(!address_callback.is_null()); | 109 DCHECK(!address_callback.is_null()); |
125 DCHECK(!link_callback.is_null()); | 110 DCHECK(!link_callback.is_null()); |
126 } | 111 } |
127 | 112 |
128 AddressTrackerLinux::~AddressTrackerLinux() { | 113 AddressTrackerLinux::~AddressTrackerLinux() { |
129 CloseSocket(); | 114 CloseSocket(); |
130 } | 115 } |
131 | 116 |
132 void AddressTrackerLinux::Init() { | 117 void AddressTrackerLinux::Init() { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 sizeof(peer))); | 181 sizeof(peer))); |
197 if (rv < 0) { | 182 if (rv < 0) { |
198 PLOG(ERROR) << "Could not send NETLINK request"; | 183 PLOG(ERROR) << "Could not send NETLINK request"; |
199 AbortAndForceOnline(); | 184 AbortAndForceOnline(); |
200 return; | 185 return; |
201 } | 186 } |
202 | 187 |
203 // Consume pending message to populate links_online_, but don't notify. | 188 // Consume pending message to populate links_online_, but don't notify. |
204 ReadMessages(&address_changed, &link_changed, &tunnel_changed); | 189 ReadMessages(&address_changed, &link_changed, &tunnel_changed); |
205 { | 190 { |
206 AddressTrackerAutoLock lock(*this, is_offline_lock_); | 191 AddressTrackerAutoLock lock(*this, connection_type_lock_); |
207 is_offline_initialized_ = true; | 192 connection_type_initialized_ = true; |
208 is_offline_initialized_cv_.Signal(); | 193 connection_type_initialized_cv_.Signal(); |
209 } | 194 } |
210 | 195 |
211 if (tracking_) { | 196 if (tracking_) { |
212 rv = base::MessageLoopForIO::current()->WatchFileDescriptor( | 197 rv = base::MessageLoopForIO::current()->WatchFileDescriptor( |
213 netlink_fd_, true, base::MessageLoopForIO::WATCH_READ, &watcher_, this); | 198 netlink_fd_, true, base::MessageLoopForIO::WATCH_READ, &watcher_, this); |
214 if (rv < 0) { | 199 if (rv < 0) { |
215 PLOG(ERROR) << "Could not watch NETLINK socket"; | 200 PLOG(ERROR) << "Could not watch NETLINK socket"; |
216 AbortAndForceOnline(); | 201 AbortAndForceOnline(); |
217 return; | 202 return; |
218 } | 203 } |
219 } | 204 } |
220 } | 205 } |
221 | 206 |
222 void AddressTrackerLinux::AbortAndForceOnline() { | 207 void AddressTrackerLinux::AbortAndForceOnline() { |
223 CloseSocket(); | 208 CloseSocket(); |
224 AddressTrackerAutoLock lock(*this, is_offline_lock_); | 209 AddressTrackerAutoLock lock(*this, connection_type_lock_); |
225 is_offline_ = false; | 210 current_connection_type_ = NetworkChangeNotifier::CONNECTION_UNKNOWN; |
226 is_offline_initialized_ = true; | 211 connection_type_initialized_ = true; |
227 is_offline_initialized_cv_.Signal(); | 212 connection_type_initialized_cv_.Signal(); |
228 } | 213 } |
229 | 214 |
230 AddressTrackerLinux::AddressMap AddressTrackerLinux::GetAddressMap() const { | 215 AddressTrackerLinux::AddressMap AddressTrackerLinux::GetAddressMap() const { |
231 AddressTrackerAutoLock lock(*this, address_map_lock_); | 216 AddressTrackerAutoLock lock(*this, address_map_lock_); |
232 return address_map_; | 217 return address_map_; |
233 } | 218 } |
234 | 219 |
235 base::hash_set<int> AddressTrackerLinux::GetOnlineLinks() const { | 220 base::hash_set<int> AddressTrackerLinux::GetOnlineLinks() const { |
236 AddressTrackerAutoLock lock(*this, online_links_lock_); | 221 AddressTrackerAutoLock lock(*this, online_links_lock_); |
237 return online_links_; | 222 return online_links_; |
238 } | 223 } |
239 | 224 |
240 NetworkChangeNotifier::ConnectionType | 225 NetworkChangeNotifier::ConnectionType |
241 AddressTrackerLinux::GetCurrentConnectionType() { | 226 AddressTrackerLinux::GetCurrentConnectionType() { |
242 // http://crbug.com/125097 | 227 // http://crbug.com/125097 |
243 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 228 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
244 AddressTrackerAutoLock lock(*this, is_offline_lock_); | 229 AddressTrackerAutoLock lock(*this, connection_type_lock_); |
245 // Make sure the initial offline state is set before returning. | 230 // Make sure the initial connection type is set before returning. |
246 while (!is_offline_initialized_) { | 231 while (!connection_type_initialized_) { |
247 is_offline_initialized_cv_.Wait(); | 232 connection_type_initialized_cv_.Wait(); |
248 } | 233 } |
249 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. | 234 return current_connection_type_; |
250 // http://crbug.com/160537 | |
251 return is_offline_ ? NetworkChangeNotifier::CONNECTION_NONE : | |
252 NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
253 } | 235 } |
254 | 236 |
255 void AddressTrackerLinux::ReadMessages(bool* address_changed, | 237 void AddressTrackerLinux::ReadMessages(bool* address_changed, |
256 bool* link_changed, | 238 bool* link_changed, |
257 bool* tunnel_changed) { | 239 bool* tunnel_changed) { |
258 *address_changed = false; | 240 *address_changed = false; |
259 *link_changed = false; | 241 *link_changed = false; |
260 *tunnel_changed = false; | 242 *tunnel_changed = false; |
261 char buffer[4096]; | 243 char buffer[4096]; |
262 bool first_loop = true; | 244 bool first_loop = true; |
263 for (;;) { | 245 for (;;) { |
264 int rv = HANDLE_EINTR(recv(netlink_fd_, | 246 int rv = HANDLE_EINTR(recv(netlink_fd_, |
265 buffer, | 247 buffer, |
266 sizeof(buffer), | 248 sizeof(buffer), |
267 // Block the first time through loop. | 249 // Block the first time through loop. |
268 first_loop ? 0 : MSG_DONTWAIT)); | 250 first_loop ? 0 : MSG_DONTWAIT)); |
269 first_loop = false; | 251 first_loop = false; |
270 if (rv == 0) { | 252 if (rv == 0) { |
271 LOG(ERROR) << "Unexpected shutdown of NETLINK socket."; | 253 LOG(ERROR) << "Unexpected shutdown of NETLINK socket."; |
272 return; | 254 return; |
273 } | 255 } |
274 if (rv < 0) { | 256 if (rv < 0) { |
275 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) | 257 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
276 break; | 258 break; |
277 PLOG(ERROR) << "Failed to recv from netlink socket"; | 259 PLOG(ERROR) << "Failed to recv from netlink socket"; |
278 return; | 260 return; |
279 } | 261 } |
280 HandleMessage(buffer, rv, address_changed, link_changed, tunnel_changed); | 262 HandleMessage(buffer, rv, address_changed, link_changed, tunnel_changed); |
281 } | 263 } |
282 if (*link_changed) { | 264 if (*link_changed || *address_changed) { |
pauljensen
2015/01/15 15:05:10
doesn't need curly braces anymore
derekjchow1
2015/01/15 20:32:51
Done.
| |
283 bool is_offline; | 265 UpdateCurrentConnectionType(); |
284 { | |
285 AddressTrackerAutoLock lock(*this, online_links_lock_); | |
286 is_offline = online_links_.empty(); | |
287 } | |
288 AddressTrackerAutoLock lock(*this, is_offline_lock_); | |
289 is_offline_ = is_offline; | |
290 } | 266 } |
291 } | 267 } |
292 | 268 |
293 void AddressTrackerLinux::HandleMessage(char* buffer, | 269 void AddressTrackerLinux::HandleMessage(char* buffer, |
294 size_t length, | 270 size_t length, |
295 bool* address_changed, | 271 bool* address_changed, |
296 bool* link_changed, | 272 bool* link_changed, |
297 bool* tunnel_changed) { | 273 bool* tunnel_changed) { |
298 DCHECK(buffer); | 274 DCHECK(buffer); |
299 for (struct nlmsghdr* header = reinterpret_cast<struct nlmsghdr*>(buffer); | 275 for (struct nlmsghdr* header = reinterpret_cast<struct nlmsghdr*>(buffer); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
396 void AddressTrackerLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {} | 372 void AddressTrackerLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {} |
397 | 373 |
398 void AddressTrackerLinux::CloseSocket() { | 374 void AddressTrackerLinux::CloseSocket() { |
399 if (netlink_fd_ >= 0 && IGNORE_EINTR(close(netlink_fd_)) < 0) | 375 if (netlink_fd_ >= 0 && IGNORE_EINTR(close(netlink_fd_)) < 0) |
400 PLOG(ERROR) << "Could not close NETLINK socket."; | 376 PLOG(ERROR) << "Could not close NETLINK socket."; |
401 netlink_fd_ = -1; | 377 netlink_fd_ = -1; |
402 } | 378 } |
403 | 379 |
404 bool AddressTrackerLinux::IsTunnelInterface(const struct ifinfomsg* msg) const { | 380 bool AddressTrackerLinux::IsTunnelInterface(const struct ifinfomsg* msg) const { |
405 // Linux kernel drivers/net/tun.c uses "tun" name prefix. | 381 // Linux kernel drivers/net/tun.c uses "tun" name prefix. |
406 return strncmp(get_interface_name_(msg->ifi_index), "tun", 3) == 0; | 382 char buf[IFNAMSIZ] = {0}; |
383 return strncmp(get_interface_name_(msg->ifi_index, buf), "tun", 3) == 0; | |
384 } | |
385 | |
386 void AddressTrackerLinux::UpdateCurrentConnectionType() { | |
387 NetworkChangeNotifier::ConnectionType type = | |
388 NetworkChangeNotifier::CONNECTION_NONE; | |
389 base::hash_set<int> online_links = GetOnlineLinks(); | |
390 AddressTrackerLinux::AddressMap address_map = GetAddressMap(); | |
391 NetworkInterfaceList networks; | |
392 if (GetNetworkListImpl(&networks, 0, online_links, address_map, | |
393 get_interface_name_)) { | |
394 type = ConnectionTypeFromInterfaceList(networks); | |
395 } | |
pauljensen
2015/01/15 15:05:10
if GetNetworkListImpl fails we should fall back to
derekjchow1
2015/01/15 20:32:51
Done.
| |
396 | |
397 AddressTrackerAutoLock lock(*this, connection_type_lock_); | |
398 current_connection_type_ = type; | |
407 } | 399 } |
408 | 400 |
409 AddressTrackerLinux::AddressTrackerAutoLock::AddressTrackerAutoLock( | 401 AddressTrackerLinux::AddressTrackerAutoLock::AddressTrackerAutoLock( |
410 const AddressTrackerLinux& tracker, | 402 const AddressTrackerLinux& tracker, |
411 base::Lock& lock) | 403 base::Lock& lock) |
412 : tracker_(tracker), lock_(lock) { | 404 : tracker_(tracker), lock_(lock) { |
413 if (tracker_.tracking_) { | 405 if (tracker_.tracking_) { |
414 lock_.Acquire(); | 406 lock_.Acquire(); |
415 } else { | 407 } else { |
416 DCHECK(tracker_.thread_checker_.CalledOnValidThread()); | 408 DCHECK(tracker_.thread_checker_.CalledOnValidThread()); |
417 } | 409 } |
418 } | 410 } |
419 | 411 |
420 AddressTrackerLinux::AddressTrackerAutoLock::~AddressTrackerAutoLock() { | 412 AddressTrackerLinux::AddressTrackerAutoLock::~AddressTrackerAutoLock() { |
421 if (tracker_.tracking_) { | 413 if (tracker_.tracking_) { |
422 lock_.AssertAcquired(); | 414 lock_.AssertAcquired(); |
423 lock_.Release(); | 415 lock_.Release(); |
424 } | 416 } |
425 } | 417 } |
426 | 418 |
427 } // namespace internal | 419 } // namespace internal |
428 } // namespace net | 420 } // namespace net |
OLD | NEW |