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

Unified 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: Use || instead of or 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 side-by-side diff with in-line comments
Download patch
Index: net/base/net_util_linux.cc
diff --git a/net/base/net_util_linux.cc b/net/base/net_util_linux.cc
index 36c9eb706ecfdaaa4d5cff4abdc73aac357795fd..e41f0edae01004517422894427b97ab82656d081 100644
--- a/net/base/net_util_linux.cc
+++ b/net/base/net_util_linux.cc
@@ -103,6 +103,21 @@ NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType(
return NetworkChangeNotifier::CONNECTION_UNKNOWN;
}
+std::string GetInterfaceSSID(const std::string& ifname) {
+ base::ScopedFD ioctl_socket(socket(AF_INET, SOCK_DGRAM, 0));
+ if (!ioctl_socket.is_valid())
+ return "";
+ struct iwreq wreq = {};
+ strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
+
+ char ssid[IW_ESSID_MAX_SIZE + 1] = {0};
+ wreq.u.essid.pointer = ssid;
+ wreq.u.essid.length = IW_ESSID_MAX_SIZE;
+ if (ioctl(ioctl_socket.get(), SIOCGIWESSID, &wreq) != -1)
+ return ssid;
+ return "";
+}
+
bool GetNetworkListImpl(
NetworkInterfaceList* networks,
int policy,
@@ -172,6 +187,25 @@ bool GetNetworkListImpl(
return true;
}
+std::string GetWifiSSIDFromInterfaceListInternal(
+ const NetworkInterfaceList& interfaces,
+ internal::GetInterfaceSSIDFunction get_interface_ssid) {
+ std::string connected_ssid;
+ bool first = true;
+ for (size_t i = 0; i < interfaces.size(); ++i) {
+ if (interfaces[i].type != NetworkChangeNotifier::CONNECTION_WIFI)
+ return "";
+ std::string ssid = get_interface_ssid(interfaces[i].name);
+ 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.
+ first = false;
+ connected_ssid = ssid;
+ } else if (ssid != connected_ssid) {
+ return "";
+ }
+ }
+ return connected_ssid;
+}
+
} // namespace internal
bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
@@ -186,4 +220,13 @@ bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
&internal::AddressTrackerLinux::GetInterfaceName);
}
+std::string GetWifiSSID() {
+ NetworkInterfaceList networks;
+ if (GetNetworkList(&networks, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) {
+ return internal::GetWifiSSIDFromInterfaceListInternal(
+ networks, internal::GetInterfaceSSID);
+ }
+ return "";
+}
+
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698