Commit dcd3dc5e by “liusq”

修改ping命令

parent 5603d29d
...@@ -107,4 +107,71 @@ QString Common::GetLocalIp() { ...@@ -107,4 +107,71 @@ QString Common::GetLocalIp() {
} }
return ipAddress; return ipAddress;
} }
// 计算校验和
uint16_t Common::calculateChecksum(uint16_t *buffer, int size) {
uint32_t checksum = 0;
while (size > 1) {
checksum += *buffer++;
size -= sizeof(uint16_t);
}
if (size) {
checksum += *(uint8_t *)buffer;
}
checksum = (checksum >> 16) + (checksum & 0xffff);
checksum += (checksum >> 16);
return (uint16_t)(~checksum);
}
// 发送 ICMP 请求
bool Common::sendPingRequest(int sockfd, const char *ipAddress, int sequence) {
struct sockaddr_in destAddr;
memset(&destAddr, 0, sizeof(destAddr));
destAddr.sin_family = AF_INET;
inet_pton(AF_INET, ipAddress, &destAddr.sin_addr);
struct ICMPHeader icmpHeader;
icmpHeader.type = ICMP_ECHO;
icmpHeader.code = 0;
icmpHeader.id = getpid() & 0xffff;
icmpHeader.sequence = sequence;
icmpHeader.checksum = 0;
char packet[sizeof(struct ICMPHeader) + 32];
memcpy(packet, &icmpHeader, sizeof(icmpHeader));
memset(packet + sizeof(icmpHeader), 0, 32);
icmpHeader.checksum = calculateChecksum((uint16_t *)packet, sizeof(packet));
memcpy(packet, &icmpHeader, sizeof(icmpHeader));
if (sendto(sockfd, packet, sizeof(packet), 0, (struct sockaddr *)&destAddr, sizeof(destAddr)) < 0) {
perror("sendto");
return false;
}
return true;
}
// 接收 ICMP 回复
bool Common::receivePingReply(int sockfd, int sequence) {
char buffer[1024];
struct sockaddr_in srcAddr;
socklen_t srcAddrLen = sizeof(srcAddr);
ssize_t bytesRead = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&srcAddr, &srcAddrLen);
if (bytesRead < 0) {
perror("recvfrom");
return false;
}
struct iphdr *ipHeader = (struct iphdr *)buffer;
struct ICMPHeader *icmpHeader = (struct ICMPHeader *)(buffer + (ipHeader->ihl << 2));
if (icmpHeader->type == ICMP_ECHOREPLY && icmpHeader->sequence == sequence) {
std::cout << "Ping successful!" << std::endl;
return true;
}
return false;
}
Common::~Common(){} Common::~Common(){}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <list> #include <list>
#include "Common.h"
namespace vides_data{ namespace vides_data{
constexpr const char *HEADER_TYPE_KAY="Content-Type"; constexpr const char *HEADER_TYPE_KAY="Content-Type";
constexpr const char *HEADER_TYPE_VALUE="application/json"; constexpr const char *HEADER_TYPE_VALUE="application/json";
...@@ -350,6 +351,7 @@ struct DetectionParams { ...@@ -350,6 +351,7 @@ struct DetectionParams {
float recConfidenceThreshold; ///< 识别置信度阈值 float recConfidenceThreshold; ///< 识别置信度阈值
}; };
inline bool isVirtualMachine() inline bool isVirtualMachine()
{ {
QString dmiPath; QString dmiPath;
...@@ -493,11 +495,11 @@ inline bool pingAddress(const QString &address) { ...@@ -493,11 +495,11 @@ inline bool pingAddress(const QString &address) {
#else #else
// Linux 指令 "ping -c 1 IP" // Linux 指令 "ping -c 1 IP"
QStringList arguments; QStringList arguments;
arguments << "-c" << "ping -c 1 " + address; arguments << "-c" << "1" << address;
#endif #endif
// 启动进程 // 启动进程
cmd.start("bash", arguments); cmd.start("/bin/ping", arguments);
// 等待进程准备好读取 // 等待进程准备好读取
if (!cmd.waitForStarted()) { if (!cmd.waitForStarted()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment