Commit f38613f1 by “liusq”

新增gb28181日志08

parent eed904d5
......@@ -138,9 +138,9 @@ bool Common::sendPingRequest(int sockfd, const char *ipAddress, int sequence) {
icmpHeader.sequence = sequence;
icmpHeader.checksum = 0;
char packet[sizeof(struct ICMPHeader) + 32];
char packet[sizeof(struct ICMPHeader) + 8];
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));
memcpy(packet, &icmpHeader, sizeof(icmpHeader));
......@@ -157,6 +157,13 @@ bool Common::receivePingReply(int sockfd, int sequence) {
char buffer[1024];
struct sockaddr_in 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);
if (bytesRead < 0) {
......@@ -187,7 +194,8 @@ bool Common::pingAddress(const QString &address) {
perror("socket");
return 1;
}
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
int sequence = 1;
if (sendPingRequest(sockfd, ipAddress, sequence)) {
if (receivePingReply(sockfd, sequence)) {
......@@ -224,12 +232,12 @@ bool GetLocalMac(const char *eth_inf, char *mac)
}
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[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);
close(sd);
......
......@@ -30,6 +30,7 @@
#include <linux/route.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <fcntl.h>
#define NAME_SIZE 8
#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