pdinklag
pdinklag

Reputation: 1260

IcmpSendEcho2 - Asynchronous invocation is not asynchronous

I am using IcmpSendEcho2 to send an ICMP echo request to a host I would like to check. However, I need to handle the response asynchronously in a separate receiver thread.

In order to accomplish that, I create a new Windows event handle (CreateEvent) for every request I send out. The receiving thread will be notified and waits for incoming replies using WaitForMultipleObjects.

This event notification system works fine, but I cannot get IcmpSendEcho2 to be invoked asynchronously. The MSDN says that

The IcmpSendEcho2 function is called asynchronously when either the ApcRoutine or Event parameters are specified.

I do specify the Event parameter, but the calling thread is still locked until either a response arrived or the Timeout time has passed. I would expect that the method returns immediately and the supplied event gets signaled as soon as a response arrives or the timeout time has passed. At least that's my understanding of "asynchronous".

This is my invocation of the function, the parameters provided are valid:

dwResult = IcmpSendEcho2(
    icmpFileHandle,
    eventHandle,
    NULL, //no apcRoutine   
    NULL, //no apcContext
    ((sockaddr_in*)addrInfo->ai_addr)->sin_addr.S_un.S_addr,
    &requestData,
    sizeof(requestData),
    &requestOptions,
    &icmp4ReplyBuffer,
    sizeof(icmp4ReplyBuffer),
    30000);

Does the MSDN have a different understanding of "asynchronous", or is there anything I am doing wrong?

Upvotes: 3

Views: 2323

Answers (1)

jweyrich
jweyrich

Reputation: 32240

According to the official documentation:

The IcmpSendEcho2 function sends an IPv4 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 echo responses, if any.

Your understanding of asynchronous is correct, and it should return immediately if you passed a non-NULL event.

If it isn't working as expected, there are 3 possibilities:

  1. Your eventHandle is NULL, meaning the CreateEvent failed and returned a NULL handle. Have you checked?
  2. You're closing the ICMP file handle (IcmpCloseHandle) right after the asynchronous IcmpSendEcho call (suggested by OP in comments).
  3. If you're running a firewall application, it could be hooking that API and unintentionally breaking the asynchronous requirement.
  4. You could have found a Windows bug.

Upvotes: 4

Related Questions