Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: net/base/net_util_linux.cc

Issue 878513008: Add a function to get Wifi SSID to net_util. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove NetworkInterfaceList argument. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
108 if (ioctl_socket < 0)
109 return "";
110 struct iwreq wreq;
111 memset(&wreq, 0, sizeof(wreq));
pauljensen 2015/02/09 12:28:05 Can you combine these two lines like line 87?
meacer 2015/02/10 04:42:50 Done.
112 strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
113
114 char ssid[IW_ESSID_MAX_SIZE + 1] = {0};
115 wreq.u.essid.pointer = ssid;
116 wreq.u.essid.length = IW_ESSID_MAX_SIZE;
117 int rv = ioctl(ioctl_socket, SIOCGIWESSID, &wreq);
118 close(ioctl_socket);
pauljensen 2015/02/09 12:28:05 Can you eliminate this line using ScopedFD like li
meacer 2015/02/10 04:42:50 Good point, done.
119 return (rv == 0) ? ssid : "";
120 }
121
106 bool GetNetworkListImpl( 122 bool GetNetworkListImpl(
107 NetworkInterfaceList* networks, 123 NetworkInterfaceList* networks,
108 int policy, 124 int policy,
109 const base::hash_set<int>& online_links, 125 const base::hash_set<int>& online_links,
110 const internal::AddressTrackerLinux::AddressMap& address_map, 126 const internal::AddressTrackerLinux::AddressMap& address_map,
111 GetInterfaceNameFunction get_interface_name) { 127 GetInterfaceNameFunction get_interface_name) {
112 std::map<int, std::string> ifnames; 128 std::map<int, std::string> ifnames;
113 129
114 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = 130 for (internal::AddressTrackerLinux::AddressMap::const_iterator it =
115 address_map.begin(); 131 address_map.begin();
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 GetInterfaceConnectionType(ifname); 181 GetInterfaceConnectionType(ifname);
166 182
167 networks->push_back( 183 networks->push_back(
168 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, 184 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first,
169 it->second.ifa_prefixlen, ip_attributes)); 185 it->second.ifa_prefixlen, ip_attributes));
170 } 186 }
171 187
172 return true; 188 return true;
173 } 189 }
174 190
191 std::string GetWifiSSIDFromInterfaceListInternal(
192 const NetworkInterfaceList& interfaces,
193 internal::GetInterfaceSSIDFunction get_interface_ssid) {
194 std::string connected_ssid;
195 bool first = true;
196 for (size_t i = 0; i < interfaces.size(); ++i) {
197 if (interfaces[i].type != NetworkChangeNotifier::CONNECTION_WIFI)
198 return "";
199 std::string ssid = get_interface_ssid(interfaces[i].name);
200 if (first) {
201 first = false;
202 connected_ssid = ssid;
203 } else if (ssid != connected_ssid) {
204 return "";
205 }
206 }
207 return connected_ssid;
208 }
209
175 } // namespace internal 210 } // namespace internal
176 211
177 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { 212 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
178 if (networks == NULL) 213 if (networks == NULL)
179 return false; 214 return false;
180 215
181 internal::AddressTrackerLinux tracker; 216 internal::AddressTrackerLinux tracker;
182 tracker.Init(); 217 tracker.Init();
183 218
184 return internal::GetNetworkListImpl( 219 return internal::GetNetworkListImpl(
185 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), 220 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(),
186 &internal::AddressTrackerLinux::GetInterfaceName); 221 &internal::AddressTrackerLinux::GetInterfaceName);
187 } 222 }
188 223
224 std::string GetWifiSSID() {
225 NetworkInterfaceList networks;
226 if (GetNetworkList(&networks, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) {
227 return internal::GetWifiSSIDFromInterfaceListInternal(
228 networks, internal::GetInterfaceSSID);
229 }
230 return "";
231 }
232
189 } // namespace net 233 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698