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..ef8757cfc6b7dbf15730998cfab00affda7c03eb 100644 |
| --- a/net/base/net_util_linux.cc |
| +++ b/net/base/net_util_linux.cc |
| @@ -4,8 +4,9 @@ |
| #include "net/base/net_util_linux.h" |
| -#include <net/if.h> |
| -#include <netinet/in.h> |
| +#include <linux/if.h> |
| +#include <linux/wireless.h> |
| +#include <sys/ioctl.h> |
|
pauljensen
2015/01/13 13:14:30
I think this should be moved below <set> for alpha
derekjchow1
2015/01/13 21:32:39
Done.
|
| #include <set> |
| #include <sys/types.h> |
| @@ -69,6 +70,49 @@ inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { |
| #endif |
| } |
| +char* GetInterfaceName(unsigned int interface_index, char* buf) { |
| + int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0); |
| + if (ioctl_socket < 0) { |
| + strncpy(buf, "", IFNAMSIZ); |
|
pauljensen
2015/01/13 13:14:30
Can we remove this strncpy and add a memset(buf, 0
derekjchow1
2015/01/13 21:32:35
Done.
|
| + 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) { |
| + strncpy(buf, "", IFNAMSIZ); |
|
pauljensen
2015/01/13 13:14:30
Ditto.
derekjchow1
2015/01/13 21:32:35
Done.
|
| + return buf; |
| + } |
| + strncpy(buf, ifr.ifr_name, IFNAMSIZ); |
|
pauljensen
2015/01/13 13:14:30
This could result in an unterminated string causin
derekjchow1
2015/01/13 21:32:35
Done.
|
| + return buf; |
| +} |
| + |
| +NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( |
| + std::string ifname) { |
| + NetworkChangeNotifier::ConnectionType type = |
| + NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| + |
| + int s = socket(AF_INET, SOCK_STREAM, 0); |
| + if (s == -1) { |
| + return type; |
| + } |
| + |
| + struct iwreq pwrq; |
| + memset(&pwrq, 0, sizeof(pwrq)); |
| + strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ); |
| + |
| + if (ioctl(s, SIOCGIWNAME, &pwrq) != -1) { |
| + type = NetworkChangeNotifier::CONNECTION_WIFI; |
| + } else { |
| + type = NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| + } |
| + |
| + close(s); |
| + return type; |
| +} |
| + |
| bool GetNetworkListImpl( |
| NetworkInterfaceList* networks, |
| int policy, |
| @@ -112,7 +156,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 +172,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 +192,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::GetInterfaceName); |
| } |
| } // namespace net |