Library tutorials & articles

How to PING

The Class

Now let's get down and dirty and write some code. First off, I have no idea how to send ICMP packets with the socket library. I'll leave that for the pros. My sample code will use a little known ICMP. DLL found in the System32 folder. This DLL exports eight functions. I'll limit our discussion to the three important ones.

HANDLE WINAPI IcmpCreateFile (VOID);
BOOL WINAPI IcmpCloseHandle (HANDLE IcmpHandle);
DWORD WINAPI IcmpSendEcho (HANDLE IcmpHandle,
IPAddr DestinationAddress, LPVOID RequestData,
    WORD RequestSize, PIP_OPTION_INFORMATION RequestOptions,
    LPVOID ReplyBuffer, DWORD ReplySize, DWORD Timeout);

The IcmpCreateFile function returns a handle that is used to execute ICMP requests. The IcmpCloseHandle function closes the handle created by IcmpCreateFile. The last function is IcmpSendEcho. This function given a handle will send one ICMP packet to a host and wait a specified timeout period or until a reply is received.

HANDLE icmphandle = IcmpCreateFile();
char reply[sizeof(icmp_echo_reply)+8];
icmp_echo_reply* iep = (icmp_echo_reply*)&reply;
iep->RoundTripTime = 0xffffffff;

DWORD dw = IcmpSendEcho(icmphandle,
    *((u_long*) host->h_addr_list[0]),
    0,0, NULL,
    reply, sizeof(icmp_echo_reply)+8,5000);

if (dw == 0)
{
    throw IcmpException("send echo failed");
}

IcmpCloseHandle(icmphandle);

The code for sending one ICMP packet with zero content is very minimal. Create the handle, setup the reply buffer, call the IcmpSendEcho method, test for error conditions and close the handle. The only part that is left is to figure out how to construct the *((u_long*) host-> h_addr_list[0]) or host address.

hostent * host;
in_addr inaddr;
inaddr.s_addr = ::inet_addr(strAddress.c_str());
if (inaddr.s_addr == INADDR_NONE)
{
    host = ::gethostbyname(strAddress.c_str());
}
else
{
    host = ::gethostbyaddr((const char *)&inaddr,
    sizeof(inaddr), AF_INET);
}

if (host == NULL)
{
    throw IcmpException("invalid SMTP server");
}

In this code sample, I determine if the address is an IP address or a name that should be resolved to an IP address. Then I construct the host structure from the name or address.

Comments

  1. 31 Jul 2007 at 09:43
    You can also see a screencast how to ping .

  2. 28 Mar 2006 at 17:36

    hfhg

  3. 30 Nov 2005 at 18:13
    Hi Everyone.I'm kind a newbie in C++ but recently I got a special task and I need to develop a ping plotter, a route tracer and a netstat utility in C++. I would really appreciate any kind of help (some source code or anything). Thx...
  4. 14 Nov 2005 at 18:26

    What is the name of library to include ?


    include ???



    In the icmp.h and ping.cpp


    Thanks

  5. 14 Oct 2004 at 20:37
    HI everyone,
    i need to develop a ping plotter + a route tracer in c # but the problem is i am a newbie and dont know much so how do i go about this or if any source code is available for pining in c # would be grateful
  6. 01 Jan 1999 at 00:00

    This thread is for discussions of How to PING.

Leave a comment

Sign in or Join us (it's free).

Randy Charles Morin Randy's article are Copyright 1998-2003 Randy Charles Morin

We'd love to hear what you think! Submit ideas or give us feedback