转自:http://blog.csdn.net/joker0910/article/details/7855998
ifconfig是我们查看/设定网口状态常用的命令,其实这个命令就是对一些系统函数的封装,通过对struct ifreq的修改,我们可以很轻易地设定网口状态,获取网口状态。
这个结构体位于<net/if.h>头文件中,打开去看看吧,对照着英文注释,很容易找到自己想要的东西。而网卡相关的一些ioctl操作, 也就是用于查询/修改状态的标记位,都在<linux/sockios.h>中,很清晰的,找份源码看看吧。下面是一个小示例,演示了获取信息的过程,如果有需要,也可以启用那个修改状态的函数,注意,修改状态标记位的时候,需要用到超级用户的权限。
- /**
- * \file getifstat.c
- * \author wzj
- * \brief 访问这个struct ifconf 修改,查询状态
- * \version
- * \note
- * \date: 2012年08月11日星期六22:55:25
- */
- #include <net/if.h> /* for ifconf */
- #include <linux/sockios.h> /* for net status mask */
- #include <netinet/in.h> /* for sockaddr_in */
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <stdio.h>
- #define MAX_INTERFACE (16)
- void port_status(unsigned int flags);
- /* set == 0: do clean , set == 1: do set! */
- int set_if_flags(char *pif_name, int sock, int staus, int set)
- {
- struct ifreq ifr;
- int ret = 0;
- strncpy(ifr.ifr_name, pif_name, strlen(pif_name) + 1);
- ret = ioctl(sock, SIOCGIFFLAGS, &ifr);
- if(ret)
- return -1;
- /* set or clean */
- if(set)
- ifr.ifr_flags |= status;
- else
- ifr.ifr_flags &= ~status;
- /* set flags */
- ret = ioctl(sock, SIOCSIFFLAGS, &ifr);
- if(ret)
- return -1;
- return 0;
- }
- int get_if_info(int fd)
- {
- struct ifreq buf[MAX_INTERFACE];
- struct ifconf ifc;
- int ret = 0;
- int if_num = 0;
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = (caddr_t) buf;
- ret = ioctl(fd, SIOCGIFCONF, (char*)&ifc);
- if(ret)
- {
- printf("get if config info failed");
- return -1;
- }
- /* 网口总数 ifc.ifc_len 应该是一个出入参数 */
- if_num = ifc.ifc_len/sizeof(struct ifreq);
- printf("interface num is interface = %d\n", if_num);
- while(if_num-- > 0)
- {
- printf("net device: %s\n", buf[if_num].ifr_name);
- /* 获取第n个网口信息 */
- ret = ioctl(fd, SIOCGIFFLAGS, (char*)&buf[if_num]);
- if(ret)
- continue;
- /* 获取网口状态 */
- port_status(buf[if_num].ifr_flags);
- /* 获取当前网卡的ip地址 */
- ret = ioctl(fd, SIOCGIFADDR, (char*)&buf[if_num]);
- if(ret)
- continue;
- printf("IP address is: \n%s\n", inet_ntoa(((struct sockaddr_in *)(&buf[if_num].ifr_addr))->sin_addr));
- /* 获取当前网卡的mac */
- ret = ioctl(fd, SIOCGIFHWADDR, (char*)&buf[if_num]);
- if(ret)
- continue;
- printf("%02x:%02x:%02x:%02x:%02x:%02x\n\n",
- (unsigned char)buf[if_num].ifr_hwaddr.sa_data[0],
- (unsigned char)buf[if_num].ifr_hwaddr.sa_data[1],
- (unsigned char)buf[if_num].ifr_hwaddr.sa_data[2],
- (unsigned char)buf[if_num].ifr_hwaddr.sa_data[3],
- (unsigned char)buf[if_num].ifr_hwaddr.sa_data[4],
- (unsigned char)buf[if_num].ifr_hwaddr.sa_data[5]
- );
- }
- }
- void port_status(unsigned int flags)
- {
- if(flags & IFF_UP)
- {
- printf("is up\n");
- }
- if(flags & IFF_BROADCAST)
- {
- printf("is broadcast\n");
- }
- if(flags & IFF_LOOPBACK)
- {
- printf("is loop back\n");
- }
- if(flags & IFF_POINTOPOINT)
- {
- printf("is point to point\n");
- }
- if(flags & IFF_RUNNING)
- {
- printf("is running\n");
- }
- if(flags & IFF_PROMISC)
- {
- printf("is promisc\n");
- }
- }
- int main()
- {
- int fd;
- fd = socket(AF_INET, SOCK_DGRAM, 0);
- if(fd > 0)
- {
- get_if_info(fd);
- close(fd);
- }
- return 0;
- }