From 3fe4b41f201c2d3c10a10c5e45d989be4930b2f2 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Fri, 18 Apr 2025 19:51:59 +0200 Subject: [PATCH] [IPHLPAPI] Retrieve DHCP information from the registry --- dll/win32/iphlpapi/dhcp.h | 9 ++- dll/win32/iphlpapi/dhcp_reactos.c | 107 ++++++++++++++++++++++------- dll/win32/iphlpapi/iphlpapi_main.c | 9 +-- 3 files changed, 88 insertions(+), 37 deletions(-) diff --git a/dll/win32/iphlpapi/dhcp.h b/dll/win32/iphlpapi/dhcp.h index ba98fb62f09..0a42be2a91b 100644 --- a/dll/win32/iphlpapi/dhcp.h +++ b/dll/win32/iphlpapi/dhcp.h @@ -9,10 +9,9 @@ #ifndef WINE_DHCP_H_ #define WINE_DHCP_H_ -DWORD getDhcpInfoForAdapter(DWORD AdapterIndex, - PBOOL DhcpEnabled, - PDWORD DhcpServer, - time_t *LeaseObtained, - time_t *LeaseExpires); +DWORD +getDhcpInfoForAdapter( + DWORD AdapterIndex, + PIP_ADAPTER_INFO ptr); #endif /* ndef WINE_DHCP_H_ */ diff --git a/dll/win32/iphlpapi/dhcp_reactos.c b/dll/win32/iphlpapi/dhcp_reactos.c index ecfedda9e97..f544f50bb90 100644 --- a/dll/win32/iphlpapi/dhcp_reactos.c +++ b/dll/win32/iphlpapi/dhcp_reactos.c @@ -8,35 +8,94 @@ #include "iphlpapi_private.h" -DWORD APIENTRY DhcpRosGetAdapterInfo(DWORD AdapterIndex, - PBOOL DhcpEnabled, - PDWORD DhcpServer, - time_t *LeaseObtained, - time_t *LeaseExpires); - -DWORD getDhcpInfoForAdapter(DWORD AdapterIndex, - PBOOL DhcpEnabled, - PDWORD DhcpServer, - time_t *LeaseObtained, - time_t *LeaseExpires) +DWORD +getDhcpInfoForAdapter( + DWORD AdapterIndex, + PIP_ADAPTER_INFO ptr) { - DWORD Status, Version = 0; + const char *ifname = NULL; + HKEY hKeyInterfaces = NULL, hKeyInterface = NULL; + DWORD dwValue, dwSize, dwType; + DWORD ret = ERROR_SUCCESS; - Status = DhcpCApiInitialize(&Version); - if (Status != ERROR_SUCCESS) + ptr->DhcpEnabled = 0; + ptr->LeaseObtained = 0; + ptr->LeaseExpires = 0; + strcpy(ptr->DhcpServer.IpAddress.String, ""); + + ifname = getInterfaceNameByIndex(AdapterIndex); + if (!ifname) + return ERROR_OUTOFMEMORY; + + ret = RegOpenKeyExW(HKEY_LOCAL_MACHINE, + L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces", + 0, + KEY_READ, + &hKeyInterfaces); + if (ret != ERROR_SUCCESS) + goto done; + + ret = RegOpenKeyExA(hKeyInterfaces, + ifname, + 0, + KEY_READ, + &hKeyInterface); + if (ret != ERROR_SUCCESS) + goto done; + + dwSize = sizeof(ptr->DhcpEnabled); + ret = RegQueryValueExW(hKeyInterface, + L"EnableDHCP", + NULL, + &dwType, + (PBYTE)&ptr->DhcpEnabled, + &dwSize); + if (ret != ERROR_SUCCESS) + ptr->DhcpEnabled = 0; + + if (ptr->DhcpEnabled != 0) { - /* We assume that the DHCP service isn't running yet */ - *DhcpEnabled = FALSE; - *DhcpServer = htonl(INADDR_NONE); - *LeaseObtained = 0; - *LeaseExpires = 0; - return ERROR_SUCCESS; + dwSize = sizeof(ptr->LeaseObtained); + ret = RegQueryValueExW(hKeyInterface, + L"LeaseObtainedTime", + NULL, + &dwType, + (PBYTE)&dwValue, + &dwSize); + if (ret == ERROR_SUCCESS) + ptr->LeaseObtained = (time_t)dwValue; + + dwSize = sizeof(dwValue); + ret = RegQueryValueExW(hKeyInterface, + L"LeaseTerminatesTime", + NULL, + &dwType, + (PBYTE)&dwValue, + &dwSize); + if (ret == ERROR_SUCCESS) + ptr->LeaseExpires = (time_t)dwValue; + + dwSize = sizeof(ptr->DhcpServer.IpAddress.String); + ret = RegQueryValueExA(hKeyInterface, + "DhcpServer", + NULL, + &dwType, + (PBYTE)&ptr->DhcpServer.IpAddress.String, + &dwSize); + if (ret != ERROR_SUCCESS) + strcpy(ptr->DhcpServer.IpAddress.String, ""); } + ret = ERROR_SUCCESS; - Status = DhcpRosGetAdapterInfo(AdapterIndex, DhcpEnabled, DhcpServer, - LeaseObtained, LeaseExpires); +done: + if (hKeyInterface) + RegCloseKey(hKeyInterface); - DhcpCApiCleanup(); + if (hKeyInterfaces) + RegCloseKey(hKeyInterfaces); - return Status; + if (ifname) + consumeInterfaceName(ifname); + + return ret; } diff --git a/dll/win32/iphlpapi/iphlpapi_main.c b/dll/win32/iphlpapi/iphlpapi_main.c index a2fccb2cf58..8ebd354b1d5 100644 --- a/dll/win32/iphlpapi/iphlpapi_main.c +++ b/dll/win32/iphlpapi/iphlpapi_main.c @@ -686,8 +686,6 @@ DWORD WINAPI GetAdapterIndex(LPWSTR AdapterName, PULONG IfIndex) DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen) { DWORD ret; - BOOL dhcpEnabled; - DWORD dhcpServer; TRACE("pAdapterInfo %p, pOutBufLen %p\n", pAdapterInfo, pOutBufLen); if (!pOutBufLen) @@ -771,12 +769,7 @@ DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen) ptr->IpAddressList.Context = ptr->Index; toIPAddressString(getInterfaceGatewayByIndex(table->indexes[ndx]), ptr->GatewayList.IpAddress.String); - getDhcpInfoForAdapter(table->indexes[ndx], &dhcpEnabled, - &dhcpServer, &ptr->LeaseObtained, - &ptr->LeaseExpires); - ptr->DhcpEnabled = (DWORD) dhcpEnabled; - toIPAddressString(dhcpServer, - ptr->DhcpServer.IpAddress.String); + getDhcpInfoForAdapter(table->indexes[ndx], ptr); if (winsEnabled) { ptr->HaveWins = TRUE; memcpy(ptr->PrimaryWinsServer.IpAddress.String,