#include #include #include #include #include #include #include #include #include #include #include #include "get_interface.h" int interface_num=0;//接口数量 INTERFACE net_interface[MAXINTERFACES];//接口数据 /****************************************************************** 函 数: int get_interface_num() 功 能: 获取接口数量 参 数: 无 *******************************************************************/ int get_interface_num(){ return interface_num; } /****************************************************************** 函 数: int getinterface() 功 能: 获取接口信息 参 数: 无 *******************************************************************/ void getinterface(){ struct ifreq buf[MAXINTERFACES]; /* ifreq结构数组 */ struct ifconf ifc; /* ifconf结构 */ int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); /* 初始化ifconf结构 */ ifc.ifc_len = sizeof(buf); ifc.ifc_buf = (caddr_t) buf; /* 获得接口列表 */ if (ioctl(sock_raw_fd, SIOCGIFCONF, (char *) &ifc) == -1){ perror("SIOCGIFCONF ioctl"); return ; } interface_num = ifc.ifc_len / sizeof(struct ifreq); /* 接口数量 */ printf("interface_num=%d\n\n", interface_num); char buff[20]=""; int ip; int if_len = interface_num; while (if_len-- > 0){ /* 遍历每个接口 */ printf("%s\n", buf[if_len].ifr_name); /* 接口名称 */ sprintf(net_interface[if_len].name, "%s", buf[if_len].ifr_name); /* 接口名称 */ printf("-%d-%s--\n",if_len,net_interface[if_len].name); /* 获得接口标志 */ if (!(ioctl(sock_raw_fd, SIOCGIFFLAGS, (char *) &buf[if_len]))){ /* 接口状态 */ if (buf[if_len].ifr_flags & IFF_UP){ printf("UP\n"); net_interface[if_len].flag = 1; } else{ printf("DOWN\n"); net_interface[if_len].flag = 0; } }else{ char str[256]; sprintf(str, "SIOCGIFFLAGS ioctl %s", buf[if_len].ifr_name); perror(str); } /* IP地址 */ if (!(ioctl(sock_raw_fd, SIOCGIFADDR, (char *) &buf[if_len]))){ printf("IP:%s\n",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr)); bzero(buff,sizeof(buff)); sprintf(buff, "%s", (char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr)); inet_pton(AF_INET, buff, &ip); memcpy(net_interface[if_len].ip, &ip, 4); }else{ char str[256]; sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name); perror(str); } /* 子网掩码 */ if (!(ioctl(sock_raw_fd, SIOCGIFNETMASK, (char *) &buf[if_len]))){ printf("netmask:%s\n",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr)); bzero(buff,sizeof(buff)); sprintf(buff, "%s", (char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr)); inet_pton(AF_INET, buff, &ip); memcpy(net_interface[if_len].netmask, &ip, 4); }else{ char str[256]; sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name); perror(str); } /* 广播地址 */ if (!(ioctl(sock_raw_fd, SIOCGIFBRDADDR, (char *) &buf[if_len]))){ printf("br_ip:%s\n",(char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr)); bzero(buff,sizeof(buff)); sprintf(buff, "%s", (char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))->sin_addr)); inet_pton(AF_INET, buff, &ip); memcpy(net_interface[if_len].br_ip, &ip, 4); }else{ char str[256]; sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name); perror(str); } /*MAC地址 */ if (!(ioctl(sock_raw_fd, SIOCGIFHWADDR, (char *) &buf[if_len]))){ printf("MAC:%02x:%02x:%02x:%02x:%02x:%02x\n\n", (unsigned char) buf[if_len].ifr_hwaddr.sa_data[0], (unsigned char) buf[if_len].ifr_hwaddr.sa_data[1], (unsigned char) buf[if_len].ifr_hwaddr.sa_data[2], (unsigned char) buf[if_len].ifr_hwaddr.sa_data[3], (unsigned char) buf[if_len].ifr_hwaddr.sa_data[4], (unsigned char) buf[if_len].ifr_hwaddr.sa_data[5]); memcpy(net_interface[if_len].mac, (unsigned char *)buf[if_len].ifr_hwaddr.sa_data, 6); }else{ char str[256]; sprintf(str, "SIOCGIFHWADDR ioctl %s", buf[if_len].ifr_name); perror(str); } }//–while end close(sock_raw_fd); //关闭socket }