system_win32.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2016 - 2020, 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 != 0)
  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. if(curlx_verify_windows_version(6, 0, PLATFORM_WINNT,
  89. VERSION_GREATER_THAN_EQUAL)) {
  90. Curl_isVistaOrGreater = TRUE;
  91. }
  92. else
  93. Curl_isVistaOrGreater = FALSE;
  94. QueryPerformanceFrequency(&Curl_freq);
  95. return CURLE_OK;
  96. }
  97. /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */
  98. void Curl_win32_cleanup(long init_flags)
  99. {
  100. if(s_hIpHlpApiDll) {
  101. FreeLibrary(s_hIpHlpApiDll);
  102. s_hIpHlpApiDll = NULL;
  103. Curl_if_nametoindex = NULL;
  104. }
  105. #ifdef USE_WINDOWS_SSPI
  106. Curl_sspi_global_cleanup();
  107. #endif
  108. if(init_flags & CURL_GLOBAL_WIN32) {
  109. #ifdef USE_WINSOCK
  110. WSACleanup();
  111. #endif
  112. }
  113. }
  114. #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH)
  115. #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008
  116. #endif
  117. #if !defined(LOAD_LIBRARY_SEARCH_SYSTEM32)
  118. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0x00000800
  119. #endif
  120. /* We use our own typedef here since some headers might lack these */
  121. typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD);
  122. /* See function definitions in winbase.h */
  123. #ifdef UNICODE
  124. # ifdef _WIN32_WCE
  125. # define LOADLIBARYEX L"LoadLibraryExW"
  126. # else
  127. # define LOADLIBARYEX "LoadLibraryExW"
  128. # endif
  129. #else
  130. # define LOADLIBARYEX "LoadLibraryExA"
  131. #endif
  132. /*
  133. * Curl_load_library()
  134. *
  135. * This is used to dynamically load DLLs using the most secure method available
  136. * for the version of Windows that we are running on.
  137. *
  138. * Parameters:
  139. *
  140. * filename [in] - The filename or full path of the DLL to load. If only the
  141. * filename is passed then the DLL will be loaded from the
  142. * Windows system directory.
  143. *
  144. * Returns the handle of the module on success; otherwise NULL.
  145. */
  146. HMODULE Curl_load_library(LPCTSTR filename)
  147. {
  148. #ifndef CURL_WINDOWS_APP
  149. HMODULE hModule = NULL;
  150. LOADLIBRARYEX_FN pLoadLibraryEx = NULL;
  151. /* Get a handle to kernel32 so we can access it's functions at runtime */
  152. HMODULE hKernel32 = GetModuleHandle(TEXT("kernel32"));
  153. if(!hKernel32)
  154. return NULL;
  155. /* Attempt to find LoadLibraryEx() which is only available on Windows 2000
  156. and above */
  157. pLoadLibraryEx =
  158. CURLX_FUNCTION_CAST(LOADLIBRARYEX_FN,
  159. (GetProcAddress(hKernel32, LOADLIBARYEX)));
  160. /* Detect if there's already a path in the filename and load the library if
  161. there is. Note: Both back slashes and forward slashes have been supported
  162. since the earlier days of DOS at an API level although they are not
  163. supported by command prompt */
  164. if(_tcspbrk(filename, TEXT("\\/"))) {
  165. /** !checksrc! disable BANNEDFUNC 1 **/
  166. hModule = pLoadLibraryEx ?
  167. pLoadLibraryEx(filename, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  168. LoadLibrary(filename);
  169. }
  170. /* Detect if KB2533623 is installed, as LOAD_LIBRARY_SEARCH_SYSTEM32 is only
  171. supported on Windows Vista, Windows Server 2008, Windows 7 and Windows
  172. Server 2008 R2 with this patch or natively on Windows 8 and above */
  173. else if(pLoadLibraryEx && GetProcAddress(hKernel32, "AddDllDirectory")) {
  174. /* Load the DLL from the Windows system directory */
  175. hModule = pLoadLibraryEx(filename, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
  176. }
  177. else {
  178. /* Attempt to get the Windows system path */
  179. UINT systemdirlen = GetSystemDirectory(NULL, 0);
  180. if(systemdirlen) {
  181. /* Allocate space for the full DLL path (Room for the null terminator
  182. is included in systemdirlen) */
  183. size_t filenamelen = _tcslen(filename);
  184. TCHAR *path = malloc(sizeof(TCHAR) * (systemdirlen + 1 + filenamelen));
  185. if(path && GetSystemDirectory(path, systemdirlen)) {
  186. /* Calculate the full DLL path */
  187. _tcscpy(path + _tcslen(path), TEXT("\\"));
  188. _tcscpy(path + _tcslen(path), filename);
  189. /* Load the DLL from the Windows system directory */
  190. /** !checksrc! disable BANNEDFUNC 1 **/
  191. hModule = pLoadLibraryEx ?
  192. pLoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH) :
  193. LoadLibrary(path);
  194. }
  195. free(path);
  196. }
  197. }
  198. return hModule;
  199. #else
  200. /* the Universal Windows Platform (UWP) can't do this */
  201. (void)filename;
  202. return NULL;
  203. #endif
  204. }
  205. #endif /* WIN32 */