title: C語言獲取網卡狀態
date: 2020/2/25 17:29:28
toc: true
C語言獲取網卡狀態
http://www.360doc.com/content/17/0510/17/8335678_652752795.shtml
struct ifreq
這個結構定義在include/net/if.h,用來配置ip地址,激活接口,配置MTU等接口信息的
struct ifconf
通常是用來保存所有接口信息的
應用
想要獲取當前網口網線插入狀態,需要用到ifreq結構體,獲取網卡的信息,然后socket結合網卡驅動的ioctl,就可以得到與網線插入狀態相關的數據。
用ioctl獲得本地ip地址時要用到兩個結構體ifconf和ifreq,它們對於大多數人來說都是比較陌生的,這里給大家一種比較簡單的理解方法,當然只一種幫助理解的方法,在描述中可能會有一些地方與真實定義有所出入,僅供參考.
首先先認識一下ifconf和ifreq:
//ifconf通常是用來保存所有接口信息的
//if.h
struct ifconf
{
int ifc_len; /* size of buffer */
union
{
char *ifcu_buf; /* input from user->kernel*/
struct ifreq *ifcu_req; /* return from kernel->user*/
} ifc_ifcu;
};
#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
#define ifc_req ifc_ifcu.ifcu_req /* array of structures */
//ifreq用來保存某個接口的信息
//if.h
struct ifreq
{
char ifr_name[IFNAMSIZ];
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
short ifru_flags;
int ifru_metric;
caddr_t ifru_data;
} ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr
上邊這兩個結構看起來比較復雜,我們現在把它們簡單化一些:比如說現在我們向實現獲得本地IP的功能。
我們的做法是:
- 先通過ioctl獲得本地所有接口的信息,並保存在ifconf中
- 再從ifconf中取出每一個ifreq中表示ip地址的信息
具體使用時我們可以認為ifconf就有兩個成員:ifc_len 和ifc_buf, 如圖一所示:
ifc_len:表示用來存放所有接口信息的緩沖區長度
ifc_buf:表示存放接口信息的緩沖區
所以我們需要在程序開始時對ifconf的ifc_len和ifc_buf進行初始化
接下來使用ioctl獲取所有接口信息,完成后ifc_len內存放實際獲得的接口信息總長度
並且信息被存放在ifc_buf中。
如下圖示:(假設讀到兩個接口信息)
接下來我們只需要從一個一個的接口信息獲取ip地址信息即可。
下面有一個簡單的參考:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/if.h>
int main()
{
int i=0;
int sockfd;
struct ifconf ifconf;
unsigned char buf[512];
struct ifreq *ifreq;
//初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
{
perror("socket" );
exit(1);
}
ioctl(sockfd, SIOCGIFCONF, &ifconf); //獲取所有接口信息
//接下來一個一個的獲取IP地址
ifreq = (struct ifreq*)buf;
for (i=(ifconf.ifc_len/sizeof (struct ifreq)); i>0; i--)
{
// if(ifreq->ifr_flags == AF_INET){ //for ipv4
printf("name = [%s]/n" , ifreq->ifr_name);
printf("local addr = [%s]/n" ,inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
ifreq++;
// }
}
return 0;
}
完整的代碼
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include <errno.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <linux/ethtool.h>
int cshell_netlink_status(const char *if_name)
{
char buffer[BUFSIZ];
char cmd[100];
FILE *read_fp;
int chars_read;
int ret =0;
memset( buffer, 0, BUFSIZ );
memset( cmd, 0, 100 );
sprintf(cmd, "ifconfig -a | grep %s",if_name);
read_fp = popen(cmd, "r");
if ( read_fp != NULL )
{
chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
pclose(read_fp);
if (chars_read > 0)
{
ret = 1;
}
else
{
fprintf(stderr, "DEVICE_NONE\r\n");
return 0;
}
}
if(ret == 1)
{
memset( buffer, 0, BUFSIZ );
memset( cmd, 0, 100 );
sprintf(cmd, "ifconfig |grep %s",if_name);
read_fp = popen(cmd, "r");
if ( read_fp != NULL )
{
chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
pclose(read_fp);
if (chars_read > 0)
{
ret = 2;
}
else
{
fprintf(stderr, "DEVICE_DOWN\r\n");
return 1;
}
}
}
if(ret == 2)
{
memset( buffer, 0, BUFSIZ );
memset( cmd, 0, 100 );
sprintf(cmd, "ifconfig %s | grep RUNNING | awk '{print $3}'",if_name);
read_fp = popen(cmd, "r");
if ( read_fp != NULL )
{
chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
pclose(read_fp);
if (chars_read > 0)
{
fprintf(stderr, "DEVICE_LINKED\r\n");
return 3;
}
else
{
fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
return 2;
}
}
}
return -1;
}
int c_netlink_status(const char *if_name )
{
int fd = -1;
struct ifreq ifr;
struct ifconf ifc;
struct ifreq ifrs_buf[100];
int if_number =0;
int i;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
fprintf(stderr, "%s: socket error [%d] %s\r\n",if_name, errno, strerror(errno));
close(fd);
return -1;
}
ifc.ifc_len = sizeof(ifrs_buf);
ifc.ifc_buf = (caddr_t)ifrs_buf;
if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) <0)
{
fprintf(stderr, "%s: ioctl SIOCGIFCONF error [%d] %s\r\n",if_name, errno, strerror(errno));
close(fd);
return -1;
}
if_number = ifc.ifc_len / sizeof(struct ifreq);
for(i=0; i< if_number; i++)
{
if(strcmp(if_name,ifrs_buf[i].ifr_name ) == 0)
{
break;
}
}
if(i >= if_number)
{
close(fd);
fprintf(stderr, "DEVICE_NONE\r\n");
return 0;
}
bzero(&ifr, sizeof(ifr));
strncpy(ifr.ifr_name, if_name, IFNAMSIZ-1);
ifr.ifr_name[IFNAMSIZ-1] = 0;
if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) <0)
{
fprintf(stderr, "%s: ioctl SIOCGIFFLAGS error [%d] %s\r\n",if_name, errno, strerror(errno));
close(fd);
return -1;
}
#if 1
if(!(ifr.ifr_flags & IFF_UP))
{
close(fd);
fprintf(stderr, "DEVICE_DOWN\r\n");
return 1;
}
if(!(ifr.ifr_flags & IFF_RUNNING))
{
close(fd);
fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
return 2 ;
}
fprintf(stderr, "DEVICE_LINKED\r\n");
return 3;
#else
{
struct ethtool_value edata;
if(!(ifr.ifr_flags & IFF_UP) || !(ifr.ifr_flags & IFF_RUNNING))
{
close(fd);
fprintf(stderr, "%s: DOWN\r\n",if_name);
return 1;
}
edata.cmd = ETHTOOL_GLINK;
edata.data = 0;
ifr.ifr_data = (char *) &edata;
if(ioctl( fd, SIOCETHTOOL, &ifr ) < 0)
{
fprintf(stderr, "%s: ioctl SIOCETHTOOL error [%d] %s\r\n",if_name, errno, strerror(errno));
close(fd);
return -1;
}
if(edata.data == 0)
{
fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
return 2;
}
else
{
fprintf(stderr, "DEVICE_LINKED\r\n");
return 3;
}
}
#endif
}