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