[IPCONFIG] Call DHCP functions directly

Use DhcpAcquireParameters and DhcpReleaseParameters instead of iphlpapi functions.
This commit is contained in:
Eric Kohl
2025-07-19 13:31:25 +02:00
parent cb0c31945e
commit 6ebf15d838
2 changed files with 49 additions and 28 deletions

View File

@@ -3,5 +3,5 @@ include_directories(${REACTOS_SOURCE_DIR}/sdk/lib/conutils)
add_executable(ipconfig ipconfig.c ipconfig.rc)
set_module_type(ipconfig win32cui UNICODE)
target_link_libraries(ipconfig conutils ${PSEH_LIB})
add_importlibs(ipconfig user32 iphlpapi dnsapi advapi32 msvcrt kernel32 ntdll)
add_importlibs(ipconfig user32 iphlpapi dnsapi dhcpcsvc advapi32 msvcrt kernel32 ntdll)
add_cd_file(TARGET ipconfig DESTINATION reactos/system32 FOR all)

View File

@@ -12,6 +12,7 @@
#define WIN32_NO_STATUS
#include <stdarg.h>
#include <stdlib.h>
#include <windef.h>
#include <winbase.h>
#include <winnls.h>
@@ -21,16 +22,23 @@
#include <stdio.h>
#include <tchar.h>
#include <time.h>
//#include <winsock2.h>
//#include <iptypes.h>
#include <iphlpapi.h>
#include <ndk/rtlfuncs.h>
#include <inaddr.h>
#include <windns.h>
#include <windns_undoc.h>
#include <dhcpcsdk.h>
#include <dhcpcapi.h>
#include <strsafe.h>
#include <conutils.h>
#include "resource.h"
#define NDEBUG
#include <debug.h>
typedef struct _RECORDTYPE
{
WORD wRecordType;
@@ -220,6 +228,25 @@ VOID DoFormatMessage(LONG ErrorCode)
}
}
LPWSTR
GetUnicodeAdapterName(
_In_ LPSTR pszAnsiName)
{
LPWSTR pszUnicodeName;
int i, len;
len = strlen(pszAnsiName);
pszUnicodeName = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
if (pszUnicodeName == NULL)
return NULL;
for (i = 0; i < len; i++)
pszUnicodeName[i] = (WCHAR)pszAnsiName[i];
pszUnicodeName[i] = UNICODE_NULL;
return pszUnicodeName;
}
VOID
GetAdapterFriendlyName(
_In_ LPSTR lpClass,
@@ -809,24 +836,6 @@ MatchWildcard(
return TRUE;
}
static
VOID
BuildAdapterMap(
PIP_ADAPTER_INDEX_MAP pAdapterMap,
PIP_ADAPTER_INFO pAdapterInfo)
{
int i, l1, l2;
pAdapterMap->Index = pAdapterInfo->Index;
wcscpy(pAdapterMap->Name, L"\\DEVICE\\TCPIP_");
l1 = wcslen(pAdapterMap->Name);
l2 = strlen(pAdapterInfo->AdapterName);
for (i = 0; i < l2; i++)
pAdapterMap->Name[i + l1] = (WCHAR)pAdapterInfo->AdapterName[i];
pAdapterMap->Name[i + l1] = UNICODE_NULL;
}
VOID
Release(
LPWSTR pszAdapterName)
@@ -836,9 +845,10 @@ Release(
ULONG adaptOutBufLen = 0;
ULONG ret = 0;
WCHAR szFriendlyName[MAX_PATH];
WCHAR szUnicodeAdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
MIB_IFROW mibEntry;
IP_ADAPTER_INDEX_MAP AdapterMap;
BOOL bFoundAdapter = FALSE;
DWORD dwVersion;
ConResPrintf(StdOut, IDS_HEADER);
@@ -864,6 +874,8 @@ Release(
goto done;
}
DhcpCApiInitialize(&dwVersion);
pAdapter = pAdapterInfo;
while (pAdapter)
@@ -884,10 +896,11 @@ Release(
{
if (strcmp(pAdapter->IpAddressList.IpAddress.String, "0.0.0.0"))
{
BuildAdapterMap(&AdapterMap, pAdapter);
mbstowcs(szUnicodeAdapterName, pAdapter->AdapterName, strlen(pAdapter->AdapterName) + 1);
DPRINT1("AdapterName: %S\n", szUnicodeAdapterName);
/* Call IpReleaseAddress to release the IP address on the specified adapter. */
ret = IpReleaseAddress(&AdapterMap);
/* Call DhcpReleaseParameters to release the IP address on the specified adapter. */
ret = DhcpReleaseParameters(szUnicodeAdapterName);
if (ret != NO_ERROR)
{
ConResPrintf(StdOut, IDS_DHCPRELEASEERROR, szFriendlyName);
@@ -913,6 +926,8 @@ Release(
pAdapter = pAdapter->Next;
}
DhcpCApiCleanup();
if (bFoundAdapter == FALSE)
{
ConResPrintf(StdOut, IDS_DHCPNOADAPTER);
@@ -936,9 +951,10 @@ Renew(
ULONG adaptOutBufLen = 0;
ULONG ret = 0;
WCHAR szFriendlyName[MAX_PATH];
WCHAR szUnicodeAdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
MIB_IFROW mibEntry;
IP_ADAPTER_INDEX_MAP AdapterMap;
BOOL bFoundAdapter = FALSE;
DWORD dwVersion;
ConResPrintf(StdOut, IDS_HEADER);
@@ -964,6 +980,8 @@ Renew(
goto done;
}
DhcpCApiInitialize(&dwVersion);
pAdapter = pAdapterInfo;
while (pAdapter)
@@ -982,10 +1000,11 @@ Renew(
{
if (pAdapter->DhcpEnabled)
{
BuildAdapterMap(&AdapterMap, pAdapter);
mbstowcs(szUnicodeAdapterName, pAdapter->AdapterName, strlen(pAdapter->AdapterName) + 1);
DPRINT1("AdapterName: %S\n", szUnicodeAdapterName);
/* Call IpRenewAddress to renew the IP address on the specified adapter. */
ret = IpRenewAddress(&AdapterMap);
/* Call DhcpAcquireParameters to renew the IP address on the specified adapter. */
ret = DhcpAcquireParameters(szUnicodeAdapterName);
if (ret != NO_ERROR)
{
ConResPrintf(StdOut, IDS_DHCPRENEWERROR, szFriendlyName);
@@ -1006,6 +1025,8 @@ Renew(
pAdapter = pAdapter->Next;
}
DhcpCApiCleanup();
if (bFoundAdapter == FALSE)
{
ConResPrintf(StdOut, IDS_DHCPNOADAPTER);
@@ -1239,7 +1260,7 @@ int wmain(int argc, wchar_t *argv[])
}
else if (!_tcsnicmp(&argv[1][1], _T("ALL"), _tcslen(&argv[1][1])))
{
DoAll = TRUE;
DoAll = TRUE;
}
else if (!_tcsnicmp(&argv[1][1], _T("RELEASE"), _tcslen(&argv[1][1])))
{