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

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

Issue 2071273004: Call GetAdaptersAddresses in the way recommended by MSDN. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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 | « no previous file | 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) 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"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/strings/string_piece.h" 12 #include "base/strings/string_piece.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
17 #include "base/win/scoped_handle.h" 17 #include "base/win/scoped_handle.h"
18 #include "net/base/escape.h" 18 #include "net/base/escape.h"
19 #include "net/base/ip_endpoint.h" 19 #include "net/base/ip_endpoint.h"
20 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.h"
21 #include "url/gurl.h" 21 #include "url/gurl.h"
22 22
23 namespace net { 23 namespace net {
24 24
25 namespace { 25 namespace {
26 26
27 // Max number of times to retry GetAdaptersAddresses due to
28 // ERROR_BUFFER_OVERFLOW. If GetAdaptersAddresses returns this indefinitely
29 // due to some Windows bug, we don't want to be stuck in an endless loop.
30 const int MAX_GETADAPTERSADDRESSES_TRIES = 100;
pauljensen 2016/06/20 11:32:49 100 seems like a lot. I imagine 10 should be plen
pauljensen 2016/06/20 11:32:49 how about moving this constant into the function?
Taylor_Brandstetter 2016/06/21 17:06:04 Done.
Taylor_Brandstetter 2016/06/21 17:06:05 Done. I thought to make it a large number so that
31
27 // Converts Windows defined types to NetworkInterfaceType. 32 // Converts Windows defined types to NetworkInterfaceType.
28 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { 33 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) {
29 NetworkChangeNotifier::ConnectionType type = 34 NetworkChangeNotifier::ConnectionType type =
30 NetworkChangeNotifier::CONNECTION_UNKNOWN; 35 NetworkChangeNotifier::CONNECTION_UNKNOWN;
31 if (ifType == IF_TYPE_ETHERNET_CSMACD) { 36 if (ifType == IF_TYPE_ETHERNET_CSMACD) {
32 type = NetworkChangeNotifier::CONNECTION_ETHERNET; 37 type = NetworkChangeNotifier::CONNECTION_ETHERNET;
33 } else if (ifType == IF_TYPE_IEEE80211) { 38 } else if (ifType == IF_TYPE_IEEE80211) {
34 type = NetworkChangeNotifier::CONNECTION_WIFI; 39 type = NetworkChangeNotifier::CONNECTION_WIFI;
35 } 40 }
36 // TODO(mallinath) - Cellular? 41 // TODO(mallinath) - Cellular?
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 } 195 }
191 } 196 }
192 } 197 }
193 } 198 }
194 return true; 199 return true;
195 } 200 }
196 201
197 } // namespace internal 202 } // namespace internal
198 203
199 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { 204 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
200 ULONG len = 0; 205 // Use an initial buffer size of 15KB, as recommended by MSDN.
pauljensen 2016/06/20 11:32:49 can you provide a link to this recommendation?
Taylor_Brandstetter 2016/06/21 17:06:04 Done.
206 ULONG len = 15360;
pauljensen 2016/06/21 20:32:05 Actually I think Windows may want you to use 15000
Taylor_Brandstetter 2016/06/21 20:45:18 Done, good catch.
201 ULONG flags = 0; 207 ULONG flags = 0;
202 // GetAdaptersAddresses() may require IO operations. 208 // GetAdaptersAddresses() may require IO operations.
203 base::ThreadRestrictions::AssertIOAllowed(); 209 base::ThreadRestrictions::AssertIOAllowed();
204 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); 210 // Call GetAdaptersAddresses in a loop, because the required size may
205 if (result != ERROR_BUFFER_OVERFLOW) { 211 // increase between successive calls, resulting in ERROR_BUFFER_OVERFLOW
212 // multiple times.
213 std::unique_ptr<char[]> buf;
pauljensen 2016/06/20 11:32:49 can we type this as IP_ADAPTER_ADDRESSES*?
Taylor_Brandstetter 2016/06/21 17:06:05 I think that may be confusing, because the buffer'
pauljensen 2016/06/21 17:20:10 I assume IP_ADAPTER_ADDRESSES is POD so it's silly
Taylor_Brandstetter 2016/06/21 19:50:51 Well... It depends on your definition of POD. It c
pauljensen 2016/06/21 20:32:04 I think the POD definition is pretty clear; isn't
214 IP_ADAPTER_ADDRESSES* adapters;
215 ULONG result;
216 int tries = 0;
217 do {
218 buf.reset(new char[len]);
pauljensen 2016/06/20 11:32:49 it would be nice if the first 15k buffer could be
Taylor_Brandstetter 2016/06/21 17:06:04 Done.
219 adapters = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get());
220 result = GetAdaptersAddresses(AF_UNSPEC, flags, nullptr, adapters, &len);
221 } while (result == ERROR_BUFFER_OVERFLOW &&
222 ++tries < MAX_GETADAPTERSADDRESSES_TRIES);
223 if (result == ERROR_NO_DATA) {
206 // There are 0 networks. 224 // There are 0 networks.
207 return true; 225 return true;
208 } 226 } 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; 227 LOG(ERROR) << "GetAdaptersAddresses failed: " << result;
215 return false; 228 return false;
216 } 229 }
217 230
218 return internal::GetNetworkListImpl(networks, policy, adapters); 231 return internal::GetNetworkListImpl(networks, policy, adapters);
219 } 232 }
220 233
221 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { 234 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
222 auto conn_info = GetConnectionAttributes(); 235 auto conn_info = GetConnectionAttributes();
223 236
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 318
306 if (!conn_info.get()) 319 if (!conn_info.get())
307 return ""; 320 return "";
308 321
309 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; 322 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid;
310 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), 323 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID),
311 dot11_ssid.uSSIDLength); 324 dot11_ssid.uSSIDLength);
312 } 325 }
313 326
314 } // namespace net 327 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698