Fire Lancer
Fire Lancer

Reputation: 30135

IcmpSendEcho fails but "ping" succeeds

I have been looking at using IcmpSendEcho, and found that it will fail to ping certain devices (e.g. my BT Home Hub 4) with GetLastError reporting 11010. While for other devices it works fine (when executing on the same system). In comparison, ping.exe succeeds on all these devices, but I have no idea how the implementation of Ping differs. All cases I have tried so far have been IPv4, which I provided directly (so no DNS etc.).

    hIcmpFile = IcmpCreateFile();
    ipAddress = inet_addr(ipAddressStr);
    ...hIcmpFile is reused
    static const WORD sendSize = 32;
    static const DWORD replySize = sizeof(ICMP_ECHO_REPLY) + sendSize;
    char sendData[sendSize] = { 0 };
    char replyBuffer[replySize];
    auto ret = IcmpSendEcho(hIcmpFile, ipAddress, sendData, sendSize, NULL, replyBuffer, replySize, 1000);
    if (ret == 0)
    {
        auto error = GetLastError();

The only other report I have found is what would cause ICMPsendEcho to fail when ping.exe succeeds. However those answers appear to differ from my problem. I have tried using different payload sizes, and I have tried IcmpSendEcho2, that also failed for the same devices.

Upvotes: 0

Views: 4122

Answers (1)

Ben
Ben

Reputation: 437

I've been having a similar problem, but I think the issue is due to the icmp request timing out before you get a reply.

My code is based heavily on the example code from the MSDN page for IcmpSendEcho, only I added a number of retries on failure. My code runs in the evenings, when machines are likely to have gone to sleep or some other low power state which means they take a few seconds to wake up and reply.

Usually my output logs state that the first ping attempt fails with error 11010. The second attempt always succeeds. So I'm guessing the first ping gives the machine a poke and wakes it up, but I miss the delayed reply. The second ping succeeds.

So try either adding a longer timeout or just add a few retries.

Upvotes: 1

Related Questions