< GetIpInterfaceTable & IP Statistics Examples | IP Helper Functions Main | Chap 14: The Mailslots >
What do we have in this chapter 13 part 13?
|
Add the Ipstat.h header file, storing the function prototypes (You can also include the prototypes directly in the source file eliminating this header file). Create a new empty Win32 console mode application and add the project/solution name.
Add the source code. |
#ifndef _IPSTAT_H
#define _IPSTAT_H
// Link to ws2_32.lib
#include <winsock2.h>
// Link to Iphlpapi.lib
#include <iphlpapi.h>
#include <assert.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iptypes.h>
// general max string length
#define MAX_STRLEN 128
void DoGetConnTable(char* pszProto);
void DoGetStat(char* pszProto);
void DumpTcpTable(PMIB_TCPTABLE pTcpTable);
void DumpUdpTable(PMIB_UDPTABLE pUdpTable);
DWORD MyGetTcpTable(PMIB_TCPTABLE pTcpTable, BOOL fOrder);
DWORD MyGetUdpTable(PMIB_UDPTABLE pUdpTable, BOOL fOrder);
DWORD MyGetIpStatistics(PMIB_IPSTATS pIpStats);
DWORD MyGetIcmpStatistics(PMIB_ICMP pIcmpStats);
DWORD MyGetTcpStatistics(PMIB_TCPSTATS pTcpStats);
DWORD MyGetUdpStatistics(PMIB_UDPSTATS pUdpStats);
void PrintIpStats(PMIB_IPSTATS pStats);
void PrintIcmpStats(MIBICMPINFO *pStats);
void PrintTcpStats(PMIB_TCPSTATS pStats);
void PrintUdpStats(PMIB_UDPSTATS pStats);
#endif // _IPSTAT_H
Build and run the project.
The GetTcpTable() function retrieves the IPv4 TCP connection table. Create a new empty Win32 console mode application and add the project/solution name.
Add the following code.
// Link to Ws2_32.lib
#include <winsock2.h>
#include <ws2tcpip.h>
// Link to Iphlpapi.lib
#include <iphlpapi.h>
#include <stdio.h>
// Note: could also use malloc() and free()
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int main(int argc, char **argv)
{
// Declare and initialize variables
PMIB_TCPTABLE pTcpTable;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
char szLocalAddr[128];
char szRemoteAddr[128];
struct in_addr IpAddr;
int i;
pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
if (pTcpTable == NULL)
{
printf("Error allocating memory!\n");
return 1;
}
dwSize = sizeof (MIB_TCPTABLE);
// Make an initial call to GetTcpTable to
// get the necessary size into the dwSize variable
if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == ERROR_INSUFFICIENT_BUFFER)
{
FREE(pTcpTable);
pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);
if (pTcpTable == NULL)
{
printf("Error allocating memory\n");
return 1;
}
}
// Make a second call to GetTcpTable to get
// the actual data we require
if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR)
{
printf("\tLocal Addr\tLocal Port\tRemote Addr\tRemote Port\n");
printf("Number of entries: %d\n", (int) pTcpTable->dwNumEntries);
for (i = 0; i < (int) pTcpTable->dwNumEntries; i++)
{
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
printf("\nTCP[%d] State: %ld - ", i, pTcpTable->table[i].dwState);
switch (pTcpTable->table[i].dwState)
{
case MIB_TCP_STATE_CLOSED:
printf("CLOSED\n");
break;
case MIB_TCP_STATE_LISTEN:
printf("LISTEN\n");
break;
case MIB_TCP_STATE_SYN_SENT:
printf("SYN-SENT\n");
break;
case MIB_TCP_STATE_SYN_RCVD:
printf("SYN-RECEIVED\n");
break;
case MIB_TCP_STATE_ESTAB:
printf("ESTABLISHED\n");
break;
case MIB_TCP_STATE_FIN_WAIT1:
printf("FIN-WAIT-1\n");
break;
case MIB_TCP_STATE_FIN_WAIT2:
printf("FIN-WAIT-2 \n");
break;
case MIB_TCP_STATE_CLOSE_WAIT:
printf("CLOSE-WAIT\n");
break;
case MIB_TCP_STATE_CLOSING:
printf("CLOSING\n");
break;
case MIB_TCP_STATE_LAST_ACK:
printf("LAST-ACK\n");
break;
case MIB_TCP_STATE_TIME_WAIT:
printf("TIME-WAIT\n");
break;
case MIB_TCP_STATE_DELETE_TCB:
printf("DELETE-TCB\n");
break;
default:
printf("UNKNOWN dwState value\n");
break;
}
printf("TCP[%d]:%s%15d%20s%15d\n", i, szLocalAddr,
ntohs((u_short)pTcpTable->table[i].dwLocalPort), szRemoteAddr,ntohs((u_short)pTcpTable->table[i].dwRemotePort));
//printf("\tTCP[%d] Local Addr: %s\n", i, szLocalAddr);
// printf("\tTCP[%d] Local Port: %d \n", i, ntohs((u_short)pTcpTable->table[i].dwLocalPort));
//printf("\tTCP[%d] Remote Addr: %s\n", i, szRemoteAddr);
//printf("\tTCP[%d] Remote Port: %d\n", i, ntohs((u_short)pTcpTable->table[i].dwRemotePort));
}
}
else
{
printf("\tGetTcpTable() failed with return value %d\n", dwRetVal);
FREE(pTcpTable);
return 1;
}
return 0;
}
Build and run the project.
-------------------------------------------------------------
< GetIpInterfaceTable & IP Statistics Examples | IP Helper Functions Main | Chap 14: The Mailslots >