Commit 75c1ac2a by “liusq”

修改ping命令-02

parent dcd3dc5e
......@@ -425,6 +425,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
ScopeSemaphoreExit guard([this]() {
semaphore.release(); // 释放信号量
});
Common & instace= Common::getInstance();
cv::Mat image;
MediaFaceImage* mediaFaceImage= MediaFaceImage::getInstance();
qint64 currentTime= QDateTime::currentSecsSinceEpoch();
......@@ -434,7 +435,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
if (offlineCount >= 3) { // 判断是否连续3次返回0
qInfo() <<QString("SN(%1): 设备离线").arg(sSn);
QString ip=QString::fromUtf8(loginParam->sDevId);
bool is_ping=vides_data::pingAddress(ip);
bool is_ping=instace.pingAddress(ip);
qDebug() << sSn<<":ping 的结果"<<is_ping;
if(is_ping){
......
#include "Common.h"
#include "ScopeSemaphoreExit.h"
Common::Common(){}
......@@ -173,5 +174,31 @@ bool Common::receivePingReply(int sockfd, int sequence) {
return false;
}
bool Common::pingAddress(const QString &address) {
QByteArray && byJsonIp =address.toUtf8();
const char * ipAddress= byJsonIp.data();
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
ScopeSemaphoreExit guard([sockfd]() {
close(sockfd);
});
if (sockfd < 0) {
perror("socket");
return 1;
}
int sequence = 1;
if (sendPingRequest(sockfd, ipAddress, sequence)) {
if (receivePingReply(sockfd, sequence)) {
qInfo()<<"Ping to " << ipAddress << " successful!";
return true;
} else {
qInfo()<<"Ping to " << ipAddress << " failed!";
return false;
}
}
return false;
}
Common::~Common(){}
......@@ -11,7 +11,21 @@
#include <QMessageAuthenticationCode>
#include <QNetworkInterface>
#include <map>
#include <QDebug>
#include <iostream>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <sys/socket.h>
#include <unistd.h>
// ICMP 头部结构体
struct ICMPHeader {
uint8_t type; // 类型
uint8_t code; // 代码
uint16_t checksum; // 校验和
uint16_t id; // 标识符
uint16_t sequence; // 序列号
};
class Common
{
public:
......@@ -39,6 +53,14 @@ public:
QString getVideoDownload();
void setVideoDownload(QString videoDownload);
uint16_t calculateChecksum(uint16_t *buffer, int size);
bool sendPingRequest(int sockfd, const char *ipAddress, int sequence);
bool receivePingReply(int sockfd, int sequence);
bool pingAddress(const QString &address) ;
QString getImages();
void setImages(QString images);
......
......@@ -241,7 +241,9 @@ int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort,
return loginResult;
}
bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int hDevice ){
if(ip.length()>0 && vides_data::pingAddress(ip)){
Common & instace= Common::getInstance();
if(ip.length()>0 && instace.pingAddress(ip)){
qInfo()<<QString("SN(%1): 当前ip已被使用%2").arg(sSn).arg(ip);
return false;
}
......
......@@ -535,6 +535,8 @@ inline bool pingAddress(const QString &address) {
}
inline QString findReachableIp() {
Common & instace= Common::getInstance();
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (const QHostAddress &address : ipAddressesList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && !address.isLoopback()) {
......@@ -544,7 +546,7 @@ inline QString findReachableIp() {
for (int i = 254; i >= 1; --i) { // 从 254 开始递减
QString ip = currentSubnet + QString::number(i);
qInfo() << "Found findReachableIp IP:" << ip;
if (!pingAddress(ip)) {
if (!instace.pingAddress(ip)) {
qInfo() << "ping 不通的ip:" << ip;
return ip;
}
......
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