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