struct ifconf和struct ifreq,获取网线插入状态
这两天看用C获取当前网口的插入网线状态的程序,遇见了这两个不熟悉的结构体,看了头文件中的说明和详细。
struct ifreq
这个结构定义在include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的
- /* 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
- # define IFNAMSIZ IF_NAMESIZE
- union
- {
- char ifrn_name[IFNAMSIZ]; /* Interface 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 int ifru_flags;
- int ifru_ivalue;
- int ifru_mtu;
- struct ifmap ifru_map;
- char ifru_slave[IFNAMSIZ]; /* Just fits the size */
- char ifru_newname[IFNAMSIZ];
- __caddr_t ifru_data;
- } 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 _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
- # define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
- # define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)
struct ifconf
通常是用来保存所有接口信息的
- /* 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
- {
- __caddr_t ifcu_buf;
- struct ifreq *ifcu_req;
- } ifc_ifcu;
- };
- # define ifc_buf ifc_ifcu.ifcu_buf /* Buffer address. */
- # define ifc_req ifc_ifcu.ifcu_req /* Array of structures. */
- # define _IOT_ifconf _IOT(_IOTS(struct ifconf),1,0,0,0,0) /* not right */
应用
想要获取当前网口网线插入状态,需要用到ifreq结构体,获取网卡的信息,然后socket结合网卡驱动的ioctl,就可以得到与网线插入状态相关的数据。
另外推荐一个百度文库的文章,包含代码
http://wenku.baidu.com/view/59f4508d680203d8ce2f2412.html
struct ifconf和struct ifreq,获取网线插入状态的更多相关文章
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...
- linux网络接口,struct ifreq struct ifconf结构
网络相关的ioctl请求的request参数及arg地址必须指向的数据类型如下表所示: 接口 SIOCGIFCONF SIOCSIFADDR SIOCGIFADDR SIOCSIFBRDADDR SI ...
- struct ifreq 获取IP 和mac和修改mac
2012-09-11 14:26 struct ifreq 获取IP 和mac和修改mac 配置ip地址和mask地址: ifconfig eth0 192.168.50.22 netmask 25 ...
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- gettimeofday(struct timeval *tv, struct timezone *tz)函数
gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...
- Go基础系列:struct和嵌套struct
struct struct定义结构,结构由字段(field)组成,每个field都有所属数据类型,在一个struct中,每个字段名都必须唯一. 说白了就是拿来存储数据的,只不过可自定义化的程度很高,用 ...
- linux高精度struct timespec 和 struct timeval
一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...
- [转载]彻底弄清struct和typedef struct
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
- struct和typedef struct彻底明白了
struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...
随机推荐
- 《APUE》第四章笔记(2)
下面介绍对stat结构的各个成员的操作函数. 先贴个stat结构的图: access函数: #include <unistd.h> int access(const char *pathn ...
- PC软件-实用工具 True Launch Bar
True Launch Bar 官网 增强及自定义window任务栏快捷方式管理. 有免费版的Free Launch Bar 官网
- PHP页面中文乱码分析
php出现出现乱码的原因:页面文件的编码方式(.html,.php等).html.head中指定浏览器的编码方式.MySql数据库传输的编码方式.Apache字符集. PHP页面中文乱码出现的原因有几 ...
- js设置cookie过期及清除浏览器对应名称的cookie
js设置cookie过期也就相当于清除浏览器对应名称的cookie的例子. 代码: function ClearCookie() { var expires = new Date(); expir ...
- Linux下GPIO驱动(二) ----s3c_gpio_cfgpin();gpio_set_value();
首先来看s3c_gpio_cfgpin(); int s3c_gpio_cfgpin(unsigned int pin, unsigned int config) { struct s3c_gpio_ ...
- import information website
1. 一个很好的展示如何review paper和response to reviewer的网站:openview 该网站给出了许多paper review的例子(如何你是reviewer,那么可以参 ...
- maven编译的时候排除junit测试类
maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...
- C#压缩、解压缩文件(夹)(rar、zip)
主要是使用Rar.exe压缩解压文件(夹)(*.rar),另外还有使用SevenZipSharp.dll.zLib1.dll.7z.dll压缩解压文件(夹)(*.zip).需要注意的几点如下: 1.注 ...
- Javascript与C#相互调用
javascript调用C#代码 前台js调用后台代码方法,此方法只适用于后台方法的修饰符为public或protected,如果是其他修饰符会报错. >后台C#代码: protected in ...
- POJ 3308 Paratroopers(最小割EK)
题目链接 题意 : 有一个n*m的矩阵,L个伞兵可能落在某些点上,这些点的坐标已知,需要在某些位置安上一些枪,然后每个枪可以将一行或者一列的伞兵击毙.把这种枪安装到不同行的行首.或者不同列的列首,费用 ...