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

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

Issue 2890773002: When creating sockets for ioctl() use AF_INET6 or AF_INET (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « net/base/network_interfaces_linux.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/network_interfaces_linux.h" 5 #include "net/base/network_interfaces_linux.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #if !defined(OS_ANDROID) 9 #if !defined(OS_ANDROID)
10 #include <linux/ethtool.h> 10 #include <linux/ethtool.h>
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 69 }
70 70
71 } // namespace 71 } // namespace
72 72
73 namespace internal { 73 namespace internal {
74 74
75 // Gets the connection type for interface |ifname| by checking for wireless 75 // Gets the connection type for interface |ifname| by checking for wireless
76 // or ethtool extensions. 76 // or ethtool extensions.
77 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( 77 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType(
78 const std::string& ifname) { 78 const std::string& ifname) {
79 base::ScopedFD s(socket(AF_INET, SOCK_STREAM, 0)); 79 base::ScopedFD s = GetSocketForIoctl();
80 if (!s.is_valid()) 80 if (!s.is_valid())
81 return NetworkChangeNotifier::CONNECTION_UNKNOWN; 81 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
82 82
83 // Test wireless extensions for CONNECTION_WIFI 83 // Test wireless extensions for CONNECTION_WIFI
84 struct iwreq pwrq = {}; 84 struct iwreq pwrq = {};
85 strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ - 1); 85 strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
86 if (ioctl(s.get(), SIOCGIWNAME, &pwrq) != -1) 86 if (ioctl(s.get(), SIOCGIWNAME, &pwrq) != -1)
87 return NetworkChangeNotifier::CONNECTION_WIFI; 87 return NetworkChangeNotifier::CONNECTION_WIFI;
88 88
89 #if !defined(OS_ANDROID) 89 #if !defined(OS_ANDROID)
90 // Test ethtool for CONNECTION_ETHERNET 90 // Test ethtool for CONNECTION_ETHERNET
91 struct ethtool_cmd ecmd = {}; 91 struct ethtool_cmd ecmd = {};
92 ecmd.cmd = ETHTOOL_GSET; 92 ecmd.cmd = ETHTOOL_GSET;
93 struct ifreq ifr = {}; 93 struct ifreq ifr = {};
94 ifr.ifr_data = &ecmd; 94 ifr.ifr_data = &ecmd;
95 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); 95 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
96 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) 96 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1)
97 return NetworkChangeNotifier::CONNECTION_ETHERNET; 97 return NetworkChangeNotifier::CONNECTION_ETHERNET;
98 #endif // !defined(OS_ANDROID) 98 #endif // !defined(OS_ANDROID)
99 99
100 return NetworkChangeNotifier::CONNECTION_UNKNOWN; 100 return NetworkChangeNotifier::CONNECTION_UNKNOWN;
101 } 101 }
102 102
103 std::string GetInterfaceSSID(const std::string& ifname) { 103 std::string GetInterfaceSSID(const std::string& ifname) {
104 base::ScopedFD ioctl_socket(socket(AF_INET, SOCK_DGRAM, 0)); 104 base::ScopedFD ioctl_socket = GetSocketForIoctl();
105 if (!ioctl_socket.is_valid()) 105 if (!ioctl_socket.is_valid())
106 return ""; 106 return "";
107 struct iwreq wreq = {}; 107 struct iwreq wreq = {};
108 strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1); 108 strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
109 109
110 char ssid[IW_ESSID_MAX_SIZE + 1] = {0}; 110 char ssid[IW_ESSID_MAX_SIZE + 1] = {0};
111 wreq.u.essid.pointer = ssid; 111 wreq.u.essid.pointer = ssid;
112 wreq.u.essid.length = IW_ESSID_MAX_SIZE; 112 wreq.u.essid.length = IW_ESSID_MAX_SIZE;
113 if (ioctl(ioctl_socket.get(), SIOCGIWESSID, &wreq) != -1) 113 if (ioctl(ioctl_socket.get(), SIOCGIWESSID, &wreq) != -1)
114 return ssid; 114 return ssid;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 std::string ssid = get_interface_ssid(interfaces[i].name); 194 std::string ssid = get_interface_ssid(interfaces[i].name);
195 if (i == 0) { 195 if (i == 0) {
196 connected_ssid = ssid; 196 connected_ssid = ssid;
197 } else if (ssid != connected_ssid) { 197 } else if (ssid != connected_ssid) {
198 return ""; 198 return "";
199 } 199 }
200 } 200 }
201 return connected_ssid; 201 return connected_ssid;
202 } 202 }
203 203
204 base::ScopedFD GetSocketForIoctl() {
205 base::ScopedFD ioctl_socket(socket(AF_INET6, SOCK_DGRAM, 0));
206 if (ioctl_socket.is_valid())
207 return ioctl_socket;
208 return base::ScopedFD(socket(AF_INET, SOCK_DGRAM, 0));
209 }
210
204 } // namespace internal 211 } // namespace internal
205 212
206 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { 213 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
207 if (networks == NULL) 214 if (networks == NULL)
208 return false; 215 return false;
209 216
210 internal::AddressTrackerLinux tracker; 217 internal::AddressTrackerLinux tracker;
211 tracker.Init(); 218 tracker.Init();
212 219
213 return internal::GetNetworkListImpl( 220 return internal::GetNetworkListImpl(
214 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), 221 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(),
215 &internal::AddressTrackerLinux::GetInterfaceName); 222 &internal::AddressTrackerLinux::GetInterfaceName);
216 } 223 }
217 224
218 std::string GetWifiSSID() { 225 std::string GetWifiSSID() {
219 // On Android, obtain the SSID using the Android-specific APIs. 226 // On Android, obtain the SSID using the Android-specific APIs.
220 #if defined(OS_ANDROID) 227 #if defined(OS_ANDROID)
221 return android::GetWifiSSID(); 228 return android::GetWifiSSID();
222 #endif 229 #endif
223 NetworkInterfaceList networks; 230 NetworkInterfaceList networks;
224 if (GetNetworkList(&networks, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { 231 if (GetNetworkList(&networks, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) {
225 return internal::GetWifiSSIDFromInterfaceListInternal( 232 return internal::GetWifiSSIDFromInterfaceListInternal(
226 networks, internal::GetInterfaceSSID); 233 networks, internal::GetInterfaceSSID);
227 } 234 }
228 return ""; 235 return "";
229 } 236 }
230 237
231 } // namespace net 238 } // namespace net
OLDNEW
« no previous file with comments | « net/base/network_interfaces_linux.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698