使用ioctl的SIOCGIFCONF能夠讀取全部網卡信息。ioctl調用後返回指向ifconf的結構鏈表,其中包含了指向ifreq的結構指針。ifconf及ifreq定義在net/if.h中。編程
《UNIX網絡編程》中提供了get_ifi_info函數的實現方法,使用這種方式來獲取網絡信息。在LINUX下,這種方式不能得到IPV6的網卡信息。《UNIX網絡編程》中有以下描述:網絡
在支持IPV6的系統中,沒有關於對SIOCGIFCONF請求是否返回IPV6地址的標準。咱們給支持IPV6的新系統增長了一個case語句, 這是爲了預防萬一。問題在於ifreq中的聯合把返回的地址定義成一個通用的16字節套接口地址結構,適合16字節的IPV4 socket_in結構,但對於24字節的IPV6 socket_in6結構過小了。若是返回IPV6地址,將可能破環現有的在每一個ifreq結構中採用固定大小的套接口地址結構的代碼。socket
經測試,在fedor6-2.6.18kernel中沒法返回ipv6地址,事實上,返回的地址簇老是AF_INET,而並不是AF_INET6。
這種方法的實現代碼以下:
net_if.hide
#ifndef __NET_INF_H
#define __NET_INF_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define IFI_NAME 16
#define IFI_HADDR 8
typedef struct ifi_info
{
char ifi_name[IFI_NAME];
u_char ifi_haddr[IFI_HADDR];
u_short ifi_hlen;
short ifi_flags;
short ifi_myflags;
struct sockaddr *ifi_addr;
struct sockaddr *ifi_brdaddr;
struct sockaddr *ifi_dstaddr;
struct ifi_info *ifi_next;
}ifi_info;
#define IFI_ALIAS 1
struct ifi_info *get_ifi_info(int, int);
void free_ifi_info(struct ifi_info *);
#endif
net_if.cwordpress
#include "net_if.h"
ifi_info *get_ifi_info(int family, int doaliases)
{
ifi_info *ifi, *ifihead, **ifipnext;
int sockfd, len, lastlen, flags, myflags;
char *ptr, *buf, lastname[IFNAMSIZ], *cptr;
struct ifconf ifc;
struct ifreq *ifr, ifrcopy;
struct sockaddr_in *sinptr;
if ((sockfd=socket(family, SOCK_DGRAM, 0))<0)
{
printf("socket error.\n");
exit(1);
}
lastlen = 0;
len = 10*sizeof(struct ifreq);
while (1)
{
buf = (char*)malloc(len);
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (ioctl(sockfd, SIOCGIFCONF, &ifc)<0)
{
if (errno!=EINVAL||lastlen!=0)
{
printf("ioctl error.\n");
}
}
else
{
if (ifc.ifc_len == lastlen)
break;
lastlen = ifc.ifc_len;
}
len += 10*sizeof(struct ifreq);
free(buf);
}
ifihead = NULL;
ifipnext = &ifihead;
lastname[0] = 0;
for (ptr = buf; ptrifr->ifr_addr.sa_len?sizeof(struct sockaddr):ifr->ifr_addr.sa_len;
#else
switch (ifr->ifr_addr.sa_family)
{
#ifdef IPV6
case AF_INET6:
len = sizeof(struct sockaddr_in6);
break;
#endif
case AF_INET:
default:
len = sizeof(struct sockaddr);
break;
}
#endif
ptr += sizeof(ifr->ifr_name) + len;
if (ifr->ifr_addr.sa_family != family)
continue;
myflags = 0;
if ((cptr=strchr(ifr->ifr_name, ':'))!=NULL)
*cptr = 0;
if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ)==0)
{
if (doaliases == 0)
continue;
myflags = IFI_ALIAS;
}
memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
ifrcopy = *ifr;
ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
flags = ifrcopy.ifr_flags;
if ((flags&IFF_UP)==0)
continue;
/*
if ((flags&IFF_BROADCAST)==0)
continue;
*/
ifi = calloc(1, sizeof(struct ifi_info));
*ifipnext = ifi;
ifipnext = &ifi->ifi_next;
ifi->ifi_flags = flags;
ifi->ifi_myflags = myflags;
memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
ifi->ifi_name[IFI_NAME-1] = '\0';
switch (ifr->ifr_addr.sa_family)
{
case AF_INET:
sinptr = (struct sockaddr_in *)&ifr->ifr_addr;
if (ifi->ifi_addr == NULL)
{
ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in));
memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
#ifdef SIOCGIFBRDADDR
if (flags & IFF_BROADCAST)
{
ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy);
sinptr = (struct sockaddr_in *)&ifrcopy.ifr_broadaddr;
ifi->ifi_brdaddr = calloc(1, sizeof(struct sockaddr_in));
memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in));
}
#endif
#ifdef SIOCGIFDSTADDR
if (flags & IFF_POINTOPOINT)
{
ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy);
sinptr = (struct sockaddr_in*)&ifrcopy.ifr_dstaddr;
ifi->ifi_dstaddr = calloc(1, sizeof(struct sockaddr_in));
memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in));
}
#endif
}
break;
default:
break;
}
}
free(buf);
return(ifihead);
}
void free_ifi_info(ifi_info *ifihead)
{
ifi_info *ifi, *ifinext;
for (ifi=ifihead; ifi!=NULL; ifi=ifinext)
{
if (ifi->ifi_addr!=NULL)
free(ifi->ifi_addr);
if (ifi->ifi_brdaddr!=NULL)
free(ifi->ifi_brdaddr);
if (ifi->ifi_dstaddr!=NULL)
free(ifi->ifi_dstaddr);
ifinext = ifi->ifi_next;
free(ifi);
}
}
char *sock_ntop(const struct sockaddr *sa, socklen_t salen)
{
char portstr[7];
static char str[128];
switch (sa->sa_family)
{
case AF_INET:
{
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str))==NULL)
return NULL;
if (ntohs(sin->sin_port)!=0)
{
snprintf(portstr, sizeof(portstr), ".%d", ntohs(sin->sin_port));
strcat(str, portstr);
}
return str;
}
break;
case AF_INET6:
{
struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
if (inet_ntop(AF_INET6, &sin->sin6_addr, str, sizeof(str))==NULL)
return NULL;
if (ntohs(sin->sin6_port)!=0)
{
snprintf(portstr, sizeof(portstr), ".%d", ntohs(sin->sin6_port));
strcat(str, portstr);
}
return str;
}
break;
default:
return NULL;
break;
}
}
int main(int argc, char *argv[])
{
ifi_info *ifi, *ifihead;
struct sockaddr *sa;
u_char *ptr;
int i, family, doaliases;
if (argc!=3)
{
printf("usage: ./prifinfo
");
exit(1);
}
if (strcmp(argv[1], "inet4") == 0)
family = AF_INET;
#ifdef IPV6
else if (strcmp(argv[1], "inet6") == 0)
family =AF_INET6;
#endif
else
{
printf("invalid
");
exit(1);
}
doaliases = atoi(argv[2]);
for(ifihead = ifi = get_ifi_info(family, doaliases);
ifi!=NULL;ifi=ifi->ifi_next)
{
printf("%s:<", ifi->ifi_name);
if (ifi->ifi_flags&IFF_UP) printf("UP");
if (ifi->ifi_flags&IFF_BROADCAST) printf("BCAST");
if (ifi->ifi_flags&IFF_MULTICAST) printf("MCAST");
if (ifi->ifi_flags&IFF_LOOPBACK) printf("LOOP");
if (ifi->ifi_flags&IFF_POINTOPOINT) printf("P2P");
printf(">\n");
if ((i=ifi->ifi_hlen)>0)
{
ptr = ifi->ifi_haddr;
do
{
printf("%s%x", (i==ifi->ifi_hlen)?" ":":", *ptr++);
}while(--i>0);
printf("\n");
}
if ((sa=ifi->ifi_addr)!=NULL)
printf(" IP addr: %s\n",
sock_ntop(sa, sizeof(*sa)));
if ((sa=ifi->ifi_brdaddr)!=NULL)
printf(" broadcast addr: %s\n",
sock_ntop(sa, sizeof(*sa)));
if ((sa=ifi->ifi_dstaddr)!=NULL)
printf(" destnation addr: %s\n",
sock_ntop(sa, sizeof(*sa)));
}
free_ifi_info(ifihead);
exit(0);
}
使用gcc net_if.c -o net_if -DIPV6編譯,在IPV4模式下運行輸出爲:函數
[root@localhost net_if]./net_if inet4 1
lo:
IP addr: 127.0.0.1
eth1:
IP addr: 192.168.1.2
broadcast addr: 192.168.1.255
eth0:
IP addr: 192.168.125.99
broadcast addr: 192.168.125.255oop
執行./net_if inet6 1在輸出爲空。測試
第二種方式是使用getifaddrs函數獲取,須要包含ifaddrs.h頭文件,這種方式能夠得到IPV6地址,改寫的《UNIX網絡編程》中的get_ifi_info函數以下所示:指針
znet.h接口
#ifndef __ZNET_H__
#define __ZNET_H__
#include
#include
#include
#include
#include
#include
#include
#include
#define IFI_NAME 16 /* same as IFNAMSIZ in
*/
#define IFI_HADDR 8 /* allow for 64-bit EUI-64 in future */
struct ifi_info {
char ifi_name[IFI_NAME]; /* interface name, null-terminated */
short ifi_index; /* interface index */
short ifi_flags; /* IFF_xxx constants from
*/
struct sockaddr *ifi_addr; /* primary address */
struct sockaddr *ifi_brdaddr;/* broadcast address */
struct ifi_info *ifi_next; /* next of these structures */
};
struct ifi_info* get_ifi_info(int, int);
void free_ifi_info(struct ifi_info *);
#endif
znet.c
#include "znet.h"
struct ifi_info* get_ifi_info(int family, int doaliases) {
struct ifi_info *ifi, *ifihead, **ifipnext,*p;
struct sockaddr_in *sinptr;
struct sockaddr_in6 *sin6ptr;
struct ifaddrs *ifas;
// char addr[128];
int sockfd;
ifihead = NULL;
ifipnext = &ifihead;
if(getifaddrs(&ifas)!=0)
return ;
for(;ifas!=NULL;ifas=(*ifas).ifa_next) {
if (((*ifas).ifa_addr)->sa_family != family)
continue; // ignore if not desired address family
/*
printf("%s %d\n",(*ifas).ifa_name,((*ifas).ifa_addr)->sa_family);
if(((*ifas).ifa_addr)->sa_family!=AF_INET6)
inet_ntop(AF_INET,&(((struct sockaddr_in *)((*ifas).ifa_addr))->sin_addr),addr,sizeof(addr));
else
inet_ntop(AF_INET6,&(((struct sockaddr_in6 *)((*ifas).ifa_addr))->sin6_addr),addr,sizeof(addr));
printf("%s\t",addr);
printf("\n");
*/
ifi = (struct ifi_info*)calloc(1,sizeof(struct ifi_info));
*ifipnext = ifi;
ifipnext = &ifi->ifi_next;
ifi->ifi_flags = (*ifas).ifa_flags;
memcpy(ifi->ifi_name, (*ifas).ifa_name, IFI_NAME);
ifi->ifi_name[IFI_NAME-1] = '\0';
switch (((*ifas).ifa_addr)->sa_family) {
case AF_INET:
sinptr = (struct sockaddr_in *) (*ifas).ifa_addr;
ifi->ifi_addr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
#ifdef SIOCGIFBRDADDR
if (ifi->ifi_flags & IFF_BROADCAST) {
sinptr = (struct sockaddr_in *) (*ifas).ifa_broadaddr;
ifi->ifi_brdaddr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in));
memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in));
}
#endif
break;
case AF_INET6:
sin6ptr = (struct sockaddr_in6 *) (*ifas).ifa_addr;
ifi->ifi_addr = (struct sockaddr*)calloc(1, sizeof(struct sockaddr_in6));
memcpy(ifi->ifi_addr, sin6ptr, sizeof(struct sockaddr_in6));
break;
default:
break;
}
}
freeifaddrs(ifas);
return(ifihead);
}
int main(int argc, char *argv[]) {
int family;
if (argc!=2) {
printf("usage: ./znet
\n");
exit(1);
}
if (strcmp(argv[1], "inet4") == 0)
family = AF_INET;
else if (strcmp(argv[1], "inet6") == 0)
family =AF_INET6;
else {
printf("invalid
\n");
exit(1);
}
char addr[128];
struct ifi_info *ifi, *ifihead;
printf("name\tflag\tIP\t\tbroadcastaddr\n");
for (ifihead = ifi = get_ifi_info(family,1); ifi != NULL; ifi = ifi->ifi_next) {
printf("%s\t",ifi->ifi_name);
printf("%d\t",ifi->ifi_flags);
if((ifi->ifi_addr)->sa_family!=AF_INET6)
inet_ntop(AF_INET,&(((struct sockaddr_in *)(ifi->ifi_addr))->sin_addr),addr,sizeof(addr));
else
inet_ntop(AF_INET6,&(((struct sockaddr_in6 *)(ifi->ifi_addr))->sin6_addr),addr,sizeof(addr));
printf("%s\t",addr);
#ifdef SIOCGIFBRDADDR
if ((ifi->ifi_flags & IFF_BROADCAST) && (ifi->ifi_addr)->sa_family!=AF_INET6) {
inet_ntop(AF_INET,&(((struct sockaddr_in *) (ifi->ifi_brdaddr))->sin_addr),addr,sizeof(addr));
printf("%s\t",addr);
}
#endif
printf("\n+++++++++++++++++++++++++++++++++++++++++++\n");
}
return 0;
}
這段代碼輸出以下:
[root@localhost net_if]./znet inet4 name flag IP broadcastaddr lo 73 127.0.0.1 ++++++++++++++++++++++++++++++ eth1 4099 192.168.1.2 192.168.1.255 ++++++++++++++++++++++++++++++ eth0 4163 192.168.125.99 192.168.125.255 ++++++++++++++++++++++++++++++ [root@localhost net_if]./znet inet6 name flag IP broadcastaddr lo 73 ::1 ++++++++++++++++++++++++++++++ eth1 4163 2001:250:1800:1::1 ++++++++++++++++++++++++++++++ eth0 4163 2001:250:1888:1::1 ++++++++++++++++++++++++++++++