[NETSTAT]
authorEric Kohl <eric.kohl@reactos.org>
Sun, 3 Apr 2016 14:10:31 +0000 (14:10 +0000)
committerEric Kohl <eric.kohl@reactos.org>
Sun, 3 Apr 2016 14:10:31 +0000 (14:10 +0000)
- 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

reactos/base/applications/network/netstat/netstat.c

index 616d0e1..9412ebb 100644 (file)
@@ -83,20 +83,18 @@ DWORD DoFormatMessage(DWORD ErrorCode)
  */
 BOOL ParseCmdline(int argc, char* argv[])
 {
+    LPSTR Proto;
+    CHAR c;
     INT i;
 
-    TCHAR Proto[5];
-
     if ((argc == 1) || (_istdigit(*argv[1])))
         bNoOptions = TRUE;
 
     /* Parse command line for options we have been given. */
     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')
             {
                 switch (tolower(c))
@@ -113,31 +111,29 @@ BOOL ParseCmdline(int argc, char* argv[])
                     case 'n' :
                         bDoShowNumbers = TRUE;
                         break;
-                    case 's' :
-                        bDoShowProtoStats = TRUE;
-                        break;
                     case 'p' :
                         bDoShowProtoCons = TRUE;
-
-                        strncpy(Proto, (++argv)[i], sizeof(Proto));
-                        if (!_tcsicmp( "IP", Proto ))
+                        Proto = argv[i+1];
+                        if (!_stricmp("IP", Proto))
                             Protocol = IP;
-                        else if (!_tcsicmp( "ICMP", Proto ))
+                        else if (!_stricmp("ICMP", Proto))
                             Protocol = ICMP;
-                        else if (!_tcsicmp( "TCP", Proto ))
+                        else if (!_stricmp("TCP", Proto))
                             Protocol = TCP;
-                        else if (!_tcsicmp( "UDP", Proto ))
+                        else if (!_stricmp("UDP", Proto))
                             Protocol = UDP;
                         else
                         {
                             Usage();
                             return EXIT_FAILURE;
                         }
-                        --i; /* move pointer back down to previous argv */
                         break;
                     case 'r' :
                         bDoShowRouteTable = TRUE;
                         break;
+                    case 's' :
+                        bDoShowProtoStats = TRUE;
+                        break;
                     case 'v' :
                         _tprintf(_T("got v\n"));
                         bDoDispSeqComp = TRUE;
@@ -243,7 +239,7 @@ BOOL DisplayOutput()
         _tprintf(_T("\nActive Connections\n"));
         _tprintf(_T("\n  Proto  Local Address          Foreign Address        State\n"));
         ShowTcpTable();
-               if (bDoShowAllCons)
+        if (bDoShowAllCons)
             ShowUdpTable();
     }
     return EXIT_SUCCESS;
@@ -452,11 +448,18 @@ VOID ShowTcpTable()
             /* I've split this up so it's easier to follow */
             GetIpHostName(TRUE, tcpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN);
             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(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"),
             Host, Remote, TcpState[tcpTable->table[i].dwState]);
@@ -585,7 +588,7 @@ GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], int NameLen)
 VOID Usage()
 {
     _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"
     "  -e            Displays Ethernet statistics. May be combined with -s\n"
     "                option\n"