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

Unified Diff: net/base/net_util_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/net_util_linux.cc
diff --git a/net/base/net_util_linux.cc b/net/base/net_util_linux.cc
index e4e0f7f3277e9110cb944ea0714ea9136f36ea18..1a5bb5c3e368676c672a5c0c6dd7a9d4423f05c5 100644
--- a/net/base/net_util_linux.cc
+++ b/net/base/net_util_linux.cc
@@ -4,9 +4,12 @@
#include "net/base/net_util_linux.h"
-#include <net/if.h>
-#include <netinet/in.h>
+#include <linux/ethtool.h>
+#include <linux/if.h>
+#include <linux/sockios.h>
+#include <linux/wireless.h>
#include <set>
+#include <sys/ioctl.h>
#include <sys/types.h>
#include "base/files/file_path.h"
@@ -69,6 +72,39 @@ inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) {
#endif
}
+// Gets the connection type for interface |ifname| by checking for wireless
+// or ethtool extensions.
+NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType(
+ const std::string& ifname) {
+ int s = socket(AF_INET, SOCK_STREAM, 0);
+ if (s == -1)
+ return NetworkChangeNotifier::CONNECTION_UNKNOWN;
+
+ // Test wireless extensions for CONNECTION_WIFI
+ struct iwreq pwrq;
+ memset(&pwrq, 0, sizeof(pwrq));
+ strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ);
+ if (ioctl(s, SIOCGIWNAME, &pwrq) != -1) {
+ close(s);
+ return NetworkChangeNotifier::CONNECTION_WIFI;
+ }
+
+ // Test ethtool for CONNECTION_ETHERNET
+ struct ethtool_cmd ecmd;
pauljensen 2015/01/28 19:05:04 This struct needs initialization; you can simply d
derekjchow1 2015/01/28 20:39:12 Done.
+ ecmd.cmd = ETHTOOL_GSET;
+ struct ifreq ifr;
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_data = (void*)&ecmd;
pauljensen 2015/01/28 19:05:04 I don't think a cast is necessary when going to vo
derekjchow1 2015/01/28 20:39:12 Done.
+ strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ);
+ if (ioctl(s, SIOCETHTOOL, &ifr) != -1) {
+ close(s);
+ return NetworkChangeNotifier::CONNECTION_ETHERNET;
+ }
+
+ close(s);
pauljensen 2015/01/28 19:05:04 Please use a ScopedFD so we can remove all the clo
derekjchow1 2015/01/28 20:39:12 Done.
+ return NetworkChangeNotifier::CONNECTION_UNKNOWN;
+}
+
bool GetNetworkListImpl(
NetworkInterfaceList* networks,
int policy,
@@ -112,7 +148,7 @@ bool GetNetworkListImpl(
ifnames.find(it->second.ifa_index);
std::string ifname;
if (itname == ifnames.end()) {
- char buffer[IF_NAMESIZE] = {0};
+ char buffer[IFNAMSIZ] = {0};
if (get_interface_name(it->second.ifa_index, buffer)) {
ifname = ifnames[it->second.ifa_index] = buffer;
} else {
@@ -128,9 +164,11 @@ bool GetNetworkListImpl(
if (ShouldIgnoreInterface(ifname, policy))
continue;
+ NetworkChangeNotifier::ConnectionType type =
+ GetInterfaceConnectionType(ifname);
+
networks->push_back(
- NetworkInterface(ifname, ifname, it->second.ifa_index,
- NetworkChangeNotifier::CONNECTION_UNKNOWN, it->first,
+ NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first,
it->second.ifa_prefixlen, ip_attributes));
}
@@ -146,9 +184,9 @@ bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
internal::AddressTrackerLinux tracker;
tracker.Init();
- return internal::GetNetworkListImpl(networks, policy,
- tracker.GetOnlineLinks(),
- tracker.GetAddressMap(), &if_indextoname);
+ return internal::GetNetworkListImpl(
+ networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(),
+ &internal::AddressTrackerLinux::GetInterfaceName);
}
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698