A New Way to Ping

Environment: VC6, WIN32

   On several occasions I've needed to use the functionality of the TCP ping utility programmatically. On one occasion I actually needed to get the hops count. I know there are numerous examples of using the ICMP interface but Microsoft has stated this interface is subject to change. It's also complicated to understand and program.

In searching the SDK I found the iphlpapi function GetRTTAndHopCount. This was just what I needed, one system function that does what I want. It also doesn't use any of the arcane sockets structures. The SDK documentation gives this description:

BOOL GetRTTAndHopCount(
  IPAddr DestIpAddress,  // destination IP address 
  PULONG HopCount,       // returned hop count
  ULONG MaxHops,         // limit on number of hops to search
  PULONG RTT             // round-trip time
);

Like a lot of the Win32 functions, if the function returns false you call GetLastError() for the specific error, typically a Winsock error. I find that the error returned when a host fails to respond is 11010, which the winsock2.h file describes as WSA_QOS_ADMISSION_FAILURE. This is not very informative (and maybe wrong). I think using the Boolean return is sufficient (they replied or they didn't).

The following is an example of how to use this new function.

#include <iphlpapi.h>
//-----------------------------------------------------------------
void CNewPingDlg::OnPingbutton()
{
int rc;
IPAddr ia;

    UpdateData(TRUE);
    ia = inet_addr (m_ip_address);
    rc = NewPing(ia, (ULONG*)&m_hops_count,(ULONG*)&m_rtt);
    if (rc == 0)
        m_status = "Get RTTL and Hops Succeeded";
    else
        m_status.Format("Host not responding or no route, rc = %d",rc);
    UpdateData(FALSE);
    MessageBeep(1000);
    return;
}
//-------------------------------------------------------------
int NewPing(IPAddr ia, ULONG *hops_count, ULONG *rtt)
{
boolean IsOk;

  IsOk = GetRTTAndHopCount(ia, hops_count,128, rtt); //the iphlpapi call
  if (IsOk == TRUE)	return 0;
  return GetLastError();
}
//---------------------------------------------------------------

    That's it. The only thing I did was hide and hard code the maximum hops to search in this function. More than 128 hops is a long way off, if ever. This function could be easily be put into a separate thread to make an asynchronous version.

Downloads

Download demo project - 28 Kb
Download source - 4 Kb



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds