#include "getHardwareInfo.h"
#if EXECUTION_GETHARDWATRINFO == 1 // Linux
/**
* @brief removeBlank 删除字符串中的空格
* @param str 需要处理的字符串
* @return 无返回值
*/
void removeBlank(char *str)
{
char *str_c = str;
int i,j=0;
for(i=0;str[i]!='\0';i++) {
if(str[i]!=' ') {
str_c[j++]=str[i];
}
}
str_c[j]='\0';
str = str_c;
}
/**
* @brief getCpuId 获取Linux下的CPU序列号
* @param id 存储获取到的CPU序列号的字符串变量
* @return 0:获取成功 其他返回值:获取失败
*/
int getCpuId(char *id)
{
unsigned int s1,s2;
asm volatile
( "movl $0x01,%%eax ; \n\t"
"xorl %%edx,%%edx ;\n\t"
"cpuid ;\n\t"
"movl %%edx , %0;\n\t"
"movl %%eax , %1;\n\t"
:"=m"(s1),"=m"(s2)
);
if(0 == s1 && 0 == s2) {
return -1;
}
char cpu_id[32] = {0};
sprintf(cpu_id, "%08X-%08X", htonl(s2), htonl(s1));
strcpy(id, cpu_id);
return 0;
}
/**
* @brief getDiskId 获取Linux下的硬盘序列号
* @param hd_name 硬盘所在位置 例:/dev/sda
* @param id 存储获取到的硬盘序列号的字符串变量
* @return 0:获取成功 其他返回值:获取失败
*/
int getDiskId(char *hd_name, char *id)
{
struct hd_driveid hid;
int fd = open(hd_name,O_RDONLY | O_NONBLOCK);
if(fd <0) {
perror("open fd");
return -1;
}
if(ioctl(fd,HDIO_GET_IDENTITY,&hid)<0) {
perror("ioctl");
return -1;
}
close(fd);
char disk_id[32] = {0};
sprintf(disk_id, "%s", hid.serial_no);
removeBlank(disk_id); //删除字符串中的空格
strcpy(id, disk_id);
return 0;
}
/**
* @brief get_iface_name 获取网口名字
* @param get_iface OUT 结构体
* @return 成功返回1 错误返回负数
*/
int get_iface_name(net_iface *get_iface)
{
int sock_get_iface;
struct ifconf ifc_get_iface;
struct ifreq *ifr_get_iface;
//初始化 ifconf
char buf[512];
ifc_get_iface.ifc_len = 512;
ifc_get_iface.ifc_buf = buf;
memset(get_iface,0,sizeof(net_iface));
sock_get_iface = socket(AF_INET,SOCK_DGRAM,0);
if(sock_get_iface < 0) {
perror("SOCKET:");
return -1;
}
if(ioctl(sock_get_iface ,SIOCGIFCONF,&ifc_get_iface) < 0) {
perror("ioctl");
return -1;
}
ifr_get_iface = (struct ifreq*)buf;
get_iface->sum_n = ifc_get_iface.ifc_len/sizeof(struct ifreq);
int control_n = 1;
for(int i = (ifc_get_iface.ifc_len/sizeof(struct ifreq)); i > 0; i--) {
if(strcmp(inet_ntoa(((struct sockaddr_in*)&(ifr_get_iface->ifr_addr))->sin_addr),"127.0.0.1") == 0) {
ifr_get_iface++;
get_iface->sum_n--;
continue;
}
else {
switch (control_n) {
case 1:
strcpy(get_iface->net_name1,ifr_get_iface->ifr_name);
strcpy(get_iface->net_ip1,inet_ntoa(((struct sockaddr_in*)&(ifr_get_iface->ifr_addr))->sin_addr));
break;
case 2:
strcpy(get_iface->net_name2,ifr_get_iface->ifr_name);
strcpy(get_iface->net_ip2,inet_ntoa(((struct sockaddr_in*)&(ifr_get_iface->ifr_addr))->sin_addr));
break;
case 3:
strcpy(get_iface->net_name3,ifr_get_iface->ifr_name);
strcpy(get_iface->net_ip3,inet_ntoa(((struct sockaddr_in*)&(ifr_get_iface->ifr_addr))->sin_addr));
break;
case 4:
strcpy(get_iface->net_name4,ifr_get_iface->ifr_name);
strcpy(get_iface->net_ip4,inet_ntoa(((struct sockaddr_in*)&(ifr_get_iface->ifr_addr))->sin_addr));
break;
case 5:
strcpy(get_iface->net_name5,ifr_get_iface->ifr_name);
strcpy(get_iface->net_ip5,inet_ntoa(((struct sockaddr_in*)&(ifr_get_iface->ifr_addr))->sin_addr));
break;
default:
break;
}
}
control_n++;
ifr_get_iface++;
}
close(sock_get_iface);
return 1;
}
/**
* @brief getNetMac 获取Linux下的所有网卡MAC序列号(最多五个网卡设备)
* @param get_iface 存储网卡信息的结构体
* @return 0:获取成功 其他返回值:获取失败
*/
int getNetMac(net_iface *get_iface)
{
int i = 0;
int sock;
get_iface_name(get_iface);
//printf("get_iface->sum_n: %d\n",get_iface->sum_n);
for(i=1;i<=get_iface->sum_n;i++) {
switch(i) {
case 1:
if((sock = socket(AF_INET,SOCK_STREAM,0))<0) {
perror("socket");
return 2;
}
struct ifreq ifreq1;
strcpy(ifreq1.ifr_name,get_iface->net_name1);
printf("%s ",ifreq1.ifr_name);
if(ioctl(sock,SIOCGIFHWADDR,&ifreq1)<0) {
perror("ioctl ");
return 3;
}
sprintf(get_iface->net_mac1,"%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifreq1.ifr_hwaddr.sa_data[0],
(unsigned char)ifreq1.ifr_hwaddr.sa_data[1],
(unsigned char)ifreq1.ifr_hwaddr.sa_data[2],
(unsigned char)ifreq1.ifr_hwaddr.sa_data[3],
(unsigned char)ifreq1.ifr_hwaddr.sa_data[4],
(unsigned char)ifreq1.ifr_hwaddr.sa_data[5]);
printf("MAC1: %s\n",get_iface->net_mac1);
close(sock);
break;
case 2:
if((sock = socket(AF_INET,SOCK_STREAM,0))<0) {
perror("socket");
return 2;
}
struct ifreq ifreq2;
strcpy(ifreq2.ifr_name,get_iface->net_name2);
printf("%s ",ifreq2.ifr_name);
if(ioctl(sock,SIOCGIFHWADDR,&ifreq2)<0) {
perror("ioctl ");
return 3;
}
sprintf(get_iface->net_mac2,"%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifreq2.ifr_hwaddr.sa_data[0],
(unsigned char)ifreq2.ifr_hwaddr.sa_data[1],
(unsigned char)ifreq2.ifr_hwaddr.sa_data[2],
(unsigned char)ifreq2.ifr_hwaddr.sa_data[3],
(unsigned char)ifreq2.ifr_hwaddr.sa_data[4],
(unsigned char)ifreq2.ifr_hwaddr.sa_data[5]);
printf("MAC2: %s\n",get_iface->net_mac2);
close(sock);
break;
case 3:
if((sock = socket(AF_INET,SOCK_STREAM,0))<0) {
perror("socket");
return 2;
}
struct ifreq ifreq3;
strcpy(ifreq3.ifr_name,get_iface->net_name3);
printf("%s ",ifreq3.ifr_name);
if(ioctl(sock,SIOCGIFHWADDR,&ifreq3)<0) {
perror("ioctl ");
return 3;
}
sprintf(get_iface->net_mac3,"%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifreq3.ifr_hwaddr.sa_data[0],
(unsigned char)ifreq3.ifr_hwaddr.sa_data[1],
(unsigned char)ifreq3.ifr_hwaddr.sa_data[2],
(unsigned char)ifreq3.ifr_hwaddr.sa_data[3],
(unsigned char)ifreq3.ifr_hwaddr.sa_data[4],
(unsigned char)ifreq3.ifr_hwaddr.sa_data[5]);
printf("MAC3: %s\n",get_iface->net_mac3);
close(sock);
break;
case 4:
评论2