本想通過ioctl來擷取所有的接口,後來發現隻能擷取已有ip的接口。
可以通過ioctl擷取的網絡接口參數
request參數 | 說明 | 類型 |
SIOCGIFCONF | 擷取所有接口資訊 | struct ifconf |
SIOCGIFADDR | 擷取接口位址 | struct ifreq |
SIOCGIFFLAGS | 擷取接口狀态 | struct ifreq |
SIOCGIFNETMASK | 擷取子網路遮罩 | struct ifreq |
SIOCGIFBRDADDR | 擷取廣播位址 | struct ifreq |
先放結果
eth0接口無ip位址

eth0接口有ip位址
代碼:
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
main()
{
int socketfd;
struct ifconf ifc;
struct ifreq *ifr, if_hwaddr;
int i;
char buff[1024];
socketfd=socket(AF_INET,SOCK_DGRAM,0);
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if (ioctl(socketfd, SIOCGIFCONF, &ifc) < 0) {
return -1;
}
ifr = ifc.ifc_req;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0;ifr++)
{
printf("--interface:%s-----------\n",ifr->ifr_name);
}
}