[NETSH] Support the flags CMD_FLAG_ONLINE, CMD_FLAG_LOCAL and CMD_FLAG_HIDDEN

This commit is contained in:
Eric Kohl
2026-07-18 16:57:00 +02:00
parent ae50d745ca
commit 0943343296
5 changed files with 154 additions and 82 deletions
+20 -20
View File
@@ -29,9 +29,8 @@ PCONTEXT_ENTRY pCurrentContext = NULL;
PCONTEXT_STACK_ENTRY pContextStackHead = NULL;
PCONTEXT_STACK_ENTRY pContextStackTail = NULL;
PWSTR pszMachine = NULL;
static BOOL bOnline = TRUE;
PWSTR g_pszMachine = NULL;
BOOL g_bOnline = TRUE;
/* FUNCTIONS ******************************************************************/
@@ -581,7 +580,7 @@ ExitCommand(
_In_ LPCVOID pvData,
_Out_ BOOL *pbDone)
{
if (bOnline == FALSE)
if (g_bOnline == FALSE)
CommitContext(pRootContext, NETSH_FLUSH);
*pbDone = TRUE;
@@ -617,7 +616,7 @@ OfflineCommand(
{
DPRINT("OfflineCommand()\n");
CommitContext(pRootContext, NETSH_UNCOMMIT);
bOnline = FALSE;
g_bOnline = FALSE;
return ERROR_SUCCESS;
}
@@ -635,7 +634,7 @@ OnlineCommand(
{
DPRINT("OnlineCommand()\n");
CommitContext(pRootContext, NETSH_COMMIT);
bOnline = TRUE;
g_bOnline = TRUE;
return ERROR_SUCCESS;
}
@@ -734,18 +733,18 @@ SetMachineCommand(
if ((dwArgCount - dwCurrentIndex) > 1)
return ERROR_SHOW_USAGE;
if (pszMachine != NULL)
if (g_pszMachine != NULL)
{
HeapFree(GetProcessHeap(), 0, pszMachine);
pszMachine = NULL;
HeapFree(GetProcessHeap(), 0, g_pszMachine);
g_pszMachine = NULL;
}
if ((dwArgCount - dwCurrentIndex) == 1)
{
pszMachine = HeapAlloc(GetProcessHeap(), 0, (sizeof(argv[dwCurrentIndex]) + 1) * sizeof(WCHAR));
if (pszMachine == NULL)
g_pszMachine = HeapAlloc(GetProcessHeap(), 0, (sizeof(argv[dwCurrentIndex]) + 1) * sizeof(WCHAR));
if (g_pszMachine == NULL)
return ERROR_NOT_ENOUGH_MEMORY;
wcscpy(pszMachine, argv[dwCurrentIndex]);
wcscpy(g_pszMachine, argv[dwCurrentIndex]);
}
return dwError;
@@ -774,12 +773,12 @@ SetModeCommand(
if (!_wcsicmp(argv[dwCurrentIndex], L"offline"))
{
CommitContext(pRootContext, NETSH_UNCOMMIT);
bOnline = FALSE;
g_bOnline = FALSE;
}
else if (!_wcsicmp(argv[dwCurrentIndex], L"online"))
{
CommitContext(pRootContext, NETSH_COMMIT);
bOnline = TRUE;
g_bOnline = TRUE;
}
else
{
@@ -802,7 +801,7 @@ ShowModeCommand(
_Out_ BOOL *pbDone)
{
DPRINT("ShowModeCommand()\n");
ConPuts(StdOut, bOnline ? L"online\n\n" : L"offline\n\n");
ConPuts(StdOut, g_bOnline ? L"online\n\n" : L"offline\n\n");
return ERROR_SUCCESS;
}
@@ -839,19 +838,19 @@ CreateRootContext(VOID)
pGroup = AddCommandGroup(pRootContext, L"add", IDS_HLP_GROUP_ADD, 0, NULL);
if (pGroup)
{
AddGroupCommand(pGroup, L"helper", AddHelperCommand, IDS_HLP_ADD_HELPER, IDS_HLP_ADD_HELPER_EX, 0, NULL);
AddGroupCommand(pGroup, L"helper", AddHelperCommand, IDS_HLP_ADD_HELPER, IDS_HLP_ADD_HELPER_EX, CMD_FLAG_LOCAL, NULL);
}
pGroup = AddCommandGroup(pRootContext, L"delete", IDS_HLP_GROUP_DELETE, 0, NULL);
if (pGroup)
{
AddGroupCommand(pGroup, L"helper", DeleteHelperCommand, IDS_HLP_DEL_HELPER, IDS_HLP_DEL_HELPER_EX, 0, NULL);
AddGroupCommand(pGroup, L"helper", DeleteHelperCommand, IDS_HLP_DEL_HELPER, IDS_HLP_DEL_HELPER_EX, CMD_FLAG_LOCAL, NULL);
}
pGroup = AddCommandGroup(pRootContext, L"set", IDS_HLP_GROUP_SET, 0, NULL);
if (pGroup)
{
AddGroupCommand(pGroup, L"machine", SetMachineCommand, IDS_HLP_SET_MACHINE, IDS_HLP_SET_MACHINE_EX, 0, NULL);
AddGroupCommand(pGroup, L"machine", SetMachineCommand, IDS_HLP_SET_MACHINE, IDS_HLP_SET_MACHINE_EX, CMD_FLAG_ONLINE, NULL);
AddGroupCommand(pGroup, L"mode", SetModeCommand, IDS_HLP_SET_MODE, IDS_HLP_SET_MODE_EX, 0, NULL);
}
@@ -959,6 +958,7 @@ RegisterContext(
pContext->pfnDumpFn = pChildContext->pfnDumpFn;
pContext->pfnConnectFn = pChildContext->pfnConnectFn;
pContext->pfnOsVersionCheck = pChildContext->pfnOsVersionCheck;
pContext->dwFlags = pChildContext->dwFlags;
pContext->ulPriority = (pChildContext->dwFlags & CMD_FLAG_PRIORITY) ?
pChildContext->ulPriority : DEFAULT_CONTEXT_PRIORITY;
@@ -1003,8 +1003,8 @@ RegisterContext(
if (pContext->pfnConnectFn)
{
dwError = pContext->pfnConnectFn(pszMachine);
DPRINT("pfnConnectFn(%S) returned %lu\n", pszMachine, dwError);
dwError = pContext->pfnConnectFn(g_pszMachine);
DPRINT("pfnConnectFn(%S) returned %lu\n", g_pszMachine, dwError);
}
}
+98 -52
View File
@@ -87,18 +87,21 @@ PrintShortGroupCommands(
WCHAR szBuffer1[TINY_HELP_BUFFER_SIZE];
WCHAR szBuffer2[SMALL_HELP_BUFFER_SIZE];
pCommand = pGroup->pCommandListHead;
while (pCommand != NULL)
for (pCommand = pGroup->pCommandListHead; pCommand != NULL; pCommand = pCommand->pNext)
{
DPRINT("CheckVersion (Command) %S %S", pGroup->pwszCmdGroupToken, pCommand->pwszCmdToken);
if (CheckOsVersion(pCommand->pfnOsVersionCheck))
{
_swprintf(szBuffer1, L"%s %s", pGroup->pwszCmdGroupToken, pCommand->pwszCmdToken);
LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer2, _countof(szBuffer2));
if (!CheckOsVersion(pCommand->pfnOsVersionCheck))
continue;
ConPrintf(StdOut, L"%-15s - %s", szBuffer1, szBuffer2);
}
pCommand = pCommand->pNext;
if (((pCommand->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pCommand->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pCommand->dwFlags & CMD_FLAG_HIDDEN))
continue;
_swprintf(szBuffer1, L"%s %s", pGroup->pwszCmdGroupToken, pCommand->pwszCmdToken);
LoadStringW(pContext->hModule, pCommand->dwShortCmdHelpToken, szBuffer2, _countof(szBuffer2));
ConPrintf(StdOut, L"%-15s - %s", szBuffer1, szBuffer2);
}
}
@@ -133,27 +136,45 @@ PrintContext(
PrintCurrentContextHeader(pContext);
/* Count short commands */
pCommand = pContext->pCommandListHead;
while (pCommand != NULL)
for (pCommand = pContext->pCommandListHead; pCommand != NULL; pCommand = pCommand->pNext)
{
if (!CheckOsVersion(pCommand->pfnOsVersionCheck))
continue;
if (((pCommand->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pCommand->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pCommand->dwFlags & CMD_FLAG_HIDDEN))
continue;
dwCount++;
pCommand = pCommand->pNext;
}
/* Count short groups */
pGroup = pContext->pGroupListHead;
while (pGroup != NULL)
for (pGroup = pContext->pGroupListHead; pGroup != NULL; pGroup = pGroup->pNext)
{
if (!CheckOsVersion(pGroup->pfnOsVersionCheck))
continue;
if (((pGroup->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pGroup->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pGroup->dwFlags & CMD_FLAG_HIDDEN))
continue;
dwCount++;
pGroup = pGroup->pNext;
}
/* Count short subcontexts */
pSubContext = pContext->pSubContextHead;
while (pSubContext != NULL)
for (pSubContext = pContext->pSubContextHead; pSubContext != NULL; pSubContext = pSubContext->pNext)
{
if (!CheckOsVersion(pSubContext->pfnOsVersionCheck))
continue;
if (((pSubContext->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pSubContext->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pSubContext->dwFlags & CMD_FLAG_HIDDEN))
continue;
dwCount++;
pSubContext = pSubContext->pNext;
}
pHelpArray = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwCount * sizeof(HELP_ENTRY));
@@ -163,38 +184,56 @@ PrintContext(
dwIndex = 0;
/* Add short commands */
pCommand = pContext->pCommandListHead;
while (pCommand != NULL)
for (pCommand = pContext->pCommandListHead; pCommand != NULL; pCommand = pCommand->pNext)
{
if (!CheckOsVersion(pCommand->pfnOsVersionCheck))
continue;
if (((pCommand->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pCommand->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pCommand->dwFlags & CMD_FLAG_HIDDEN))
continue;
pHelpArray[dwIndex].Type = Command;
pHelpArray[dwIndex].pszCommand = pCommand->pwszCmdToken;
pHelpArray[dwIndex].dwHelpId = pCommand->dwShortCmdHelpToken;
pHelpArray[dwIndex].Pointer.pCommand = pCommand;
dwIndex++;
pCommand = pCommand->pNext;
}
/* Add short groups */
pGroup = pContext->pGroupListHead;
while (pGroup != NULL)
for (pGroup = pContext->pGroupListHead; pGroup != NULL; pGroup = pGroup->pNext)
{
if (!CheckOsVersion(pGroup->pfnOsVersionCheck))
continue;
if (((pGroup->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pGroup->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pGroup->dwFlags & CMD_FLAG_HIDDEN))
continue;
pHelpArray[dwIndex].Type = Group;
pHelpArray[dwIndex].pszCommand = pGroup->pwszCmdGroupToken;
pHelpArray[dwIndex].dwHelpId = pGroup->dwShortCmdHelpToken;
pHelpArray[dwIndex].Pointer.pGroup = pGroup;
dwIndex++;
pGroup = pGroup->pNext;
}
/* Count short subcontexts */
pSubContext = pContext->pSubContextHead;
while (pSubContext != NULL)
for (pSubContext = pContext->pSubContextHead; pSubContext != NULL; pSubContext = pSubContext->pNext)
{
if (!CheckOsVersion(pSubContext->pfnOsVersionCheck))
continue;
if (((pSubContext->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pSubContext->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pSubContext->dwFlags & CMD_FLAG_HIDDEN))
continue;
pHelpArray[dwIndex].Type = SubContext;
pHelpArray[dwIndex].pszCommand = pSubContext->pszContextName;
pHelpArray[dwIndex].Pointer.pSubContext = pSubContext;
dwIndex++;
pSubContext = pSubContext->pNext;
}
qsort(pHelpArray, dwCount, sizeof(HELP_ENTRY), HelpCompare);
@@ -204,29 +243,20 @@ PrintContext(
switch (pHelpArray[dwIndex].Type)
{
case Command:
if (CheckOsVersion(pHelpArray[dwIndex].Pointer.pCommand->pfnOsVersionCheck))
{
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, _countof(szBuffer)) == 0)
szBuffer[0] = UNICODE_NULL;
ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
}
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, _countof(szBuffer)) == 0)
szBuffer[0] = UNICODE_NULL;
ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
break;
case Group:
if (CheckOsVersion(pHelpArray[dwIndex].Pointer.pGroup->pfnOsVersionCheck))
{
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, _countof(szBuffer)) == 0)
szBuffer[0] = UNICODE_NULL;
ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
}
if (LoadStringW(pContext->hModule, pHelpArray[dwIndex].dwHelpId, szBuffer, _countof(szBuffer)) == 0)
szBuffer[0] = UNICODE_NULL;
ConPrintf(StdOut, L"%-15s - %s", pHelpArray[dwIndex].pszCommand, szBuffer);
break;
case SubContext:
if (CheckOsVersion(pHelpArray[dwIndex].Pointer.pSubContext->pfnOsVersionCheck))
{
GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, _countof(szBuffer));
ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pHelpArray[dwIndex].pszCommand, szBuffer);
}
GetContextFullName(pHelpArray[dwIndex].Pointer.pSubContext, szBuffer, _countof(szBuffer));
ConPrintf(StdOut, L"%-15s - Changes to the \"%s\" context.\n", pHelpArray[dwIndex].pszCommand, szBuffer);
break;
}
}
@@ -258,11 +288,17 @@ PrintSubcontexts(
return;
dwCount = 0;
pSubContext = pContext->pSubContextHead;
while (pSubContext != NULL)
for (pSubContext = pContext->pSubContextHead; pSubContext != NULL; pSubContext = pSubContext->pNext)
{
if (!CheckOsVersion(pSubContext->pfnOsVersionCheck))
continue;
if (((pSubContext->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pSubContext->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pSubContext->dwFlags & CMD_FLAG_HIDDEN))
continue;
dwCount++;
pSubContext = pSubContext->pNext;
}
pSubContextArray = HeapAlloc(GetProcessHeap(), 0, dwCount * sizeof(PCONTEXT_ENTRY));
@@ -270,12 +306,18 @@ PrintSubcontexts(
return;
dwIndex = 0;
pSubContext = pContext->pSubContextHead;
while (pSubContext != NULL)
for (pSubContext = pContext->pSubContextHead; pSubContext != NULL; pSubContext = pSubContext->pNext)
{
if (!CheckOsVersion(pSubContext->pfnOsVersionCheck))
continue;
if (((pSubContext->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pSubContext->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pSubContext->dwFlags & CMD_FLAG_HIDDEN))
continue;
pSubContextArray[dwIndex] = pSubContext;
dwIndex++;
pSubContext = pSubContext->pNext;
}
qsort(pSubContextArray, dwCount, sizeof(PCONTEXT_ENTRY), SubContextCompare);
@@ -283,8 +325,7 @@ PrintSubcontexts(
ConResPrintf(StdOut, IDS_SUBCONTEXT_HEADER);
for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
{
if (CheckOsVersion(pSubContextArray[dwIndex]->pfnOsVersionCheck))
ConPrintf(StdOut, L" %s", pSubContextArray[dwIndex]->pszContextName);
ConPrintf(StdOut, L" %s", pSubContextArray[dwIndex]->pszContextName);
}
ConPuts(StdOut, L"\n");
@@ -307,6 +348,11 @@ PrintCommandHelp(
if (!CheckOsVersion(pCommand->pfnOsVersionCheck))
return;
if (((pCommand->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pCommand->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)) ||
(pCommand->dwFlags & CMD_FLAG_HIDDEN))
return;
dwLength += wcslen(pCommand->pwszCmdToken);
if (pGroup)
dwLength += (wcslen(pGroup->pwszCmdGroupToken) + 1);
+28 -4
View File
@@ -201,6 +201,14 @@ InterpretCommand(
break;
}
if (((pCommand->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pCommand->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)))
{
dwError = ERROR_CMD_NOT_FOUND;
State = STATE_DONE;
break;
}
/* Check for help keywords */
if (((dwArgIndex + 1) == (dwArgCount - 1)) &&
((_wcsicmp(argv[dwArgIndex + 1], L"?") == 0) || (_wcsicmp(argv[dwArgIndex + 1], L"help") == 0)))
@@ -213,7 +221,7 @@ InterpretCommand(
if (pCommand->pfnCmdHandler != NULL)
{
dwArgIndex++;
dwError = pCommand->pfnCmdHandler(pszMachine, argv, dwArgIndex, dwArgCount, 0, NULL, bDone);
dwError = pCommand->pfnCmdHandler(g_pszMachine, argv, dwArgIndex, dwArgCount, 0, NULL, bDone);
if (dwError != ERROR_SUCCESS)
{
if (dwError == ERROR_SHOW_USAGE)
@@ -248,6 +256,14 @@ InterpretCommand(
break;
}
if (((pGroup->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pGroup->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)))
{
dwError = ERROR_CMD_NOT_FOUND;
State = STATE_DONE;
break;
}
/* Check for group without command */
if (dwArgIndex == (dwArgCount - 1))
{
@@ -288,6 +304,14 @@ InterpretCommand(
break;
}
if (((pTempSubContext->dwFlags & CMD_FLAG_LOCAL) && (g_pszMachine != NULL)) ||
((pTempSubContext->dwFlags & CMD_FLAG_ONLINE) && (g_bOnline == FALSE)))
{
dwError = ERROR_CMD_NOT_FOUND;
State = STATE_DONE;
break;
}
if (pTempSubContext == pCurrentContext)
{
if (dwArgIndex != (dwArgCount - 1))
@@ -302,7 +326,7 @@ InterpretCommand(
DPRINT("Set current context\n");
pCurrentContext = pTempSubContext;
if (pCurrentContext->pfnConnectFn)
dwError = pCurrentContext->pfnConnectFn(pszMachine);
dwError = pCurrentContext->pfnConnectFn(g_pszMachine);
State = STATE_DONE;
break;
}
@@ -443,8 +467,8 @@ InterpretInteractive(VOID)
memset(args_vector, 0, sizeof(args_vector));
/* Shown just before the input where the user places commands */
if (pszMachine)
ConPrintf(StdOut, L"[%s] ", pszMachine);
if (g_pszMachine)
ConPrintf(StdOut, L"[%s] ", g_pszMachine);
PrintPrompt(pCurrentContext);
ConPuts(StdOut, L">");
+5 -5
View File
@@ -182,15 +182,15 @@ wmain(
if ((index + 1) < argc)
{
index++;
pszMachine = HeapAlloc(GetProcessHeap(), 0, (wcslen(argv[index]) + 1) * sizeof(WCHAR));
if (pszMachine == NULL)
g_pszMachine = HeapAlloc(GetProcessHeap(), 0, (wcslen(argv[index]) + 1) * sizeof(WCHAR));
if (g_pszMachine == NULL)
{
dwError = ERROR_NOT_ENOUGH_MEMORY;
PrintError(g_hModule, dwError);
goto done;
}
wcscpy(pszMachine, argv[index]);
wcscpy(g_pszMachine, argv[index]);
}
else
{
@@ -254,8 +254,8 @@ wmain(
done:
/* FIXME: Cleanup code goes here */
if (pszMachine != NULL)
HeapFree(GetProcessHeap(), 0, pszMachine);
if (g_pszMachine != NULL)
HeapFree(GetProcessHeap(), 0, g_pszMachine);
if (pszCommand != NULL)
HeapFree(GetProcessHeap(), 0, pszCommand);
+3 -1
View File
@@ -109,6 +109,7 @@ typedef struct _CONTEXT_ENTRY
GUID Guid;
HMODULE hModule;
ULONG ulPriority;
DWORD dwFlags;
PNS_CONTEXT_COMMIT_FN pfnCommitFn;
PNS_CONTEXT_DUMP_FN pfnDumpFn;
PNS_CONTEXT_CONNECT_FN pfnConnectFn;
@@ -133,7 +134,8 @@ extern PCONTEXT_ENTRY pCurrentContext;
extern PHELPER_ENTRY pHelperListHead;
extern HMODULE g_hModule;
extern PWSTR pszMachine;
extern PWSTR g_pszMachine;
extern BOOL g_bOnline;
extern UINT VersionInfoArchitecture;
extern UINT VersionInfoOsProductSuite;