[IFMON] Implement a basic dump functionality

This commit is contained in:
Eric Kohl
2025-10-14 17:19:00 +02:00
parent d033301772
commit 82aaf15654
4 changed files with 164 additions and 2 deletions

View File

@@ -60,6 +60,33 @@ InterfaceShowInterface(
}
static
DWORD
WINAPI
InterfaceDumpFn(
_In_ LPCWSTR pwszRouter,
_In_ LPWSTR *ppwcArguments,
_In_ DWORD dwArgCount,
_In_ LPCVOID pvData)
{
DPRINT("InterfaceDumpFn(%S %p %lu %p)\n", pwszRouter, ppwcArguments, dwArgCount, pvData);
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
PrintMessage(L"# Interface Configuration\n");
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
PrintMessage(L"pushd\n");
PrintMessage(L"interface\n");
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
PrintMessage(L"popd\n");
PrintMessage(L"# End of Interface Configuration\n");
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
return ERROR_SUCCESS;
}
DWORD
WINAPI
InterfaceStart(
@@ -68,7 +95,7 @@ InterfaceStart(
{
NS_CONTEXT_ATTRIBUTES ContextAttributes;
DPRINT1("InterfaceStart()\n");
DPRINT("InterfaceStart()\n");
ZeroMemory(&ContextAttributes, sizeof(ContextAttributes));
ContextAttributes.dwVersion = 1;
@@ -81,6 +108,8 @@ InterfaceStart(
ContextAttributes.ulNumGroups = sizeof(InterfaceGroups) / sizeof(CMD_GROUP_ENTRY);
ContextAttributes.pCmdGroups = InterfaceGroups;
ContextAttributes.pfnDumpFn = InterfaceDumpFn;
RegisterContext(&ContextAttributes);
return ERROR_SUCCESS;
@@ -93,7 +122,7 @@ RegisterInterfaceHelper(VOID)
{
NS_HELPER_ATTRIBUTES HelperAttributes;
DPRINT1("RegisterInterfaceHelper()\n");
DPRINT("RegisterInterfaceHelper()\n");
ZeroMemory(&HelperAttributes, sizeof(HelperAttributes));
HelperAttributes.dwVersion = 1;

View File

@@ -328,6 +328,124 @@ IpShowDns(
}
static
DWORD
WINAPI
IpDumpFn(
_In_ LPCWSTR pwszRouter,
_In_ LPWSTR *ppwcArguments,
_In_ DWORD dwArgCount,
_In_ LPCVOID pvData)
{
PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL, Ptr;
PIP_ADAPTER_UNICAST_ADDRESS pUnicastAddress;
PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsServer;
WCHAR AddressBuffer[17], MaskBuffer[17];
ULONG adaptOutBufLen = 15000;
ULONG Flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST;
DWORD Error = ERROR_SUCCESS;
DPRINT("IpDumpFn(%S %p %lu %p)\n", pwszRouter, ppwcArguments, dwArgCount, pvData);
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
PrintMessage(L"# Interface IP Configuration\n");
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
PrintMessage(L"pushd\n");
PrintMessage(L"interface ip\n");
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
/* set required buffer size */
pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
if (pAdapterAddresses == NULL)
{
Error = ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen);
if (Error == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterAddresses);
pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
if (pAdapterAddresses == NULL)
{
Error = ERROR_NOT_ENOUGH_MEMORY;
goto done;
}
}
Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen);
if (Error != ERROR_SUCCESS)
goto done;
Ptr = pAdapterAddresses;
while (Ptr)
{
if (Ptr->IfType != IF_TYPE_SOFTWARE_LOOPBACK)
{
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
PrintMessageFromModule(hDllInstance, IDS_DUMP_IP_HEADER, Ptr->FriendlyName);
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
if (Ptr->Flags & IP_ADAPTER_DHCP_ENABLED)
{
PrintMessage(L"set address name=\"%s\" source=dhcp\n",
Ptr->FriendlyName);
}
else
{
pUnicastAddress = Ptr->FirstUnicastAddress;
while (pUnicastAddress)
{
FormatIPv4Address(AddressBuffer, &pUnicastAddress->Address);
wcscpy(MaskBuffer, L"?");
PrintMessage(L"set address name=\"%s\" source=static address=%s mask=%s\n",
Ptr->FriendlyName, AddressBuffer, MaskBuffer);
pUnicastAddress = pUnicastAddress->Next;
}
}
if (Ptr->Flags & IP_ADAPTER_DHCP_ENABLED)
{
PrintMessage(L"set dns name=\"%s\" source=dhcp\n",
Ptr->FriendlyName);
}
else
{
pDnsServer = Ptr->FirstDnsServerAddress;
while (pDnsServer)
{
FormatIPv4Address(AddressBuffer, &pDnsServer->Address);
PrintMessage(L"set dns name=\"%s\" source=%s address=%s\n",
Ptr->FriendlyName);
pDnsServer = pDnsServer->Next;
}
}
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
}
Ptr = Ptr->Next;
}
done:
if (pAdapterAddresses)
free(pAdapterAddresses);
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
PrintMessage(L"popd\n");
PrintMessage(L"# End of Interface IP Configuration\n");
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
return ERROR_SUCCESS;
}
static
DWORD
WINAPI
@@ -350,6 +468,8 @@ IpStart(
ContextAttributes.ulNumGroups = sizeof(IpGroups) / sizeof(CMD_GROUP_ENTRY);
ContextAttributes.pCmdGroups = IpGroups;
ContextAttributes.pfnDumpFn = IpDumpFn;
RegisterContext(&ContextAttributes);
return ERROR_SUCCESS;

View File

@@ -25,4 +25,13 @@ BEGIN
IDS_SUBNETMASKS " Subnet Masks: %s\n"
IDS_EMPTYLINE " %s\n"
IDS_DUMP_NEWLINE "\n"
IDS_DUMP_HEADERLINE "# ----------------------------------\n"
IDS_DUMP_IP_HEADER "# Interface IP Configuration for ""%s""\n"
END

View File

@@ -22,3 +22,7 @@
#define IDS_SUBNETMASKS 308
#define IDS_EMPTYLINE 400
#define IDS_DUMP_NEWLINE 800
#define IDS_DUMP_HEADERLINE 801
#define IDS_DUMP_IP_HEADER 802