Compare commits

...

6 Commits

Author SHA1 Message Date
Joachim Henze
71142b486c [NETSTAT] Whitespace debloat for consistency to other funcs 2023-06-24 01:58:13 +02:00
Joachim Henze
f09d9d9ad1 [NETSTAT] Match Windows exact screen output and ERRORLEVEL like a freak
which means:
ERRORLEVEL 0 for 'netstat -abnop' and displaying Active-string + table header
ERRORLEVEL 1 for 'netstat -abnop bullshit' and displaying usage help
ERRORLEVEL 0 for 'netstat -abnop tcp' and displaying Active-string + table header + contents
ERRORLEVEL 0 for 'netstat -abnop udp' and displaying Active-string + table header + contents
2023-06-24 01:07:32 +02:00
Joachim Henze
c39e0a5be4 [NETSTAT] And the last nitpick, ready 4 review now 2023-06-24 00:20:19 +02:00
Joachim Henze
62ed5f18a3 [NETSTAT] Unrelated formatting nitpicks
Since we do touch the file anyway.
2023-06-24 00:11:27 +02:00
Joachim Henze
5db7c3b5d2 [NETSTAT] Anticipate a formatting nitpick from reviewers
By the way the crash was a regression of
0.4.11-dev-814-g 2b55073360
2023-06-23 23:53:50 +02:00
Joachim Henze
15f9460824 [NETSTAT] Fix crash when parsing the protocol CORE-19005 2023-06-23 23:39:34 +02:00

View File

@@ -1,16 +1,13 @@
/* /*
* PROJECT: ReactOS netstat utility * PROJECT: ReactOS netstat utility
* LICENSE: GPL - See COPYING in the top level directory * LICENSE: GPL - See COPYING in the top level directory
* FILE: base/applications/network/netstat/netstat.c
* PURPOSE: display IP stack statistics * PURPOSE: display IP stack statistics
* COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com> * COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com>
*/ */
/* /*
* TODO: * TODO:
* sort function return values.
* implement -b, -t and -v * implement -b, -t and -v
* clean up GetIpHostName * clean up GetIpHostName
* command line parser needs more work
*/ */
#include <stdio.h> #include <stdio.h>
@@ -62,9 +59,19 @@ VOID DoFormatMessage(DWORD ErrorCode)
} }
/* /*
* * Display table header
*/
VOID DisplayTableHeader(VOID)
{
ConResPuts(StdOut, IDS_DISPLAY_THEADER);
if (bDoShowProcessId)
ConResPuts(StdOut, IDS_DISPLAY_PROCESS);
else
ConPuts(StdOut, L"\n");
}
/*
* Parse command line parameters and set any options * Parse command line parameters and set any options
*
*/ */
BOOL ParseCmdline(int argc, wchar_t* argv[]) BOOL ParseCmdline(int argc, wchar_t* argv[])
{ {
@@ -103,6 +110,13 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
break; break;
case L'p': case L'p':
bDoShowProtoCons = TRUE; bDoShowProtoCons = TRUE;
if (i+1 >= argc)
{
ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
DisplayTableHeader();
return TRUE;
}
Proto = argv[i+1]; Proto = argv[i+1];
if (!_wcsicmp(L"IP", Proto)) if (!_wcsicmp(L"IP", Proto))
Protocol = IP; Protocol = IP;
@@ -113,10 +127,7 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
else if (!_wcsicmp(L"UDP", Proto)) else if (!_wcsicmp(L"UDP", Proto))
Protocol = UDP; Protocol = UDP;
else else
{ goto StopParsingAndShowUsageHelp;
ConResPuts(StdErr, IDS_USAGE);
return FALSE;
}
break; break;
case L'r': case L'r':
bDoShowRouteTable = TRUE; bDoShowRouteTable = TRUE;
@@ -133,7 +144,8 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
ConPuts(StdErr, L"'v' option is FIXME (Accepted option though unimplemented feature).\n"); ConPuts(StdErr, L"'v' option is FIXME (Accepted option though unimplemented feature).\n");
bDoDispSeqComp = TRUE; bDoDispSeqComp = TRUE;
break; break;
default : default:
StopParsingAndShowUsageHelp:
ConResPuts(StdErr, IDS_USAGE); ConResPuts(StdErr, IDS_USAGE);
return FALSE; return FALSE;
} }
@@ -146,28 +158,11 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
else else
return FALSE; return FALSE;
} }
// else
// {
// ConResPrintf(StdErr, IDS_USAGE);
// return FALSE;
// }
} }
return TRUE; return TRUE;
} }
/*
* Display table header
*/
VOID DisplayTableHeader(VOID)
{
ConResPuts(StdOut, IDS_DISPLAY_THEADER);
if (bDoShowProcessId)
ConResPuts(StdOut, IDS_DISPLAY_PROCESS);
else
ConPuts(StdOut, L"\n");
}
/* /*
* Simulate Microsofts netstat utility output * Simulate Microsofts netstat utility output
*/ */
@@ -248,7 +243,7 @@ VOID ShowIpStatistics(VOID)
PMIB_IPSTATS pIpStats; PMIB_IPSTATS pIpStats;
DWORD dwRetVal; DWORD dwRetVal;
pIpStats = (MIB_IPSTATS*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS)); pIpStats = (MIB_IPSTATS*)HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS));
if ((dwRetVal = GetIpStatistics(pIpStats)) == NO_ERROR) if ((dwRetVal = GetIpStatistics(pIpStats)) == NO_ERROR)
{ {
@@ -284,7 +279,7 @@ VOID ShowIcmpStatistics(VOID)
PMIB_ICMP pIcmpStats; PMIB_ICMP pIcmpStats;
DWORD dwRetVal; DWORD dwRetVal;
pIcmpStats = (MIB_ICMP*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP)); pIcmpStats = (MIB_ICMP*)HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP));
if ((dwRetVal = GetIcmpStatistics(pIcmpStats)) == NO_ERROR) if ((dwRetVal = GetIcmpStatistics(pIcmpStats)) == NO_ERROR)
{ {
@@ -323,7 +318,6 @@ VOID ShowIcmpStatistics(VOID)
} }
HeapFree(GetProcessHeap(), 0, pIcmpStats); HeapFree(GetProcessHeap(), 0, pIcmpStats);
} }
VOID ShowTcpStatistics(VOID) VOID ShowTcpStatistics(VOID)
@@ -374,12 +368,12 @@ VOID ShowEthernetStatistics(VOID)
DWORD dwSize = 0; DWORD dwSize = 0;
DWORD dwRetVal = 0; DWORD dwRetVal = 0;
pIfTable = (MIB_IFTABLE*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE)); pIfTable = (MIB_IFTABLE*)HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE));
if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{ {
HeapFree(GetProcessHeap(), 0, pIfTable); HeapFree(GetProcessHeap(), 0, pIfTable);
pIfTable = (MIB_IFTABLE*) HeapAlloc(GetProcessHeap(), 0, dwSize); pIfTable = (MIB_IFTABLE*)HeapAlloc(GetProcessHeap(), 0, dwSize);
if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR) if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR)
{ {
@@ -418,17 +412,17 @@ BOOL ShowTcpTable(VOID)
CHAR PID[64]; CHAR PID[64];
/* Get the table of TCP endpoints */ /* Get the table of TCP endpoints */
dwSize = sizeof (MIB_TCPTABLE_OWNER_PID); dwSize = sizeof(MIB_TCPTABLE_OWNER_PID);
/* Should also work when we get new connections between 2 GetTcpTable() /* Should also work when we get new connections between 2 GetTcpTable()
* calls: */ * calls: */
do do
{ {
tcpTable = (PMIB_TCPTABLE_OWNER_PID) HeapAlloc(GetProcessHeap(), 0, dwSize); tcpTable = (PMIB_TCPTABLE_OWNER_PID)HeapAlloc(GetProcessHeap(), 0, dwSize);
error = GetExtendedTcpTable(tcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); error = GetExtendedTcpTable(tcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
if ( error != NO_ERROR ) if (error != NO_ERROR)
HeapFree(GetProcessHeap(), 0, tcpTable); HeapFree(GetProcessHeap(), 0, tcpTable);
} }
while ( error == ERROR_INSUFFICIENT_BUFFER ); while (error == ERROR_INSUFFICIENT_BUFFER);
if (error != NO_ERROR) if (error != NO_ERROR)
{ {
@@ -498,7 +492,7 @@ BOOL ShowUdpTable(VOID)
DoFormatMessage(error); DoFormatMessage(error);
return FALSE; return FALSE;
} }
udpTable = (PMIB_UDPTABLE_OWNER_PID) HeapAlloc(GetProcessHeap(), 0, dwSize); udpTable = (PMIB_UDPTABLE_OWNER_PID)HeapAlloc(GetProcessHeap(), 0, dwSize);
error = GetExtendedUdpTable(udpTable, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0); error = GetExtendedUdpTable(udpTable, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0);
if (error) if (error)
{ {
@@ -511,7 +505,6 @@ BOOL ShowUdpTable(VOID)
/* Dump the UDP table */ /* Dump the UDP table */
for (i = 0; i < udpTable->dwNumEntries; i++) for (i = 0; i < udpTable->dwNumEntries; i++)
{ {
/* I've split this up so it's easier to follow */ /* I've split this up so it's easier to follow */
GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, sizeof(HostIp)); GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, sizeof(HostIp));
GetPortName(udpTable->table[i].dwLocalPort, "udp", HostPort, sizeof(HostPort)); GetPortName(udpTable->table[i].dwLocalPort, "udp", HostPort, sizeof(HostPort));
@@ -549,7 +542,7 @@ GetPortName(UINT Port, PCSTR Proto, CHAR Name[], INT NameLen)
} }
/* Try to translate to a name */ /* Try to translate to a name */
if ((pServent = getservbyport(Port, Proto))) if ((pServent = getservbyport(Port, Proto)))
strcpy(Name, pServent->s_name ); strcpy(Name, pServent->s_name);
else else
sprintf(Name, "%d", htons((WORD)Port)); sprintf(Name, "%d", htons((WORD)Port));
return Name; return Name;
@@ -609,10 +602,8 @@ GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], INT NameLen)
} }
/* /*
*
* Parse command line parameters and set any options * Parse command line parameters and set any options
* Run display output, looping over set intervals if a number is given * Run display output, looping over set interval if a number is given
*
*/ */
int wmain(int argc, wchar_t *argv[]) int wmain(int argc, wchar_t *argv[])
{ {