system_win32.c 7.4 KB

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