Chromium Code Reviews| 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..d15232c8f39832e915c35884ef909115f1105c93 100644 |
| --- a/net/base/net_util_linux.cc |
| +++ b/net/base/net_util_linux.cc |
| @@ -4,12 +4,16 @@ |
| #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" |
| +#include "base/files/scoped_file.h" |
| #include "base/logging.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -69,6 +73,33 @@ 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) { |
| + base::ScopedFD s(socket(AF_INET, SOCK_STREAM, 0)); |
| + if (!s.is_valid()) |
| + return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| + |
| + // Test wireless extensions for CONNECTION_WIFI |
| + struct iwreq pwrq = {}; |
| + memset(&pwrq, 0, sizeof(pwrq)); |
|
pauljensen
2015/01/28 21:15:55
this line is now unnecessary
derekjchow1
2015/01/29 00:35:04
Done.
|
| + strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ); |
| + if (ioctl(s.get(), SIOCGIWNAME, &pwrq) != -1) |
| + return NetworkChangeNotifier::CONNECTION_WIFI; |
| + |
| + // Test ethtool for CONNECTION_ETHERNET |
| + struct ethtool_cmd ecmd = {}; |
| + ecmd.cmd = ETHTOOL_GSET; |
|
pauljensen
2015/01/28 21:15:55
how about combining this line into the previous li
derekjchow1
2015/01/29 00:35:04
Done.
|
| + struct ifreq ifr = {}; |
| + ifr.ifr_data = &ecmd; |
|
pauljensen
2015/01/28 21:15:55
ditto like so:
struct ifreq ifr = { .ifr_data = &e
derekjchow1
2015/01/29 00:35:04
Done.
|
| + strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ); |
| + if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) |
| + return NetworkChangeNotifier::CONNECTION_ETHERNET; |
| + |
| + return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| +} |
| + |
| bool GetNetworkListImpl( |
| NetworkInterfaceList* networks, |
| int policy, |
| @@ -112,7 +143,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 +159,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 +179,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 |