Skip to content

Commit fb2a0d1

Browse files
committed
[NETSH] Implement the dump command
1 parent 3cdc3d1 commit fb2a0d1

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

base/applications/network/netsh/context.c

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,96 @@ DeleteContext(
307307
}
308308

309309

310+
static
311+
int
312+
DumpCompare(
313+
_In_ const void *p1,
314+
_In_ const void *p2)
315+
{
316+
return ((PCONTEXT_ENTRY)p1)->ulPriority - ((PCONTEXT_ENTRY)p2)->ulPriority;
317+
}
318+
319+
320+
static
321+
DWORD
322+
DumpContext(
323+
_In_ PCONTEXT_ENTRY pContext,
324+
_In_ LPCWSTR pwszMachine,
325+
_In_ LPWSTR *ppwcArguments,
326+
_In_ DWORD dwArgCount,
327+
_In_ LPCVOID pvData)
328+
{
329+
PCONTEXT_ENTRY pSubContext, *pSortArray = NULL;
330+
DWORD dwCount, dwIndex;
331+
DWORD dwError = ERROR_SUCCESS;
332+
333+
DPRINT("DumpContext()\n");
334+
335+
if (pContext->pfnDumpFn)
336+
{
337+
dwError = pContext->pfnDumpFn(pwszMachine,
338+
ppwcArguments,
339+
dwArgCount,
340+
pvData);
341+
if (dwError != ERROR_SUCCESS)
342+
{
343+
DPRINT1("Dump function failed (Error %lu)\n", dwError);
344+
return dwError;
345+
}
346+
}
347+
348+
if (pContext->pSubContextHead == NULL)
349+
return dwError;
350+
351+
/* Count the sub-contexts */
352+
dwCount = 0;
353+
pSubContext = pContext->pSubContextHead;
354+
while (pSubContext)
355+
{
356+
dwCount++;
357+
pSubContext = pSubContext->pNext;
358+
}
359+
360+
/* Allocate the sort array */
361+
pSortArray = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(PCONTEXT_ENTRY));
362+
if (pSortArray == NULL)
363+
return ERROR_NOT_ENOUGH_MEMORY;
364+
365+
/* Fill the sort array */
366+
dwIndex = 0;
367+
pSubContext = pContext->pSubContextHead;
368+
while (pSubContext)
369+
{
370+
pSortArray[dwIndex] = pSubContext;
371+
dwIndex++;
372+
pSubContext = pSubContext->pNext;
373+
}
374+
375+
/* Sort the array */
376+
qsort(pSortArray, dwCount, sizeof(PCONTEXT_ENTRY), DumpCompare);
377+
378+
/* Dump the sub-contexts */
379+
for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
380+
{
381+
dwError = DumpContext(pSortArray[dwIndex],
382+
pwszMachine,
383+
ppwcArguments,
384+
dwArgCount,
385+
pvData);
386+
if (dwError != ERROR_SUCCESS)
387+
{
388+
DPRINT1("Dump function failed (Error %lu)\n", dwError);
389+
break;
390+
}
391+
}
392+
393+
/* Free the sort array */
394+
HeapFree(GetProcessHeap(), 0, pSortArray);
395+
396+
return dwError;
397+
}
398+
399+
310400
DWORD
311401
WINAPI
312402
UpCommand(
@@ -325,6 +415,27 @@ UpCommand(
325415
}
326416

327417

418+
DWORD
419+
WINAPI
420+
DumpCommand(
421+
LPCWSTR pwszMachine,
422+
LPWSTR *ppwcArguments,
423+
DWORD dwCurrentIndex,
424+
DWORD dwArgCount,
425+
DWORD dwFlags,
426+
LPCVOID pvData,
427+
BOOL *pbDone)
428+
{
429+
DPRINT("DumpCommand()\n");
430+
431+
return DumpContext(pCurrentContext,
432+
pwszMachine,
433+
ppwcArguments,
434+
dwArgCount,
435+
pvData);
436+
}
437+
438+
328439
DWORD
329440
WINAPI
330441
ExecCommand(
@@ -464,11 +575,12 @@ CreateRootContext(VOID)
464575
pRootContext->hModule = GetModuleHandle(NULL);
465576

466577
AddContextCommand(pRootContext, L"..", UpCommand, IDS_HLP_UP, IDS_HLP_UP_EX, 0);
467-
AddContextCommand(pRootContext, L"?", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0);
578+
AddContextCommand(pRootContext, L"?", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0);
468579
AddContextCommand(pRootContext, L"bye", ExitCommand, IDS_HLP_EXIT, IDS_HLP_EXIT_EX, 0);
580+
AddContextCommand(pRootContext, L"dump", DumpCommand, IDS_HLP_DUMP, IDS_HLP_DUMP_EX, 0);
469581
AddContextCommand(pRootContext, L"exec", ExecCommand, IDS_HLP_EXEC, IDS_HLP_EXEC_EX, 0);
470582
AddContextCommand(pRootContext, L"exit", ExitCommand, IDS_HLP_EXIT, IDS_HLP_EXIT_EX, 0);
471-
AddContextCommand(pRootContext, L"help", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0);
583+
AddContextCommand(pRootContext, L"help", NULL, IDS_HLP_HELP, IDS_HLP_HELP_EX, 0);
472584
AddContextCommand(pRootContext, L"popd", PopdCommand, IDS_HLP_POPD, IDS_HLP_POPD_EX, 0);
473585
AddContextCommand(pRootContext, L"pushd", PushdCommand, IDS_HLP_PUSHD, IDS_HLP_PUSHD_EX, 0);
474586
AddContextCommand(pRootContext, L"quit", ExitCommand, IDS_HLP_EXIT, IDS_HLP_EXIT_EX, 0);
@@ -582,6 +694,9 @@ RegisterContext(
582694
pContext = AddContext(pParentContext, pChildContext->pwszContext, (GUID*)&pChildContext->guidHelper);
583695
if (pContext != NULL)
584696
{
697+
pContext->pfnDumpFn = pChildContext->pfnDumpFn;
698+
pContext->ulPriority = (pChildContext->dwFlags & CMD_FLAG_PRIORITY) ? pChildContext->ulPriority : 100;
699+
585700
if ((pHelper != NULL) && (pHelper->pDllEntry != NULL))
586701
{
587702
pContext->hModule = pHelper->pDllEntry->hModule;

base/applications/network/netsh/lang/en-US.rc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ BEGIN
3737
IDS_HLP_PUSHD_EX "Syntax: pushd\n\n Stores the current context on the stack.\n\n"
3838
IDS_HLP_EXEC "Runs a script file.\n"
3939
IDS_HLP_EXEC_EX "Syntax: exec <ScriptFile>\n\n Loads the script file and runs it.\n\n"
40+
IDS_HLP_DUMP "Displays a configuration script.\n"
41+
IDS_HLP_DUMP_EX "Syntax: dump\n\n Creates a script that contains the current configuration. If saved to a\n\
42+
file, the script can be used to altered configuration settings.\n\n"
4043

4144
IDS_HLP_ADD_HELPER "Installs a helper DLL.\n"
4245
IDS_HLP_ADD_HELPER_EX "Syntax: add helper <dll file name>\n\n Installs the specified helper DLL in netsh.\n\n"

base/applications/network/netsh/precomp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ typedef struct _CONTEXT_ENTRY
106106
PWSTR pszContextName;
107107
GUID Guid;
108108
HMODULE hModule;
109+
PNS_CONTEXT_DUMP_FN pfnDumpFn;
110+
ULONG ulPriority;
109111

110112
PCOMMAND_ENTRY pCommandListHead;
111113
PCOMMAND_ENTRY pCommandListTail;

base/applications/network/netsh/resource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#define IDS_HLP_PUSHD_EX 309
3434
#define IDS_HLP_EXEC 310
3535
#define IDS_HLP_EXEC_EX 311
36+
#define IDS_HLP_DUMP 312
37+
#define IDS_HLP_DUMP_EX 313
3638

3739
#define IDS_HLP_ADD_HELPER 320
3840
#define IDS_HLP_ADD_HELPER_EX 321

0 commit comments

Comments
 (0)