system_win32.c 7.8 KB

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