Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息。
ioctl
首先看ioctl()用法
ioctl()原型如下:
#include <sys/ioctl.h>
int ioctl(int fd, int request, ...);
参数:
fd : 文件描述符
request: 表示要请求的信息。如IP地址、网络掩码等
... : 可变参数,后面的可变参数根据request而定
比如我们请求所有网络接口的清单:
struct ifconf IoCtlReq;
...
ioctl(Sock, SIOCGIFCONF, &IoCtlReq ) //请求得到的东西放到IoCtlReq里面去,需要传地址
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
typedef uint32_t uint32;
#define MAX_IF 10
int main(int argc, char *argv[])
{
struct ifreq ifVec[MAX_IF] = {0}; /* 用来保存所有接口 */
int sock = -1;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0 )) < 0 )
fprintf(stderr, "Error:%d, cannot open RAM;\n", sock);
struct ifconf ioIfConf;
ioIfConf.ifc_buf = (void *)ifVec;
ioIfConf.ifc_len = sizeof(ifVec);
printf("Len:%d\n", ioIfConf.ifc_len);
if (ioctl(sock, SIOCGIFCONF, &ioIfConf) < 0 ) /* 获取所有网络接口信息 */
fprintf(stderr, "Error:%d ioctl IFCONF\n", sock);
printf("Len:%d\n", ioIfConf.ifc_len); /* 和前面到len对比,发现ioctl修改里len到大小 */
struct ifreq *ifPt;
struct ifreq *ifEndPt;
ifPt = ifVec;
ifEndPt = (void *)((char *)ifVec + ioIfConf.ifc_len);
for (ifPt = ifVec; ifPt < ifEndPt; ifPt++) {
struct ifreq ifReq;
if (ifPt->ifr_addr.sa_family != AF_INET ) {
continue;
}
uint32 u32_addr, u32_mask;
/* 打印ip地址 */
char ipDotBuf[16], subnetDotBuf[16], maskDotBuf[16]; // 保存点分十进制到ip地址
u32_addr = ((struct sockaddr_in *)&ifPt->ifr_addr)->sin_addr.s_addr;
inet_ntop(AF_INET, &u32_addr, ipDotBuf, (socklen_t )sizeof(ipDotBuf));
printf("IP Address: %s\n", ipDotBuf);
/* 打印地址掩码 */
bzero(&ifReq, sizeof(struct ifreq));
memcpy(ifReq.ifr_name, ifPt->ifr_name, sizeof(ifReq.ifr_name));
if (ioctl(sock, SIOCGIFNETMASK, &ifReq ) < 0) {
fprintf(stderr, "Error: %d, cannot get mask\n", errno);
}
else {
u32_mask = ((struct sockaddr_in *)&ifReq.ifr_addr)->sin_addr.s_addr;
inet_ntop(AF_INET, &u32_mask, maskDotBuf, (socklen_t )sizeof(maskDotBuf));
printf("Mask: %s\n", maskDotBuf);
}
/* 打印MTU */
bzero(&ifReq, sizeof(struct ifreq));
memcpy(ifReq.ifr_name, ifPt->ifr_name, sizeof(ifReq.ifr_name));
if (ioctl(sock, SIOCGIFMTU, &ifReq ) < 0){
fprintf(stderr, "Error: %d, cannot get MTU\n", errno);
}
else {
printf("SIOCGIFMTU:%d\n", ifReq.ifr_mtu);
}
/* 其他信息的打印方式与掩码和MTU相同 */
}
}
运行结果如下: