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