1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2016 - 2022, 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.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 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
29 #include <curl/curl.h>
30 #include "system_win32.h"
31 #include "version_win32.h"
32 #include "curl_sspi.h"
35 /* The last #include files should be: */
36 #include "curl_memory.h"
39 LARGE_INTEGER Curl_freq;
40 bool Curl_isVistaOrGreater;
42 /* Handle of iphlpapp.dll */
43 static HMODULE s_hIpHlpApiDll = NULL;
45 /* Pointer to the if_nametoindex function */
46 IF_NAMETOINDEX_FN Curl_if_nametoindex = NULL;
48 /* Curl_win32_init() performs win32 global initialization */
49 CURLcode Curl_win32_init(long flags)
51 /* CURL_GLOBAL_WIN32 controls the *optional* part of the initialization which
52 is just for Winsock at the moment. Any required win32 initialization
53 should take place after this block. */
54 if(flags & CURL_GLOBAL_WIN32) {
56 WORD wVersionRequested;
60 wVersionRequested = MAKEWORD(2, 2);
61 res = WSAStartup(wVersionRequested, &wsaData);
64 /* Tell the user that we couldn't find a usable */
66 return CURLE_FAILED_INIT;
68 /* Confirm that the Windows Sockets DLL supports what we need.*/
69 /* Note that if the DLL supports versions greater */
70 /* than wVersionRequested, it will still return */
71 /* wVersionRequested in wVersion. wHighVersion contains the */
72 /* highest supported version. */
74 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
75 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
76 /* Tell the user that we couldn't find a usable */
80 return CURLE_FAILED_INIT;
82 /* The Windows Sockets DLL is acceptable. Proceed. */
83 #elif defined(USE_LWIPSOCK)
86 } /* CURL_GLOBAL_WIN32 */
88 #ifdef USE_WINDOWS_SSPI
90 CURLcode result = Curl_sspi_global_init();
96 s_hIpHlpApiDll = Curl_load_library(TEXT("iphlpapi.dll"));
98 /* Get the address of the if_nametoindex function */
99 IF_NAMETOINDEX_FN pIfNameToIndex =
100 CURLX_FUNCTION_CAST(IF_NAMETOINDEX_FN,
101 (GetProcAddress(s_hIpHlpApiDll, "if_nametoindex")));
104 Curl_if_nametoindex = pIfNameToIndex;
107 /* curlx_verify_windows_version must be called during init at least once
108 because it has its own initialization routine. */
109 if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
110 VERSION_GREATER_THAN_EQUAL)) {
111 Curl_isVistaOrGreater = TRUE;
114 Curl_isVistaOrGreater = FALSE;
116 QueryPerformanceFrequency(&Curl_freq);
120 /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
121 void Curl_win32_cleanup(long init_flags)
124 FreeLibrary(s_hIpHlpApiDll);
125 s_hIpHlpApiDll = NULL;
126 Curl_if_nametoindex = NULL;
129 #ifdef USE_WINDOWS_SSPI
130 Curl_sspi_global_cleanup();
133 if(init_flags & CURL_GLOBAL_WIN32) {
140 #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
141 #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
144 #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
145 #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
148 /* We use our own typedef here since some headers might lack these */
149 typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
151 /* See function definitions in winbase.h */
154 # define LOADLIBARYEX L"LoadLibraryExW"
156 # define LOADLIBARYEX "LoadLibraryExW"
159 # define LOADLIBARYEX "LoadLibraryExA"
163 * Curl_load_library()
165 * This is used to dynamically load DLLs using the most secure method available
166 * for the version of Windows that we are running on.
170 * filename [in] - The filename or full path of the DLL to load. If only the
171 * filename is passed then the DLL will be loaded from the
172 * Windows system directory.
174 * Returns the handle of the module on success; otherwise NULL.
176 HMODULE Curl_load_library(LPCTSTR filename)
178 #ifndef CURL_WINDOWS_APP
179 HMODULE hModule = NULL;
180 LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
182 /* Get a handle to kernel32 so we can access it's functions at runtime */
183 HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
187 /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
190 CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
191 (GetProcAddress(hKernel32, LOADLIBARYEX)));
193 /* Detect if there's already a path in the filename and load the library if
194 there is. Note: Both back slashes and forward slashes have been supported
195 since the earlier days of DOS at an API level although they are not
196 supported by command prompt */
197 if(_tcspbrk(filename, TEXT("\\/"))) {
198 /** !checksrc! disable BANNEDFUNC 1 **/
199 hModule = pLoadLibraryEx ?
200 pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
201 LoadLibrary(filename);
203 /* Detect if KB2533623 is installed, as LOAD_LIBRARY_SEARCH_SYSTEM32 is only
204 supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
205 Server 2008 R2 with this patch or natively on Windows 8 and above */
206 else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
207 /* Load the DLL from the Windows system directory */
208 hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
211 /* Attempt to get the Windows system path */
212 UINT systemdirlen = GetSystemDirectory(NULL, 0);
214 /* Allocate space for the full DLL path (Room for the null terminator
215 is included in systemdirlen) */
216 size_t filenamelen = _tcslen(filename);
217 TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
218 if(path && GetSystemDirectory(path, systemdirlen)) {
219 /* Calculate the full DLL path */
220 _tcscpy(path + _tcslen(path), TEXT("\\"));
221 _tcscpy(path + _tcslen(path), filename);
223 /* Load the DLL from the Windows system directory */
224 /** !checksrc! disable BANNEDFUNC 1 **/
225 hModule = pLoadLibraryEx ?
226 pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
235 /* the Universal Windows Platform (UWP) can't do this */