60 ULONG ipaddr = htonl(info.getIP());
62 HANDLE h_icmp = IcmpCreateFile();
63 if (h_icmp == INVALID_HANDLE_VALUE)
65 throw SocketException(
"Unable to create ICMP socket", GetLastError());
68 DWORD reply_size =
sizeof(ICMP_ECHO_REPLY) +
sizeof(data);
69 LPVOID reply_buffer =
reinterpret_cast<void *
>(malloc(reply_size));
70 if (reply_buffer ==
nullptr)
72 IcmpCloseHandle(h_icmp);
73 throw std::runtime_error(
"Could not allocate memory");
76 DWORD result = IcmpSendEcho(h_icmp, ipaddr, data,
77 sizeof(data),
nullptr,
78 reply_buffer, reply_size, 1000);
80 IcmpCloseHandle(h_icmp);
83 reinterpret_cast<ICMP_ECHO_REPLY *>(reply_buffer)->Status == IP_SUCCESS)
97 const std::string command =
"ping -c 1 -W 1 " +
ip2string(info.
getIP());
100 if (!(in = popen(command.c_str(),
"r")))
102 throw std::runtime_error(
"Could not execute ping command.");
105 const int exit_code = pclose(in);
107 return exit_code == 0;
std::string ip2string(const uint32_t ip)
uint32_t getIP() const
Returns the current IP address of the device.
bool checkReachabilityOfSensor(const DeviceInfo &info)
Check whether an device is reachable via ICMP.