[0.4.13][NETSTAT] Picky Backports up to master-state 2023-07-06

0.4.15-dev-6256-g 8ef47d2e5e [NETSTAT] Optimize a bit (#5405)
partially 0.4.15-dev-6245-g 5ee97b9537 [NETSTAT] -b flag implies -o flag on Windows XP/2003. CORE-19006 (#5377)
partially 0.4.15-dev-6211-g 40864bc15c [NETSTAT] Fix crash when parsing the protocol CORE-19005 (#5363)
partially 0.4.14-dev-479-g 1fa2780796 [NETSTAT] Fix output formats
This commit is contained in:
Joachim Henze 2023-07-06 00:26:34 +02:00
parent 7d4c736506
commit f074f61f65
3 changed files with 47 additions and 63 deletions

View file

@ -6,7 +6,7 @@ BEGIN
NETSTAT [-a] [-e] [-n] [-o] [-p proto] [-r] [-s] [interval]\n\n\
-a Displays all connections and listening ports.\n\
-e Displays Ethernet statistics. May be combined with -s\n\
option\n\
option.\n\
-n Displays address and port numbers in numeric form.\n\
-o Displays the process ID for each connection.\n\
-p proto Shows connections for protocol 'proto' TCP or UDP.\n\
@ -18,7 +18,7 @@ NETSTAT [-a] [-e] [-n] [-o] [-p proto] [-r] [-s] [interval]\n\n\
the -p option may be used to specify a subset of the default.\n\
interval Redisplays selected statistics every 'interval' seconds.\n\
Press CTRL+C to stop redisplaying. By default netstat will\n\
print the current information only once.\n\n"
print the current information only once.\n"
IDS_DISPLAY_THEADER "\n Proto Local Address Foreign Address State"
IDS_DISPLAY_PROCESS " Process\n"
IDS_ACTIVE_CONNECT "\nActive Connections\n"
@ -37,9 +37,9 @@ BEGIN
IDS_IP_OUT_REQUEST " Output Requests = %lu\n"
IDS_IP_ROUTE_DISCARD " Routing Discards = %lu\n"
IDS_IP_DISCARD_OUT_PACK " Discarded Output Packets = %lu\n"
IDS_IP_OUT_PACKET_NO_ROUTE " Output Packets No Route = %lu\n"
IDS_IP_OUT_PACKET_NO_ROUTE " Output Packet No Route = %lu\n"
IDS_IP_REASSEMBLE_REQUIRED " Reassembly Required = %lu\n"
IDS_IP_REASSEMBLE_SUCCESS " Reassembly Succesful = %lu\n"
IDS_IP_REASSEMBLE_SUCCESS " Reassembly Successful = %lu\n"
IDS_IP_REASSEMBLE_FAILURE " Reassembly Failures = %lu\n"
IDS_IP_DATAG_FRAG_SUCCESS " Datagrams Successfully Fragmented = %lu\n"
IDS_IP_DATAG_FRAG_FAILURE " Datagrams Failing Fragmentation = %lu\n"
@ -91,17 +91,17 @@ STRINGTABLE
BEGIN
IDS_ETHERNET_INTERFACE_STAT "Interface Statistics\n\n"
IDS_ETHERNET_THEADER " Received Sent\n\n"
IDS_ETHERNET_BYTES " Bytes %14lu %15lu\n"
IDS_ETHERNET_UNICAST_PACKET " Unicast packets %14lu %15lu\n"
IDS_ETHERNET_NON_UNICAST_PACKET " Non-unicast packets %14lu %15lu\n"
IDS_ETHERNET_DISCARD " Discards %14lu %15lu\n"
IDS_ETHERNET_ERROR " Errors %14lu %15lu\n"
IDS_ETHERNET_UNKNOWN " Unknown Protocols %14lu\n"
IDS_ETHERNET_BYTES "Bytes %14lu %15lu\n"
IDS_ETHERNET_UNICAST_PACKET "Unicast packets %14lu %15lu\n"
IDS_ETHERNET_NON_UNICAST_PACKET "Non-unicast packets %14lu %15lu\n"
IDS_ETHERNET_DISCARD "Discards %14lu %15lu\n"
IDS_ETHERNET_ERROR "Errors %14lu %15lu\n"
IDS_ETHERNET_UNKNOWN "Unknown protocols %14lu\n"
END
STRINGTABLE
BEGIN
IDS_ERROR_WSA_START "ERROR: WSAStartup() failed : %d\n"
IDS_ERROR_WSA_START "ERROR: WSAStartup() failed: %d\n"
IDS_ERROR_ROUTE "ERROR: Cannot find 'route.exe'\n"
IDS_ERROR_TCP_SNAPSHOT "ERROR: Failed to snapshot TCP endpoints.\n"
IDS_ERROR_UDP_ENDPOINT "ERROR: Failed to snapshot UDP endpoints.\n"

View file

@ -106,7 +106,7 @@ END
STRINGTABLE
BEGIN
IDS_ERROR_WSA_START "BŁĄD: Procedura WSAStartup() nie powiodła się : %d\n"
IDS_ERROR_WSA_START "BŁĄD: Procedura WSAStartup() nie powiodła się: %d\n"
IDS_ERROR_ROUTE "BŁĄD: Nie można odnaleźć pliku 'route.exe'\n"
IDS_ERROR_TCP_SNAPSHOT "BŁĄD: Nie powiodło się utworzenie migawki dla punktów końcowych TCP.\n"
IDS_ERROR_UDP_ENDPOINT "BŁĄD: Nie powiodło się utworzenie migawki dla punktów końcowych UDP.\n"

View file

@ -1,16 +1,13 @@
/*
* PROJECT: ReactOS netstat utility
* LICENSE: GPL - See COPYING in the top level directory
* FILE: base/applications/network/netstat/netstat.c
* PURPOSE: display IP stack statistics
* COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com>
*/
/*
* TODO:
* sort function return values.
* implement -b, -o and -v
* implement -b, -t and -v
* clean up GetIpHostName
* command line parser needs more work
*/
#include <stdio.h>
@ -61,10 +58,18 @@ VOID DoFormatMessage(DWORD ErrorCode)
NULL, ErrorCode, LANG_USER_DEFAULT);
}
VOID DisplayTableHeader(VOID)
{
ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
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
*
*/
BOOL ParseCmdline(int argc, wchar_t* argv[])
{
@ -89,6 +94,7 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
break;
case L'b':
bDoShowProcName = TRUE;
bDoShowProcessId = TRUE;
break;
case L'e':
bDoShowEthStats = TRUE;
@ -96,8 +102,17 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
case L'n':
bDoShowNumbers = TRUE;
break;
case L'o':
bDoShowProcessId = TRUE;
break;
case L'p':
bDoShowProtoCons = TRUE;
if (i+1 >= argc)
{
DisplayTableHeader();
return TRUE;
}
Proto = argv[i+1];
if (!_wcsicmp(L"IP", Proto))
Protocol = IP;
@ -108,10 +123,7 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
else if (!_wcsicmp(L"UDP", Proto))
Protocol = UDP;
else
{
ConResPuts(StdOut, IDS_USAGE);
return FALSE;
}
goto StopParsingAndShowUsageHelp;
break;
case L'r':
bDoShowRouteTable = TRUE;
@ -119,15 +131,11 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
case L's':
bDoShowProtoStats = TRUE;
break;
case L'o':
bDoShowProcessId = TRUE;
break;
case L'v':
// FIXME!
ConPuts(StdOut, L"got v\n");
bDoDispSeqComp = TRUE;
break;
default :
default:
StopParsingAndShowUsageHelp:
ConResPuts(StdOut, IDS_USAGE);
return FALSE;
}
@ -140,28 +148,11 @@ BOOL ParseCmdline(int argc, wchar_t* argv[])
else
return FALSE;
}
// else
// {
// ConResPrintf(StdOut, IDS_USAGE);
// return FALSE;
// }
}
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
*/
@ -204,13 +195,11 @@ BOOL DisplayOutput(VOID)
case TCP:
if (bDoShowProtoStats)
ShowTcpStatistics();
ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
DisplayTableHeader();
return ShowTcpTable();
case UDP:
if (bDoShowProtoStats)
ShowUdpStatistics();
ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
DisplayTableHeader();
return (bDoShowAllCons ? ShowUdpTable() : TRUE);
default:
@ -227,7 +216,6 @@ BOOL DisplayOutput(VOID)
}
else
{
ConResPuts(StdOut, IDS_ACTIVE_CONNECT);
DisplayTableHeader();
if (ShowTcpTable() && bDoShowAllCons)
ShowUdpTable();
@ -241,7 +229,7 @@ VOID ShowIpStatistics(VOID)
PMIB_IPSTATS pIpStats;
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)
{
@ -277,7 +265,7 @@ VOID ShowIcmpStatistics(VOID)
PMIB_ICMP pIcmpStats;
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)
{
@ -316,7 +304,6 @@ VOID ShowIcmpStatistics(VOID)
}
HeapFree(GetProcessHeap(), 0, pIcmpStats);
}
VOID ShowTcpStatistics(VOID)
@ -367,12 +354,12 @@ VOID ShowEthernetStatistics(VOID)
DWORD dwSize = 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)
{
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)
{
@ -411,17 +398,17 @@ BOOL ShowTcpTable(VOID)
CHAR PID[64];
/* 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()
* calls: */
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);
if ( error != NO_ERROR )
if (error != NO_ERROR)
HeapFree(GetProcessHeap(), 0, tcpTable);
}
while ( error == ERROR_INSUFFICIENT_BUFFER );
while (error == ERROR_INSUFFICIENT_BUFFER);
if (error != NO_ERROR)
{
@ -491,7 +478,7 @@ BOOL ShowUdpTable(VOID)
DoFormatMessage(error);
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);
if (error)
{
@ -504,7 +491,6 @@ BOOL ShowUdpTable(VOID)
/* Dump the UDP table */
for (i = 0; i < udpTable->dwNumEntries; i++)
{
/* I've split this up so it's easier to follow */
GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, sizeof(HostIp));
GetPortName(udpTable->table[i].dwLocalPort, "udp", HostPort, sizeof(HostPort));
@ -542,7 +528,7 @@ GetPortName(UINT Port, PCSTR Proto, CHAR Name[], INT NameLen)
}
/* Try to translate to a name */
if ((pServent = getservbyport(Port, Proto)))
strcpy(Name, pServent->s_name );
strcpy(Name, pServent->s_name);
else
sprintf(Name, "%d", htons((WORD)Port));
return Name;
@ -602,10 +588,8 @@ GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], INT NameLen)
}
/*
*
* 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[])
{