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 <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
ssrlive 2018-04-20 22:59:37 +08:00 committed by cjihrig
parent 0dd95819ba
commit 17eaa956bd
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
3 changed files with 34 additions and 2 deletions

View File

@ -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);

View File

@ -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");
}
}

View File

@ -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_ */