2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS netstat utility
23 * FILE: apps/utils/net/netstat/netstat.c
24 * PURPOSE: display IP stack statistics
25 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
27 * Ged Murphy 19/09/05 Created
28 * Some ideas/code taken from Rob Dickinson's original app
34 * sort function return values.
35 * implement -b, -o and -v
36 * clean up GetIpHostName
37 * command line parser needs more work
48 enum ProtoType
{IP
, TCP
, UDP
, ICMP
} Protocol
;
49 DWORD Interval
; /* time to pause between printing output */
51 /* TCP endpoint states */
52 TCHAR TcpState
[][32] = {
70 * format message string and display output
72 DWORD
DoFormatMessage(DWORD ErrorCode
)
77 if ((RetVal
= FormatMessage(
78 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
79 FORMAT_MESSAGE_FROM_SYSTEM
|
80 FORMAT_MESSAGE_IGNORE_INSERTS
,
83 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
88 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
91 /* return number of TCHAR's stored in output buffer
92 * excluding '\0' - as FormatMessage does*/
102 * Parse command line parameters and set any options
105 BOOL
ParseCmdline(int argc
, char* argv
[])
111 if ((argc
== 1) || (_istdigit(*argv
[1])))
114 /* Parse command line for options we have been given. */
115 for (i
= 1; i
< argc
; i
++)
117 if ( (argc
> 1)&&(argv
[i
][0] == '-') )
121 while ((c
= *++argv
[i
]) != '\0')
126 bDoShowAllCons
= TRUE
;
129 bDoShowProcName
= TRUE
;
132 bDoShowEthStats
= TRUE
;
135 bDoShowNumbers
= TRUE
;
138 bDoShowProtoStats
= TRUE
;
141 bDoShowProtoCons
= TRUE
;
143 strncpy(Proto
, (++argv
)[i
], sizeof(Proto
));
144 if (!_tcsicmp( "IP", Proto
))
146 else if (!_tcsicmp( "ICMP", Proto
))
148 else if (!_tcsicmp( "TCP", Proto
))
150 else if (!_tcsicmp( "UDP", Proto
))
157 (--argv
)[i
]; /* move pointer back down to previous argv */
160 bDoShowRouteTable
= TRUE
;
163 _tprintf(_T("got v\n"));
164 bDoDispSeqComp
= TRUE
;
172 else if (_istdigit(*argv
[i
]))
174 if (_stscanf(argv
[i
], "%lu", &Interval
) != EOF
)
191 * Simulate Microsofts netstat utility output
197 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
202 if (bDoShowRouteTable
)
204 /* mingw doesn't have lib for _tsystem */
205 if (system("route print") == -1)
207 _tprintf(_T("cannot find 'route.exe'\n"));
215 ShowEthernetStatistics();
219 if (bDoShowProtoCons
)
224 if (bDoShowProtoStats
)
231 if (bDoShowProtoStats
)
233 ShowIcmpStatistics();
238 if (bDoShowProtoStats
)
240 _tprintf(_T("\nActive Connections\n"));
241 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
245 if (bDoShowProtoStats
)
247 _tprintf(_T("\nActive Connections\n"));
248 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
255 else if (bDoShowProtoStats
)
258 ShowIcmpStatistics();
263 else //if (bDoShowAllCons)
265 _tprintf(_T("\nActive Connections\n"));
266 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
276 VOID
ShowIpStatistics()
278 PMIB_IPSTATS pIpStats
;
281 pIpStats
= (MIB_IPSTATS
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS
));
283 if ((dwRetVal
= GetIpStatistics(pIpStats
)) == NO_ERROR
)
285 _tprintf(_T("\nIPv4 Statistics\n\n"));
286 _tprintf(_T(" %-34s = %lu\n"), _T("Packets Recieved"), pIpStats
->dwInReceives
);
287 _tprintf(_T(" %-34s = %lu\n"), _T("Received Header Errors"), pIpStats
->dwInHdrErrors
);
288 _tprintf(_T(" %-34s = %lu\n"), _T("Received Address Errors"), pIpStats
->dwInAddrErrors
);
289 _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams Forwarded"), pIpStats
->dwForwDatagrams
);
290 _tprintf(_T(" %-34s = %lu\n"), _T("Unknown Protocols Recieved"), pIpStats
->dwInUnknownProtos
);
291 _tprintf(_T(" %-34s = %lu\n"), _T("Received Packets Discarded"), pIpStats
->dwInDiscards
);
292 _tprintf(_T(" %-34s = %lu\n"), _T("Recieved Packets Delivered"), pIpStats
->dwInDelivers
);
293 _tprintf(_T(" %-34s = %lu\n"), _T("Output Requests"), pIpStats
->dwOutRequests
);
294 _tprintf(_T(" %-34s = %lu\n"), _T("Routing Discards"), pIpStats
->dwRoutingDiscards
);
295 _tprintf(_T(" %-34s = %lu\n"), _T("Discarded Output Packets"), pIpStats
->dwOutDiscards
);
296 _tprintf(_T(" %-34s = %lu\n"), _T("Output Packets No Route"), pIpStats
->dwOutNoRoutes
);
297 _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Required"), pIpStats
->dwReasmReqds
);
298 _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Succesful"), pIpStats
->dwReasmOks
);
299 _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Failures"), pIpStats
->dwReasmFails
);
300 // _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams succesfully fragmented"), NULL); /* FIXME: what is this one? */
301 _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams Failing Fragmentation"), pIpStats
->dwFragFails
);
302 _tprintf(_T(" %-34s = %lu\n"), _T("Fragments Created"), pIpStats
->dwFragCreates
);
305 DoFormatMessage(dwRetVal
);
307 HeapFree(GetProcessHeap(), 0, pIpStats
);
310 VOID
ShowIcmpStatistics()
312 PMIB_ICMP pIcmpStats
;
315 pIcmpStats
= (MIB_ICMP
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP
));
317 if ((dwRetVal
= GetIcmpStatistics(pIcmpStats
)) == NO_ERROR
)
319 _tprintf(_T("\nICMPv4 Statistics\n\n"));
320 _tprintf(_T(" Received Sent\n"));
321 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Messages"),
322 pIcmpStats
->stats
.icmpInStats
.dwMsgs
, pIcmpStats
->stats
.icmpOutStats
.dwMsgs
);
323 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Errors"),
324 pIcmpStats
->stats
.icmpInStats
.dwErrors
, pIcmpStats
->stats
.icmpOutStats
.dwErrors
);
325 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Destination Unreachable"),
326 pIcmpStats
->stats
.icmpInStats
.dwDestUnreachs
, pIcmpStats
->stats
.icmpOutStats
.dwDestUnreachs
);
327 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Time Exceeded"),
328 pIcmpStats
->stats
.icmpInStats
.dwTimeExcds
, pIcmpStats
->stats
.icmpOutStats
.dwTimeExcds
);
329 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Parameter Problems"),
330 pIcmpStats
->stats
.icmpInStats
.dwParmProbs
, pIcmpStats
->stats
.icmpOutStats
.dwParmProbs
);
331 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Source Quenches"),
332 pIcmpStats
->stats
.icmpInStats
.dwSrcQuenchs
, pIcmpStats
->stats
.icmpOutStats
.dwSrcQuenchs
);
333 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Redirects"),
334 pIcmpStats
->stats
.icmpInStats
.dwRedirects
, pIcmpStats
->stats
.icmpOutStats
.dwRedirects
);
335 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Echos"),
336 pIcmpStats
->stats
.icmpInStats
.dwEchos
, pIcmpStats
->stats
.icmpOutStats
.dwEchos
);
337 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Echo Replies"),
338 pIcmpStats
->stats
.icmpInStats
.dwEchoReps
, pIcmpStats
->stats
.icmpOutStats
.dwEchoReps
);
339 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Timestamps"),
340 pIcmpStats
->stats
.icmpInStats
.dwTimestamps
, pIcmpStats
->stats
.icmpOutStats
.dwTimestamps
);
341 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Timestamp Replies"),
342 pIcmpStats
->stats
.icmpInStats
.dwTimestampReps
, pIcmpStats
->stats
.icmpOutStats
.dwTimestampReps
);
343 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Address Masks"),
344 pIcmpStats
->stats
.icmpInStats
.dwAddrMasks
, pIcmpStats
->stats
.icmpOutStats
.dwAddrMasks
);
345 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Address Mask Replies"),
346 pIcmpStats
->stats
.icmpInStats
.dwAddrMaskReps
, pIcmpStats
->stats
.icmpOutStats
.dwAddrMaskReps
);
349 DoFormatMessage(dwRetVal
);
351 HeapFree(GetProcessHeap(), 0, pIcmpStats
);
355 VOID
ShowTcpStatistics()
357 PMIB_TCPSTATS pTcpStats
;
360 pTcpStats
= (MIB_TCPSTATS
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_TCPSTATS
));
362 if ((dwRetVal
= GetTcpStatistics(pTcpStats
)) == NO_ERROR
)
364 _tprintf(_T("\nTCP Statistics for IPv4\n\n"));
365 _tprintf(_T(" %-35s = %lu\n"), _T("Active Opens"), pTcpStats
->dwActiveOpens
);
366 _tprintf(_T(" %-35s = %lu\n"), _T("Passive Opens"), pTcpStats
->dwPassiveOpens
);
367 _tprintf(_T(" %-35s = %lu\n"), _T("Failed Connection Attempts"), pTcpStats
->dwAttemptFails
);
368 _tprintf(_T(" %-35s = %lu\n"), _T("Reset Connections"), pTcpStats
->dwEstabResets
);
369 _tprintf(_T(" %-35s = %lu\n"), _T("Current Connections"), pTcpStats
->dwCurrEstab
);
370 _tprintf(_T(" %-35s = %lu\n"), _T("Segments Recieved"), pTcpStats
->dwInSegs
);
371 _tprintf(_T(" %-35s = %lu\n"), _T("Segments Sent"), pTcpStats
->dwOutSegs
);
372 _tprintf(_T(" %-35s = %lu\n"), _T("Segments Retransmitted"), pTcpStats
->dwRetransSegs
);
375 DoFormatMessage(dwRetVal
);
377 HeapFree(GetProcessHeap(), 0, pTcpStats
);
380 VOID
ShowUdpStatistics()
382 PMIB_UDPSTATS pUdpStats
;
385 pUdpStats
= (MIB_UDPSTATS
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_UDPSTATS
));
387 if ((dwRetVal
= GetUdpStatistics(pUdpStats
)) == NO_ERROR
)
389 _tprintf(_T("\nUDP Statistics for IPv4\n\n"));
390 _tprintf(_T(" %-21s = %lu\n"), _T("Datagrams Recieved"), pUdpStats
->dwInDatagrams
);
391 _tprintf(_T(" %-21s = %lu\n"), _T("No Ports"), pUdpStats
->dwNoPorts
);
392 _tprintf(_T(" %-21s = %lu\n"), _T("Recieve Errors"), pUdpStats
->dwInErrors
);
393 _tprintf(_T(" %-21s = %lu\n"), _T("Datagrams Sent"), pUdpStats
->dwOutDatagrams
);
396 DoFormatMessage(dwRetVal
);
398 HeapFree(GetProcessHeap(), 0, pUdpStats
);
401 VOID
ShowEthernetStatistics()
403 PMIB_IFTABLE pIfTable
;
407 pIfTable
= (MIB_IFTABLE
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE
));
409 if (GetIfTable(pIfTable
, &dwSize
, 0) == ERROR_INSUFFICIENT_BUFFER
)
411 HeapFree(GetProcessHeap(), 0, pIfTable
);
412 pIfTable
= (MIB_IFTABLE
*) HeapAlloc(GetProcessHeap(), 0, dwSize
);
414 if ((dwRetVal
= GetIfTable(pIfTable
, &dwSize
, 0)) == NO_ERROR
)
416 _tprintf(_T("Interface Statistics\n\n"));
417 _tprintf(_T(" Received Sent\n\n"));
418 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Bytes"),
419 pIfTable
->table
[0].dwInOctets
, pIfTable
->table
[0].dwOutOctets
);
420 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Unicast packets"),
421 pIfTable
->table
[0].dwInUcastPkts
, pIfTable
->table
[0].dwOutUcastPkts
);
422 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Non-unicast packets"),
423 pIfTable
->table
[0].dwInNUcastPkts
, pIfTable
->table
[0].dwOutNUcastPkts
);
424 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Discards"),
425 pIfTable
->table
[0].dwInDiscards
, pIfTable
->table
[0].dwOutDiscards
);
426 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Errors"),
427 pIfTable
->table
[0].dwInErrors
, pIfTable
->table
[0].dwOutErrors
);
428 _tprintf(_T("%-20s %14lu\n"), _T("Unknown Protocols"),
429 pIfTable
->table
[0].dwInUnknownProtos
);
432 DoFormatMessage(dwRetVal
);
434 HeapFree(GetProcessHeap(), 0, pIfTable
);
439 PMIB_TCPTABLE tcpTable
;
442 CHAR HostIp
[HOSTNAMELEN
], HostPort
[PORTNAMELEN
];
443 CHAR RemoteIp
[HOSTNAMELEN
], RemotePort
[PORTNAMELEN
];
444 CHAR Host
[ADDRESSLEN
];
445 CHAR Remote
[ADDRESSLEN
];
447 /* Get the table of TCP endpoints */
449 error
= GetTcpTable(NULL
, &dwSize
, TRUE
);
450 if (error
!= ERROR_INSUFFICIENT_BUFFER
)
452 printf("Failed to snapshot TCP endpoints.\n");
453 DoFormatMessage(error
);
456 tcpTable
= (PMIB_TCPTABLE
) HeapAlloc(GetProcessHeap(), 0, dwSize
);
457 error
= GetTcpTable(tcpTable
, &dwSize
, TRUE
);
460 printf("Failed to snapshot TCP endpoints table.\n");
461 DoFormatMessage(error
);
462 HeapFree(GetProcessHeap(), 0, tcpTable
);
466 /* Dump the TCP table */
467 for (i
= 0; i
< tcpTable
->dwNumEntries
; i
++)
469 /* If we aren't showing all connections, only display established, close wait
470 * and time wait. This is the default output for netstat */
471 if (bDoShowAllCons
|| (tcpTable
->table
[i
].dwState
== MIB_TCP_STATE_ESTAB
)
472 || (tcpTable
->table
[i
].dwState
== MIB_TCP_STATE_CLOSE_WAIT
)
473 || (tcpTable
->table
[i
].dwState
== MIB_TCP_STATE_TIME_WAIT
))
475 /* I've split this up so it's easier to follow */
476 GetIpHostName(TRUE
, tcpTable
->table
[i
].dwLocalAddr
, HostIp
, HOSTNAMELEN
);
477 GetPortName(tcpTable
->table
[i
].dwLocalPort
, "tcp", HostPort
, PORTNAMELEN
);
478 GetIpHostName(FALSE
, tcpTable
->table
[i
].dwRemoteAddr
, RemoteIp
, HOSTNAMELEN
);
479 GetPortName(tcpTable
->table
[i
].dwRemotePort
, "tcp", RemotePort
, PORTNAMELEN
);
481 sprintf(Host
, "%s:%s", HostIp
, HostPort
);
482 sprintf(Remote
, "%s:%s", RemoteIp
, RemotePort
);
484 _tprintf(_T(" %-6s %-22s %-22s %s\n"), _T("TCP"),
485 Host
, Remote
, TcpState
[tcpTable
->table
[i
].dwState
]);
488 HeapFree(GetProcessHeap(), 0, tcpTable
);
494 PMIB_UDPTABLE udpTable
;
497 CHAR HostIp
[HOSTNAMELEN
], HostPort
[PORTNAMELEN
];
498 CHAR Host
[ADDRESSLEN
];
500 /* Get the table of UDP endpoints */
502 error
= GetUdpTable(NULL
, &dwSize
, TRUE
);
503 if (error
!= ERROR_INSUFFICIENT_BUFFER
)
505 printf("Failed to snapshot UDP endpoints.\n");
506 DoFormatMessage(error
);
509 udpTable
= (PMIB_UDPTABLE
) HeapAlloc(GetProcessHeap(), 0, dwSize
);
510 error
= GetUdpTable(udpTable
, &dwSize
, TRUE
);
513 printf("Failed to snapshot UDP endpoints table.\n");
514 DoFormatMessage(error
);
515 HeapFree(GetProcessHeap(), 0, udpTable
);
519 /* Dump the UDP table */
520 for (i
= 0; i
< udpTable
->dwNumEntries
; i
++)
523 /* I've split this up so it's easier to follow */
524 GetIpHostName(TRUE
, udpTable
->table
[i
].dwLocalAddr
, HostIp
, HOSTNAMELEN
);
525 GetPortName(udpTable
->table
[i
].dwLocalPort
, "tcp", HostPort
, PORTNAMELEN
);
527 sprintf(Host
, "%s:%s", HostIp
, HostPort
);
529 _tprintf(_T(" %-6s %-22s %-22s\n"), _T("UDP"), Host
, _T(":*:"));
532 HeapFree(GetProcessHeap(), 0, udpTable
);
537 * Translate port numbers into their text equivalent if there is one
540 GetPortName(UINT Port
, PCSTR Proto
, CHAR Name
[], INT NameLen
)
542 struct servent
*pSrvent
;
546 sprintf(Name
, "%d", htons((WORD
)Port
));
549 /* Try to translate to a name */
550 if ((pSrvent
= getservbyport(Port
, Proto
)))
551 strcpy(Name
, pSrvent
->s_name
);
553 sprintf(Name
, "%d", htons((WORD
)Port
));
561 // Translate IP addresses into their name-resolved form if possible.
564 GetIpHostName(BOOL Local
, UINT IpAddr
, CHAR Name
[], int NameLen
)
566 // struct hostent *phostent;
569 // Does the user want raw numbers?
570 nIpAddr
= htonl(IpAddr
);
571 if (bDoShowNumbers
) {
572 sprintf(Name
, "%d.%d.%d.%d",
573 (nIpAddr
>> 24) & 0xFF,
574 (nIpAddr
>> 16) & 0xFF,
575 (nIpAddr
>> 8) & 0xFF,
582 // Try to translate to a name
585 sprintf(Name
, "%d.%d.%d.%d",
586 (nIpAddr
>> 24) & 0xFF,
587 (nIpAddr
>> 16) & 0xFF,
588 (nIpAddr
>> 8) & 0xFF,
591 gethostname(Name
, NameLen
);
593 } else if (IpAddr
== 0x0100007f) {
595 if (gethostname(Name
, NameLen
) != 0)
596 DoFormatMessage(WSAGetLastError());
598 strcpy(Name
, "localhost");
600 // } else if (phostent = gethostbyaddr((char*)&ipaddr, sizeof(nipaddr), PF_INET)) {
601 // strcpy(name, phostent->h_name);
603 sprintf(Name
, "%d.%d.%d.%d",
604 ((nIpAddr
>> 24) & 0x000000FF),
605 ((nIpAddr
>> 16) & 0x000000FF),
606 ((nIpAddr
>> 8) & 0x000000FF),
607 ((nIpAddr
) & 0x000000FF));
614 _tprintf(_T("Displays current TCP/IP protocol statistics and network connections.\n\n"
615 "NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]\n\n"
616 " -a Displays all connections and listening ports.\n"
617 " -e Displays Ethernet statistics. May be combined with -s\n"
619 " -n Displays address and port numbers in numeric form.\n"
620 " -p proto Shows connections for protocol 'proto' TCP or UDP.\n"
621 " If used with the -s option to display\n"
622 " per-protocol statistics, 'proto' may be TCP, UDP, or IP.\n"
623 " -r Displays the current routing table.\n"
624 " -s Displays per-protocol statistics. By default, Statistics are\n"
625 " shown for IP, ICMP, TCP and UDP;\n"
626 " the -p option may be used to specify a subset of the default.\n"
627 " interval Redisplays selected statistics every 'interval' seconds.\n"
628 " Press CTRL+C to stop redisplaying. By default netstat will\n"
629 " print the current information only once.\n"));
636 * Parse command line parameters and set any options
637 * Run display output, looping over set intervals if a number is given
640 int main(int argc
, char *argv
[])
644 if (WSAStartup(MAKEWORD(2, 2), &wsaData
) != 0)
646 _tprintf(_T("WSAStartup() failed : %d\n"), WSAGetLastError());
650 if (ParseCmdline(argc
, argv
))
659 Sleep(Interval
*1000);