- add sources.
[platform/framework/web/crosswalk.git] / src / net / base / net_util_win.cc
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.
4
5 #include "net/base/net_util.h"
6
7 #include <iphlpapi.h>
8 #include <wlanapi.h>
9
10 #include <algorithm>
11
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 "base/win/windows_version.h"
22 #include "net/base/escape.h"
23 #include "net/base/ip_endpoint.h"
24 #include "net/base/net_errors.h"
25 #include "url/gurl.h"
26
27 namespace {
28
29 struct WlanApi {
30   typedef DWORD (WINAPI *WlanOpenHandleFunc)(
31       DWORD, VOID*, DWORD*, HANDLE*);
32   typedef DWORD (WINAPI *WlanEnumInterfacesFunc)(
33       HANDLE, VOID*, WLAN_INTERFACE_INFO_LIST**);
34   typedef DWORD (WINAPI *WlanQueryInterfaceFunc)(
35       HANDLE, const GUID*, WLAN_INTF_OPCODE, VOID*, DWORD*, VOID**,
36       WLAN_OPCODE_VALUE_TYPE*);
37   typedef VOID (WINAPI *WlanFreeMemoryFunc)(VOID*);
38   typedef DWORD (WINAPI *WlanCloseHandleFunc)(HANDLE, VOID*);
39
40   WlanApi() : initialized(false) {
41     // Use an absolute path to load the DLL to avoid DLL preloading attacks.
42     static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll";
43     wchar_t path[MAX_PATH] = {0};
44     ExpandEnvironmentStrings(kDLL, path, arraysize(path));
45     module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
46     if (!module)
47       return;
48
49     open_handle_func = reinterpret_cast<WlanOpenHandleFunc>(
50         ::GetProcAddress(module, "WlanOpenHandle"));
51     enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>(
52         ::GetProcAddress(module, "WlanEnumInterfaces"));
53     query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>(
54         ::GetProcAddress(module, "WlanQueryInterface"));
55     free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>(
56         ::GetProcAddress(module, "WlanFreeMemory"));
57     close_handle_func = reinterpret_cast<WlanCloseHandleFunc>(
58         ::GetProcAddress(module, "WlanCloseHandle"));
59     initialized = open_handle_func && enum_interfaces_func &&
60                   query_interface_func && free_memory_func &&
61                   close_handle_func;
62   }
63
64   HMODULE module;
65   WlanOpenHandleFunc open_handle_func;
66   WlanEnumInterfacesFunc enum_interfaces_func;
67   WlanQueryInterfaceFunc query_interface_func;
68   WlanFreeMemoryFunc free_memory_func;
69   WlanCloseHandleFunc close_handle_func;
70   bool initialized;
71 };
72
73 }  // namespace
74
75 namespace net {
76
77 bool FileURLToFilePath(const GURL& url, base::FilePath* file_path) {
78   *file_path = base::FilePath();
79   std::wstring& file_path_str = const_cast<std::wstring&>(file_path->value());
80   file_path_str.clear();
81
82   if (!url.is_valid())
83     return false;
84
85   std::string path;
86   std::string host = url.host();
87   if (host.empty()) {
88     // URL contains no host, the path is the filename. In this case, the path
89     // will probably be preceeded with a slash, as in "/C:/foo.txt", so we
90     // trim out that here.
91     path = url.path();
92     size_t first_non_slash = path.find_first_not_of("/\\");
93     if (first_non_slash != std::string::npos && first_non_slash > 0)
94       path.erase(0, first_non_slash);
95   } else {
96     // URL contains a host: this means it's UNC. We keep the preceeding slash
97     // on the path.
98     path = "\\\\";
99     path.append(host);
100     path.append(url.path());
101   }
102
103   if (path.empty())
104     return false;
105   std::replace(path.begin(), path.end(), '/', '\\');
106
107   // GURL stores strings as percent-encoded UTF-8, this will undo if possible.
108   path = UnescapeURLComponent(path,
109       UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
110
111   if (!IsStringUTF8(path)) {
112     // Not UTF-8, assume encoding is native codepage and we're done. We know we
113     // are giving the conversion function a nonempty string, and it may fail if
114     // the given string is not in the current encoding and give us an empty
115     // string back. We detect this and report failure.
116     file_path_str = base::SysNativeMBToWide(path);
117     return !file_path_str.empty();
118   }
119   file_path_str.assign(UTF8ToWide(path));
120
121   // We used to try too hard and see if |path| made up entirely of
122   // the 1st 256 characters in the Unicode was a zero-extended UTF-16.
123   // If so, we converted it to 'Latin-1' and checked if the result was UTF-8.
124   // If the check passed, we converted the result to UTF-8.
125   // Otherwise, we treated the result as the native OS encoding.
126   // However, that led to http://crbug.com/4619 and http://crbug.com/14153
127   return true;
128 }
129
130 bool GetNetworkList(NetworkInterfaceList* networks) {
131   // GetAdaptersAddresses() may require IO operations.
132   base::ThreadRestrictions::AssertIOAllowed();
133   bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA;
134   ULONG len = 0;
135   ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0;
136   // First get number of networks.
137   ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len);
138   if (result != ERROR_BUFFER_OVERFLOW) {
139     // There are 0 networks.
140     return true;
141   }
142   scoped_ptr<char[]> buf(new char[len]);
143   IP_ADAPTER_ADDRESSES *adapters =
144       reinterpret_cast<IP_ADAPTER_ADDRESSES *>(buf.get());
145   result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len);
146   if (result != NO_ERROR) {
147     LOG(ERROR) << "GetAdaptersAddresses failed: " << result;
148     return false;
149   }
150
151   for (IP_ADAPTER_ADDRESSES *adapter = adapters; adapter != NULL;
152        adapter = adapter->Next) {
153     // Ignore the loopback device.
154     if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
155       continue;
156     }
157
158     if (adapter->OperStatus != IfOperStatusUp) {
159       continue;
160     }
161
162     std::string name = adapter->AdapterName;
163
164     for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress;
165          address; address = address->Next) {
166       int family = address->Address.lpSockaddr->sa_family;
167       if (family == AF_INET || family == AF_INET6) {
168         IPEndPoint endpoint;
169         if (endpoint.FromSockAddr(address->Address.lpSockaddr,
170                                   address->Address.iSockaddrLength)) {
171           // XP has no OnLinkPrefixLength field.
172           size_t net_prefix = is_xp ? 0 : address->OnLinkPrefixLength;
173           if (is_xp) {
174             // Prior to Windows Vista the FirstPrefix pointed to the list with
175             // single prefix for each IP address assigned to the adapter.
176             // Order of FirstPrefix does not match order of FirstUnicastAddress,
177             // so we need to find corresponding prefix.
178             for (IP_ADAPTER_PREFIX* prefix = adapter->FirstPrefix; prefix;
179                  prefix = prefix->Next) {
180               int prefix_family = prefix->Address.lpSockaddr->sa_family;
181               IPEndPoint network_endpoint;
182               if (prefix_family == family &&
183                   network_endpoint.FromSockAddr(prefix->Address.lpSockaddr,
184                       prefix->Address.iSockaddrLength) &&
185                   IPNumberMatchesPrefix(endpoint.address(),
186                                         network_endpoint.address(),
187                                         prefix->PrefixLength)) {
188                 net_prefix = std::max<size_t>(net_prefix, prefix->PrefixLength);
189               }
190             }
191           }
192           networks->push_back(
193               NetworkInterface(adapter->AdapterName, endpoint.address(),
194                                net_prefix));
195         }
196       }
197     }
198   }
199
200   return true;
201 }
202
203 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
204   static base::LazyInstance<WlanApi>::Leaky lazy_wlanapi =
205       LAZY_INSTANCE_INITIALIZER;
206
207   struct WlanApiHandleTraits {
208     typedef HANDLE Handle;
209
210     static bool CloseHandle(HANDLE handle) {
211       return lazy_wlanapi.Get().close_handle_func(handle, NULL) ==
212           ERROR_SUCCESS;
213     }
214     static bool IsHandleValid(HANDLE handle) {
215       return base::win::HandleTraits::IsHandleValid(handle);
216     }
217     static HANDLE NullHandle() {
218       return base::win::HandleTraits::NullHandle();
219     }
220   };
221
222   typedef base::win::GenericScopedHandle<
223       WlanApiHandleTraits,
224       base::win::DummyVerifierTraits> WlanHandle;
225
226   struct WlanApiDeleter {
227     inline void operator()(void* ptr) const {
228       lazy_wlanapi.Get().free_memory_func(ptr);
229     }
230   };
231
232   const WlanApi& wlanapi = lazy_wlanapi.Get();
233   if (!wlanapi.initialized)
234     return WIFI_PHY_LAYER_PROTOCOL_NONE;
235
236   WlanHandle client;
237   DWORD cur_version = 0;
238   const DWORD kMaxClientVersion = 2;
239   DWORD result = wlanapi.open_handle_func(kMaxClientVersion, NULL, &cur_version,
240                                           client.Receive());
241   if (result != ERROR_SUCCESS)
242     return WIFI_PHY_LAYER_PROTOCOL_NONE;
243
244   WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL;
245   result = wlanapi.enum_interfaces_func(client, NULL, &interface_list_ptr);
246   if (result != ERROR_SUCCESS)
247     return WIFI_PHY_LAYER_PROTOCOL_NONE;
248   scoped_ptr<WLAN_INTERFACE_INFO_LIST, WlanApiDeleter> interface_list(
249       interface_list_ptr);
250
251   // Assume at most one connected wifi interface.
252   WLAN_INTERFACE_INFO* info = NULL;
253   for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) {
254     if (interface_list->InterfaceInfo[i].isState ==
255         wlan_interface_state_connected) {
256       info = &interface_list->InterfaceInfo[i];
257       break;
258     }
259   }
260
261   if (info == NULL)
262     return WIFI_PHY_LAYER_PROTOCOL_NONE;
263
264   WLAN_CONNECTION_ATTRIBUTES* conn_info_ptr;
265   DWORD conn_info_size = 0;
266   WLAN_OPCODE_VALUE_TYPE op_code;
267   result = wlanapi.query_interface_func(
268       client, &info->InterfaceGuid, wlan_intf_opcode_current_connection, NULL,
269       &conn_info_size, reinterpret_cast<VOID**>(&conn_info_ptr), &op_code);
270   if (result != ERROR_SUCCESS)
271     return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
272   scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, WlanApiDeleter> conn_info(
273       conn_info_ptr);
274
275   switch (conn_info->wlanAssociationAttributes.dot11PhyType) {
276     case dot11_phy_type_fhss:
277       return WIFI_PHY_LAYER_PROTOCOL_ANCIENT;
278     case dot11_phy_type_dsss:
279       return WIFI_PHY_LAYER_PROTOCOL_B;
280     case dot11_phy_type_irbaseband:
281       return WIFI_PHY_LAYER_PROTOCOL_ANCIENT;
282     case dot11_phy_type_ofdm:
283       return WIFI_PHY_LAYER_PROTOCOL_A;
284     case dot11_phy_type_hrdsss:
285       return WIFI_PHY_LAYER_PROTOCOL_B;
286     case dot11_phy_type_erp:
287       return WIFI_PHY_LAYER_PROTOCOL_G;
288     case dot11_phy_type_ht:
289       return WIFI_PHY_LAYER_PROTOCOL_N;
290     default:
291       return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
292   }
293 }
294
295 }  // namespace net