ReactOS  0.4.12-dev-43-g63b00d8
netstat.c File Reference
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winsock2.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <iphlpapi.h>
#include "netstat.h"
Include dependency graph for netstat.c:

Go to the source code of this file.

Macros

#define WIN32_NO_STATUS
 
#define _INC_WINDOWS
 

Enumerations

enum  ProtoType { IP, TCP, UDP, ICMP }
 

Functions

DWORD DoFormatMessage (DWORD ErrorCode)
 
BOOL ParseCmdline (int argc, char *argv[])
 
VOID DisplayTableHeader ()
 
BOOL DisplayOutput ()
 
VOID ShowIpStatistics ()
 
VOID ShowIcmpStatistics ()
 
VOID ShowTcpStatistics ()
 
VOID ShowUdpStatistics ()
 
VOID ShowEthernetStatistics ()
 
VOID ShowTcpTable ()
 
VOID ShowUdpTable ()
 
PCHAR GetPortName (UINT Port, PCSTR Proto, CHAR Name[], INT NameLen)
 
PCHAR GetIpHostName (BOOL Local, UINT IpAddr, CHAR Name[], int NameLen)
 
VOID Usage ()
 
int main (int argc, char *argv[])
 

Variables

enum ProtoType Protocol
 
DWORD Interval
 
TCHAR TcpState [][32]
 

Macro Definition Documentation

◆ _INC_WINDOWS

#define _INC_WINDOWS

Definition at line 20 of file netstat.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 16 of file netstat.c.

Enumeration Type Documentation

◆ ProtoType

Enumerator
IP 
TCP 
UDP 
ICMP 

Definition at line 29 of file netstat.c.

29 {IP, TCP, UDP, ICMP} Protocol;
Definition: netstat.c:29
Definition: netstat.c:29
Definition: netstat.c:29
enum ProtoType Protocol
Definition: netstat.c:29

Function Documentation

◆ DisplayOutput()

BOOL DisplayOutput ( )

Definition at line 183 of file netstat.c.

Referenced by main().

184 {
185  if (bNoOptions)
186  {
188  ShowTcpTable();
189  return EXIT_SUCCESS;
190  }
191 
192  if (bDoShowRouteTable)
193  {
194  /* mingw doesn't have lib for _tsystem */
195  if (system("route print") == -1)
196  {
197  _tprintf(_T("cannot find 'route.exe'\n"));
198  return EXIT_FAILURE;
199  }
200  return EXIT_SUCCESS;
201  }
202 
203  if (bDoShowEthStats)
204  {
206  return EXIT_SUCCESS;
207  }
208 
209  if (bDoShowProtoCons)
210  {
211  switch (Protocol)
212  {
213  case IP :
214  if (bDoShowProtoStats)
215  {
217  return EXIT_SUCCESS;
218  }
219  break;
220  case ICMP :
221  if (bDoShowProtoStats)
222  {
224  return EXIT_SUCCESS;
225  }
226  break;
227  case TCP :
228  if (bDoShowProtoStats)
230  _tprintf(_T("\nActive Connections\n"));
232  ShowTcpTable();
233  break;
234  case UDP :
235  if (bDoShowProtoStats)
237  _tprintf(_T("\nActive Connections\n"));
239  ShowUdpTable();
240  break;
241  default :
242  break;
243  }
244  }
245  else if (bDoShowProtoStats)
246  {
251  return EXIT_SUCCESS;
252  }
253  else
254  {
255  _tprintf(_T("\nActive Connections\n"));
257  ShowTcpTable();
258  if (bDoShowAllCons)
259  ShowUdpTable();
260  }
261  return EXIT_SUCCESS;
262 }
BOOL bDoShowProtoCons
Definition: netstat.h:13
#define _tprintf
Definition: tchar.h:506
VOID ShowUdpStatistics()
Definition: netstat.c:368
Definition: netstat.c:29
VOID ShowTcpTable()
Definition: netstat.c:425
VOID DisplayTableHeader()
Definition: netstat.c:170
VOID ShowTcpStatistics()
Definition: netstat.c:343
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
VOID ShowEthernetStatistics()
Definition: netstat.c:389
int __cdecl system(_In_opt_z_ const char *_Command)
BOOL bDoShowEthStats
Definition: netstat.h:11
#define _T(x)
Definition: vfdio.h:22
VOID ShowUdpTable()
Definition: netstat.c:497
Definition: netstat.c:29
#define EXIT_FAILURE
Definition: jerror.c:33
Definition: netstat.c:29
BOOL bDoShowProtoStats
Definition: netstat.h:15
VOID ShowIcmpStatistics()
Definition: netstat.c:298
BOOL bDoShowAllCons
Definition: netstat.h:9
VOID ShowIpStatistics()
Definition: netstat.c:264
BOOL bNoOptions
Definition: netstat.h:8
Definition: netstat.c:29
BOOL bDoShowRouteTable
Definition: netstat.h:14

◆ DisplayTableHeader()

VOID DisplayTableHeader ( )

Definition at line 170 of file netstat.c.

Referenced by DisplayOutput().

171 {
172  _tprintf(_T("\n Proto Local Address Foreign Address State"));
173  if (bDoShowProcessId)
174  _tprintf(_T(" Process\n"));
175  else
176  _tprintf(_T("\n"));
177 }
#define _tprintf
Definition: tchar.h:506
BOOL bDoShowProcessId
Definition: netstat.h:16
#define _T(x)
Definition: vfdio.h:22

◆ DoFormatMessage()

DWORD DoFormatMessage ( DWORD  ErrorCode)

Definition at line 52 of file netstat.c.

Referenced by GetIpHostName(), ShowEthernetStatistics(), ShowIcmpStatistics(), ShowIpStatistics(), ShowTcpStatistics(), ShowTcpTable(), ShowUdpStatistics(), and ShowUdpTable().

53 {
54  LPVOID lpMsgBuf;
55  DWORD RetVal;
56 
57  if ((RetVal = FormatMessage(
61  NULL,
62  ErrorCode,
63  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
64  (LPTSTR) &lpMsgBuf,
65  0,
66  NULL )))
67  {
68  _tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
69 
70  LocalFree(lpMsgBuf);
71  /* return number of TCHAR's stored in output buffer
72  * excluding '\0' - as FormatMessage does*/
73  return RetVal;
74  }
75  else
76  return 0;
77 }
#define _tprintf
Definition: tchar.h:506
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4418
#define LANG_NEUTRAL
Definition: nls.h:22
#define SUBLANG_DEFAULT
Definition: nls.h:168
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
smooth NULL
Definition: ftsmooth.c:416
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
#define _T(x)
Definition: vfdio.h:22
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FormatMessage
Definition: winbase.h:3609
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:401
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define MAKELANGID(p, s)
Definition: nls.h:15

◆ GetIpHostName()

PCHAR GetIpHostName ( BOOL  Local,
UINT  IpAddr,
CHAR  Name[],
int  NameLen 
)

Definition at line 575 of file netstat.c.

Referenced by ShowTcpTable(), and ShowUdpTable().

576 {
577 // struct hostent *phostent;
578  UINT nIpAddr;
579 
580  /* display dotted decimal */
581  nIpAddr = htonl(IpAddr);
582  if (bDoShowNumbers) {
583  sprintf(Name, "%d.%d.%d.%d",
584  (nIpAddr >> 24) & 0xFF,
585  (nIpAddr >> 16) & 0xFF,
586  (nIpAddr >> 8) & 0xFF,
587  (nIpAddr) & 0xFF);
588  return Name;
589  }
590 
591  Name[0] = _T('\0');
592 
593  /* try to resolve the name */
594  if (!IpAddr) {
595  if (!Local) {
596  sprintf(Name, "%d.%d.%d.%d",
597  (nIpAddr >> 24) & 0xFF,
598  (nIpAddr >> 16) & 0xFF,
599  (nIpAddr >> 8) & 0xFF,
600  (nIpAddr) & 0xFF);
601  } else {
602  if (gethostname(Name, NameLen) != 0)
604  }
605  } else if (IpAddr == 0x0100007f) {
606  if (Local) {
607  if (gethostname(Name, NameLen) != 0)
609  } else {
610  _tcsncpy(Name, _T("localhost"), 10);
611  }
612 // } else if (phostent = gethostbyaddr((char*)&ipaddr, sizeof(nipaddr), PF_INET)) {
613 // strcpy(name, phostent->h_name);
614  } else {
615  sprintf(Name, "%d.%d.%d.%d",
616  ((nIpAddr >> 24) & 0x000000FF),
617  ((nIpAddr >> 16) & 0x000000FF),
618  ((nIpAddr >> 8) & 0x000000FF),
619  ((nIpAddr) & 0x000000FF));
620  }
621  return Name;
622 }
#define htonl(x)
Definition: module.h:212
_TCHAR * _tcsncpy(_TCHAR *dst, const _TCHAR *src, size_t n)
Definition: tcsncpy.h:9
BOOL bDoShowNumbers
Definition: netstat.h:12
#define sprintf(buf, format,...)
Definition: sprintf.c:55
INT WSAAPI gethostname(OUT char FAR *name, IN INT namelen)
Definition: getxbyxx.c:397
struct NameRec_ * Name
Definition: cdprocs.h:464
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
#define _T(x)
Definition: vfdio.h:22
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
unsigned int UINT
Definition: ndis.h:50

◆ GetPortName()

PCHAR GetPortName ( UINT  Port,
PCSTR  Proto,
CHAR  Name[],
INT  NameLen 
)

Definition at line 554 of file netstat.c.

Referenced by ShowTcpTable(), and ShowUdpTable().

555 {
556  struct servent *pServent;
557 
558  if (bDoShowNumbers)
559  {
560  sprintf(Name, "%d", htons((WORD)Port));
561  return Name;
562  }
563  /* Try to translate to a name */
564  if ((pServent = getservbyport(Port, Proto)))
565  strcpy(Name, pServent->s_name );
566  else
567  sprintf(Name, "%d", htons((WORD)Port));
568  return Name;
569 }
CPPORT Port[4]
Definition: headless.c:34
BOOL bDoShowNumbers
Definition: netstat.h:12
#define sprintf(buf, format,...)
Definition: sprintf.c:55
struct NameRec_ * Name
Definition: cdprocs.h:464
char * s_name
Definition: winsock.h:159
unsigned short WORD
Definition: ntddk_ex.h:93
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define htons(x)
Definition: module.h:213
PSERVENT WSAAPI getservbyport(IN int port, IN const char FAR *proto)
Definition: getxbyxx.c:431

◆ main()

int main ( int argc  ,
char argv[] 
)

Definition at line 651 of file netstat.c.

652 {
653  WSADATA wsaData;
654 
655  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
656  {
657  _tprintf(_T("WSAStartup() failed : %d\n"), WSAGetLastError());
658  return -1;
659  }
660 
661  if (ParseCmdline(argc, argv))
662  return -1;
663 
664  if (bLoopOutput)
665  {
666  while (1)
667  {
668  if (DisplayOutput())
669  return -1;
670  Sleep(Interval*1000);
671  }
672  }
673 
674  if (DisplayOutput())
675  return -1;
676  else
677  return 0;
678 }
static int argc
Definition: ServiceArgs.c:12
#define _tprintf
Definition: tchar.h:506
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
#define MAKEWORD(a, b)
Definition: typedefs.h:247
BOOL ParseCmdline(int argc, char *argv[])
Definition: netstat.c:84
static char ** argv
Definition: ServiceArgs.c:11
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
DWORD Interval
Definition: netstat.c:30
#define _T(x)
Definition: vfdio.h:22
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
BOOL DisplayOutput()
Definition: netstat.c:183
BOOL bLoopOutput
Definition: netstat.h:18

◆ ParseCmdline()

BOOL ParseCmdline ( int  argc,
char argv[] 
)

Definition at line 84 of file netstat.c.

Referenced by main().

85 {
86  LPSTR Proto;
87  CHAR c;
88  INT i;
89 
90  if ((argc == 1) || (_istdigit(*argv[1])))
91  bNoOptions = TRUE;
92 
93  /* Parse command line for options we have been given. */
94  for (i = 1; i < argc; i++)
95  {
96  if ((argc > 1) && (argv[i][0] == '-' || argv[i][0] == '/'))
97  {
98  while ((c = *++argv[i]) != '\0')
99  {
100  switch (tolower(c))
101  {
102  case 'a' :
104  break;
105  case 'b' :
107  break;
108  case 'e' :
110  break;
111  case 'n' :
113  break;
114  case 'p' :
116  Proto = argv[i+1];
117  if (!_stricmp("IP", Proto))
118  Protocol = IP;
119  else if (!_stricmp("ICMP", Proto))
120  Protocol = ICMP;
121  else if (!_stricmp("TCP", Proto))
122  Protocol = TCP;
123  else if (!_stricmp("UDP", Proto))
124  Protocol = UDP;
125  else
126  {
127  Usage();
128  return EXIT_FAILURE;
129  }
130  break;
131  case 'r' :
133  break;
134  case 's' :
136  break;
137  case 'o' :
139  break;
140  case 'v' :
141  _tprintf(_T("got v\n"));
143  break;
144  default :
145  Usage();
146  return EXIT_FAILURE;
147  }
148  }
149  }
150  else if (_istdigit(*argv[i]))
151  {
152  if (_stscanf(argv[i], "%lu", &Interval) != EOF)
153  bLoopOutput = TRUE;
154  else
155  return EXIT_FAILURE;
156  }
157 // else
158 // {
159 // Usage();
160 // EXIT_FAILURE;
161 // }
162  }
163 
164  return EXIT_SUCCESS;
165 }
BOOL bDoShowProtoCons
Definition: netstat.h:13
static int argc
Definition: ServiceArgs.c:12
#define _istdigit
Definition: tchar.h:1494
#define _tprintf
Definition: tchar.h:506
BOOL bDoShowProcessId
Definition: netstat.h:16
#define TRUE
Definition: types.h:120
Definition: netstat.c:29
char CHAR
Definition: xmlstorage.h:175
#define _stscanf
Definition: tchar.h:557
BOOL bDoShowNumbers
Definition: netstat.h:12
char * LPSTR
Definition: xmlstorage.h:182
#define _stricmp
Definition: cat.c:22
int32_t INT
Definition: typedefs.h:56
static char ** argv
Definition: ServiceArgs.c:11
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
GLenum GLclampf GLint i
Definition: glfuncs.h:14
BOOL bDoDispSeqComp
Definition: netstat.h:17
BOOL bDoShowEthStats
Definition: netstat.h:11
DWORD Interval
Definition: netstat.c:30
#define _T(x)
Definition: vfdio.h:22
Definition: netstat.c:29
#define EXIT_FAILURE
Definition: jerror.c:33
Definition: netstat.c:29
BOOL bDoShowProtoStats
Definition: netstat.h:15
BOOL bDoShowAllCons
Definition: netstat.h:9
BOOL bNoOptions
Definition: netstat.h:8
Definition: netstat.c:29
#define EOF
Definition: stdio.h:24
#define c
Definition: ke_i.h:80
BOOL bLoopOutput
Definition: netstat.h:18
int tolower(int c)
Definition: utclib.c:902
BOOL bDoShowRouteTable
Definition: netstat.h:14
VOID Usage()
Definition: netstat.c:624
BOOL bDoShowProcName
Definition: netstat.h:10

◆ ShowEthernetStatistics()

VOID ShowEthernetStatistics ( )

Definition at line 389 of file netstat.c.

Referenced by DisplayOutput().

390 {
391  PMIB_IFTABLE pIfTable;
392  DWORD dwSize = 0;
393  DWORD dwRetVal = 0;
394 
395  pIfTable = (MIB_IFTABLE*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IFTABLE));
396 
397  if (GetIfTable(pIfTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
398  {
399  HeapFree(GetProcessHeap(), 0, pIfTable);
400  pIfTable = (MIB_IFTABLE*) HeapAlloc(GetProcessHeap(), 0, dwSize);
401 
402  if ((dwRetVal = GetIfTable(pIfTable, &dwSize, 0)) == NO_ERROR)
403  {
404  _tprintf(_T("Interface Statistics\n\n"));
405  _tprintf(_T(" Received Sent\n\n"));
406  _tprintf(_T("%-20s %14lu %15lu\n"), _T("Bytes"),
407  pIfTable->table[0].dwInOctets, pIfTable->table[0].dwOutOctets);
408  _tprintf(_T("%-20s %14lu %15lu\n"), _T("Unicast packets"),
409  pIfTable->table[0].dwInUcastPkts, pIfTable->table[0].dwOutUcastPkts);
410  _tprintf(_T("%-20s %14lu %15lu\n"), _T("Non-unicast packets"),
411  pIfTable->table[0].dwInNUcastPkts, pIfTable->table[0].dwOutNUcastPkts);
412  _tprintf(_T("%-20s %14lu %15lu\n"), _T("Discards"),
413  pIfTable->table[0].dwInDiscards, pIfTable->table[0].dwOutDiscards);
414  _tprintf(_T("%-20s %14lu %15lu\n"), _T("Errors"),
415  pIfTable->table[0].dwInErrors, pIfTable->table[0].dwOutErrors);
416  _tprintf(_T("%-20s %14lu\n"), _T("Unknown Protocols"),
417  pIfTable->table[0].dwInUnknownProtos);
418  }
419  else
420  DoFormatMessage(dwRetVal);
421  }
422  HeapFree(GetProcessHeap(), 0, pIfTable);
423 }
MIB_IFROW table[1]
Definition: ifmib.h:66
DWORD dwInOctets
Definition: ifmib.h:47
#define _tprintf
Definition: tchar.h:506
DWORD dwInUcastPkts
Definition: ifmib.h:48
DWORD dwInNUcastPkts
Definition: ifmib.h:49
DWORD dwInDiscards
Definition: ifmib.h:50
#define NO_ERROR
Definition: dderror.h:5
DWORD dwInUnknownProtos
Definition: ifmib.h:52
DWORD WINAPI GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
DWORD dwOutDiscards
Definition: ifmib.h:56
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
struct _MIB_IFTABLE MIB_IFTABLE
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwOutUcastPkts
Definition: ifmib.h:54
DWORD dwInErrors
Definition: ifmib.h:51
DWORD dwOutOctets
Definition: ifmib.h:53
DWORD dwOutErrors
Definition: ifmib.h:57
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
DWORD dwOutNUcastPkts
Definition: ifmib.h:55
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

◆ ShowIcmpStatistics()

VOID ShowIcmpStatistics ( )

Definition at line 298 of file netstat.c.

Referenced by DisplayOutput().

299 {
300  PMIB_ICMP pIcmpStats;
301  DWORD dwRetVal;
302 
303  pIcmpStats = (MIB_ICMP*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_ICMP));
304 
305  if ((dwRetVal = GetIcmpStatistics(pIcmpStats)) == NO_ERROR)
306  {
307  _tprintf(_T("\nICMPv4 Statistics\n\n"));
308  _tprintf(_T(" Received Sent\n"));
309  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Messages"),
310  pIcmpStats->stats.icmpInStats.dwMsgs, pIcmpStats->stats.icmpOutStats.dwMsgs);
311  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Errors"),
312  pIcmpStats->stats.icmpInStats.dwErrors, pIcmpStats->stats.icmpOutStats.dwErrors);
313  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Destination Unreachable"),
315  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Time Exceeded"),
316  pIcmpStats->stats.icmpInStats.dwTimeExcds, pIcmpStats->stats.icmpOutStats.dwTimeExcds);
317  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Parameter Problems"),
318  pIcmpStats->stats.icmpInStats.dwParmProbs, pIcmpStats->stats.icmpOutStats.dwParmProbs);
319  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Source Quenches"),
320  pIcmpStats->stats.icmpInStats.dwSrcQuenchs, pIcmpStats->stats.icmpOutStats.dwSrcQuenchs);
321  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Redirects"),
322  pIcmpStats->stats.icmpInStats.dwRedirects, pIcmpStats->stats.icmpOutStats.dwRedirects);
323  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Echos"),
324  pIcmpStats->stats.icmpInStats.dwEchos, pIcmpStats->stats.icmpOutStats.dwEchos);
325  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Echo Replies"),
326  pIcmpStats->stats.icmpInStats.dwEchoReps, pIcmpStats->stats.icmpOutStats.dwEchoReps);
327  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Timestamps"),
328  pIcmpStats->stats.icmpInStats.dwTimestamps, pIcmpStats->stats.icmpOutStats.dwTimestamps);
329  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Timestamp Replies"),
331  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Address Masks"),
332  pIcmpStats->stats.icmpInStats.dwAddrMasks, pIcmpStats->stats.icmpOutStats.dwAddrMasks);
333  _tprintf(_T(" %-25s %-11lu %lu\n"), _T("Address Mask Replies"),
335  }
336  else
337  DoFormatMessage(dwRetVal);
338 
339  HeapFree(GetProcessHeap(), 0, pIcmpStats);
340 
341 }
DWORD dwEchos
Definition: ipmib.h:183
#define _tprintf
Definition: tchar.h:506
MIBICMPSTATS icmpInStats
Definition: ipmib.h:193
DWORD dwTimestamps
Definition: ipmib.h:185
DWORD dwTimeExcds
Definition: ipmib.h:179
DWORD dwEchoReps
Definition: ipmib.h:184
#define NO_ERROR
Definition: dderror.h:5
MIBICMPINFO stats
Definition: ipmib.h:199
DWORD dwErrors
Definition: ipmib.h:177
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
DWORD dwTimestampReps
Definition: ipmib.h:186
DWORD dwParmProbs
Definition: ipmib.h:180
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
DWORD WINAPI GetIcmpStatistics(PMIB_ICMP pStats)
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwRedirects
Definition: ipmib.h:182
DWORD dwAddrMasks
Definition: ipmib.h:187
MIBICMPSTATS icmpOutStats
Definition: ipmib.h:194
DWORD dwSrcQuenchs
Definition: ipmib.h:181
struct _MIB_ICMP MIB_ICMP
DWORD dwDestUnreachs
Definition: ipmib.h:178
DWORD dwAddrMaskReps
Definition: ipmib.h:188
#define HeapFree(x, y, z)
Definition: compat.h:394
DWORD dwMsgs
Definition: ipmib.h:176

◆ ShowIpStatistics()

VOID ShowIpStatistics ( )

Definition at line 264 of file netstat.c.

Referenced by DisplayOutput().

265 {
266  PMIB_IPSTATS pIpStats;
267  DWORD dwRetVal;
268 
269  pIpStats = (MIB_IPSTATS*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPSTATS));
270 
271  if ((dwRetVal = GetIpStatistics(pIpStats)) == NO_ERROR)
272  {
273  _tprintf(_T("\nIPv4 Statistics\n\n"));
274  _tprintf(_T(" %-34s = %lu\n"), _T("Packets Received"), pIpStats->dwInReceives);
275  _tprintf(_T(" %-34s = %lu\n"), _T("Received Header Errors"), pIpStats->dwInHdrErrors);
276  _tprintf(_T(" %-34s = %lu\n"), _T("Received Address Errors"), pIpStats->dwInAddrErrors);
277  _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams Forwarded"), pIpStats->dwForwDatagrams);
278  _tprintf(_T(" %-34s = %lu\n"), _T("Unknown Protocols Received"), pIpStats->dwInUnknownProtos);
279  _tprintf(_T(" %-34s = %lu\n"), _T("Received Packets Discarded"), pIpStats->dwInDiscards);
280  _tprintf(_T(" %-34s = %lu\n"), _T("Received Packets Delivered"), pIpStats->dwInDelivers);
281  _tprintf(_T(" %-34s = %lu\n"), _T("Output Requests"), pIpStats->dwOutRequests);
282  _tprintf(_T(" %-34s = %lu\n"), _T("Routing Discards"), pIpStats->dwRoutingDiscards);
283  _tprintf(_T(" %-34s = %lu\n"), _T("Discarded Output Packets"), pIpStats->dwOutDiscards);
284  _tprintf(_T(" %-34s = %lu\n"), _T("Output Packets No Route"), pIpStats->dwOutNoRoutes);
285  _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Required"), pIpStats->dwReasmReqds);
286  _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Succesful"), pIpStats->dwReasmOks);
287  _tprintf(_T(" %-34s = %lu\n"), _T("Reassembly Failures"), pIpStats->dwReasmFails);
288  // _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams successfully fragmented"), NULL); /* FIXME: what is this one? */
289  _tprintf(_T(" %-34s = %lu\n"), _T("Datagrams Failing Fragmentation"), pIpStats->dwFragFails);
290  _tprintf(_T(" %-34s = %lu\n"), _T("Fragments Created"), pIpStats->dwFragCreates);
291  }
292  else
293  DoFormatMessage(dwRetVal);
294 
295  HeapFree(GetProcessHeap(), 0, pIpStats);
296 }
struct _MIB_IPSTATS MIB_IPSTATS
#define _tprintf
Definition: tchar.h:506
DWORD dwInReceives
Definition: ipmib.h:148
DWORD dwReasmReqds
Definition: ipmib.h:160
DWORD dwInDiscards
Definition: ipmib.h:153
DWORD dwInHdrErrors
Definition: ipmib.h:149
DWORD dwInDelivers
Definition: ipmib.h:154
#define NO_ERROR
Definition: dderror.h:5
DWORD dwForwDatagrams
Definition: ipmib.h:151
DWORD WINAPI GetIpStatistics(PMIB_IPSTATS pStats)
DWORD dwInUnknownProtos
Definition: ipmib.h:152
DWORD dwOutDiscards
Definition: ipmib.h:157
DWORD dwFragCreates
Definition: ipmib.h:165
DWORD dwRoutingDiscards
Definition: ipmib.h:156
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
DWORD dwReasmOks
Definition: ipmib.h:161
DWORD dwOutRequests
Definition: ipmib.h:155
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
DWORD dwReasmFails
Definition: ipmib.h:162
DWORD dwOutNoRoutes
Definition: ipmib.h:158
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwInAddrErrors
Definition: ipmib.h:150
DWORD dwFragFails
Definition: ipmib.h:164
#define HeapFree(x, y, z)
Definition: compat.h:394

◆ ShowTcpStatistics()

VOID ShowTcpStatistics ( )

Definition at line 343 of file netstat.c.

Referenced by DisplayOutput().

344 {
345  PMIB_TCPSTATS pTcpStats;
346  DWORD dwRetVal;
347 
348  pTcpStats = (MIB_TCPSTATS*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_TCPSTATS));
349 
350  if ((dwRetVal = GetTcpStatistics(pTcpStats)) == NO_ERROR)
351  {
352  _tprintf(_T("\nTCP Statistics for IPv4\n\n"));
353  _tprintf(_T(" %-35s = %lu\n"), _T("Active Opens"), pTcpStats->dwActiveOpens);
354  _tprintf(_T(" %-35s = %lu\n"), _T("Passive Opens"), pTcpStats->dwPassiveOpens);
355  _tprintf(_T(" %-35s = %lu\n"), _T("Failed Connection Attempts"), pTcpStats->dwAttemptFails);
356  _tprintf(_T(" %-35s = %lu\n"), _T("Reset Connections"), pTcpStats->dwEstabResets);
357  _tprintf(_T(" %-35s = %lu\n"), _T("Current Connections"), pTcpStats->dwCurrEstab);
358  _tprintf(_T(" %-35s = %lu\n"), _T("Segments Received"), pTcpStats->dwInSegs);
359  _tprintf(_T(" %-35s = %lu\n"), _T("Segments Sent"), pTcpStats->dwOutSegs);
360  _tprintf(_T(" %-35s = %lu\n"), _T("Segments Retransmitted"), pTcpStats->dwRetransSegs);
361  }
362  else
363  DoFormatMessage(dwRetVal);
364 
365  HeapFree(GetProcessHeap(), 0, pTcpStats);
366 }
DWORD dwAttemptFails
Definition: tcpmib.h:181
#define _tprintf
Definition: tchar.h:506
struct _MIB_TCPSTATS MIB_TCPSTATS
#define NO_ERROR
Definition: dderror.h:5
DWORD dwInSegs
Definition: tcpmib.h:184
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
DWORD dwOutSegs
Definition: tcpmib.h:185
DWORD dwCurrEstab
Definition: tcpmib.h:183
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
DWORD dwEstabResets
Definition: tcpmib.h:182
DWORD dwRetransSegs
Definition: tcpmib.h:186
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwPassiveOpens
Definition: tcpmib.h:180
#define HeapFree(x, y, z)
Definition: compat.h:394
DWORD dwActiveOpens
Definition: tcpmib.h:179
DWORD WINAPI GetTcpStatistics(PMIB_TCPSTATS pStats)

◆ ShowTcpTable()

VOID ShowTcpTable ( )

Definition at line 425 of file netstat.c.

Referenced by DisplayOutput().

426 {
427  PMIB_TCPTABLE_OWNER_PID tcpTable;
428  DWORD error, dwSize;
429  DWORD i;
430  CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN];
431  CHAR RemoteIp[HOSTNAMELEN], RemotePort[PORTNAMELEN];
432  CHAR Host[ADDRESSLEN];
434  CHAR PID[64];
435 
436  /* Get the table of TCP endpoints */
437  dwSize = sizeof (MIB_TCPTABLE_OWNER_PID);
438  /* Should also work when we get new connections between 2 GetTcpTable()
439  * calls: */
440  do
441  {
443  error = GetExtendedTcpTable(tcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
444  if ( error != NO_ERROR )
445  HeapFree(GetProcessHeap(), 0, tcpTable);
446  }
447  while ( error == ERROR_INSUFFICIENT_BUFFER );
448 
449  if (error != NO_ERROR)
450  {
451  printf("Failed to snapshot TCP endpoints.\n");
452  DoFormatMessage(error);
454  }
455 
456  /* Dump the TCP table */
457  for (i = 0; i < tcpTable->dwNumEntries; i++)
458  {
459  /* If we aren't showing all connections, only display established, close wait
460  * and time wait. This is the default output for netstat */
461  if (bDoShowAllCons || (tcpTable->table[i].dwState == MIB_TCP_STATE_ESTAB)
462  || (tcpTable->table[i].dwState == MIB_TCP_STATE_CLOSE_WAIT)
463  || (tcpTable->table[i].dwState == MIB_TCP_STATE_TIME_WAIT))
464  {
465  /* I've split this up so it's easier to follow */
466  GetIpHostName(TRUE, tcpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN);
467  GetPortName(tcpTable->table[i].dwLocalPort, "tcp", HostPort, PORTNAMELEN);
468  sprintf(Host, "%s:%s", HostIp, HostPort);
469 
470  if (tcpTable->table[i].dwState == MIB_TCP_STATE_LISTEN)
471  {
472  sprintf(Remote, "%s:0", HostIp);
473  }
474  else
475  {
476  GetIpHostName(FALSE, tcpTable->table[i].dwRemoteAddr, RemoteIp, HOSTNAMELEN);
477  GetPortName(tcpTable->table[i].dwRemotePort, "tcp", RemotePort, PORTNAMELEN);
478  sprintf(Remote, "%s:%s", RemoteIp, RemotePort);
479  }
480 
481  if (bDoShowProcessId)
482  {
483  sprintf(PID, "%ld", tcpTable->table[i].dwOwningPid);
484  }
485  else
486  {
487  PID[0] = 0;
488  }
489 
490  _tprintf(_T(" %-6s %-22s %-22s %-11s %s\n"), _T("TCP"),
491  Host, Remote, TcpState[tcpTable->table[i].dwState], PID);
492  }
493  }
494  HeapFree(GetProcessHeap(), 0, tcpTable);
495 }
#define _tprintf
Definition: tchar.h:506
BOOL bDoShowProcessId
Definition: netstat.h:16
#define TRUE
Definition: types.h:120
#define error(str)
Definition: mkdosfs.c:1605
struct _MIB_TCPTABLE_OWNER_PID MIB_TCPTABLE_OWNER_PID
char CHAR
Definition: xmlstorage.h:175
TCHAR TcpState[][32]
Definition: netstat.c:33
#define NO_ERROR
Definition: dderror.h:5
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLenum GLclampf GLint i
Definition: glfuncs.h:14
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
#define PORTNAMELEN
Definition: netstat.h:4
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
PCHAR GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], int NameLen)
Definition: netstat.c:575
unsigned long DWORD
Definition: ntddk_ex.h:95
#define EXIT_FAILURE
Definition: jerror.c:33
BOOL bDoShowAllCons
Definition: netstat.h:9
struct _MIB_TCPTABLE_OWNER_PID * PMIB_TCPTABLE_OWNER_PID
DWORD WINAPI GetExtendedTcpTable(PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder, ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved)
#define AF_INET
Definition: tcpip.h:117
#define ADDRESSLEN
Definition: netstat.h:5
void exit(int exitcode)
Definition: _exit.c:33
PCHAR GetPortName(UINT Port, PCSTR Proto, CHAR Name[], INT NameLen)
Definition: netstat.c:554
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define HOSTNAMELEN
Definition: netstat.h:3
MIB_TCPROW_OWNER_PID table[1]
Definition: tcpmib.h:115
#define printf
Definition: config.h:203
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

◆ ShowUdpStatistics()

VOID ShowUdpStatistics ( )

Definition at line 368 of file netstat.c.

Referenced by DisplayOutput().

369 {
370  PMIB_UDPSTATS pUdpStats;
371  DWORD dwRetVal;
372 
373  pUdpStats = (MIB_UDPSTATS*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_UDPSTATS));
374 
375  if ((dwRetVal = GetUdpStatistics(pUdpStats)) == NO_ERROR)
376  {
377  _tprintf(_T("\nUDP Statistics for IPv4\n\n"));
378  _tprintf(_T(" %-21s = %lu\n"), _T("Datagrams Received"), pUdpStats->dwInDatagrams);
379  _tprintf(_T(" %-21s = %lu\n"), _T("No Ports"), pUdpStats->dwNoPorts);
380  _tprintf(_T(" %-21s = %lu\n"), _T("Receive Errors"), pUdpStats->dwInErrors);
381  _tprintf(_T(" %-21s = %lu\n"), _T("Datagrams Sent"), pUdpStats->dwOutDatagrams);
382  }
383  else
384  DoFormatMessage(dwRetVal);
385 
386  HeapFree(GetProcessHeap(), 0, pUdpStats);
387 }
#define _tprintf
Definition: tchar.h:506
#define NO_ERROR
Definition: dderror.h:5
struct _MIB_UDPSTATS MIB_UDPSTATS
DWORD dwInDatagrams
Definition: udpmib.h:78
DWORD dwOutDatagrams
Definition: udpmib.h:81
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
DWORD dwNoPorts
Definition: udpmib.h:79
DWORD WINAPI GetUdpStatistics(PMIB_UDPSTATS pStats)
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD dwInErrors
Definition: udpmib.h:80
#define HeapFree(x, y, z)
Definition: compat.h:394

◆ ShowUdpTable()

VOID ShowUdpTable ( )

Definition at line 497 of file netstat.c.

Referenced by DisplayOutput().

498 {
500  DWORD error, dwSize;
501  DWORD i;
502  CHAR HostIp[HOSTNAMELEN], HostPort[PORTNAMELEN];
503  CHAR Host[ADDRESSLEN];
504  CHAR PID[64];
505 
506  /* Get the table of UDP endpoints */
507  dwSize = 0;
508  error = GetExtendedUdpTable(NULL, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0);
509  if (error != ERROR_INSUFFICIENT_BUFFER)
510  {
511  printf("Failed to snapshot UDP endpoints.\n");
512  DoFormatMessage(error);
514  }
516  error = GetExtendedUdpTable(udpTable, &dwSize, TRUE, AF_INET, UDP_TABLE_OWNER_PID, 0);
517  if (error)
518  {
519  printf("Failed to snapshot UDP endpoints table.\n");
520  DoFormatMessage(error);
521  HeapFree(GetProcessHeap(), 0, udpTable);
523  }
524 
525  /* Dump the UDP table */
526  for (i = 0; i < udpTable->dwNumEntries; i++)
527  {
528 
529  /* I've split this up so it's easier to follow */
530  GetIpHostName(TRUE, udpTable->table[i].dwLocalAddr, HostIp, HOSTNAMELEN);
531  GetPortName(udpTable->table[i].dwLocalPort, "tcp", HostPort, PORTNAMELEN);
532 
533  sprintf(Host, "%s:%s", HostIp, HostPort);
534 
535  if (bDoShowProcessId)
536  {
537  sprintf(PID, "%ld", udpTable->table[i].dwOwningPid);
538  }
539  else
540  {
541  PID[0] = 0;
542  }
543 
544  _tprintf(_T(" %-6s %-22s %-34s %s\n"), _T("UDP"), Host, _T("*:*"), PID);
545  }
546 
547  HeapFree(GetProcessHeap(), 0, udpTable);
548 }
#define _tprintf
Definition: tchar.h:506
BOOL bDoShowProcessId
Definition: netstat.h:16
#define TRUE
Definition: types.h:120
#define error(str)
Definition: mkdosfs.c:1605
char CHAR
Definition: xmlstorage.h:175
MIB_UDPROW_OWNER_PID table[1]
Definition: udpmib.h:48
struct _MIB_UDPTABLE_OWNER_PID * PMIB_UDPTABLE_OWNER_PID
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLenum GLclampf GLint i
Definition: glfuncs.h:14
smooth NULL
Definition: ftsmooth.c:416
DWORD DoFormatMessage(DWORD ErrorCode)
Definition: netstat.c:52
#define PORTNAMELEN
Definition: netstat.h:4
#define _T(x)
Definition: vfdio.h:22
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
PCHAR GetIpHostName(BOOL Local, UINT IpAddr, CHAR Name[], int NameLen)
Definition: netstat.c:575
unsigned long DWORD
Definition: ntddk_ex.h:95
#define EXIT_FAILURE
Definition: jerror.c:33
static PMIB_UDPTABLE udpTable
Definition: main.c:1188
DWORD WINAPI GetExtendedUdpTable(PVOID pUdpTable, PDWORD pdwSize, BOOL bOrder, ULONG ulAf, UDP_TABLE_CLASS TableClass, ULONG Reserved)
#define AF_INET
Definition: tcpip.h:117
#define ADDRESSLEN
Definition: netstat.h:5
void exit(int exitcode)
Definition: _exit.c:33
PCHAR GetPortName(UINT Port, PCSTR Proto, CHAR Name[], INT NameLen)
Definition: netstat.c:554
#define HeapFree(x, y, z)
Definition: compat.h:394
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define HOSTNAMELEN
Definition: netstat.h:3
#define printf
Definition: config.h:203
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

◆ Usage()

VOID Usage ( void  )

Definition at line 624 of file netstat.c.

Referenced by ParseCmdline().

625 {
626  _tprintf(_T("\nDisplays current TCP/IP protocol statistics and network connections.\n\n"
627  "NETSTAT [-a] [-e] [-n] [-p proto] [-r] [-s] [interval]\n\n"
628  " -a Displays all connections and listening ports.\n"
629  " -e Displays Ethernet statistics. May be combined with -s\n"
630  " option\n"
631  " -n Displays address and port numbers in numeric form.\n"
632  " -p proto Shows connections for protocol 'proto' TCP or UDP.\n"
633  " If used with the -s option to display\n"
634  " per-protocol statistics, 'proto' may be TCP, UDP, or IP.\n"
635  " -r Displays the current routing table.\n"
636  " -s Displays per-protocol statistics. By default, Statistics are\n"
637  " shown for IP, ICMP, TCP and UDP;\n"
638  " the -p option may be used to specify a subset of the default.\n"
639  " -o Displays the process ID for each connection.\n"
640  " interval Redisplays selected statistics every 'interval' seconds.\n"
641  " Press CTRL+C to stop redisplaying. By default netstat will\n"
642  " print the current information only once.\n"));
643 }
#define _tprintf
Definition: tchar.h:506
#define _T(x)
Definition: vfdio.h:22

Variable Documentation

◆ Interval

◆ Protocol

◆ TcpState

TCHAR TcpState[][32]
Initial value:
= {
_T("???"),
_T("CLOSED"),
_T("LISTENING"),
_T("SYN_SENT"),
_T("SYN_RCVD"),
_T("ESTABLISHED"),
_T("FIN_WAIT1"),
_T("FIN_WAIT2"),
_T("CLOSE_WAIT"),
_T("CLOSING"),
_T("LAST_ACK"),
_T("TIME_WAIT"),
}
#define _T(x)
Definition: vfdio.h:22

Definition at line 33 of file netstat.c.

Referenced by ShowTcpTable().