Commit d0fe659b by “liusq”

修改ping命令-06

parent 742e07a4
......@@ -200,67 +200,100 @@ bool Common::pingAddress(const QString &address) {
}
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 rtentry route;
unsigned char *mac_addr;
struct sockaddr_in *addr;
struct ifaddrs *ifaddr, *ifa;
int sd;
// Create a socket to perform ioctl requests
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return;
bzero(&ifr, sizeof(struct ifreq));
if( (sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("get %s mac address socket creat error\n", eth_inf);
return false;
}
// Get the MAC address
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, interface, IFNAMSIZ - 1);
strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - 1);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
mac_addr = (unsigned char *)ifr.ifr_hwaddr.sa_data;
mac = QString::asprintf("%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
} else {
perror("SIOCGIFHWADDR");
mac = "Unknown";
if(ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
{
printf("get %s mac address error\n", eth_inf);
close(sd);
return false;
}
// Get the subnet mask
if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0) {
addr = (struct sockaddr_in *)&ifr.ifr_netmask;
subnet_mask = QString::fromUtf8(inet_ntoa(addr->sin_addr));
} else {
perror("SIOCGIFNETMASK");
subnet_mask = "Unknown";
}
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]);
// Get the default gateway
FILE *fp = fopen("/proc/net/route", "r");
if (fp != NULL) {
char buffer[256];
while (fgets(buffer, sizeof(buffer), fp)) {
char iface[IFNAMSIZ];
unsigned long dest, gateway_addr;
if (sscanf(buffer, "%s %lx %lx", iface, &dest, &gateway_addr) == 3) {
if (dest == 0) { // Default route
struct in_addr gwaddr;
gwaddr.s_addr = gateway_addr;
gateway = QString::fromUtf8(inet_ntoa(gwaddr));
break;
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;
}
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 )
{
printf("mac ioctl error\n");
return false;
}
net_mask = ( struct sockaddr_in * )&( ifr_mask.ifr_netmask );
strcpy( netmask_addr, inet_ntoa( net_mask -> sin_addr ) );
close( sock_netmask );
return true;
}
//获取本机gateway
bool GetLocalGateWay(char* gateway)
{
FILE *fp;
char buf[512];
char cmd[128];
char *tmp;
strcpy(cmd, "ip route");
fp = popen(cmd, "r");
if(NULL == fp)
{
perror("popen error");
return false;
}
fclose(fp);
} else {
perror("fopen");
gateway = "Unknown";
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
close(fd);
return true;
}
// 确定当前网络接口
......@@ -301,4 +334,90 @@ void Common::determine_interface(char *interface) {
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(){}
......@@ -30,6 +30,12 @@
#include <linux/route.h>
#include <ifaddrs.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 头部结构体
struct ICMPHeader {
uint8_t type; // 类型
......@@ -74,7 +80,9 @@ public:
//确定当前网络接口
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){
// 确定当前网络接口
instace.determine_interface(interface);
QString localMac, subnetMask, gateway;
// 获取网络信息
instace.get_network_info(interface, localMac, subnetMask, gateway);
qInfo() <<QString("SN(%1): 获取本地MAC:%2,%3,%4").arg(device.sSn).arg(localMac)
QString localMac= instace.GetLocalMac(interface);
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);
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);
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