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 #if !defined(OS_ANDROID) | 7 #if !defined(OS_ANDROID) |
8 #include <linux/ethtool.h> | 8 #include <linux/ethtool.h> |
9 #endif // !defined(OS_ANDROID) | 9 #endif // !defined(OS_ANDROID) |
10 #include <linux/if.h> | 10 #include <linux/if.h> |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 struct ifreq ifr = {}; | 96 struct ifreq ifr = {}; |
97 ifr.ifr_data = &ecmd; | 97 ifr.ifr_data = &ecmd; |
98 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); | 98 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); |
99 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) | 99 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) |
100 return NetworkChangeNotifier::CONNECTION_ETHERNET; | 100 return NetworkChangeNotifier::CONNECTION_ETHERNET; |
101 #endif // !defined(OS_ANDROID) | 101 #endif // !defined(OS_ANDROID) |
102 | 102 |
103 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 103 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
104 } | 104 } |
105 | 105 |
106 std::string GetInterfaceSSID(const std::string& ifname) { | |
107 base::ScopedFD ioctl_socket(socket(AF_INET, SOCK_DGRAM, 0)); | |
108 if (!ioctl_socket.is_valid()) | |
109 return ""; | |
110 struct iwreq wreq = {}; | |
111 strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1); | |
112 | |
113 char ssid[IW_ESSID_MAX_SIZE + 1] = {0}; | |
114 wreq.u.essid.pointer = ssid; | |
115 wreq.u.essid.length = IW_ESSID_MAX_SIZE; | |
116 if (ioctl(ioctl_socket.get(), SIOCGIWESSID, &wreq) != -1) | |
117 return ssid; | |
118 return ""; | |
119 } | |
120 | |
106 bool GetNetworkListImpl( | 121 bool GetNetworkListImpl( |
107 NetworkInterfaceList* networks, | 122 NetworkInterfaceList* networks, |
108 int policy, | 123 int policy, |
109 const base::hash_set<int>& online_links, | 124 const base::hash_set<int>& online_links, |
110 const internal::AddressTrackerLinux::AddressMap& address_map, | 125 const internal::AddressTrackerLinux::AddressMap& address_map, |
111 GetInterfaceNameFunction get_interface_name) { | 126 GetInterfaceNameFunction get_interface_name) { |
112 std::map<int, std::string> ifnames; | 127 std::map<int, std::string> ifnames; |
113 | 128 |
114 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 129 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
115 address_map.begin(); | 130 address_map.begin(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 GetInterfaceConnectionType(ifname); | 180 GetInterfaceConnectionType(ifname); |
166 | 181 |
167 networks->push_back( | 182 networks->push_back( |
168 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, | 183 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
169 it->second.ifa_prefixlen, ip_attributes)); | 184 it->second.ifa_prefixlen, ip_attributes)); |
170 } | 185 } |
171 | 186 |
172 return true; | 187 return true; |
173 } | 188 } |
174 | 189 |
190 std::string GetWifiSSIDFromInterfaceListInternal( | |
191 const NetworkInterfaceList& interfaces, | |
192 internal::GetInterfaceSSIDFunction get_interface_ssid) { | |
193 std::string connected_ssid; | |
194 bool first = true; | |
195 for (size_t i = 0; i < interfaces.size(); ++i) { | |
196 if (interfaces[i].type != NetworkChangeNotifier::CONNECTION_WIFI) | |
197 return ""; | |
198 std::string ssid = get_interface_ssid(interfaces[i].name); | |
199 if (first) { | |
pauljensen
2015/02/10 14:06:38
can we get rid of "first" and instead just use "i
meacer
2015/02/11 00:24:53
Done.
| |
200 first = false; | |
201 connected_ssid = ssid; | |
202 } else if (ssid != connected_ssid) { | |
203 return ""; | |
204 } | |
205 } | |
206 return connected_ssid; | |
207 } | |
208 | |
175 } // namespace internal | 209 } // namespace internal |
176 | 210 |
177 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 211 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
178 if (networks == NULL) | 212 if (networks == NULL) |
179 return false; | 213 return false; |
180 | 214 |
181 internal::AddressTrackerLinux tracker; | 215 internal::AddressTrackerLinux tracker; |
182 tracker.Init(); | 216 tracker.Init(); |
183 | 217 |
184 return internal::GetNetworkListImpl( | 218 return internal::GetNetworkListImpl( |
185 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), | 219 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
186 &internal::AddressTrackerLinux::GetInterfaceName); | 220 &internal::AddressTrackerLinux::GetInterfaceName); |
187 } | 221 } |
188 | 222 |
223 std::string GetWifiSSID() { | |
224 NetworkInterfaceList networks; | |
225 if (GetNetworkList(&networks, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { | |
226 return internal::GetWifiSSIDFromInterfaceListInternal( | |
227 networks, internal::GetInterfaceSSID); | |
228 } | |
229 return ""; | |
230 } | |
231 | |
189 } // namespace net | 232 } // namespace net |
OLD | NEW |