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