From 17eaa956bde35daad99b36b820daf8e8227add74 Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Fri, 20 Apr 2018 22:59:37 +0800 Subject: [PATCH] win: add Windows XP support to uv_if_indextoname() This commit attempts to dynamically load ConvertInterfaceIndexToLuid() and ConvertInterfaceLuidToNameW() from iphlpapi.dll before using them. If they are not available, UV_ENOSYS is returned. PR-URL: https://github.com/libuv/libuv/pull/1810 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig --- src/win/getaddrinfo.c | 10 ++++++++-- src/win/winapi.c | 11 +++++++++++ src/win/winapi.h | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/win/getaddrinfo.c b/src/win/getaddrinfo.c index 282d919c..5adc7663 100644 --- a/src/win/getaddrinfo.c +++ b/src/win/getaddrinfo.c @@ -392,15 +392,21 @@ int uv_if_indextoname(unsigned int ifindex, char* buffer, size_t* size) { DWORD bufsize; int r; + uv__once_init(); + if (buffer == NULL || size == NULL || *size == 0) return UV_EINVAL; - r = ConvertInterfaceIndexToLuid(ifindex, &luid); + if (pConvertInterfaceIndexToLuid == NULL) + return UV_ENOSYS; + r = pConvertInterfaceIndexToLuid(ifindex, &luid); if (r != 0) return uv_translate_sys_error(r); - r = ConvertInterfaceLuidToNameW(&luid, wname, ARRAY_SIZE(wname)); + if (pConvertInterfaceLuidToNameW == NULL) + return UV_ENOSYS; + r = pConvertInterfaceLuidToNameW(&luid, wname, ARRAY_SIZE(wname)); if (r != 0) return uv_translate_sys_error(r); diff --git a/src/win/winapi.c b/src/win/winapi.c index 4ccdf0a5..c3307861 100644 --- a/src/win/winapi.c +++ b/src/win/winapi.c @@ -55,12 +55,16 @@ sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotification; /* User32.dll function pointer */ sSetWinEventHook pSetWinEventHook; +/* iphlpapi.dll function pointer */ +sConvertInterfaceIndexToLuid pConvertInterfaceIndexToLuid = NULL; +sConvertInterfaceLuidToNameW pConvertInterfaceLuidToNameW = NULL; void uv_winapi_init(void) { HMODULE ntdll_module; HMODULE kernel32_module; HMODULE powrprof_module; HMODULE user32_module; + HMODULE iphlpapi_module; ntdll_module = GetModuleHandleA("ntdll.dll"); if (ntdll_module == NULL) { @@ -166,4 +170,11 @@ void uv_winapi_init(void) { GetProcAddress(user32_module, "SetWinEventHook"); } + iphlpapi_module = LoadLibraryA("iphlpapi.dll"); + if (iphlpapi_module != NULL) { + pConvertInterfaceIndexToLuid = (sConvertInterfaceIndexToLuid) + GetProcAddress(iphlpapi_module, "ConvertInterfaceIndexToLuid"); + pConvertInterfaceLuidToNameW = (sConvertInterfaceLuidToNameW) + GetProcAddress(iphlpapi_module, "ConvertInterfaceLuidToNameW"); + } } diff --git a/src/win/winapi.h b/src/win/winapi.h index cc54b79b..38570c2f 100644 --- a/src/win/winapi.h +++ b/src/win/winapi.h @@ -4775,4 +4775,19 @@ extern sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotifi /* User32.dll function pointer */ extern sSetWinEventHook pSetWinEventHook; +/* iphlpapi.dll function pointer */ +union _NET_LUID_LH; +typedef DWORD (WINAPI *sConvertInterfaceIndexToLuid)( + ULONG InterfaceIndex, + union _NET_LUID_LH *InterfaceLuid); + +typedef DWORD (WINAPI *sConvertInterfaceLuidToNameW)( + const union _NET_LUID_LH *InterfaceLuid, + PWSTR InterfaceName, + size_t Length); + +extern sConvertInterfaceIndexToLuid pConvertInterfaceIndexToLuid; +extern sConvertInterfaceLuidToNameW pConvertInterfaceLuidToNameW; + + #endif /* UV_WIN_WINAPI_H_ */