[NETSTAT]

- Fix parsing of the -p option and enable / as option indicator.
- Display foreign address of a listening TCP port as '<host>:0'.

svn path=/trunk/; revision=71095
This commit is contained in:
Eric Kohl 2016-04-03 14:10:31 +00:00
parent bb8967851b
commit 57bdb633c8

View file

@ -83,20 +83,18 @@ DWORD DoFormatMessage(DWORD ErrorCode)
*/ */
BOOL ParseCmdline(int argc, char* argv[]) BOOL ParseCmdline(int argc, char* argv[])
{ {
LPSTR Proto;
CHAR c;
INT i; INT i;
TCHAR Proto[5];
if ((argc == 1) || (_istdigit(*argv[1]))) if ((argc == 1) || (_istdigit(*argv[1])))
bNoOptions = TRUE; bNoOptions = TRUE;
/* Parse command line for options we have been given. */ /* Parse command line for options we have been given. */
for (i = 1; i < argc; i++) for (i = 1; i < argc; i++)
{ {
if ( (argc > 1)&&(argv[i][0] == '-') ) if ((argc > 1) && (argv[i][0] == '-' || argv[i][0] == '/'))
{ {
TCHAR c;
while ((c = *++argv[i]) != '\0') while ((c = *++argv[i]) != '\0')
{ {
switch (tolower(c)) switch (tolower(c))
@ -113,31 +111,29 @@ BOOL ParseCmdline(int argc, char* argv[])
case 'n' : case 'n' :
bDoShowNumbers = TRUE; bDoShowNumbers = TRUE;
break; break;
case 's' :
bDoShowProtoStats = TRUE;
break;
case 'p' : case 'p' :
bDoShowProtoCons = TRUE; bDoShowProtoCons = TRUE;
Proto = argv[i+1];
strncpy(Proto, (++argv)[i], sizeof(Proto)); if (!_stricmp("IP", Proto))
if (!_tcsicmp( "IP", Proto ))
Protocol = IP; Protocol = IP;
else if (!_tcsicmp( "ICMP", Proto )) else if (!_stricmp("ICMP", Proto))
Protocol = ICMP; Protocol = ICMP;
else if (!_tcsicmp( "TCP", Proto )) else if (!_stricmp("TCP", Proto))
Protocol = TCP; Protocol = TCP;
else if (!_tcsicmp( "UDP", Proto )) else if (!_stricmp("UDP", Proto))
Protocol = UDP; Protocol = UDP;
else else
{ {
Usage(); Usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
--i; /* move pointer back down to previous argv */
break; break;
case 'r' : case 'r' :
bDoShowRouteTable = TRUE; bDoShowRouteTable = TRUE;
break; break;
case 's' :
bDoShowProtoStats = TRUE;
break;
case 'v' : case 'v' :
_tprintf(_T("got v\n")); _tprintf(_T("got v\n"));
bDoDispSeqComp = TRUE; bDoDispSeqComp = TRUE;
@ -243,7 +239,7 @@ BOOL DisplayOutput()
_tprintf(_T("\nActive Connections\n")); _tprintf(_T("\nActive Connections\n"));
_tprintf(_T("\n Proto Local Address Foreign Address State\n")); _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
ShowTcpTable(); ShowTcpTable();
if (bDoShowAllCons) if (bDoShowAllCons)
ShowUdpTable(); ShowUdpTable();
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -452,11 +448,18 @@ VOID ShowTcpTable()
/* I've split this up so it's easier to follow */ /* I've split this up so it's easier to follow */
GetIpHostName(TRUE, tcpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN); GetIpHostName(TRUE, tcpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN);
GetPortName(tcpTable->table[i].dwLocalPort, "tcp", HostPort, PORTNAMELEN); GetPortName(tcpTable->table[i].dwLocalPort, "tcp", HostPort, PORTNAMELEN);
GetIpHostName(FALSE, tcpTable->table[i].dwRemoteAddr, RemoteIp, HOSTNAMELEN);
GetPortName(tcpTable->table[i].dwRemotePort, "tcp", RemotePort, PORTNAMELEN);
sprintf(Host, "%s:%s", HostIp, HostPort); sprintf(Host, "%s:%s", HostIp, HostPort);
sprintf(Remote, "%s:%s", RemoteIp, RemotePort);
if (tcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN)
{
sprintf(Remote, "%s:0", HostIp);
}
else
{
GetIpHostName(FALSE, tcpTable->table[i].dwRemoteAddr, RemoteIp, HOSTNAMELEN);
GetPortName(tcpTable->table[i].dwRemotePort, "tcp", RemotePort, PORTNAMELEN);
sprintf(Remote, "%s:%s", RemoteIp, RemotePort);
}
_tprintf(_T(" %-6s %-22s %-22s %s\n"), _T("TCP"), _tprintf(_T(" %-6s %-22s %-22s %s\n"), _T("TCP"),
Host, Remote, TcpState[tcpTable->table[i].dwState]); Host, Remote, TcpState[tcpTable->table[i].dwState]);
@ -585,7 +588,7 @@ GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], int NameLen)
VOID Usage() VOID Usage()
{ {
_tprintf(_T("\nDisplays current TCP/IP protocol statistics and network connections.\n\n" _tprintf(_T("\nDisplays current TCP/IP protocol statistics and network connections.\n\n"
"NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]\n\n" "NETSTAT [-a] [-e] [-n] [-p proto] [-r] [-s] [interval]\n\n"
" -a Displays all connections and listening ports.\n" " -a Displays all connections and listening ports.\n"
" -e Displays Ethernet statistics. May be combined with -s\n" " -e Displays Ethernet statistics. May be combined with -s\n"
" option\n" " option\n"