Commit 6b97cdf9 by 郭峰

Merge branch 'feature-1007488' into 'release'

修改ping命令-06

See merge request !45
parents 53105180 d0fe659b
...@@ -200,67 +200,100 @@ bool Common::pingAddress(const QString &address) { ...@@ -200,67 +200,100 @@ bool Common::pingAddress(const QString &address) {
} }
return false; return false;
} }
void Common::get_network_info(const char *interface, QString &mac, QString &subnet_mask, QString &gateway) {
int fd; // 获取本机mac
bool GetLocalMac(const char *eth_inf, char *mac)
{
struct ifreq ifr; struct ifreq ifr;
struct rtentry route; int sd;
unsigned char *mac_addr;
struct sockaddr_in *addr;
struct ifaddrs *ifaddr, *ifa;
// Create a socket to perform ioctl requests bzero(&ifr, sizeof(struct ifreq));
fd = socket(AF_INET, SOCK_DGRAM, 0); if( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
if (fd < 0) { {
perror("socket"); printf("get %s mac address socket creat error\n", eth_inf);
return; return false;
} }
// Get the MAC address strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - 1);
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1); if(ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
{
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { printf("get %s mac address error\n", eth_inf);
mac_addr = (unsigned char *)ifr.ifr_hwaddr.sa_data; close(sd);
mac = QString::asprintf("%02x:%02x:%02x:%02x:%02x:%02x", return false;
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
} else {
perror("SIOCGIFHWADDR");
mac = "Unknown";
} }
// Get the subnet mask snprintf(mac, MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0) { (unsigned char)ifr.ifr_hwaddr.sa_data[0],
addr = (struct sockaddr_in *)&ifr.ifr_netmask; (unsigned char)ifr.ifr_hwaddr.sa_data[1],
subnet_mask = QString::fromUtf8(inet_ntoa(addr->sin_addr)); (unsigned char)ifr.ifr_hwaddr.sa_data[2],
} else { (unsigned char)ifr.ifr_hwaddr.sa_data[3],
perror("SIOCGIFNETMASK"); (unsigned char)ifr.ifr_hwaddr.sa_data[4],
subnet_mask = "Unknown"; (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
close(sd);
return true;
}
//获取本机mask
bool GetLocalNetMask(const char *eth_inf,char* netmask_addr)
{
int sock_netmask;
struct ifreq ifr_mask;
struct sockaddr_in *net_mask;
sock_netmask = socket( AF_INET, SOCK_STREAM, 0 );
if( sock_netmask == -1)
{
perror("create socket failture...GetLocalNetMask\n");
return false;
} }
// Get the default gateway memset(&ifr_mask, 0, sizeof(ifr_mask));
FILE *fp = fopen("/proc/net/route", "r"); strncpy(ifr_mask.ifr_name, eth_inf, sizeof(ifr_mask.ifr_name )-1);
if (fp != NULL) {
char buffer[256]; if( (ioctl( sock_netmask, SIOCGIFNETMASK, &ifr_mask ) ) < 0 )
while (fgets(buffer, sizeof(buffer), fp)) { {
char iface[IFNAMSIZ]; printf("mac ioctl error\n");
unsigned long dest, gateway_addr; return false;
if (sscanf(buffer, "%s %lx %lx", iface, &dest, &gateway_addr) == 3) { }
if (dest == 0) { // Default route
struct in_addr gwaddr; net_mask = ( struct sockaddr_in * )&( ifr_mask.ifr_netmask );
gwaddr.s_addr = gateway_addr; strcpy( netmask_addr, inet_ntoa( net_mask -> sin_addr ) );
gateway = QString::fromUtf8(inet_ntoa(gwaddr)); close( sock_netmask );
break; return true;
} }
} //获取本机gateway
} bool GetLocalGateWay(char* gateway)
fclose(fp); {
} else { FILE *fp;
perror("fopen"); char buf[512];
gateway = "Unknown"; char cmd[128];
char *tmp;
strcpy(cmd, "ip route");
fp = popen(cmd, "r");
if(NULL == fp)
{
perror("popen error");
return false;
}
while(fgets(buf, sizeof(buf), fp) != NULL)
{
tmp =buf;
while(*tmp && isspace(*tmp))
++ tmp;
if(strncmp(tmp, "default", strlen("default")) == 0)
break;
} }
sscanf(buf, "%*s%*s%s", gateway);
pclose(fp);
// Clean up return true;
close(fd);
} }
// 确定当前网络接口 // 确定当前网络接口
...@@ -301,4 +334,90 @@ void Common::determine_interface(char *interface) { ...@@ -301,4 +334,90 @@ void Common::determine_interface(char *interface) {
freeifaddrs(ifaddr); freeifaddrs(ifaddr);
} }
QString Common::GetLocalMac(const char *eth_inf) {
struct ifreq ifr;
int sd;
char mac[MAC_SIZE]; // local buffer for mac address
bzero(&ifr, sizeof(struct ifreq));
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
qDebug() << "get" << eth_inf << "mac address socket create error";
return QString();
}
strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - 1);
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) {
qDebug() << "get" << eth_inf << "mac address error";
close(sd);
return QString();
}
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]);
close(sd);
return QString(mac); // convert C-string to QString
}
QString Common::GetLocalNetMask(const char *eth_inf) {
int sock_netmask;
struct ifreq ifr_mask;
struct sockaddr_in *net_mask;
sock_netmask = socket(AF_INET, SOCK_STREAM, 0);
if (sock_netmask == -1) {
perror("create socket failure...GetLocalNetMask\n");
return QString();
}
memset(&ifr_mask, 0, sizeof(ifr_mask));
strncpy(ifr_mask.ifr_name, eth_inf, sizeof(ifr_mask.ifr_name)-1);
if ((ioctl(sock_netmask, SIOCGIFNETMASK, &ifr_mask)) < 0) {
qDebug() << "netmask ioctl error";
close(sock_netmask);
return QString();
}
net_mask = (struct sockaddr_in*)&(ifr_mask.ifr_netmask);
QString netmaskAddr = QString(inet_ntoa(net_mask->sin_addr)); // Convert to QString
close(sock_netmask);
return netmaskAddr;
}
QString Common::GetLocalGateWay() {
FILE *fp;
char buf[512];
char cmd[128];
char gateway[INET_ADDRSTRLEN] = {0}; // Store the gateway address
strcpy(cmd, "ip route");
fp = popen(cmd, "r");
if (NULL == fp) {
perror("popen error");
return QString();
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
char *tmp = buf;
while (*tmp && isspace(*tmp))
++tmp;
if (strncmp(tmp, "default", strlen("default")) == 0)
break;
}
sscanf(buf, "%*s%*s%s", gateway); // Capture the third word as gateway
pclose(fp);
return QString(gateway); // Convert C-string to QString
}
Common::~Common(){} Common::~Common(){}
...@@ -30,6 +30,12 @@ ...@@ -30,6 +30,12 @@
#include <linux/route.h> #include <linux/route.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <netdb.h> #include <netdb.h>
#define NAME_SIZE 8
#define MAC_SIZE 18
#define IP_SIZE 16
#define MASK_SIZE 24
#define GATEWAY_SIZE 24
// ICMP 头部结构体 // ICMP 头部结构体
struct ICMPHeader { struct ICMPHeader {
uint8_t type; // 类型 uint8_t type; // 类型
...@@ -74,7 +80,9 @@ public: ...@@ -74,7 +80,9 @@ public:
//确定当前网络接口 //确定当前网络接口
void determine_interface(char *interface); void determine_interface(char *interface);
void get_network_info(const char *interface, QString &mac, QString &subnet_mask, QString &gateway) ; QString GetLocalMac(const char *eth_inf); // 获取本机mac
QString GetLocalNetMask(const char *eth_inf); //获取本机mask
QString GetLocalGateWay();//获取本机gateway
......
...@@ -29,13 +29,15 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ ...@@ -29,13 +29,15 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
// 确定当前网络接口 // 确定当前网络接口
instace.determine_interface(interface); instace.determine_interface(interface);
QString localMac, subnetMask, gateway;
// 获取网络信息 // 获取网络信息
instace.get_network_info(interface, localMac, subnetMask, gateway); QString localMac= instace.GetLocalMac(interface);
qInfo() <<QString("SN(%1): 获取本地MAC:%2,%3,%4").arg(device.sSn).arg(localMac) QString subnetMask= instace.GetLocalNetMask(interface);
QString gateway= instace.GetLocalGateWay();
qInfo() <<QString("SN(%1): 获取本地MAC:%2,subnetMask:%3,gateway:%4").arg(device.sSn).arg(localMac)
.arg(subnetMask).arg(gateway); .arg(subnetMask).arg(gateway);
if ( localMac=="Unknown" && subnetMask=="Unknown" && gateway=="Unknown" ) { if ( localMac.length()==0 && subnetMask.length()==0 && gateway.length()==0 ) {
qInfo() <<QString("SN(%1): 获取本地MAC失败").arg(device.sSn); qInfo() <<QString("SN(%1): 获取本地MAC失败").arg(device.sSn);
return false ; return false ;
} }
......
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