Skip to content

Commit 82aaf15

Browse files
committed
[IFMON] Implement a basic dump functionality
1 parent d033301 commit 82aaf15

File tree

4 files changed

+164
-2
lines changed

4 files changed

+164
-2
lines changed

dll/win32/ifmon/interface.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ InterfaceShowInterface(
6060
}
6161

6262

63+
static
64+
DWORD
65+
WINAPI
66+
InterfaceDumpFn(
67+
_In_ LPCWSTR pwszRouter,
68+
_In_ LPWSTR *ppwcArguments,
69+
_In_ DWORD dwArgCount,
70+
_In_ LPCVOID pvData)
71+
{
72+
DPRINT("InterfaceDumpFn(%S %p %lu %p)\n", pwszRouter, ppwcArguments, dwArgCount, pvData);
73+
74+
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
75+
PrintMessage(L"# Interface Configuration\n");
76+
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
77+
PrintMessage(L"pushd\n");
78+
PrintMessage(L"interface\n");
79+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
80+
81+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
82+
PrintMessage(L"popd\n");
83+
PrintMessage(L"# End of Interface Configuration\n");
84+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
85+
86+
return ERROR_SUCCESS;
87+
}
88+
89+
6390
DWORD
6491
WINAPI
6592
InterfaceStart(
@@ -68,7 +95,7 @@ InterfaceStart(
6895
{
6996
NS_CONTEXT_ATTRIBUTES ContextAttributes;
7097

71-
DPRINT1("InterfaceStart()\n");
98+
DPRINT("InterfaceStart()\n");
7299

73100
ZeroMemory(&ContextAttributes, sizeof(ContextAttributes));
74101
ContextAttributes.dwVersion = 1;
@@ -81,6 +108,8 @@ InterfaceStart(
81108
ContextAttributes.ulNumGroups = sizeof(InterfaceGroups) / sizeof(CMD_GROUP_ENTRY);
82109
ContextAttributes.pCmdGroups = InterfaceGroups;
83110

111+
ContextAttributes.pfnDumpFn = InterfaceDumpFn;
112+
84113
RegisterContext(&ContextAttributes);
85114

86115
return ERROR_SUCCESS;
@@ -93,7 +122,7 @@ RegisterInterfaceHelper(VOID)
93122
{
94123
NS_HELPER_ATTRIBUTES HelperAttributes;
95124

96-
DPRINT1("RegisterInterfaceHelper()\n");
125+
DPRINT("RegisterInterfaceHelper()\n");
97126

98127
ZeroMemory(&HelperAttributes, sizeof(HelperAttributes));
99128
HelperAttributes.dwVersion = 1;

dll/win32/ifmon/ip.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,124 @@ IpShowDns(
328328
}
329329

330330

331+
static
332+
DWORD
333+
WINAPI
334+
IpDumpFn(
335+
_In_ LPCWSTR pwszRouter,
336+
_In_ LPWSTR *ppwcArguments,
337+
_In_ DWORD dwArgCount,
338+
_In_ LPCVOID pvData)
339+
{
340+
PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL, Ptr;
341+
PIP_ADAPTER_UNICAST_ADDRESS pUnicastAddress;
342+
PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsServer;
343+
WCHAR AddressBuffer[17], MaskBuffer[17];
344+
ULONG adaptOutBufLen = 15000;
345+
ULONG Flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST;
346+
DWORD Error = ERROR_SUCCESS;
347+
348+
DPRINT("IpDumpFn(%S %p %lu %p)\n", pwszRouter, ppwcArguments, dwArgCount, pvData);
349+
350+
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
351+
PrintMessage(L"# Interface IP Configuration\n");
352+
PrintMessageFromModule(hDllInstance, IDS_DUMP_HEADERLINE);
353+
PrintMessage(L"pushd\n");
354+
PrintMessage(L"interface ip\n");
355+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
356+
357+
/* set required buffer size */
358+
pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
359+
if (pAdapterAddresses == NULL)
360+
{
361+
Error = ERROR_NOT_ENOUGH_MEMORY;
362+
goto done;
363+
}
364+
365+
Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen);
366+
if (Error == ERROR_BUFFER_OVERFLOW)
367+
{
368+
free(pAdapterAddresses);
369+
pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
370+
if (pAdapterAddresses == NULL)
371+
{
372+
Error = ERROR_NOT_ENOUGH_MEMORY;
373+
goto done;
374+
}
375+
}
376+
377+
Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen);
378+
if (Error != ERROR_SUCCESS)
379+
goto done;
380+
381+
Ptr = pAdapterAddresses;
382+
while (Ptr)
383+
{
384+
if (Ptr->IfType != IF_TYPE_SOFTWARE_LOOPBACK)
385+
{
386+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
387+
PrintMessageFromModule(hDllInstance, IDS_DUMP_IP_HEADER, Ptr->FriendlyName);
388+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
389+
390+
if (Ptr->Flags & IP_ADAPTER_DHCP_ENABLED)
391+
{
392+
PrintMessage(L"set address name=\"%s\" source=dhcp\n",
393+
Ptr->FriendlyName);
394+
}
395+
else
396+
{
397+
pUnicastAddress = Ptr->FirstUnicastAddress;
398+
while (pUnicastAddress)
399+
{
400+
FormatIPv4Address(AddressBuffer, &pUnicastAddress->Address);
401+
wcscpy(MaskBuffer, L"?");
402+
403+
PrintMessage(L"set address name=\"%s\" source=static address=%s mask=%s\n",
404+
Ptr->FriendlyName, AddressBuffer, MaskBuffer);
405+
406+
pUnicastAddress = pUnicastAddress->Next;
407+
}
408+
}
409+
410+
if (Ptr->Flags & IP_ADAPTER_DHCP_ENABLED)
411+
{
412+
PrintMessage(L"set dns name=\"%s\" source=dhcp\n",
413+
Ptr->FriendlyName);
414+
}
415+
else
416+
{
417+
pDnsServer = Ptr->FirstDnsServerAddress;
418+
while (pDnsServer)
419+
{
420+
FormatIPv4Address(AddressBuffer, &pDnsServer->Address);
421+
422+
PrintMessage(L"set dns name=\"%s\" source=%s address=%s\n",
423+
Ptr->FriendlyName);
424+
425+
pDnsServer = pDnsServer->Next;
426+
}
427+
428+
}
429+
430+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
431+
}
432+
433+
Ptr = Ptr->Next;
434+
}
435+
436+
done:
437+
if (pAdapterAddresses)
438+
free(pAdapterAddresses);
439+
440+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
441+
PrintMessage(L"popd\n");
442+
PrintMessage(L"# End of Interface IP Configuration\n");
443+
PrintMessageFromModule(hDllInstance, IDS_DUMP_NEWLINE);
444+
445+
return ERROR_SUCCESS;
446+
}
447+
448+
331449
static
332450
DWORD
333451
WINAPI
@@ -350,6 +468,8 @@ IpStart(
350468
ContextAttributes.ulNumGroups = sizeof(IpGroups) / sizeof(CMD_GROUP_ENTRY);
351469
ContextAttributes.pCmdGroups = IpGroups;
352470

471+
ContextAttributes.pfnDumpFn = IpDumpFn;
472+
353473
RegisterContext(&ContextAttributes);
354474

355475
return ERROR_SUCCESS;

dll/win32/ifmon/lang/en-US.rc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ BEGIN
2525
IDS_SUBNETMASKS " Subnet Masks: %s\n"
2626

2727
IDS_EMPTYLINE " %s\n"
28+
29+
30+
31+
IDS_DUMP_NEWLINE "\n"
32+
IDS_DUMP_HEADERLINE "# ----------------------------------\n"
33+
34+
IDS_DUMP_IP_HEADER "# Interface IP Configuration for ""%s""\n"
35+
36+
2837
END

dll/win32/ifmon/resource.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@
2222
#define IDS_SUBNETMASKS 308
2323

2424
#define IDS_EMPTYLINE 400
25+
26+
#define IDS_DUMP_NEWLINE 800
27+
#define IDS_DUMP_HEADERLINE 801
28+
#define IDS_DUMP_IP_HEADER 802

0 commit comments

Comments
 (0)