| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/net_util_linux.h" | 5 #include "net/base/net_util_linux.h" |
| 6 | 6 |
| 7 #include <net/if.h> | 7 #if !defined(OS_ANDROID) |
| 8 #include <netinet/in.h> | 8 #include <linux/ethtool.h> |
| 9 #endif // !defined(OS_ANDROID) |
| 10 #include <linux/if.h> |
| 11 #include <linux/sockios.h> |
| 12 #include <linux/wireless.h> |
| 9 #include <set> | 13 #include <set> |
| 14 #include <sys/ioctl.h> |
| 10 #include <sys/types.h> | 15 #include <sys/types.h> |
| 11 | 16 |
| 12 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/files/scoped_file.h" |
| 13 #include "base/logging.h" | 19 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_tokenizer.h" | 22 #include "base/strings/string_tokenizer.h" |
| 17 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 18 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
| 19 #include "net/base/address_tracker_linux.h" | 25 #include "net/base/address_tracker_linux.h" |
| 20 #include "net/base/escape.h" | 26 #include "net/base/escape.h" |
| 21 #include "net/base/ip_endpoint.h" | 27 #include "net/base/ip_endpoint.h" |
| 22 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 namespace internal { | 68 namespace internal { |
| 63 | 69 |
| 64 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { | 70 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { |
| 65 #if defined(OS_ANDROID) | 71 #if defined(OS_ANDROID) |
| 66 return ip.begin(); | 72 return ip.begin(); |
| 67 #else | 73 #else |
| 68 return ip.data(); | 74 return ip.data(); |
| 69 #endif | 75 #endif |
| 70 } | 76 } |
| 71 | 77 |
| 78 // Gets the connection type for interface |ifname| by checking for wireless |
| 79 // or ethtool extensions. |
| 80 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( |
| 81 const std::string& ifname) { |
| 82 base::ScopedFD s(socket(AF_INET, SOCK_STREAM, 0)); |
| 83 if (!s.is_valid()) |
| 84 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 85 |
| 86 // Test wireless extensions for CONNECTION_WIFI |
| 87 struct iwreq pwrq = {}; |
| 88 strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ - 1); |
| 89 if (ioctl(s.get(), SIOCGIWNAME, &pwrq) != -1) |
| 90 return NetworkChangeNotifier::CONNECTION_WIFI; |
| 91 |
| 92 #if !defined(OS_ANDROID) |
| 93 // Test ethtool for CONNECTION_ETHERNET |
| 94 struct ethtool_cmd ecmd = {}; |
| 95 ecmd.cmd = ETHTOOL_GSET; |
| 96 struct ifreq ifr = {}; |
| 97 ifr.ifr_data = &ecmd; |
| 98 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); |
| 99 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) |
| 100 return NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 101 #endif // !defined(OS_ANDROID) |
| 102 |
| 103 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 104 } |
| 105 |
| 72 bool GetNetworkListImpl( | 106 bool GetNetworkListImpl( |
| 73 NetworkInterfaceList* networks, | 107 NetworkInterfaceList* networks, |
| 74 int policy, | 108 int policy, |
| 75 const base::hash_set<int>& online_links, | 109 const base::hash_set<int>& online_links, |
| 76 const internal::AddressTrackerLinux::AddressMap& address_map, | 110 const internal::AddressTrackerLinux::AddressMap& address_map, |
| 77 GetInterfaceNameFunction get_interface_name) { | 111 GetInterfaceNameFunction get_interface_name) { |
| 78 std::map<int, std::string> ifnames; | 112 std::map<int, std::string> ifnames; |
| 79 | 113 |
| 80 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 114 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
| 81 address_map.begin(); | 115 address_map.begin(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 105 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags, | 139 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags, |
| 106 &ip_attributes)) | 140 &ip_attributes)) |
| 107 continue; | 141 continue; |
| 108 } | 142 } |
| 109 | 143 |
| 110 // Find the name of this link. | 144 // Find the name of this link. |
| 111 std::map<int, std::string>::const_iterator itname = | 145 std::map<int, std::string>::const_iterator itname = |
| 112 ifnames.find(it->second.ifa_index); | 146 ifnames.find(it->second.ifa_index); |
| 113 std::string ifname; | 147 std::string ifname; |
| 114 if (itname == ifnames.end()) { | 148 if (itname == ifnames.end()) { |
| 115 char buffer[IF_NAMESIZE] = {0}; | 149 char buffer[IFNAMSIZ] = {0}; |
| 116 if (get_interface_name(it->second.ifa_index, buffer)) { | 150 ifname.assign(get_interface_name(it->second.ifa_index, buffer)); |
| 117 ifname = ifnames[it->second.ifa_index] = buffer; | 151 // Ignore addresses whose interface name can't be retrieved. |
| 118 } else { | 152 if (ifname.empty()) |
| 119 // Ignore addresses whose interface name can't be retrieved. | |
| 120 continue; | 153 continue; |
| 121 } | 154 ifnames[it->second.ifa_index] = ifname; |
| 122 } else { | 155 } else { |
| 123 ifname = itname->second; | 156 ifname = itname->second; |
| 124 } | 157 } |
| 125 | 158 |
| 126 // Based on the interface name and policy, determine whether we | 159 // Based on the interface name and policy, determine whether we |
| 127 // should ignore it. | 160 // should ignore it. |
| 128 if (ShouldIgnoreInterface(ifname, policy)) | 161 if (ShouldIgnoreInterface(ifname, policy)) |
| 129 continue; | 162 continue; |
| 130 | 163 |
| 164 NetworkChangeNotifier::ConnectionType type = |
| 165 GetInterfaceConnectionType(ifname); |
| 166 |
| 131 networks->push_back( | 167 networks->push_back( |
| 132 NetworkInterface(ifname, ifname, it->second.ifa_index, | 168 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
| 133 NetworkChangeNotifier::CONNECTION_UNKNOWN, it->first, | |
| 134 it->second.ifa_prefixlen, ip_attributes)); | 169 it->second.ifa_prefixlen, ip_attributes)); |
| 135 } | 170 } |
| 136 | 171 |
| 137 return true; | 172 return true; |
| 138 } | 173 } |
| 139 | 174 |
| 140 } // namespace internal | 175 } // namespace internal |
| 141 | 176 |
| 142 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 177 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 143 if (networks == NULL) | 178 if (networks == NULL) |
| 144 return false; | 179 return false; |
| 145 | 180 |
| 146 internal::AddressTrackerLinux tracker; | 181 internal::AddressTrackerLinux tracker; |
| 147 tracker.Init(); | 182 tracker.Init(); |
| 148 | 183 |
| 149 return internal::GetNetworkListImpl(networks, policy, | 184 return internal::GetNetworkListImpl( |
| 150 tracker.GetOnlineLinks(), | 185 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
| 151 tracker.GetAddressMap(), &if_indextoname); | 186 &internal::AddressTrackerLinux::GetInterfaceName); |
| 152 } | 187 } |
| 153 | 188 |
| 154 } // namespace net | 189 } // namespace net |
| OLD | NEW |