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