https://git.reactos.org/?p=reactos.git;a=commitdiff;h=a38b133dd1379417889ad…
commit a38b133dd1379417889ad165650748e01add7dd1
Author: Eric Kohl <eric.kohl(a)reactos.org>
AuthorDate: Thu Jun 22 12:40:46 2023 +0200
Commit: Eric Kohl <eric.kohl(a)reactos.org>
CommitDate: Thu Jun 22 12:40:46 2023 +0200
[NET] Add the net session command
---
base/applications/network/net/CMakeLists.txt | 1 +
base/applications/network/net/cmdSession.c | 154 +++++++++++++++++++++++++++
base/applications/network/net/main.c | 2 +-
base/applications/network/net/net.h | 3 +
4 files changed, 159 insertions(+), 1 deletion(-)
diff --git a/base/applications/network/net/CMakeLists.txt
b/base/applications/network/net/CMakeLists.txt
index bac107000f2..a990a9c2f6f 100644
--- a/base/applications/network/net/CMakeLists.txt
+++ b/base/applications/network/net/CMakeLists.txt
@@ -14,6 +14,7 @@ list(APPEND SOURCE
cmdHelpMsg.c
cmdLocalGroup.c
cmdPause.c
+ cmdSession.c
cmdShare.c
cmdStart.c
cmdStatistics.c
diff --git a/base/applications/network/net/cmdSession.c
b/base/applications/network/net/cmdSession.c
new file mode 100644
index 00000000000..90763b31ac6
--- /dev/null
+++ b/base/applications/network/net/cmdSession.c
@@ -0,0 +1,154 @@
+/*
+ * COPYRIGHT: See COPYING in the top level directory
+ * PROJECT: ReactOS net command
+ * FILE: base/applications/network/net/cmdSession.c
+ * PROGRAMMERS: Eric Kohl <eric.kohl(a)reactos.org>
+ */
+
+#include "net.h"
+
+static
+VOID
+SecondsToDurationString(
+ _Out_ PWSTR DurationString,
+ _In_ size_t DurationStringSize,
+ _In_ DWORD dwDuration)
+{
+ DWORD dwHours, dwRemainingSeconds, dwMinutes, dwSeconds;
+
+ dwHours = dwDuration / 3600;
+ dwRemainingSeconds = dwDuration % 3600;
+ dwMinutes = dwRemainingSeconds / 60;
+ dwSeconds = dwRemainingSeconds % 60;
+
+ StringCchPrintfW(DurationString, DurationStringSize, L"%02lu:%02lu:%02lu",
dwHours, dwMinutes, dwSeconds);
+}
+
+
+NET_API_STATUS
+EnumSessions(
+ _In_ PWSTR pszComputerName,
+ _In_ BOOL bList)
+{
+ PSESSION_INFO_2 pBuffer = NULL;
+ WCHAR DurationBuffer[10];
+ DWORD dwRead = 0, dwTotal = 0, i;
+ DWORD ResumeHandle = 0;
+ NET_API_STATUS Status;
+
+ Status = NetSessionEnum(pszComputerName,
+ NULL,
+ NULL,
+ 2,
+ (LPBYTE*)&pBuffer,
+ MAX_PREFERRED_LENGTH,
+ &dwRead,
+ &dwTotal,
+ &ResumeHandle);
+ if ((Status != NERR_Success) && (Status != ERROR_MORE_DATA))
+ {
+// PrintMessageStringV(3502, Status);
+ ConPrintf(StdOut, L"System error %lu has occurred.\n\n", Status);
+ return Status;
+ }
+
+ if (dwTotal == 0)
+ {
+ PrintMessageString(3683);
+ }
+ else
+ {
+ ConPuts(StdOut, L"\n");
+ PrintMessageString(4750);
+ PrintPadding(L'-', 79);
+ ConPuts(StdOut, L"\n");
+
+ for (i = 0; i < dwRead; i++)
+ {
+ if (pBuffer[i].sesi2_cname)
+ {
+ SecondsToDurationString(DurationBuffer,
+ ARRAYSIZE(DurationBuffer),
+ pBuffer[i].sesi2_idle_time);
+
+ ConPrintf(StdOut, L"%-22.22s %-20.20s %-17.17s %-5lu
%-8.8s\n",
+ pBuffer[i].sesi2_cname,
+ pBuffer[i].sesi2_username,
+ pBuffer[i].sesi2_cltype_name,
+ pBuffer[i].sesi2_num_opens,
+ DurationBuffer);
+ }
+ }
+ }
+
+ NetApiBufferFree(pBuffer);
+
+ return NERR_Success;
+}
+
+
+INT
+cmdSession(
+ _In_ INT argc,
+ _In_ WCHAR **argv)
+{
+ PWSTR pszComputerName = NULL;
+ BOOL bList = FALSE;
+ BOOL bDelete = FALSE;
+ INT i = 0;
+ NET_API_STATUS Status;
+ INT result = 0;
+
+ for (i = 2; i < argc; i++)
+ {
+ if (argv[i][0] == L'\\' && argv[i][1] == L'\\' &&
pszComputerName == NULL)
+ {
+ pszComputerName = argv[i];
+ i++;
+ }
+ else if (_wcsicmp(argv[i], L"/list") == 0)
+ {
+ bList = TRUE;
+ continue;
+ }
+ else if (_wcsicmp(argv[i], L"/delete") == 0)
+ {
+ bDelete = TRUE;
+ continue;
+ }
+ else if (_wcsicmp(argv[i], L"/help") == 0)
+ {
+ PrintMessageString(4381);
+ ConPuts(StdOut, L"\n");
+ PrintNetMessage(MSG_SESSION_SYNTAX);
+ PrintNetMessage(MSG_SESSION_HELP);
+ return 0;
+ }
+ else
+ {
+ PrintMessageString(4381);
+ ConPuts(StdOut, L"\n");
+ PrintNetMessage(MSG_SESSION_SYNTAX);
+ return 1;
+ }
+ }
+
+ if (bDelete)
+ Status = NetSessionDel(pszComputerName, NULL, NULL);
+ else
+ Status = EnumSessions(pszComputerName, bList);
+
+ if (Status == NERR_Success)
+ {
+ PrintErrorMessage(ERROR_SUCCESS);
+ }
+ else
+ {
+ PrintErrorMessage(Status);
+ result = 1;
+ }
+
+ return result;
+}
+
+/* EOF */
diff --git a/base/applications/network/net/main.c b/base/applications/network/net/main.c
index af56a0cbc2d..2da58adf5d8 100644
--- a/base/applications/network/net/main.c
+++ b/base/applications/network/net/main.c
@@ -30,7 +30,7 @@ COMMAND cmds[] =
{L"helpmsg", cmdHelpMsg},
{L"localgroup", cmdLocalGroup},
{L"pause", cmdPause},
- {L"session", unimplemented},
+ {L"session", cmdSession},
{L"share", cmdShare},
{L"start", cmdStart},
{L"statistics", cmdStatistics},
diff --git a/base/applications/network/net/net.h b/base/applications/network/net/net.h
index 7840027bd1a..a3659f73b33 100644
--- a/base/applications/network/net/net.h
+++ b/base/applications/network/net/net.h
@@ -22,6 +22,8 @@
#include <lm.h>
#include <ndk/rtlfuncs.h>
+#include <strsafe.h>
+
#include <conutils.h>
#include <net_msg.h>
@@ -82,6 +84,7 @@ INT cmdHelp(INT argc, WCHAR **argv);
INT cmdHelpMsg(INT argc, WCHAR **argv);
INT cmdLocalGroup(INT argc, WCHAR **argv);
INT cmdPause(INT argc, WCHAR **argv);
+INT cmdSession(INT argc, WCHAR **argv);
INT cmdShare(INT argc, WCHAR **argv);
INT cmdStart(INT argc, WCHAR **argv);
INT cmdStatistics(INT argc, WCHAR **argv);