linux c 获取网络接口信息 ioct l函数 ifreq ifconf 结构体 简介-程序员宅基地

目录

ioctl

struct ifreq

struct ifconf


 

ioctl

原型如下:

#include <sys/ioctl.h>
int ioctl(int fd, int request, ...);

参数:

    fd     : 文件描述符

request:  表示要请求的信息。如IP地址、网络掩码等

     ...     :  后面的可变参数根据request而定

比如我们请求所有网络接口的清单:

struct ifconf IoCtlReq;
...
ioctl( Sock, SIOCGIFCONF, &IoCtlReq )

与接口相关的request如下表所示

SIOCGIFCONF

SIOCSIFADDR

SIOCGIFADDR

SIOCSIFFLAGS

SIOCGIFFLAGS

SIOCSIFDSTADDR

SIOCGIFDSTADDR

SIOCGIFBRDADDR

SIOCSIFBRDADDR

SIOCGIFNETMASK

SIOCSIFNETMASK

SIOCGIFMETRIC

SIOCSIFMETRIC

SIOCGIFMTU

SIOCxxx

获取所有接口的清单

设置接口地址

获取接口地址

设置接口标志

获取接口标志

设置点到点地址

获取点到点地址

获取广播地址

设置广播地址

获取子网掩码

设置子网掩码

获取接口的测度

设置接口的测度

获取接口MTU

(还有很多取决于系统的实现)

struct ifconf

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

struct ifreq

 

 

struct ifreq

结构体 struct ifreq用来保存某个接口的信息。

// if.h
/*
 * Interface request structure used for socket
 * ioctl's.  All interface ioctl's must have parameter
 * definitions which begin with ifr_name.  The
 * remainder may be interface specific.
 */
struct ifreq {
#define IFHWADDRLEN	6
	union
	{
		char	ifrn_name[IFNAMSIZ];		/* if name, e.g. "en0" */
	} ifr_ifrn;
	
	union {
		struct	sockaddr ifru_addr;
		struct	sockaddr ifru_dstaddr;
		struct	sockaddr ifru_broadaddr;
		struct	sockaddr ifru_netmask;
		struct  sockaddr ifru_hwaddr;
		short	ifru_flags;
		int	ifru_ivalue;
		int	ifru_mtu;
		struct  ifmap ifru_map;
		char	ifru_slave[IFNAMSIZ];	/* Just fits the size */
		char	ifru_newname[IFNAMSIZ];
		void __user *	ifru_data;
		struct	if_settings ifru_settings;
	} ifr_ifru;
};
#define ifr_name	ifr_ifrn.ifrn_name	/* interface name 	*/
#define ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address 		*/
#define	ifr_addr	ifr_ifru.ifru_addr	/* address		*/
#define	ifr_dstaddr	ifr_ifru.ifru_dstaddr	/* other end of p-p lnk	*/
#define	ifr_broadaddr	ifr_ifru.ifru_broadaddr	/* broadcast address	*/
#define	ifr_netmask	ifr_ifru.ifru_netmask	/* interface net mask	*/
#define	ifr_flags	ifr_ifru.ifru_flags	/* flags		*/
#define	ifr_metric	ifr_ifru.ifru_ivalue	/* metric		*/
#define	ifr_mtu		ifr_ifru.ifru_mtu	/* mtu			*/
#define ifr_map		ifr_ifru.ifru_map	/* device map		*/
#define ifr_slave	ifr_ifru.ifru_slave	/* slave device		*/
#define	ifr_data	ifr_ifru.ifru_data	/* for use by interface	*/
#define ifr_ifindex	ifr_ifru.ifru_ivalue	/* interface index	*/
#define ifr_bandwidth	ifr_ifru.ifru_ivalue    /* link bandwidth	*/
#define ifr_qlen	ifr_ifru.ifru_ivalue	/* Queue length 	*/
#define ifr_newname	ifr_ifru.ifru_newname	/* New name		*/
#define ifr_settings	ifr_ifru.ifru_settings	/* Device/proto settings*/

ifr_name 标识了某一接口。

可以通过ioctl获取该接口的信息。如:

ioctl(Sock, SIOCGIFNETMASK, &IfReq);//获取网络接口地址掩码

该代码需要先对IfReq->ifr_name赋值,然后获取与IfReq->ifr_name向匹配的网络接口 的地址掩码

 

struct ifconf

结构体struct ifconf通常用来保存所有接口信息

// if.h
/*
 * Structure used in SIOCGIFCONF request.
 * Used to retrieve interface configuration
 * for machine (useful for programs which
 * must know all networks accessible).
 */
struct ifconf  {
	int	ifc_len;			/* size of buffer	*/
	union {
		char __user *ifcu_buf;
		struct ifreq __user *ifcu_req;
	} ifc_ifcu;
};
#define	ifc_buf	ifc_ifcu.ifcu_buf		/* buffer address	*/
#define	ifc_req	ifc_ifcu.ifcu_req		/* array of structures	*/

该结构体可以用来获取所哟网络接口的名字和信息(不是全部信息,是ip地址)

实例:

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.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()
{
    struct ifreq ifVec[MAX_IF];//用来保存所有接口

    int sock = -1;

    if ((sock = socket( AF_INET, SOCK_DGRAM, 0 )) < 0)
    {
        fprintf(stderr, "Error:%d, cannot open RAM;\n");
    }

    // get if vector
    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");
    }

    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;
            }

            // Temp keepers of interface params...
            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相同    */
        }
    }
}

运行结果:

windeal@ubuntu:~/Windeal/apue$ ./exe 
Len:320
Len:64
IP Address: 127.0.0.1
Mask: 255.0.0.0
SIOCGIFMTU:16436
IP Address: 172.17.92.198
Mask: 255.255.254.0
SIOCGIFMTU:1500
windeal@ubuntu:~/Windeal/apue$ 

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/whatday/article/details/106470305

智能推荐

7-10 集合划分 (10 分)_7个元素的集合有多少种划分-程序员宅基地

文章浏览阅读973次,点赞3次,收藏3次。当n=4 时,集合{1,2,3,4}可以划分为15个不同的非空子集如下:{{1},{2},{3},{4}},{{1,2},{3},{4}},{{1,3},{2},{4}},{{1,4},{2},{3}},{{2,3},{1},{4}},{{2,4},{1},{3}},{{3,4},{1},{2}},{{1,2},{3,4}},{{1,3},{2,4}},{{1,4},{2,3}},{{1,2,3},{4}},{{1,2,4},{3}},{{1,3,4},{2}},{{2,3,4}_7个元素的集合有多少种划分

全网唯一的echarts树状图——单个节点样式修改_echarts 怎么在修改节点文本使用dom模板-程序员宅基地

文章浏览阅读6.2k次,点赞3次,收藏11次。前段时间一直在用Echarts做一个树状图,本来是挺简单的,Echarts官网也有,不过官网上的节点数据是封装在一个Json文件中去,这里就不再赘述,有兴趣自行去官网查看链接:https://echarts.apache.org/examples/zh/editor.html?c=tree-basicJson数据链接:https://echarts.apache.org/examples/da..._echarts 怎么在修改节点文本使用dom模板

php include传参数,解决PHP的include带参数的有关问题-程序员宅基地

文章浏览阅读1k次。php的include可以载入一个外部php文件,其参数是一个文件名,既然是文件名,就不能带参数了。php中,如果include载入的文件一定要带参数,就只有一种办法,即这个参数是一个全路径的URL地址。例如:include "http://localhost/aaa.php?id=1"; //正确include "aaa.php?id=1"; //错误其实include本义就只是用来载入一个外部..._php include 传参

iOS越狱开发 常用检测Hook 代码 闪退检测断点_ios hook exit-程序员宅基地

文章浏览阅读1.8k次。__attribute__((constructor)) static void entry(){ rebind_symbols((struct rebinding[1]){{"abort", abort_hook, (void *)&abort_old}}, 1); rebind_symbols((struct rebinding[2]){{"exit", ..._ios hook exit

python转dll_python调用dll方法-程序员宅基地

文章浏览阅读376次。【转载】python调用dll方法python调用dll方法来自http://blog.csdn.net/lf8289/article/details/2322550分类:python2008-04-2412:276833人阅读评论(6)收藏举报在python中调用dll文件中的接口比较简单,实例代码如下:如我们有一个test.dll文件,内部定义如下:extern"C"{int__st..._python 转dll 第三方库

hihocoder 1388 Periodic Signal FFT-程序员宅基地

文章浏览阅读204次。最近做了几个FFT的题,实在是如果不记录一下就转头忘了 T T传送门题意:求思路:将上式拆开,那么就变成了,前两项都是固定的值那么我们求的最大值就可以了,n的数据范围为6*1e4,所以要用FFT优化下面是我现在的理解,如果有错还恰巧被大佬看到了,求指出T T如果有两个数组:A:1到n-1;B:1到m-1那么得到的卷积C:1到m+n-2,其中,注意这里

随便推点

JAVA-扫描局域网、自定义网段IP加端口在线设备_局域网设备扫描 java-程序员宅基地

文章浏览阅读4.6k次。JAVA-扫描局域网、自定义网段IP加端口在线设备_局域网设备扫描 java

《C语言及程序设计》实践参考——M$pszi$y是嘛意思-程序员宅基地

文章浏览阅读159次。返回:贺老师课程教学链接实践要求【项目1-M$pszi$y是嘛意思?】背景:小明让同学传纸条给小丽。小丽接到会心一笑,大家却不知所云。纸条上写着M$pszi$y,两人暗中约定是,真实字符为实际字符前面的第4个!M$pszi$y是神马意思?推算一下,或从ASCII码表中查一下,自然是I love u。(1)小明请你写一个程序,在给小丽写情书时,再不..._m$是哪编程语言的语法

MBUS应用笔记/主站-程序员宅基地

文章浏览阅读422次。MBUS主站,TSS721,SSP721_mbus

基于springboot旅游网站_基于springboot的旅游网站-程序员宅基地

文章浏览阅读849次。功能模块:旅游路线、旅游景点、旅游酒店、旅游车票、旅游保险、旅游策略、订单管理、留言管理、数据分析等等。实现了在线预订、统计数据分析等功能。技术路线:springboot、springmvc、maven、layui、mybatis数据库:MySQL系统录屏:链接: https://pan.baidu.com/s/1C519vCBP66ZaoDuEHSYxfw 提取码: gxui首页:后端系统:..._基于springboot的旅游网站

qiankun(乾坤)微前端框架简介_乾坤框架-程序员宅基地

文章浏览阅读4k次,点赞10次,收藏14次。qiankun(乾坤)微前端框架简介_乾坤框架

【图神经网络】 - GNN的几个模型及论文解析(NN4G、GAT、GCN)-程序员宅基地

文章浏览阅读1.3k次,点赞19次,收藏31次。图神经网络(Graph Neural Network,GNN)是指使用神经网络来学习图结构数据,提取和发掘图结构数据中的特征和模式,满足聚类、分类、预测、分割、生成等图学习任务需求的算法总称。