Roll ICU to r205936
[chromium-blink-merge.git] / net / base / net_util_win.cc
blob36d5d2c613e0f701f827224afd3a6e9097f5f8d5
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
3 // found in the LICENSE file.
5 #include "net/base/net_util.h"
7 #include <iphlpapi.h>
8 #include <wlanapi.h>
10 #include <algorithm>
12 #include "base/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string_piece.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/thread_restrictions.h"
20 #include "base/win/scoped_handle.h"
21 #include "googleurl/src/gurl.h"
22 #include "net/base/escape.h"
23 #include "net/base/ip_endpoint.h"
24 #include "net/base/net_errors.h"
26 namespace net {
28 bool FileURLToFilePath(const GURL& url, base::FilePath* file_path) {
29 *file_path = base::FilePath();
30 std::wstring& file_path_str = const_cast<std::wstring&>(file_path->value());
31 file_path_str.clear();
33 if (!url.is_valid())
34 return false;
36 std::string path;
37 std::string host = url.host();
38 if (host.empty()) {
39 // URL contains no host, the path is the filename. In this case, the path
40 // will probably be preceeded with a slash, as in "/C:/foo.txt", so we
41 // trim out that here.
42 path = url.path();
43 size_t first_non_slash = path.find_first_not_of("/\\");
44 if (first_non_slash != std::string::npos && first_non_slash > 0)
45 path.erase(0, first_non_slash);
46 } else {
47 // URL contains a host: this means it's UNC. We keep the preceeding slash
48 // on the path.
49 path = "\\\\";
50 path.append(host);
51 path.append(url.path());
54 if (path.empty())
55 return false;
56 std::replace(path.begin(), path.end(), '/', '\\');
58 // GURL stores strings as percent-encoded UTF-8, this will undo if possible.
59 path = UnescapeURLComponent(path,
60 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
62 if (!IsStringUTF8(path)) {
63 // Not UTF-8, assume encoding is native codepage and we're done. We know we
64 // are giving the conversion function a nonempty string, and it may fail if
65 // the given string is not in the current encoding and give us an empty
66 // string back. We detect this and report failure.
67 file_path_str = base::SysNativeMBToWide(path);
68 return !file_path_str.empty();
70 file_path_str.assign(UTF8ToWide(path));
72 // We used to try too hard and see if |path| made up entirely of
73 // the 1st 256 characters in the Unicode was a zero-extended UTF-16.
74 // If so, we converted it to 'Latin-1' and checked if the result was UTF-8.
75 // If the check passed, we converted the result to UTF-8.
76 // Otherwise, we treated the result as the native OS encoding.
77 // However, that led to http://crbug.com/4619 and http://crbug.com/14153
78 return true;
81 bool GetNetworkList(NetworkInterfaceList* networks) {
82 // GetAdaptersAddresses() may require IO operations.
83 base::ThreadRestrictions::AssertIOAllowed();
85 IP_ADAPTER_ADDRESSES info_temp;
86 ULONG len = 0;
88 // First get number of networks.
89 ULONG result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, &info_temp, &len);
90 if (result != ERROR_BUFFER_OVERFLOW) {
91 // There are 0 networks.
92 return true;
95 scoped_ptr<char[]> buf(new char[len]);
96 IP_ADAPTER_ADDRESSES *adapters =
97 reinterpret_cast<IP_ADAPTER_ADDRESSES *>(buf.get());
98 result = GetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &len);
99 if (result != NO_ERROR) {
100 LOG(ERROR) << "GetAdaptersAddresses failed: " << result;
101 return false;
104 for (IP_ADAPTER_ADDRESSES *adapter = adapters; adapter != NULL;
105 adapter = adapter->Next) {
106 // Ignore the loopback device.
107 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
108 continue;
111 if (adapter->OperStatus != IfOperStatusUp) {
112 continue;
115 IP_ADAPTER_UNICAST_ADDRESS* address;
116 for (address = adapter->FirstUnicastAddress; address != NULL;
117 address = address->Next) {
118 int family = address->Address.lpSockaddr->sa_family;
119 if (family == AF_INET || family == AF_INET6) {
120 IPEndPoint endpoint;
121 if (endpoint.FromSockAddr(address->Address.lpSockaddr,
122 address->Address.iSockaddrLength)) {
123 std::string name = adapter->AdapterName;
124 networks->push_back(NetworkInterface(name, endpoint.address()));
130 return true;
133 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
134 struct WlanApi {
135 typedef DWORD (WINAPI *WlanOpenHandleFunc)(
136 DWORD, VOID*, DWORD*, HANDLE*);
137 typedef DWORD (WINAPI *WlanEnumInterfacesFunc)(
138 HANDLE, VOID*, WLAN_INTERFACE_INFO_LIST **);
139 typedef DWORD (WINAPI *WlanQueryInterfaceFunc)(
140 HANDLE, const GUID *, WLAN_INTF_OPCODE, VOID*, DWORD*, VOID**,
141 WLAN_OPCODE_VALUE_TYPE*);
142 typedef VOID (WINAPI *WlanFreeMemoryFunc)(VOID*);
143 typedef DWORD (WINAPI *WlanCloseHandleFunc)(HANDLE, VOID*);
145 WlanApi() : initialized(false) {
146 // Use an absolute path to load the DLL to avoid DLL preloading attacks.
147 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll";
148 wchar_t path[MAX_PATH] = {0};
149 ExpandEnvironmentStrings(kDLL, path, arraysize(path));
150 module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
151 if (!module)
152 return;
154 open_handle_func = reinterpret_cast<WlanOpenHandleFunc>(
155 ::GetProcAddress(module, "WlanOpenHandle"));
156 enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>(
157 ::GetProcAddress(module, "WlanEnumInterfaces"));
158 query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>(
159 ::GetProcAddress(module, "WlanQueryInterface"));
160 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>(
161 ::GetProcAddress(module, "WlanFreeMemory"));
162 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>(
163 ::GetProcAddress(module, "WlanCloseHandle"));
164 initialized = open_handle_func && enum_interfaces_func &&
165 query_interface_func && free_memory_func &&
166 close_handle_func;
169 HMODULE module;
170 WlanOpenHandleFunc open_handle_func;
171 WlanEnumInterfacesFunc enum_interfaces_func;
172 WlanQueryInterfaceFunc query_interface_func;
173 WlanFreeMemoryFunc free_memory_func;
174 WlanCloseHandleFunc close_handle_func;
175 bool initialized;
178 static base::LazyInstance<WlanApi>::Leaky lazy_wlanapi =
179 LAZY_INSTANCE_INITIALIZER;
181 struct WlanApiHandleTraits {
182 typedef HANDLE Handle;
184 static bool CloseHandle(HANDLE handle) {
185 return lazy_wlanapi.Get().close_handle_func(handle, NULL) ==
186 ERROR_SUCCESS;
188 static bool IsHandleValid(HANDLE handle) {
189 return base::win::HandleTraits::IsHandleValid(handle);
191 static HANDLE NullHandle() {
192 return base::win::HandleTraits::NullHandle();
196 typedef base::win::GenericScopedHandle<WlanApiHandleTraits,
197 base::win::VerifierTraits> WlanHandle;
199 struct WlanApiDeleter {
200 inline void operator()(void* ptr) const {
201 lazy_wlanapi.Get().free_memory_func(ptr);
205 const WlanApi& wlanapi = lazy_wlanapi.Get();
206 if (!wlanapi.initialized)
207 return WIFI_PHY_LAYER_PROTOCOL_NONE;
209 WlanHandle client;
210 DWORD cur_version = 0;
211 const DWORD kMaxClientVersion = 2;
212 DWORD result = wlanapi.open_handle_func(kMaxClientVersion, NULL, &cur_version,
213 client.Receive());
214 if (result != ERROR_SUCCESS)
215 return WIFI_PHY_LAYER_PROTOCOL_NONE;
217 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL;
218 result = wlanapi.enum_interfaces_func(client, NULL, &interface_list_ptr);
219 if (result != ERROR_SUCCESS)
220 return WIFI_PHY_LAYER_PROTOCOL_NONE;
221 scoped_ptr<WLAN_INTERFACE_INFO_LIST, WlanApiDeleter> interface_list(
222 interface_list_ptr);
224 // Assume at most one connected wifi interface.
225 WLAN_INTERFACE_INFO* info = NULL;
226 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) {
227 if (interface_list->InterfaceInfo[i].isState ==
228 wlan_interface_state_connected) {
229 info = &interface_list->InterfaceInfo[i];
230 break;
234 if (info == NULL)
235 return WIFI_PHY_LAYER_PROTOCOL_NONE;
237 WLAN_CONNECTION_ATTRIBUTES* conn_info_ptr;
238 DWORD conn_info_size = 0;
239 WLAN_OPCODE_VALUE_TYPE op_code;
240 result = wlanapi.query_interface_func(
241 client, &info->InterfaceGuid, wlan_intf_opcode_current_connection, NULL,
242 &conn_info_size, reinterpret_cast<VOID**>(&conn_info_ptr), &op_code);
243 if (result != ERROR_SUCCESS)
244 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
245 scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, WlanApiDeleter> conn_info(
246 conn_info_ptr);
248 switch (conn_info->wlanAssociationAttributes.dot11PhyType) {
249 case dot11_phy_type_fhss:
250 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT;
251 case dot11_phy_type_dsss:
252 return WIFI_PHY_LAYER_PROTOCOL_B;
253 case dot11_phy_type_irbaseband:
254 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT;
255 case dot11_phy_type_ofdm:
256 return WIFI_PHY_LAYER_PROTOCOL_A;
257 case dot11_phy_type_hrdsss:
258 return WIFI_PHY_LAYER_PROTOCOL_B;
259 case dot11_phy_type_erp:
260 return WIFI_PHY_LAYER_PROTOCOL_G;
261 case dot11_phy_type_ht:
262 return WIFI_PHY_LAYER_PROTOCOL_N;
263 default:
264 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
268 } // namespace net