Menu

Win32+ICMP API => Dealing with TIMEOUTS

2003-01-21
2012-09-26
  • Nobody/Anonymous

    Maybe some of you guys can help a beginner on Win32 coding? Im trying a very simple PING app based on the ICMP API. My code works, i can PING the host and get results. I just cant make it respect the TIMEOUT value. I will post the important part of it for you to get an idea of what im talking about.
    Any help will be fantastic, im really dying here.
     
    ...
    typedef struct tagIPINFO
    {
        u_char Ttl;        // Time To Live
        u_char Tos;        // Type Of Service
        u_char IPFlags;        // IP flags
        u_char OptSize;        // Size of options data
        u_char FAR *Options;    // Options data buffer
    }IPINFO, *PIPINFO;

    typedef struct tagICMPECHO
    {
        u_long Source;            // Source address
        u_long Status;            // IP status
        u_long RTTime;            // Round trip time in milliseconds
        u_short DataSize;        // Reply data size
        u_short Reserved;        // Unknown
        void FAR *pData;        // Reply data buffer
        IPINFO    ipInfo;            // Reply options
    }ICMPECHO, *PICMPECHO;

    ...
    hndlFile = pIcmpCreateFile();
        for (x = 0; x < num; x++)
        {
            // Set some reasonable default values
            ipInfo.Ttl = 255;
            ipInfo.Tos = 0;
            ipInfo.IPFlags = 0;
            ipInfo.OptSize = 0;
            ipInfo.Options = NULL;
            //icmpEcho.ipInfo.Ttl = 256;
            // Reqest an ICMP echo
            dwRet = pIcmpSendEcho(
                hndlFile,        // Handle from IcmpCreateFile()
                *dwAddress,        // Destination IP address
                NULL,            // Pointer to buffer to send
                0,            // Size of buffer in bytes
                &ipInfo,        // Request options
                &icmpEcho,        // Reply buffer
                sizeof(struct tagICMPECHO),
                5000);            // Time to wait in milliseconds, does not work.
            // Print the results
            iaDest.s_addr = icmpEcho.Source;
            printf("\nReply from %s  Time=%ldms  TTL=%d",
                    inet_ntoa(iaDest),
                    icmpEcho.RTTime,
                    icmpEcho.ipInfo.Ttl);
            if (icmpEcho.Status)
            {
                printf("\nError: icmpEcho.Status=%ld",
                    icmpEcho.Status);
                break;
            }
        }
        printf("\n");
        // Close the echo request file handle
        pIcmpCloseHandle(hndlFile);
        FreeLibrary(hndlIcmp);
        WSACleanup();
    }

     
    • Yiannis Mandravellos

      From the MS help files:

      The IcmpSendEcho function sends an ICMP Echo request and returns any replies. The call returns when the time-out has expired or the reply buffer is filled.

      The IcmpSendEcho2 function sends an ICMP Echo request and returns either immediately (if Event or ApcRoutine is non-NULL) or returns after the specified time-out. The ReplyBuffer contains the ICMP responses, if any.

      Yiannis.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.