system_win32.c 7.4 KB

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