system_win32.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Steve Holme, <steve_holme@hotmail.com>.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(_WIN32)
  26. #include <curl/curl.h>
  27. #include "system_win32.h"
  28. #include "version_win32.h"
  29. #include "curl_sspi.h"
  30. #include "warnless.h"
  31. /* The last #include files should be: */
  32. #include "curl_memory.h"
  33. #include "memdebug.h"
  34. LARGE_INTEGER Curl_freq;
  35. bool Curl_isVistaOrGreater;
  36. /* Handle of iphlpapp.dll */
  37. static HMODULE s_hIpHlpApiDll = NULL;
  38. /* Pointer to the if_nametoindex function */
  39. IF_NAMETOINDEX_FN Curl_if_nametoindex = NULL;
  40. /* Curl_win32_init() performs Win32 global initialization */
  41. CURLcode Curl_win32_init(long flags)
  42. {
  43. /* CURL_GLOBAL_WIN32 controls the *optional* part of the initialization which
  44. is just for Winsock at the moment. Any required Win32 initialization
  45. should take place after this block. */
  46. if(flags & CURL_GLOBAL_WIN32) {
  47. #ifdef USE_WINSOCK
  48. WORD wVersionRequested;
  49. WSADATA wsaData;
  50. int res;
  51. wVersionRequested = MAKEWORD(2, 2);
  52. res = WSAStartup(wVersionRequested, &wsaData);
  53. if(res)
  54. /* Tell the user that we could not find a usable */
  55. /* winsock.dll. */
  56. return CURLE_FAILED_INIT;
  57. /* Confirm that the Windows Sockets DLL supports what we need.*/
  58. /* Note that if the DLL supports versions greater */
  59. /* than wVersionRequested, it will still return */
  60. /* wVersionRequested in wVersion. wHighVersion contains the */
  61. /* highest supported version. */
  62. if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
  63. HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
  64. /* Tell the user that we could not find a usable */
  65. /* winsock.dll. */
  66. WSACleanup();
  67. return CURLE_FAILED_INIT;
  68. }
  69. /* The Windows Sockets DLL is acceptable. Proceed. */
  70. #elif defined(USE_LWIPSOCK)
  71. lwip_init();
  72. #endif
  73. } /* CURL_GLOBAL_WIN32 */
  74. #ifdef USE_WINDOWS_SSPI
  75. {
  76. CURLcode result = Curl_sspi_global_init();
  77. if(result)
  78. return result;
  79. }
  80. #endif
  81. s_hIpHlpApiDll = Curl_load_library(TEXT("iphlpapi.dll"));
  82. if(s_hIpHlpApiDll) {
  83. /* Get the address of the if_nametoindex function */
  84. IF_NAMETOINDEX_FN pIfNameToIndex =
  85. CURLX_FUNCTION_CAST(IF_NAMETOINDEX_FN,
  86. (GetProcAddress(s_hIpHlpApiDll, "if_nametoindex")));
  87. if(pIfNameToIndex)
  88. Curl_if_nametoindex = pIfNameToIndex;
  89. }
  90. /* curlx_verify_windows_version must be called during init at least once
  91. because it has its own initialization routine. */
  92. if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
  93. VERSION_GREATER_THAN_EQUAL)) {
  94. Curl_isVistaOrGreater = TRUE;
  95. }
  96. else
  97. Curl_isVistaOrGreater = FALSE;
  98. QueryPerformanceFrequency(&Curl_freq);
  99. return CURLE_OK;
  100. }
  101. /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
  102. void Curl_win32_cleanup(long init_flags)
  103. {
  104. if(s_hIpHlpApiDll) {
  105. FreeLibrary(s_hIpHlpApiDll);
  106. s_hIpHlpApiDll = NULL;
  107. Curl_if_nametoindex = NULL;
  108. }
  109. #ifdef USE_WINDOWS_SSPI
  110. Curl_sspi_global_cleanup();
  111. #endif
  112. if(init_flags & CURL_GLOBAL_WIN32) {
  113. #ifdef USE_WINSOCK
  114. WSACleanup();
  115. #endif
  116. }
  117. }
  118. #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
  119. #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
  120. #endif
  121. #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
  122. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  123. #endif
  124. /* We use our own typedef here since some headers might lack these */
  125. typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
  126. /* See function definitions in winbase.h */
  127. #ifdef UNICODE
  128. # ifdef _WIN32_WCE
  129. # define LOADLIBARYEX L"LoadLibraryExW"
  130. # else
  131. # define LOADLIBARYEX "LoadLibraryExW"
  132. # endif
  133. #else
  134. # define LOADLIBARYEX "LoadLibraryExA"
  135. #endif
  136. /*
  137. * Curl_load_library()
  138. *
  139. * This is used to dynamically load DLLs using the most secure method available
  140. * for the version of Windows that we are running on.
  141. *
  142. * Parameters:
  143. *
  144. * filename [in] - The filename or full path of the DLL to load. If only the
  145. * filename is passed then the DLL will be loaded from the
  146. * Windows system directory.
  147. *
  148. * Returns the handle of the module on success; otherwise NULL.
  149. */
  150. HMODULE Curl_load_library(LPCTSTR filename)
  151. {
  152. #ifndef CURL_WINDOWS_APP
  153. HMODULE hModule = NULL;
  154. LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
  155. /* Get a handle to kernel32 so we can access its functions at runtime */
  156. HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
  157. if(!hKernel32)
  158. return NULL;
  159. /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
  160. and above */
  161. pLoadLibraryEx =
  162. CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
  163. (GetProcAddress(hKernel32, LOADLIBARYEX)));
  164. /* Detect if there is already a path in the filename and load the library if
  165. there is. Note: Both back slashes and forward slashes have been supported
  166. since the earlier days of DOS at an API level although they are not
  167. supported by command prompt */
  168. if(_tcspbrk(filename, TEXT("\\/"))) {
  169. /** !checksrc! disable BANNEDFUNC 1 **/
  170. hModule = pLoadLibraryEx ?
  171. pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  172. LoadLibrary(filename);
  173. }
  174. /* Detect if KB2533623 is installed, as LOAD_LIBRARY_SEARCH_SYSTEM32 is only
  175. supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
  176. Server 2008 R2 with this patch or natively on Windows 8 and above */
  177. else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
  178. /* Load the DLL from the Windows system directory */
  179. hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  180. }
  181. else {
  182. /* Attempt to get the Windows system path */
  183. UINT systemdirlen = GetSystemDirectory(NULL, 0);
  184. if(systemdirlen) {
  185. /* Allocate space for the full DLL path (Room for the null terminator
  186. is included in systemdirlen) */
  187. size_t filenamelen = _tcslen(filename);
  188. TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
  189. if(path && GetSystemDirectory(path, systemdirlen)) {
  190. /* Calculate the full DLL path */
  191. _tcscpy(path + _tcslen(path), TEXT("\\"));
  192. _tcscpy(path + _tcslen(path), filename);
  193. /* Load the DLL from the Windows system directory */
  194. /** !checksrc! disable BANNEDFUNC 1 **/
  195. hModule = pLoadLibraryEx ?
  196. pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  197. LoadLibrary(path);
  198. }
  199. free(path);
  200. }
  201. }
  202. return hModule;
  203. #else
  204. /* the Universal Windows Platform (UWP) cannot do this */
  205. (void)filename;
  206. return NULL;
  207. #endif
  208. }
  209. #endif /* _WIN32 */