58 ULONG ipaddr = htonl(info.getIP());
60 HANDLE h_icmp = IcmpCreateFile();
61 if (h_icmp == INVALID_HANDLE_VALUE)
63 throw SocketException(
"Unable to create ICMP socket", GetLastError());
66 DWORD reply_size =
sizeof(ICMP_ECHO_REPLY) +
sizeof(data);
67 LPVOID reply_buffer =
reinterpret_cast<void *
>(malloc(reply_size));
68 if (reply_buffer ==
nullptr)
70 IcmpCloseHandle(h_icmp);
71 throw std::runtime_error(
"Could not allocate memory");
74 DWORD result = IcmpSendEcho(h_icmp, ipaddr, data,
75 sizeof(data),
nullptr,
76 reply_buffer, reply_size, 1000);
78 IcmpCloseHandle(h_icmp);
81 reinterpret_cast<ICMP_ECHO_REPLY *
>(reply_buffer)->Status == IP_SUCCESS)
95 const std::string command =
"ping -c 1 -W 1 " +
ip2string(info.
getIP());
98 if (!(in = popen(command.c_str(),
"r")))
100 throw std::runtime_error(
"Could not execute ping command.");
105 while (fread(buffer, 80, 1, in) > 0);
107 const int exit_code = pclose(in);
109 return exit_code == 0;