Getting IP statistics

GetIpStatistics() lets you get the statistics related to IP traffic. In this page, the usage of GetIpStatistics() is shown.

Sample code

The following sample code shows how to use GetIpStatistics().


#include <stdio.h>

#include <winsock2.h>
#include <iphlpapi.h>

int
main()
{
 MIB_IPSTATS ipstats;

 if (GetIpStatistics(&ipstats) == NOERROR) {
   printf("dwForwarding :     %ld\n", ipstats.dwForwarding);
   printf("dwDefaultTTL:      %ld\n", ipstats.dwDefaultTTL);
   printf("dwInReceives:      %ld\n", ipstats.dwInReceives);
   printf("dwInHdrErrors:     %ld\n", ipstats.dwInHdrErrors);
   printf("dwInAddrErrors:    %ld\n", ipstats.dwInAddrErrors);
   printf("dwForwDatagrams:   %ld\n", ipstats.dwForwDatagrams);
   printf("dwInUnknownProtos: %ld\n", ipstats.dwInUnknownProtos);
   printf("dwInDiscards:      %ld\n", ipstats.dwInDiscards);
   printf("dwInDelivers:      %ld\n", ipstats.dwInDelivers);
   printf("dwOutRequests:     %ld\n", ipstats.dwOutRequests);
   printf("dwRoutingDiscards: %ld\n", ipstats.dwRoutingDiscards);
   printf("dwOutDiscards:     %ld\n", ipstats.dwOutDiscards);
   printf("dwOutNoRoutes:     %ld\n", ipstats.dwOutNoRoutes);
   printf("dwReasmTimeout:    %ld\n", ipstats.dwReasmTimeout);
   printf("dwReasmReqds:      %ld\n", ipstats.dwReasmReqds);
   printf("dwReasmOks:        %ld\n", ipstats.dwReasmOks);
   printf("dwReasmFails:      %ld\n", ipstats.dwReasmFails);
   printf("dwFragOks:         %ld\n", ipstats.dwFragOks);
   printf("dwFragFails:       %ld\n", ipstats.dwFragFails);
   printf("dwFragCreates:     %ld\n", ipstats.dwFragCreates);
   printf("dwNumIf:           %ld\n", ipstats.dwNumIf);
   printf("dwNumAddr:         %ld\n", ipstats.dwNumAddr);
   printf("dwNumRoutes:       %ld\n", ipstats.dwNumRoutes);
 } else {
   printf("GetIpStatistics failed : %d\n", GetLastError());
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
dwForwarding :     2
dwDefaultTTL:      128
dwInReceives:      548610
dwInHdrErrors:     0
dwInAddrErrors:    283
dwForwDatagrams:   0
dwInUnknownProtos: 0
dwInDiscards:      0
dwInDelivers:      548574
dwOutRequests:     255708
dwRoutingDiscards: 0
dwOutDiscards:     0
dwOutNoRoutes:     0
dwReasmTimeout:    60
dwReasmReqds:      0
dwReasmOks:        0
dwReasmFails:      0
dwFragOks:         0
dwFragFails:       0
dwFragCreates:     0
dwNumIf:           2
dwNumAddr:         2
dwNumRoutes:       7

Structure used by GetIpStatistics()

The following structure is MIB_IPSTATS that is used by GetIpStatistics().


typedef struct _MIB_IPSTATS {
  DWORD dwForwarding;
  DWORD dwDefaultTTL;
  DWORD dwInReceives;
  DWORD dwInHdrErrors;
  DWORD dwInAddrErrors;
  DWORD dwForwDatagrams;
  DWORD dwInUnknownProtos;
  DWORD dwInDiscards;
  DWORD dwInDelivers;
  DWORD dwOutRequests;
  DWORD dwRoutingDiscards;
  DWORD dwOutDiscards;
  DWORD dwOutNoRoutes;
  DWORD dwReasmTimeout;
  DWORD dwReasmReqds;
  DWORD dwReasmOks;
  DWORD dwReasmFails;
  DWORD dwFragOks;
  DWORD dwFragFails;
  DWORD dwFragCreates;
  DWORD dwNumIf;
  DWORD dwNumAddr;
  DWORD dwNumRoutes;
} MIB_IPSTATS, *PMIB_IPSTATS;

dwForwarding Shows if IP forwarding is Enabled/Disabled.
dwDefaultTTL Default TTL (Time-To-Live).
dwInReceives Number of datagrams received.
dwInHdrErrors Number of datagrams with header errors.
dwInAddrErrors Number of datagrams with address errors.
dwForwDatagrams Number of forwarded datagrams.
dwInUnknownProtos Number of datagrams with unknown protocol.
dwInDiscards Number of discarded dagrams.
dwInDelivers Number of delivered datagrams.
dwOutRequests Number of datagrams that were requested to send. Value of dwForwDatagrams is not included.
dwRoutingDiscards Number of datagrams that were discarded and not sent.
dwOutDiscards Number of datagrams that were discarded when forwading.
dwOutNoRoutes Number of datagrams that were discarded because route did not exist.
dwReasmTimeout Timeout for fragmeted datagram reassembling.
dwReasmReqds Number of datagrams that needed reassembling.
dwReasmOks Number of successfully reassembled datagrams.
dwReasmFails Number of datagrams with reassemble failure.
dwFragOks Number of successful fragments.
dwFragFails Number of fragment failure.
dwFragCreates Number of created fragments.
dwNumIf Number of network interfaces.
dwNumAddr Number of IP addresses related to the local machine.
dwNumRoutes Number of routes in the routing table.
  

Copyright (C) GeekPage.JP. All rights reserved.