这两天看用C获取当前网口的插入网线状态的程序,遇见了这两个不熟悉的结构体,看了头文件中的说明和详细。

struct ifreq

这个结构定义在include/net/if.h,用来配置ip地址,激活接口,配置MTU等接口信息的

  1. /* Interface request structure used for socket ioctl's.  All interface
  2. ioctl's must have parameter definitions which begin with ifr_name.
  3. The remainder may be interface specific.  */
  4. struct ifreq
  5. {
  6. # define IFHWADDRLEN    6
  7. # define IFNAMSIZ   IF_NAMESIZE
  8. union
  9. {
  10. char ifrn_name[IFNAMSIZ];   /* Interface name, e.g. "en0".  */
  11. } ifr_ifrn;
  12. union
  13. {
  14. struct sockaddr ifru_addr;
  15. struct sockaddr ifru_dstaddr;
  16. struct sockaddr ifru_broadaddr;
  17. struct sockaddr ifru_netmask;
  18. struct sockaddr ifru_hwaddr;
  19. short int ifru_flags;
  20. int ifru_ivalue;
  21. int ifru_mtu;
  22. struct ifmap ifru_map;
  23. char ifru_slave[IFNAMSIZ];  /* Just fits the size */
  24. char ifru_newname[IFNAMSIZ];
  25. __caddr_t ifru_data;
  26. } ifr_ifru;
  27. };
  28. # define ifr_name   ifr_ifrn.ifrn_name  /* interface name   */
  29. # define ifr_hwaddr ifr_ifru.ifru_hwaddr    /* MAC address      */
  30. # define ifr_addr   ifr_ifru.ifru_addr  /* address      */
  31. # define ifr_dstaddr    ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */
  32. # define ifr_broadaddr  ifr_ifru.ifru_broadaddr /* broadcast address    */
  33. # define ifr_netmask    ifr_ifru.ifru_netmask   /* interface net mask   */
  34. # define ifr_flags  ifr_ifru.ifru_flags /* flags        */
  35. # define ifr_metric ifr_ifru.ifru_ivalue    /* metric       */
  36. # define ifr_mtu    ifr_ifru.ifru_mtu   /* mtu          */
  37. # define ifr_map    ifr_ifru.ifru_map   /* device map       */
  38. # define ifr_slave  ifr_ifru.ifru_slave /* slave device     */
  39. # define ifr_data   ifr_ifru.ifru_data  /* for use by interface */
  40. # define ifr_ifindex    ifr_ifru.ifru_ivalue    /* interface index      */
  41. # define ifr_bandwidth  ifr_ifru.ifru_ivalue    /* link bandwidth   */
  42. # define ifr_qlen   ifr_ifru.ifru_ivalue    /* queue length     */
  43. # define ifr_newname    ifr_ifru.ifru_newname   /* New name     */
  44. # define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
  45. # define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
  46. # define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)

struct  ifconf

通常是用来保存所有接口信息的

  1. /* Structure used in SIOCGIFCONF request.  Used to retrieve interface
  2. configuration for machine (useful for programs which must know all
  3. networks accessible).  */
  4. struct ifconf
  5. {
  6. int ifc_len;            /* Size of buffer.  */
  7. union
  8. {
  9. __caddr_t ifcu_buf;
  10. struct ifreq *ifcu_req;
  11. } ifc_ifcu;
  12. };
  13. # define ifc_buf    ifc_ifcu.ifcu_buf   /* Buffer address.  */
  14. # define ifc_req    ifc_ifcu.ifcu_req   /* Array of structures.  */
  15. # 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,获取网线插入状态的更多相关文章

  1. 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf

    转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq  结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...

  2. linux网络接口,struct ifreq struct ifconf结构

    网络相关的ioctl请求的request参数及arg地址必须指向的数据类型如下表所示: 接口 SIOCGIFCONF SIOCSIFADDR SIOCGIFADDR SIOCSIFBRDADDR SI ...

  3. 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 ...

  4. struct timespec 和 struct timeval

    time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...

  5. gettimeofday(struct timeval *tv, struct timezone *tz)函数

    gettimeofday(struct timeval *tv, struct timezone *tz)函数 功能:获取当前精确时间(Unix时间) 其中: timeval为时间 truct tim ...

  6. Go基础系列:struct和嵌套struct

    struct struct定义结构,结构由字段(field)组成,每个field都有所属数据类型,在一个struct中,每个字段名都必须唯一. 说白了就是拿来存储数据的,只不过可自定义化的程度很高,用 ...

  7. linux高精度struct timespec 和 struct timeval

    一.struct timespec 定义: typedef long time_t;#ifndef _TIMESPEC#define _TIMESPECstruct timespec {time_t ...

  8. [转载]彻底弄清struct和typedef struct

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  9. struct和typedef struct彻底明白了

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

随机推荐

  1. Apache(一)

    以下介绍是基于centos,Apache/2.2.15,进行解释 (资料来自www.netcraft.com) Apache 模块化的软件 Apache=core+module(内核+模块) core ...

  2. 【HeadFirst设计模式】10.状态模式

    定义: 允许对象在内部状态改变时改变它 行为,对象看起来好像修改了它的类. OO原则: 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 为交互对象之间的松耦合设计而努力 类应该对扩展开放, ...

  3. Newtonsoft.Json.dll反序列化JSON字符串的方法

      1.直接反序列化JSON字符串 //引用序列化.反序列化JSON字符串用到的空间 using Newtonsoft.Json; using Newtonsoft.Json.Linq; //定义一个 ...

  4. js模拟苹果菜单

    模拟苹果菜单的js代码是从网上看到的,用来做导航菜单还是蛮好看的.这里借鉴一下. 效果描述:当鼠标移动离哪个图片最近的时候,这个图片最大,鼠标离的图片越远,则图片越小: 原理:主要用到了三角形的勾股定 ...

  5. platform平台设备驱动简化示例代码

    driver.c: #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h& ...

  6. [Python][flask][flask-login]关于flask-login中各种API使用实例

    本篇博文跟上一篇[Python][flask][flask-wtf]关于flask-wtf中API使用实例教程有莫大的关系. 简介:Flask-Login 为 Flask 提供了用户会话管理.它处理了 ...

  7. WindowsPhone客户端第一次审核失败记录

    微软返回失败pdf,其中2.7.2和2.7.3没有通过,原因是: 1. 没有提供隐私策略,所以在提交App要填写的隐私策略url里加了隐私策略的网址链接. 2. 由于使用了定位服务,所以第一次进入应用 ...

  8. css3360度旋转动画

    @-webkit-keyframes Parallaxs{ from { -webkit-transform: rotate(180deg) ; -moz-transform: rotate(180d ...

  9. cursor:hand与cursor:pointer的区别介绍

    cursor:hand 与 cursor:pointer 的效果是一样的,都像光标指向链接一样,光标变成手行. cursor:hand :IE完全支持.但是在firefox是不支持的,没有效果. cu ...

  10. srvctl 命令

    SRVCTL命令可以控制RAC数据库中的instance,listener以及services.通常SRVCTL在ORACLE用户下执行.下面我们来介绍srvctl命令.1.通过SRVCTL命令来st ...