Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Unified Diff: net/base/address_tracker_linux.cc

Issue 739983005: Determine connection type in NetworkChangeNotifierLinux. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test ethtool for CONNECTION_ETHERNET Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/address_tracker_linux.cc
diff --git a/net/base/address_tracker_linux.cc b/net/base/address_tracker_linux.cc
index 27dd2019ab0dffd6392dee105b7ae11ba7b6e138..21849422b392ce5e4ab9e7d8a0ccedcb3e17ddf9 100644
--- a/net/base/address_tracker_linux.cc
+++ b/net/base/address_tracker_linux.cc
@@ -11,6 +11,7 @@
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/threading/thread_restrictions.h"
+#include "net/base/net_util_linux.h"
namespace net {
namespace internal {
@@ -76,26 +77,26 @@ bool GetAddress(const struct nlmsghdr* header,
return true;
}
-// Returns the name for the interface with interface index |interface_index|.
-// The return value points to a function-scoped static so it may be changed by
-// subsequent calls. This function could be replaced with if_indextoname() but
-// net/if.h cannot be mixed with linux/if.h so we'll stick with exclusively
-// talking to the kernel and not the C library.
-const char* GetInterfaceName(int interface_index) {
+} // namespace
+
+// static
+char* AddressTrackerLinux::GetInterfaceName(int interface_index, char* buf) {
+ memset(buf, 0, IFNAMSIZ);
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.
if (ioctl_socket < 0)
- return "";
+ return buf;
+
static struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_ifindex = interface_index;
int rv = ioctl(ioctl_socket, SIOCGIFNAME, &ifr);
close(ioctl_socket);
if (rv != 0)
- return "";
- return ifr.ifr_name;
-}
+ return buf;
-} // namespace
+ strncpy(buf, ifr.ifr_name, IFNAMSIZ - 1);
+ return buf;
+}
AddressTrackerLinux::AddressTrackerLinux()
: get_interface_name_(GetInterfaceName),
@@ -103,9 +104,9 @@ AddressTrackerLinux::AddressTrackerLinux()
link_callback_(base::Bind(&base::DoNothing)),
tunnel_callback_(base::Bind(&base::DoNothing)),
netlink_fd_(-1),
- is_offline_(true),
- is_offline_initialized_(false),
- is_offline_initialized_cv_(&is_offline_lock_),
+ connection_type_initialized_(false),
+ connection_type_initialized_cv_(&connection_type_lock_),
+ current_connection_type_(NetworkChangeNotifier::CONNECTION_NONE),
tracking_(false) {
}
@@ -117,9 +118,9 @@ AddressTrackerLinux::AddressTrackerLinux(const base::Closure& address_callback,
link_callback_(link_callback),
tunnel_callback_(tunnel_callback),
netlink_fd_(-1),
- is_offline_(true),
- is_offline_initialized_(false),
- is_offline_initialized_cv_(&is_offline_lock_),
+ connection_type_initialized_(false),
+ connection_type_initialized_cv_(&connection_type_lock_),
+ current_connection_type_(NetworkChangeNotifier::CONNECTION_NONE),
tracking_(true) {
DCHECK(!address_callback.is_null());
DCHECK(!link_callback.is_null());
@@ -203,9 +204,9 @@ void AddressTrackerLinux::Init() {
// Consume pending message to populate links_online_, but don't notify.
ReadMessages(&address_changed, &link_changed, &tunnel_changed);
{
- AddressTrackerAutoLock lock(*this, is_offline_lock_);
- is_offline_initialized_ = true;
- is_offline_initialized_cv_.Signal();
+ AddressTrackerAutoLock lock(*this, connection_type_lock_);
+ connection_type_initialized_ = true;
+ connection_type_initialized_cv_.Signal();
}
if (tracking_) {
@@ -221,10 +222,10 @@ void AddressTrackerLinux::Init() {
void AddressTrackerLinux::AbortAndForceOnline() {
CloseSocket();
- AddressTrackerAutoLock lock(*this, is_offline_lock_);
- is_offline_ = false;
- is_offline_initialized_ = true;
- is_offline_initialized_cv_.Signal();
+ AddressTrackerAutoLock lock(*this, connection_type_lock_);
+ current_connection_type_ = NetworkChangeNotifier::CONNECTION_UNKNOWN;
+ connection_type_initialized_ = true;
+ connection_type_initialized_cv_.Signal();
}
AddressTrackerLinux::AddressMap AddressTrackerLinux::GetAddressMap() const {
@@ -241,15 +242,12 @@ NetworkChangeNotifier::ConnectionType
AddressTrackerLinux::GetCurrentConnectionType() {
// http://crbug.com/125097
base::ThreadRestrictions::ScopedAllowWait allow_wait;
- AddressTrackerAutoLock lock(*this, is_offline_lock_);
- // Make sure the initial offline state is set before returning.
- while (!is_offline_initialized_) {
- is_offline_initialized_cv_.Wait();
+ AddressTrackerAutoLock lock(*this, connection_type_lock_);
+ // Make sure the initial connection type is set before returning.
+ while (!connection_type_initialized_) {
+ connection_type_initialized_cv_.Wait();
}
- // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN.
- // http://crbug.com/160537
- return is_offline_ ? NetworkChangeNotifier::CONNECTION_NONE :
- NetworkChangeNotifier::CONNECTION_UNKNOWN;
+ return current_connection_type_;
}
void AddressTrackerLinux::ReadMessages(bool* address_changed,
@@ -279,15 +277,8 @@ void AddressTrackerLinux::ReadMessages(bool* address_changed,
}
HandleMessage(buffer, rv, address_changed, link_changed, tunnel_changed);
}
- if (*link_changed) {
- bool is_offline;
- {
- AddressTrackerAutoLock lock(*this, online_links_lock_);
- is_offline = online_links_.empty();
- }
- AddressTrackerAutoLock lock(*this, is_offline_lock_);
- is_offline_ = is_offline;
- }
+ if (*link_changed || *address_changed)
+ UpdateCurrentConnectionType();
}
void AddressTrackerLinux::HandleMessage(char* buffer,
@@ -403,7 +394,26 @@ void AddressTrackerLinux::CloseSocket() {
bool AddressTrackerLinux::IsTunnelInterface(const struct ifinfomsg* msg) const {
// Linux kernel drivers/net/tun.c uses "tun" name prefix.
- return strncmp(get_interface_name_(msg->ifi_index), "tun", 3) == 0;
+ char buf[IFNAMSIZ] = {0};
+ return strncmp(get_interface_name_(msg->ifi_index, buf), "tun", 3) == 0;
+}
+
+void AddressTrackerLinux::UpdateCurrentConnectionType() {
+ NetworkChangeNotifier::ConnectionType type =
+ NetworkChangeNotifier::CONNECTION_NONE;
+ 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.
+ AddressTrackerLinux::AddressMap address_map = GetAddressMap();
+ NetworkInterfaceList networks;
+ if (GetNetworkListImpl(&networks, 0, online_links, address_map,
+ get_interface_name_)) {
+ type = NetworkChangeNotifier::ConnectionTypeFromInterfaceList(networks);
+ } else {
+ type = online_links.empty() ? NetworkChangeNotifier::CONNECTION_NONE
+ : NetworkChangeNotifier::CONNECTION_UNKNOWN;
+ }
+
+ AddressTrackerAutoLock lock(*this, connection_type_lock_);
+ current_connection_type_ = type;
}
AddressTrackerLinux::AddressTrackerAutoLock::AddressTrackerAutoLock(

Powered by Google App Engine
This is Rietveld 408576698