1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2016 - 2020, Steve Holme, <steve_holme@hotmail.com>.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
27 #include <curl/curl.h>
28 #include "system_win32.h"
29 #include "version_win32.h"
30 #include "curl_sspi.h"
33 /* The last #include files should be: */
34 #include "curl_memory.h"
37 LARGE_INTEGER Curl_freq;
38 bool Curl_isVistaOrGreater;
40 /* Handle of iphlpapp.dll */
41 static HMODULE s_hIpHlpApiDll = NULL;
43 /* Pointer to the if_nametoindex function */
44 IF_NAMETOINDEX_FN Curl_if_nametoindex = NULL;
46 /* Curl_win32_init() performs win32 global initialization */
47 CURLcode Curl_win32_init(long flags)
49 /* CURL_GLOBAL_WIN32 controls the *optional* part of the initialization which
50 is just for Winsock at the moment. Any required win32 initialization
51 should take place after this block. */
52 if(flags & CURL_GLOBAL_WIN32) {
54 WORD wVersionRequested;
58 wVersionRequested = MAKEWORD(2, 2);
59 res = WSAStartup(wVersionRequested, &wsaData);
62 /* Tell the user that we couldn't find a usable */
64 return CURLE_FAILED_INIT;
66 /* Confirm that the Windows Sockets DLL supports what we need.*/
67 /* Note that if the DLL supports versions greater */
68 /* than wVersionRequested, it will still return */
69 /* wVersionRequested in wVersion. wHighVersion contains the */
70 /* highest supported version. */
72 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
73 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
74 /* Tell the user that we couldn't find a usable */
78 return CURLE_FAILED_INIT;
80 /* The Windows Sockets DLL is acceptable. Proceed. */
81 #elif defined(USE_LWIPSOCK)
84 } /* CURL_GLOBAL_WIN32 */
86 #ifdef USE_WINDOWS_SSPI
88 CURLcode result = Curl_sspi_global_init();
94 s_hIpHlpApiDll = Curl_load_library(TEXT("iphlpapi.dll"));
96 /* Get the address of the if_nametoindex function */
97 IF_NAMETOINDEX_FN pIfNameToIndex =
98 CURLX_FUNCTION_CAST(IF_NAMETOINDEX_FN,
99 (GetProcAddress(s_hIpHlpApiDll, "if_nametoindex")));
102 Curl_if_nametoindex = pIfNameToIndex;
105 if(curlx_verify_windows_version(6, 0, PLATFORM_WINNT,
106 VERSION_GREATER_THAN_EQUAL)) {
107 Curl_isVistaOrGreater = TRUE;
110 Curl_isVistaOrGreater = FALSE;
112 QueryPerformanceFrequency(&Curl_freq);
116 /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
117 void Curl_win32_cleanup(long init_flags)
120 FreeLibrary(s_hIpHlpApiDll);
121 s_hIpHlpApiDll = NULL;
122 Curl_if_nametoindex = NULL;
125 #ifdef USE_WINDOWS_SSPI
126 Curl_sspi_global_cleanup();
129 if(init_flags & CURL_GLOBAL_WIN32) {
136 #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
137 #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
140 #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
141 #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
144 /* We use our own typedef here since some headers might lack these */
145 typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
147 /* See function definitions in winbase.h */
150 # define LOADLIBARYEX L"LoadLibraryExW"
152 # define LOADLIBARYEX "LoadLibraryExW"
155 # define LOADLIBARYEX "LoadLibraryExA"
159 * Curl_load_library()
161 * This is used to dynamically load DLLs using the most secure method available
162 * for the version of Windows that we are running on.
166 * filename [in] - The filename or full path of the DLL to load. If only the
167 * filename is passed then the DLL will be loaded from the
168 * Windows system directory.
170 * Returns the handle of the module on success; otherwise NULL.
172 HMODULE Curl_load_library(LPCTSTR filename)
174 #ifndef CURL_WINDOWS_APP
175 HMODULE hModule = NULL;
176 LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
178 /* Get a handle to kernel32 so we can access it's functions at runtime */
179 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
183 /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
186 CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
187 (GetProcAddress(hKernel32, LOADLIBARYEX)));
189 /* Detect if there's already a path in the filename and load the library if
190 there is. Note: Both back slashes and forward slashes have been supported
191 since the earlier days of DOS at an API level although they are not
192 supported by command prompt */
193 if(_tcspbrk(filename, TEXT("\\/"))) {
194 /** !checksrc! disable BANNEDFUNC 1 **/
195 hModule = pLoadLibraryEx ?
196 pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
197 LoadLibrary(filename);
199 /* Detect if KB2533623 is installed, as LOAD_LIBARY_SEARCH_SYSTEM32 is only
200 supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
201 Server 2008 R2 with this patch or natively on Windows 8 and above */
202 else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
203 /* Load the DLL from the Windows system directory */
204 hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
207 /* Attempt to get the Windows system path */
208 UINT systemdirlen = GetSystemDirectory(NULL, 0);
210 /* Allocate space for the full DLL path (Room for the null terminator
211 is included in systemdirlen) */
212 size_t filenamelen = _tcslen(filename);
213 TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
214 if(path && GetSystemDirectory(path, systemdirlen)) {
215 /* Calculate the full DLL path */
216 _tcscpy(path + _tcslen(path), TEXT("\\"));
217 _tcscpy(path + _tcslen(path), filename);
219 /* Load the DLL from the Windows system directory */
220 /** !checksrc! disable BANNEDFUNC 1 **/
221 hModule = pLoadLibraryEx ?
222 pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
231 /* the Universal Windows Platform (UWP) can't do this */