Commit 277d3943 by 郭峰

Merge branch 'feature-1007488' into 'release'

新增gb28181日志08

See merge request !53
parents 4d32a442 f38613f1
...@@ -138,9 +138,9 @@ bool Common::sendPingRequest(int sockfd, const char *ipAddress, int sequence) { ...@@ -138,9 +138,9 @@ bool Common::sendPingRequest(int sockfd, const char *ipAddress, int sequence) {
icmpHeader.sequence = sequence; icmpHeader.sequence = sequence;
icmpHeader.checksum = 0; icmpHeader.checksum = 0;
char packet[sizeof(struct ICMPHeader) + 32]; char packet[sizeof(struct ICMPHeader) + 8];
memcpy(packet, &icmpHeader, sizeof(icmpHeader)); memcpy(packet, &icmpHeader, sizeof(icmpHeader));
memset(packet + sizeof(icmpHeader), 0, 32); memset(packet + sizeof(icmpHeader), 0, 8);
icmpHeader.checksum = calculateChecksum((uint16_t *)packet, sizeof(packet)); icmpHeader.checksum = calculateChecksum((uint16_t *)packet, sizeof(packet));
memcpy(packet, &icmpHeader, sizeof(icmpHeader)); memcpy(packet, &icmpHeader, sizeof(icmpHeader));
...@@ -157,6 +157,13 @@ bool Common::receivePingReply(int sockfd, int sequence) { ...@@ -157,6 +157,13 @@ bool Common::receivePingReply(int sockfd, int sequence) {
char buffer[1024]; char buffer[1024];
struct sockaddr_in srcAddr; struct sockaddr_in srcAddr;
socklen_t srcAddrLen = sizeof(srcAddr); socklen_t srcAddrLen = sizeof(srcAddr);
struct timeval tv;
tv.tv_sec = 2; // 设置超时时间为2秒
tv.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
perror("setsockopt");
return false;
}
ssize_t bytesRead = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&srcAddr, &srcAddrLen); ssize_t bytesRead = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&srcAddr, &srcAddrLen);
if (bytesRead < 0) { if (bytesRead < 0) {
...@@ -187,7 +194,8 @@ bool Common::pingAddress(const QString &address) { ...@@ -187,7 +194,8 @@ bool Common::pingAddress(const QString &address) {
perror("socket"); perror("socket");
return 1; return 1;
} }
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
int sequence = 1; int sequence = 1;
if (sendPingRequest(sockfd, ipAddress, sequence)) { if (sendPingRequest(sockfd, ipAddress, sequence)) {
if (receivePingReply(sockfd, sequence)) { if (receivePingReply(sockfd, sequence)) {
...@@ -224,12 +232,12 @@ bool GetLocalMac(const char *eth_inf, char *mac) ...@@ -224,12 +232,12 @@ bool GetLocalMac(const char *eth_inf, char *mac)
} }
snprintf(mac, MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x", snprintf(mac, MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifr.ifr_hwaddr.sa_data[0], (unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1], (unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2], (unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3], (unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4], (unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]); (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
close(sd); close(sd);
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include <linux/route.h> #include <linux/route.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <netdb.h> #include <netdb.h>
#include <fcntl.h>
#define NAME_SIZE 8 #define NAME_SIZE 8
#define MAC_SIZE 18 #define MAC_SIZE 18
......
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