Ajay
Ajay

Reputation: 18411

Replacing gethostbyname to getaddrinfo for ICMP (IcmpSendEcho)

The legacy code has gethostbyname to get IP address of a host. The integer part of converted address is then passed to IcmpSendEcho. I am replacing this obsolete function with getaddrinfo.

DWORD GetIPAddressInt(const char* pAddress)
{
    PADDRINFOA addr = nullptr;    
    addrinfo hints = { 0 };

    hints.ai_family = AF_INET;    
    // hints.ai_socktype = ??;
    // hints.ai_protocol = ??;
    getaddrinfo(pAddress, nullptr, &hints, &addr);

    auto sockaddr_ipv4 = reinterpret_cast<sockaddr_in*>(addr->ai_addr);

    return sockaddr_ipv4->sin_addr.S_un.S_addr;
}

My question is: What about ai_socktype and ai_protocol members?

Re-iterating again, that the resultant IP address will be used for sending ICMP echo request, and hence I am wondering if RAW/ICMP are needed? For now, IPv6 is not a worry.

Upvotes: 1

Views: 2832

Answers (1)

bkausbk
bkausbk

Reputation: 2790

Concerning documentation for getaddrinfo you could just leave this fields blank (0).

A value of zero for ai_socktype indicates the caller will accept any socket type.

A value of zero for ai_protocol indicates the caller will accept any protocol.

IPv4 and IPv6 address doesn't depends on whether is used for stream or specific protocol type. So for calling IcmpSendEcho just ignore this fields.

Edit:

The socket type and protocol hint may be relevant only if the service name is specified. Service names could be "http", "tftp" etc. If you specify "tftp" service for example, you can not set "stream" socket type since tftp is datagram based. But in your case (and may be most other time) service field is left NULL. If you specify "http" service for example, the port member in ai_addr.sin_port should be filled too.

Upvotes: 1

Related Questions