2 * PROJECT: ReactOS netstat utility
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: apps/utils/net/netstat/netstat.c
5 * PURPOSE: display IP stack statistics
6 * COPYRIGHT: Copyright 2005 Ged Murphy <gedmurphy@gmail.com>
10 * sort function return values.
11 * implement -b, -o and -v
12 * clean up GetIpHostName
13 * command line parser needs more work
25 enum ProtoType
{IP
, TCP
, UDP
, ICMP
} Protocol
;
26 DWORD Interval
; /* time to pause between printing output */
28 /* TCP endpoint states */
29 TCHAR TcpState
[][32] = {
47 * format message string and display output
49 DWORD
DoFormatMessage(DWORD ErrorCode
)
54 if ((RetVal
= FormatMessage(
55 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
56 FORMAT_MESSAGE_FROM_SYSTEM
|
57 FORMAT_MESSAGE_IGNORE_INSERTS
,
60 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
65 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
68 /* return number of TCHAR's stored in output buffer
69 * excluding '\0' - as FormatMessage does*/
79 * Parse command line parameters and set any options
82 BOOL
ParseCmdline(int argc
, char* argv
[])
88 if ((argc
== 1) || (_istdigit(*argv
[1])))
91 /* Parse command line for options we have been given. */
92 for (i
= 1; i
< argc
; i
++)
94 if ( (argc
> 1)&&(argv
[i
][0] == '-') )
98 while ((c
= *++argv
[i
]) != '\0')
103 bDoShowAllCons
= TRUE
;
106 bDoShowProcName
= TRUE
;
109 bDoShowEthStats
= TRUE
;
112 bDoShowNumbers
= TRUE
;
115 bDoShowProtoStats
= TRUE
;
118 bDoShowProtoCons
= TRUE
;
120 strncpy(Proto
, (++argv
)[i
], sizeof(Proto
));
121 if (!_tcsicmp( "IP", Proto
))
123 else if (!_tcsicmp( "ICMP", Proto
))
125 else if (!_tcsicmp( "TCP", Proto
))
127 else if (!_tcsicmp( "UDP", Proto
))
134 --i
; /* move pointer back down to previous argv */
137 bDoShowRouteTable
= TRUE
;
140 _tprintf(_T("got v\n"));
141 bDoDispSeqComp
= TRUE
;
149 else if (_istdigit(*argv
[i
]))
151 if (_stscanf(argv
[i
], "%lu", &Interval
) != EOF
)
168 * Simulate Microsofts netstat utility output
174 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
179 if (bDoShowRouteTable
)
181 /* mingw doesn't have lib for _tsystem */
182 if (system("route print") == -1)
184 _tprintf(_T("cannot find 'route.exe'\n"));
192 ShowEthernetStatistics();
196 if (bDoShowProtoCons
)
201 if (bDoShowProtoStats
)
208 if (bDoShowProtoStats
)
210 ShowIcmpStatistics();
215 if (bDoShowProtoStats
)
217 _tprintf(_T("\nActive Connections\n"));
218 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
222 if (bDoShowProtoStats
)
224 _tprintf(_T("\nActive Connections\n"));
225 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
232 else if (bDoShowProtoStats
)
235 ShowIcmpStatistics();
242 _tprintf(_T("\nActive Connections\n"));
243 _tprintf(_T("\n Proto Local Address Foreign Address State\n"));
254 VOID
ShowIpStatistics()
256 PMIB_IPSTATS pIpStats
;
259 pIpStats
= (MIB_IPSTATS
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS
));
261 if ((dwRetVal
= GetIpStatistics(pIpStats
)) == NO_ERROR
)
263 _tprintf(_T("\nIPv4 Statistics\n\n"));
264 _tprintf(_T(" %-34s = %lu\n"), _T("Packets Recieved"), pIpStats
->dwInReceives
);
265 _tprintf(_T(" %-34s = %lu\n"), _T("Received Header Errors"), pIpStats
->dwInHdrErrors
);
266 _tprintf(_T(" %-34s = %lu\n"), _T("Received Address Errors"), pIpStats
->dwInAddrErrors
);
267 _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams Forwarded"), pIpStats
->dwForwDatagrams
);
268 _tprintf(_T(" %-34s = %lu\n"), _T("Unknown Protocols Recieved"), pIpStats
->dwInUnknownProtos
);
269 _tprintf(_T(" %-34s = %lu\n"), _T("Received Packets Discarded"), pIpStats
->dwInDiscards
);
270 _tprintf(_T(" %-34s = %lu\n"), _T("Recieved Packets Delivered"), pIpStats
->dwInDelivers
);
271 _tprintf(_T(" %-34s = %lu\n"), _T("Output Requests"), pIpStats
->dwOutRequests
);
272 _tprintf(_T(" %-34s = %lu\n"), _T("Routing Discards"), pIpStats
->dwRoutingDiscards
);
273 _tprintf(_T(" %-34s = %lu\n"), _T("Discarded Output Packets"), pIpStats
->dwOutDiscards
);
274 _tprintf(_T(" %-34s = %lu\n"), _T("Output Packets No Route"), pIpStats
->dwOutNoRoutes
);
275 _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Required"), pIpStats
->dwReasmReqds
);
276 _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Succesful"), pIpStats
->dwReasmOks
);
277 _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Failures"), pIpStats
->dwReasmFails
);
278 // _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams succesfully fragmented"), NULL); /* FIXME: what is this one? */
279 _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams Failing Fragmentation"), pIpStats
->dwFragFails
);
280 _tprintf(_T(" %-34s = %lu\n"), _T("Fragments Created"), pIpStats
->dwFragCreates
);
283 DoFormatMessage(dwRetVal
);
285 HeapFree(GetProcessHeap(), 0, pIpStats
);
288 VOID
ShowIcmpStatistics()
290 PMIB_ICMP pIcmpStats
;
293 pIcmpStats
= (MIB_ICMP
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP
));
295 if ((dwRetVal
= GetIcmpStatistics(pIcmpStats
)) == NO_ERROR
)
297 _tprintf(_T("\nICMPv4 Statistics\n\n"));
298 _tprintf(_T(" Received Sent\n"));
299 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Messages"),
300 pIcmpStats
->stats
.icmpInStats
.dwMsgs
, pIcmpStats
->stats
.icmpOutStats
.dwMsgs
);
301 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Errors"),
302 pIcmpStats
->stats
.icmpInStats
.dwErrors
, pIcmpStats
->stats
.icmpOutStats
.dwErrors
);
303 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Destination Unreachable"),
304 pIcmpStats
->stats
.icmpInStats
.dwDestUnreachs
, pIcmpStats
->stats
.icmpOutStats
.dwDestUnreachs
);
305 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Time Exceeded"),
306 pIcmpStats
->stats
.icmpInStats
.dwTimeExcds
, pIcmpStats
->stats
.icmpOutStats
.dwTimeExcds
);
307 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Parameter Problems"),
308 pIcmpStats
->stats
.icmpInStats
.dwParmProbs
, pIcmpStats
->stats
.icmpOutStats
.dwParmProbs
);
309 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Source Quenches"),
310 pIcmpStats
->stats
.icmpInStats
.dwSrcQuenchs
, pIcmpStats
->stats
.icmpOutStats
.dwSrcQuenchs
);
311 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Redirects"),
312 pIcmpStats
->stats
.icmpInStats
.dwRedirects
, pIcmpStats
->stats
.icmpOutStats
.dwRedirects
);
313 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Echos"),
314 pIcmpStats
->stats
.icmpInStats
.dwEchos
, pIcmpStats
->stats
.icmpOutStats
.dwEchos
);
315 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Echo Replies"),
316 pIcmpStats
->stats
.icmpInStats
.dwEchoReps
, pIcmpStats
->stats
.icmpOutStats
.dwEchoReps
);
317 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Timestamps"),
318 pIcmpStats
->stats
.icmpInStats
.dwTimestamps
, pIcmpStats
->stats
.icmpOutStats
.dwTimestamps
);
319 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Timestamp Replies"),
320 pIcmpStats
->stats
.icmpInStats
.dwTimestampReps
, pIcmpStats
->stats
.icmpOutStats
.dwTimestampReps
);
321 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Address Masks"),
322 pIcmpStats
->stats
.icmpInStats
.dwAddrMasks
, pIcmpStats
->stats
.icmpOutStats
.dwAddrMasks
);
323 _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Address Mask Replies"),
324 pIcmpStats
->stats
.icmpInStats
.dwAddrMaskReps
, pIcmpStats
->stats
.icmpOutStats
.dwAddrMaskReps
);
327 DoFormatMessage(dwRetVal
);
329 HeapFree(GetProcessHeap(), 0, pIcmpStats
);
333 VOID
ShowTcpStatistics()
335 PMIB_TCPSTATS pTcpStats
;
338 pTcpStats
= (MIB_TCPSTATS
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_TCPSTATS
));
340 if ((dwRetVal
= GetTcpStatistics(pTcpStats
)) == NO_ERROR
)
342 _tprintf(_T("\nTCP Statistics for IPv4\n\n"));
343 _tprintf(_T(" %-35s = %lu\n"), _T("Active Opens"), pTcpStats
->dwActiveOpens
);
344 _tprintf(_T(" %-35s = %lu\n"), _T("Passive Opens"), pTcpStats
->dwPassiveOpens
);
345 _tprintf(_T(" %-35s = %lu\n"), _T("Failed Connection Attempts"), pTcpStats
->dwAttemptFails
);
346 _tprintf(_T(" %-35s = %lu\n"), _T("Reset Connections"), pTcpStats
->dwEstabResets
);
347 _tprintf(_T(" %-35s = %lu\n"), _T("Current Connections"), pTcpStats
->dwCurrEstab
);
348 _tprintf(_T(" %-35s = %lu\n"), _T("Segments Recieved"), pTcpStats
->dwInSegs
);
349 _tprintf(_T(" %-35s = %lu\n"), _T("Segments Sent"), pTcpStats
->dwOutSegs
);
350 _tprintf(_T(" %-35s = %lu\n"), _T("Segments Retransmitted"), pTcpStats
->dwRetransSegs
);
353 DoFormatMessage(dwRetVal
);
355 HeapFree(GetProcessHeap(), 0, pTcpStats
);
358 VOID
ShowUdpStatistics()
360 PMIB_UDPSTATS pUdpStats
;
363 pUdpStats
= (MIB_UDPSTATS
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_UDPSTATS
));
365 if ((dwRetVal
= GetUdpStatistics(pUdpStats
)) == NO_ERROR
)
367 _tprintf(_T("\nUDP Statistics for IPv4\n\n"));
368 _tprintf(_T(" %-21s = %lu\n"), _T("Datagrams Recieved"), pUdpStats
->dwInDatagrams
);
369 _tprintf(_T(" %-21s = %lu\n"), _T("No Ports"), pUdpStats
->dwNoPorts
);
370 _tprintf(_T(" %-21s = %lu\n"), _T("Recieve Errors"), pUdpStats
->dwInErrors
);
371 _tprintf(_T(" %-21s = %lu\n"), _T("Datagrams Sent"), pUdpStats
->dwOutDatagrams
);
374 DoFormatMessage(dwRetVal
);
376 HeapFree(GetProcessHeap(), 0, pUdpStats
);
379 VOID
ShowEthernetStatistics()
381 PMIB_IFTABLE pIfTable
;
385 pIfTable
= (MIB_IFTABLE
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE
));
387 if (GetIfTable(pIfTable
, &dwSize
, 0) == ERROR_INSUFFICIENT_BUFFER
)
389 HeapFree(GetProcessHeap(), 0, pIfTable
);
390 pIfTable
= (MIB_IFTABLE
*) HeapAlloc(GetProcessHeap(), 0, dwSize
);
392 if ((dwRetVal
= GetIfTable(pIfTable
, &dwSize
, 0)) == NO_ERROR
)
394 _tprintf(_T("Interface Statistics\n\n"));
395 _tprintf(_T(" Received Sent\n\n"));
396 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Bytes"),
397 pIfTable
->table
[0].dwInOctets
, pIfTable
->table
[0].dwOutOctets
);
398 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Unicast packets"),
399 pIfTable
->table
[0].dwInUcastPkts
, pIfTable
->table
[0].dwOutUcastPkts
);
400 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Non-unicast packets"),
401 pIfTable
->table
[0].dwInNUcastPkts
, pIfTable
->table
[0].dwOutNUcastPkts
);
402 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Discards"),
403 pIfTable
->table
[0].dwInDiscards
, pIfTable
->table
[0].dwOutDiscards
);
404 _tprintf(_T("%-20s %14lu %15lu\n"), _T("Errors"),
405 pIfTable
->table
[0].dwInErrors
, pIfTable
->table
[0].dwOutErrors
);
406 _tprintf(_T("%-20s %14lu\n"), _T("Unknown Protocols"),
407 pIfTable
->table
[0].dwInUnknownProtos
);
410 DoFormatMessage(dwRetVal
);
412 HeapFree(GetProcessHeap(), 0, pIfTable
);
417 PMIB_TCPTABLE tcpTable
;
420 CHAR HostIp
[HOSTNAMELEN
], HostPort
[PORTNAMELEN
];
421 CHAR RemoteIp
[HOSTNAMELEN
], RemotePort
[PORTNAMELEN
];
422 CHAR Host
[ADDRESSLEN
];
423 CHAR Remote
[ADDRESSLEN
];
425 /* Get the table of TCP endpoints */
426 dwSize
= sizeof (MIB_TCPTABLE
);
427 /* Should also work when we get new connections between 2 GetTcpTable()
431 tcpTable
= (PMIB_TCPTABLE
) HeapAlloc(GetProcessHeap(), 0, dwSize
);
432 error
= GetTcpTable(tcpTable
, &dwSize
, TRUE
);
433 if ( error
!= NO_ERROR
)
434 HeapFree(GetProcessHeap(), 0, tcpTable
);
436 while ( error
== ERROR_INSUFFICIENT_BUFFER
);
438 if (error
!= NO_ERROR
)
440 printf("Failed to snapshot TCP endpoints.\n");
441 DoFormatMessage(error
);
445 /* Dump the TCP table */
446 for (i
= 0; i
< tcpTable
->dwNumEntries
; i
++)
448 /* If we aren't showing all connections, only display established, close wait
449 * and time wait. This is the default output for netstat */
450 if (bDoShowAllCons
|| (tcpTable
->table
[i
].dwState
== MIB_TCP_STATE_ESTAB
)
451 || (tcpTable
->table
[i
].dwState
== MIB_TCP_STATE_CLOSE_WAIT
)
452 || (tcpTable
->table
[i
].dwState
== MIB_TCP_STATE_TIME_WAIT
))
454 /* I've split this up so it's easier to follow */
455 GetIpHostName(TRUE
, tcpTable
->table
[i
].dwLocalAddr
, HostIp
, HOSTNAMELEN
);
456 GetPortName(tcpTable
->table
[i
].dwLocalPort
, "tcp", HostPort
, PORTNAMELEN
);
457 GetIpHostName(FALSE
, tcpTable
->table
[i
].dwRemoteAddr
, RemoteIp
, HOSTNAMELEN
);
458 GetPortName(tcpTable
->table
[i
].dwRemotePort
, "tcp", RemotePort
, PORTNAMELEN
);
460 sprintf(Host
, "%s:%s", HostIp
, HostPort
);
461 sprintf(Remote
, "%s:%s", RemoteIp
, RemotePort
);
463 _tprintf(_T(" %-6s %-22s %-22s %s\n"), _T("TCP"),
464 Host
, Remote
, TcpState
[tcpTable
->table
[i
].dwState
]);
467 HeapFree(GetProcessHeap(), 0, tcpTable
);
473 PMIB_UDPTABLE udpTable
;
476 CHAR HostIp
[HOSTNAMELEN
], HostPort
[PORTNAMELEN
];
477 CHAR Host
[ADDRESSLEN
];
479 /* Get the table of UDP endpoints */
481 error
= GetUdpTable(NULL
, &dwSize
, TRUE
);
482 if (error
!= ERROR_INSUFFICIENT_BUFFER
)
484 printf("Failed to snapshot UDP endpoints.\n");
485 DoFormatMessage(error
);
488 udpTable
= (PMIB_UDPTABLE
) HeapAlloc(GetProcessHeap(), 0, dwSize
);
489 error
= GetUdpTable(udpTable
, &dwSize
, TRUE
);
492 printf("Failed to snapshot UDP endpoints table.\n");
493 DoFormatMessage(error
);
494 HeapFree(GetProcessHeap(), 0, udpTable
);
498 /* Dump the UDP table */
499 for (i
= 0; i
< udpTable
->dwNumEntries
; i
++)
502 /* I've split this up so it's easier to follow */
503 GetIpHostName(TRUE
, udpTable
->table
[i
].dwLocalAddr
, HostIp
, HOSTNAMELEN
);
504 GetPortName(udpTable
->table
[i
].dwLocalPort
, "tcp", HostPort
, PORTNAMELEN
);
506 sprintf(Host
, "%s:%s", HostIp
, HostPort
);
508 _tprintf(_T(" %-6s %-22s %-22s\n"), _T("UDP"), Host
, _T("*:*"));
511 HeapFree(GetProcessHeap(), 0, udpTable
);
516 * Translate port numbers into their text equivalent if there is one
519 GetPortName(UINT Port
, PCSTR Proto
, CHAR Name
[], INT NameLen
)
521 struct servent
*pSrvent
;
525 sprintf(Name
, "%d", htons((WORD
)Port
));
528 /* Try to translate to a name */
529 if ((pSrvent
= getservbyport(Port
, Proto
)))
530 strcpy(Name
, pSrvent
->s_name
);
532 sprintf(Name
, "%d", htons((WORD
)Port
));
538 * convert addresses into dotted decimal or hostname
541 GetIpHostName(BOOL Local
, UINT IpAddr
, CHAR Name
[], int NameLen
)
543 // struct hostent *phostent;
546 /* display dotted decimal */
547 nIpAddr
= htonl(IpAddr
);
548 if (bDoShowNumbers
) {
549 sprintf(Name
, "%d.%d.%d.%d",
550 (nIpAddr
>> 24) & 0xFF,
551 (nIpAddr
>> 16) & 0xFF,
552 (nIpAddr
>> 8) & 0xFF,
559 /* try to resolve the name */
562 sprintf(Name
, "%d.%d.%d.%d",
563 (nIpAddr
>> 24) & 0xFF,
564 (nIpAddr
>> 16) & 0xFF,
565 (nIpAddr
>> 8) & 0xFF,
568 if (gethostname(Name
, NameLen
) != 0)
569 DoFormatMessage(WSAGetLastError());
571 } else if (IpAddr
== 0x0100007f) {
573 if (gethostname(Name
, NameLen
) != 0)
574 DoFormatMessage(WSAGetLastError());
576 _tcsncpy(Name
, _T("localhost"), 10);
578 // } else if (phostent = gethostbyaddr((char*)&ipaddr, sizeof(nipaddr), PF_INET)) {
579 // strcpy(name, phostent->h_name);
581 sprintf(Name
, "%d.%d.%d.%d",
582 ((nIpAddr
>> 24) & 0x000000FF),
583 ((nIpAddr
>> 16) & 0x000000FF),
584 ((nIpAddr
>> 8) & 0x000000FF),
585 ((nIpAddr
) & 0x000000FF));
592 _tprintf(_T("\nDisplays current TCP/IP protocol statistics and network connections.\n\n"
593 "NETSTAT [-a] [-e] [-n] [-s] [-p proto] [-r] [interval]\n\n"
594 " -a Displays all connections and listening ports.\n"
595 " -e Displays Ethernet statistics. May be combined with -s\n"
597 " -n Displays address and port numbers in numeric form.\n"
598 " -p proto Shows connections for protocol 'proto' TCP or UDP.\n"
599 " If used with the -s option to display\n"
600 " per-protocol statistics, 'proto' may be TCP, UDP, or IP.\n"
601 " -r Displays the current routing table.\n"
602 " -s Displays per-protocol statistics. By default, Statistics are\n"
603 " shown for IP, ICMP, TCP and UDP;\n"
604 " the -p option may be used to specify a subset of the default.\n"
605 " interval Redisplays selected statistics every 'interval' seconds.\n"
606 " Press CTRL+C to stop redisplaying. By default netstat will\n"
607 " print the current information only once.\n"));
614 * Parse command line parameters and set any options
615 * Run display output, looping over set intervals if a number is given
618 int main(int argc
, char *argv
[])
622 if (WSAStartup(MAKEWORD(2, 2), &wsaData
) != 0)
624 _tprintf(_T("WSAStartup() failed : %d\n"), WSAGetLastError());
628 if (ParseCmdline(argc
, argv
))
637 Sleep(Interval
*1000);