From 09eef8af18c7cc1a20f132843880f60f692d76c8 Mon Sep 17 00:00:00 2001 From: Steve Holme Date: Tue, 28 May 2019 22:07:33 +0100 Subject: [PATCH] url: Load if_nametoindex() dynamically from iphlpapi.dll on Windows This fixes the static dependency on iphlpapi.lib and allows curl to build for targets prior to Windows Vista. This partially reverts 170bd047. Fixes #3960 Closes #3958 --- lib/config-win32.h | 10 ---------- lib/system_win32.c | 36 ++++++++++++++++++++++++------------ lib/system_win32.h | 13 +++++++------ lib/url.c | 15 +++++++++++++-- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/lib/config-win32.h b/lib/config-win32.h index 14bd084fe..90c105476 100644 --- a/lib/config-win32.h +++ b/lib/config-win32.h @@ -154,11 +154,6 @@ #define HAVE_WS2TCPIP_H 1 #endif -/* Define if you have the header file. */ -#ifndef __SALFORDC__ -#define HAVE_IPHLPAPI_H 1 -#endif - /* ---------------------------------------------------------------- */ /* OTHER HEADER INFO */ /* ---------------------------------------------------------------- */ @@ -611,11 +606,6 @@ Vista # define HAVE_GETNAMEINFO 1 # endif #endif -#if defined(HAVE_IPHLPAPI_H) - #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600) - #define HAVE_IF_NAMETOINDEX 1 - #endif -#endif #if defined(__POCC__) # ifndef _MSC_VER diff --git a/lib/system_win32.c b/lib/system_win32.c index f7f817dd4..1143fa6aa 100644 --- a/lib/system_win32.c +++ b/lib/system_win32.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 2016 - 2017, Steve Holme, . + * Copyright (C) 2016 - 2019, Steve Holme, . * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -36,6 +36,12 @@ LARGE_INTEGER Curl_freq; bool Curl_isVistaOrGreater; +/* Handle of iphlpapp.dll */ +static HMODULE s_hIpHlpApiDll = NULL; + +/* Pointer to the if_nametoindex function */ +IF_NAMETOINDEX_FN Curl_if_nametoindex = NULL; + /* Curl_win32_init() performs win32 global initialization */ CURLcode Curl_win32_init(long flags) { @@ -89,6 +95,17 @@ CURLcode Curl_win32_init(long flags) } #endif + s_hIpHlpApiDll = Curl_load_library(TEXT("iphlpapi.dll")); + if(s_hIpHlpApiDll) { + /* Get the address of the if_nametoindex function */ + IF_NAMETOINDEX_FN pIfNameToIndex = + CURLX_FUNCTION_CAST(IF_NAMETOINDEX_FN, + (GetProcAddress(s_hIpHlpApiDll, "if_nametoindex"))); + + if(pIfNameToIndex) + Curl_if_nametoindex = pIfNameToIndex; + } + if(Curl_verify_windows_version(6, 0, PLATFORM_WINNT, VERSION_GREATER_THAN_EQUAL)) { Curl_isVistaOrGreater = TRUE; @@ -103,6 +120,12 @@ CURLcode Curl_win32_init(long flags) /* Curl_win32_cleanup() is the opposite of Curl_win32_init() */ void Curl_win32_cleanup(long init_flags) { + if(s_hIpHlpApiDll) { + FreeLibrary(s_hIpHlpApiDll); + s_hIpHlpApiDll = NULL; + Curl_if_nametoindex = NULL; + } + #ifdef USE_WINDOWS_SSPI Curl_sspi_global_cleanup(); #endif @@ -114,10 +137,6 @@ void Curl_win32_cleanup(long init_flags) } } -#if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \ - defined(USE_WINSOCK)) - - #if !defined(LOAD_WITH_ALTERED_SEARCH_PATH) #define LOAD_WITH_ALTERED_SEARCH_PATH 0x00000008 #endif @@ -140,8 +159,6 @@ typedef HMODULE (APIENTRY *LOADLIBRARYEX_FN)(LPCTSTR, HANDLE, DWORD); # define LOADLIBARYEX "LoadLibraryExA" #endif -#endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */ - /* * Curl_verify_windows_version() * @@ -334,9 +351,6 @@ bool Curl_verify_windows_version(const unsigned int majorVersion, return matched; } -#if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \ - defined(USE_WINSOCK)) - /* * Curl_load_library() * @@ -411,6 +425,4 @@ HMODULE Curl_load_library(LPCTSTR filename) return hModule; } -#endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */ - #endif /* WIN32 */ diff --git a/lib/system_win32.h b/lib/system_win32.h index 926328a9a..95f4af8a7 100644 --- a/lib/system_win32.h +++ b/lib/system_win32.h @@ -7,7 +7,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 2016, Steve Holme, . + * Copyright (C) 2016 - 2019, Steve Holme, . * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms @@ -48,20 +48,21 @@ typedef enum { PLATFORM_WINNT } PlatformIdentifier; +/* We use our own typedef here since some headers might lack this */ +typedef unsigned int(WINAPI *IF_NAMETOINDEX_FN)(char *); + +/* This is used instread of if_nametoindex if available on Windows */ +IF_NAMETOINDEX_FN Curl_if_nametoindex; + /* This is used to verify if we are running on a specific windows version */ bool Curl_verify_windows_version(const unsigned int majorVersion, const unsigned int minorVersion, const PlatformIdentifier platform, const VersionCondition condition); -#if defined(USE_WINDOWS_SSPI) || (!defined(CURL_DISABLE_TELNET) && \ - defined(USE_WINSOCK)) - /* This is used to dynamically load DLLs */ HMODULE Curl_load_library(LPCTSTR filename); -#endif /* USE_WINDOWS_SSPI || (!CURL_DISABLE_TELNET && USE_WINSOCK) */ - #endif /* WIN32 */ #endif /* HEADER_CURL_SYSTEM_WIN32_H */ diff --git a/lib/url.c b/lib/url.c index 086e214e4..eb22dcc37 100644 --- a/lib/url.c +++ b/lib/url.c @@ -95,6 +95,7 @@ bool curl_win32_idn_to_ascii(const char *in, char **out); #include "inet_pton.h" #include "getinfo.h" #include "urlapi-int.h" +#include "system_win32.h" /* And now for the protocols */ #include "ftp.h" @@ -1903,18 +1904,28 @@ static void zonefrom_url(CURLU *uh, struct connectdata *conn) if(!*endp && (scope < UINT_MAX)) /* A plain number, use it directly as a scope id. */ conn->scope_id = (unsigned int)scope; -#ifdef HAVE_IF_NAMETOINDEX +#if defined(HAVE_IF_NAMETOINDEX) else { +#elif defined(WIN32) + else if(Curl_if_nametoindex) { +#endif + +#if defined(HAVE_IF_NAMETOINDEX) || defined(WIN32) /* Zone identifier is not numeric */ unsigned int scopeidx = 0; +#if defined(WIN32) + scopeidx = Curl_if_nametoindex(zoneid); +#else scopeidx = if_nametoindex(zoneid); +#endif if(!scopeidx) infof(conn->data, "Invalid zoneid: %s; %s\n", zoneid, strerror(errno)); else conn->scope_id = scopeidx; } -#endif /* HAVE_IF_NAMETOINDEX */ +#endif /* HAVE_IF_NAMETOINDEX || WIN32 */ + free(zoneid); } }