Linux下获取所有可用网卡信息
2014-06-14 15:12
417 查看
在Linux下开发网络程序时,经常会遇到需要取本地网络接口名、IP、广播地址、子网掩码或者MAC地址等信息的需求,最常见的办法是配合SIOCGIFHWADDR、SIOCGIFADDR、SIOCGIFBRDADDR与SIOCGIFNETMASK作为参数调用函数ioctl分别获得MAC地址、IP地址、广播地址与子网掩码来实现。
struct ifreq
这个结构定义在include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的/* Interface request structure used for socket ioctl's. All interface ioctl's must have parameter definitions which begin with ifr_name. The remainder may be interface specific. */ struct ifreq { # define IFHWADDRLEN 6 # define IFNAMSIZ IF_NAMESIZE union { char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */ } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short int ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[IFNAMSIZ]; /* Just fits the size */ char ifru_newname[IFNAMSIZ]; __caddr_t ifru_data; } ifr_ifru; }; # define ifr_name ifr_ifrn.ifrn_name /* interface name */ # define ifr_hwaddr ifr_ifru.ifru_hwaddr /* MAC address */ # define ifr_addr ifr_ifru.ifru_addr /* address */ # define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-p lnk */ # define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ # define ifr_netmask ifr_ifru.ifru_netmask /* interface net mask */ # define ifr_flags ifr_ifru.ifru_flags /* flags */ # define ifr_metric ifr_ifru.ifru_ivalue /* metric */ # define ifr_mtu ifr_ifru.ifru_mtu /* mtu */ # define ifr_map ifr_ifru.ifru_map /* device map */ # define ifr_slave ifr_ifru.ifru_slave /* slave device */ # define ifr_data ifr_ifru.ifru_data /* for use by interface */ # define ifr_ifindex ifr_ifru.ifru_ivalue /* interface index */ # define ifr_bandwidth ifr_ifru.ifru_ivalue /* link bandwidth */ # define ifr_qlen ifr_ifru.ifru_ivalue /* queue length */ # define ifr_newname ifr_ifru.ifru_newname /* New name */ # define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0) # define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0) # define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)
struct ifconf
通常是用来保存所有接口信息的/* Structure used in SIOCGIFCONF request. Used to retrieve interface configuration for machine (useful for programs which must know all networks accessible). */ struct ifconf { int ifc_len; /* Size of buffer. */ union { __caddr_t ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }; # define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address. */ # define ifc_req ifc_ifcu.ifcu_req /* Array of structures. */ # define _IOT_ifconf _IOT(_IOTS(struct ifconf),1,0,0,0,0) /* not right */完整代码如下:
/*============================================================================ Name : Exercise.cbp Author : Haier Version : 1.01 Copyright : Copyright (c) 2014 Description : File Encryption in C, Ansi-style, Compile by Code::Block ============================================================================*/ #include <stdio.h> #include <string.h> #include <net/if.h> #include <sys/ioctl.h> #include <arpa/inet.h> #include <errno.h> int getLocalInfo(void) { int fd; int interfaceNum = 0; struct ifreq buf[16]; struct ifconf ifc; struct ifreq ifrcopy; char mac[16] = {0}; char ip[32] = {0}; char broadAddr[32] = {0}; char subnetMask[32] = {0}; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); close(fd); return -1; } ifc.ifc_len = sizeof(buf); ifc.ifc_buf = (caddr_t)buf; if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) { interfaceNum = ifc.ifc_len / sizeof(struct ifreq); printf("interface num = %d\n", interfaceNum); while (interfaceNum-- > 0) { printf("\ndevice name: %s\n", buf[interfaceNum].ifr_name); //ignore the interface that not up or not runing ifrcopy = buf[interfaceNum]; if (ioctl(fd, SIOCGIFFLAGS, &ifrcopy)) { printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__); close(fd); return -1; } //get the mac of this interface if (!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum]))) { memset(mac, 0, sizeof(mac)); snprintf(mac, sizeof(mac), "%02x%02x%02x%02x%02x%02x", (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[0], (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[1], (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[2], (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[3], (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[4], (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[5]); printf("device mac: %s\n", mac); } else { printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__); close(fd); return -1; } //get the IP of this interface if (!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum])) { snprintf(ip, sizeof(ip), "%s", (char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr)); printf("device ip: %s\n", ip); } else { printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__); close(fd); return -1; } //get the broad address of this interface if (!ioctl(fd, SIOCGIFBRDADDR, &buf[interfaceNum])) { snprintf(broadAddr, sizeof(broadAddr), "%s", (char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_broadaddr))->sin_addr)); printf("device broadAddr: %s\n", broadAddr); } else { printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__); close(fd); return -1; } //get the subnet mask of this interface if (!ioctl(fd, SIOCGIFNETMASK, &buf[interfaceNum])) { snprintf(subnetMask, sizeof(subnetMask), "%s", (char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_netmask))->sin_addr)); printf("device subnetMask: %s\n", subnetMask); } else { printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__); close(fd); return -1; } } } else { printf("ioctl: %s [%s:%d]\n", strerror(errno), __FILE__, __LINE__); close(fd); return -1; } close(fd); return 0; } int main(void) { getLocalInfo(); return 0; }使用ioctl函数虽然可以获取所有的信息,但是使用起来比较麻烦,如果不需要获取MAC地址,那么使用getifaddrs函数来获取更加方便与简洁。值得一提的是,在MacOS或iOS系统上(如iPhone程序开发),上述iotcl函数没法获得mac地址跟子网掩码,这个使用,使用getifaddrs函数便更有优势了。下面是使用getiaddrs函数获取网卡信息的C语言代码实现。
/*============================================================================ Name : Exercise.cbp Author : Haier Version : 1.01 Copyright : Copyright (c) 2014 Description : File Encryption in C, Ansi-style, Compile by Code::Block ============================================================================*/ #include <stdio.h> #include <ifaddrs.h> #include <arpa/inet.h> int getSubnetMask() { struct sockaddr_in *sin = NULL; struct ifaddrs *ifa = NULL, *ifList; if (getifaddrs(&ifList) < 0) return -1; for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) { if(ifa->ifa_addr->sa_family == AF_INET) { printf("\n>>> interfaceName: %s\n", ifa->ifa_name); sin = (struct sockaddr_in *)ifa->ifa_addr; printf(">>> ipAddress: %s\n", inet_ntoa(sin->sin_addr)); sin = (struct sockaddr_in *)ifa->ifa_dstaddr; printf(">>> broadcast: %s\n", inet_ntoa(sin->sin_addr)); sin = (struct sockaddr_in *)ifa->ifa_netmask; printf(">>> subnetMask: %s\n", inet_ntoa(sin->sin_addr)); } } freeifaddrs(ifList); return 0; } int main(void) { getSubnetMask(); return 0; }
相关文章推荐
- Linux下获取所有可用网卡信息
- Linux下C获取所有可用网卡信息
- Linux下C获取所有可用网卡信息
- Linux下获取所有可用网卡信息
- Linux上获取所有网卡信息
- 获取本机所有网卡的网卡名、网卡描述、网卡MAC地址、网卡IP、网卡类型等信息及网线是否插入状态
- 获取本机所有网卡的信息
- linux下的shell命令的编写,以及java如何调用linux的shell命令(java如何获取linux上的网卡的ip信息)
- Linux下如何获取网卡信息
- Linux获取所有网口及其详细信息
- linux下的shell命令的编写,以及java怎样调用linux的shell命令(java怎样获取linux上的网卡的ip信息)
- Linux下利用ioctl函数获取网卡信息
- Linux获取网卡信息
- Linux下获取网卡信息
- Linux下利用ioctl函数获取网卡信息
- Linux下利用ioctl函数获取网卡信息
- 获取linux所有用户信息
- linux下libpcap 获取当前主机的网卡信息
- linux下获取所有网卡mac地址(C语言)
- linux 下获取所有网卡mac,name