| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_win.h" | 5 #include "net/base/network_interfaces_win.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 return true; | 194 return true; |
| 195 } | 195 } |
| 196 | 196 |
| 197 } // namespace internal | 197 } // namespace internal |
| 198 | 198 |
| 199 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 199 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 200 ULONG len = 0; | 200 // Max number of times to retry GetAdaptersAddresses due to |
| 201 // ERROR_BUFFER_OVERFLOW. If GetAdaptersAddresses returns this indefinitely |
| 202 // due to an unforseen reason, we don't want to be stuck in an endless loop. |
| 203 static constexpr int MAX_GETADAPTERSADDRESSES_TRIES = 10; |
| 204 // Use an initial buffer size of 15KB, as recommended by MSDN. See: |
| 205 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).
aspx |
| 206 static constexpr int INITIAL_BUFFER_SIZE = 15000; |
| 207 |
| 208 ULONG len = INITIAL_BUFFER_SIZE; |
| 201 ULONG flags = 0; | 209 ULONG flags = 0; |
| 210 // Initial buffer allocated on stack. |
| 211 char initial_buf[INITIAL_BUFFER_SIZE]; |
| 212 // Dynamic buffer in case initial buffer isn't large enough. |
| 213 std::unique_ptr<char[]> buf; |
| 214 |
| 202 // GetAdaptersAddresses() may require IO operations. | 215 // GetAdaptersAddresses() may require IO operations. |
| 203 base::ThreadRestrictions::AssertIOAllowed(); | 216 base::ThreadRestrictions::AssertIOAllowed(); |
| 204 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); | 217 |
| 205 if (result != ERROR_BUFFER_OVERFLOW) { | 218 IP_ADAPTER_ADDRESSES* adapters = |
| 219 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(&initial_buf); |
| 220 ULONG result = |
| 221 GetAdaptersAddresses(AF_UNSPEC, flags, nullptr, adapters, &len); |
| 222 |
| 223 // If we get ERROR_BUFFER_OVERFLOW, call GetAdaptersAddresses in a loop, |
| 224 // because the required size may increase between successive calls, resulting |
| 225 // in ERROR_BUFFER_OVERFLOW multiple times. |
| 226 for (int tries = 1; result == ERROR_BUFFER_OVERFLOW && |
| 227 tries < MAX_GETADAPTERSADDRESSES_TRIES; |
| 228 ++tries) { |
| 229 buf.reset(new char[len]); |
| 230 adapters = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); |
| 231 result = GetAdaptersAddresses(AF_UNSPEC, flags, nullptr, adapters, &len); |
| 232 } |
| 233 |
| 234 if (result == ERROR_NO_DATA) { |
| 206 // There are 0 networks. | 235 // There are 0 networks. |
| 207 return true; | 236 return true; |
| 208 } | 237 } else if (result != NO_ERROR) { |
| 209 std::unique_ptr<char[]> buf(new char[len]); | |
| 210 IP_ADAPTER_ADDRESSES* adapters = | |
| 211 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); | |
| 212 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); | |
| 213 if (result != NO_ERROR) { | |
| 214 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; | 238 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; |
| 215 return false; | 239 return false; |
| 216 } | 240 } |
| 217 | 241 |
| 218 return internal::GetNetworkListImpl(networks, policy, adapters); | 242 return internal::GetNetworkListImpl(networks, policy, adapters); |
| 219 } | 243 } |
| 220 | 244 |
| 221 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 245 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
| 222 auto conn_info = GetConnectionAttributes(); | 246 auto conn_info = GetConnectionAttributes(); |
| 223 | 247 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 | 329 |
| 306 if (!conn_info.get()) | 330 if (!conn_info.get()) |
| 307 return ""; | 331 return ""; |
| 308 | 332 |
| 309 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; | 333 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; |
| 310 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), | 334 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), |
| 311 dot11_ssid.uSSIDLength); | 335 dot11_ssid.uSSIDLength); |
| 312 } | 336 } |
| 313 | 337 |
| 314 } // namespace net | 338 } // namespace net |
| OLD | NEW |