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

2023-12-26 05:58

本文主要是介绍获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://blog.csdn.net/windeal3203/article/details/39320605

Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq  结构体struct ifconf来获取网络接口的各种信息。


ioctl

首先看ioctl()用法
ioctl()原型如下:
   
#include <sys/ioctl.h>
int ioctl(int fd, int request, ...);
参数:
     fd     : 文件描述符
request:  表示要请求的信息。如IP地址、网络掩码等
       ...            后面的可变参数根据request而定

比如我们请求所有网络接口的清单:
[cpp]  view plain copy
  1. struct ifconf IoCtlReq;  
  2. ...  
  3. ioctl( Sock, SIOCGIFCONF, &IoCtlReq )  

其中 IoCtlReq 是一个

与接口相关的request如下表所示( 来源: < http://baike.baidu.com/view/1081282.htm?fr=aladdin > ):
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


关于ioctl的详细解释清查阅本博其它博文

struct ifreq

结构体 struct ifreq用来保存某个接口的信息。
[cpp]  view plain copy
  1. // if.h  
  2. /* 
  3.  * Interface request structure used for socket 
  4.  * ioctl's.  All interface ioctl's must have parameter 
  5.  * definitions which begin with ifr_name.  The 
  6.  * remainder may be interface specific. 
  7.  */  
  8. struct ifreq {  
  9. #define IFHWADDRLEN 6  
  10.     union  
  11.     {  
  12.         char    ifrn_name[IFNAMSIZ];        /* if name, e.g. "en0" */  
  13.     } ifr_ifrn;  
  14.       
  15.     union {  
  16.         struct  sockaddr ifru_addr;  
  17.         struct  sockaddr ifru_dstaddr;  
  18.         struct  sockaddr ifru_broadaddr;  
  19.         struct  sockaddr ifru_netmask;  
  20.         struct  sockaddr ifru_hwaddr;  
  21.         short   ifru_flags;  
  22.         int ifru_ivalue;  
  23.         int ifru_mtu;  
  24.         struct  ifmap ifru_map;  
  25.         char    ifru_slave[IFNAMSIZ];   /* Just fits the size */  
  26.         char    ifru_newname[IFNAMSIZ];  
  27.         void __user *   ifru_data;  
  28.         struct  if_settings ifru_settings;  
  29.     } ifr_ifru;  
  30. };  
  31. #define ifr_name    ifr_ifrn.ifrn_name  /* interface name   */  
  32. #define ifr_hwaddr  ifr_ifru.ifru_hwaddr    /* MAC address      */  
  33. #define ifr_addr    ifr_ifru.ifru_addr  /* address      */  
  34. #define ifr_dstaddr ifr_ifru.ifru_dstaddr   /* other end of p-p lnk */  
  35. #define ifr_broadaddr   ifr_ifru.ifru_broadaddr /* broadcast address    */  
  36. #define ifr_netmask ifr_ifru.ifru_netmask   /* interface net mask   */  
  37. #define ifr_flags   ifr_ifru.ifru_flags /* flags        */  
  38. #define ifr_metric  ifr_ifru.ifru_ivalue    /* metric       */  
  39. #define ifr_mtu     ifr_ifru.ifru_mtu   /* mtu          */  
  40. #define ifr_map     ifr_ifru.ifru_map   /* device map       */  
  41. #define ifr_slave   ifr_ifru.ifru_slave /* slave device     */  
  42. #define ifr_data    ifr_ifru.ifru_data  /* for use by interface */  
  43. #define ifr_ifindex ifr_ifru.ifru_ivalue    /* interface index  */  
  44. #define ifr_bandwidth   ifr_ifru.ifru_ivalue    /* link bandwidth   */  
  45. #define ifr_qlen    ifr_ifru.ifru_ivalue    /* Queue length     */  
  46. #define ifr_newname ifr_ifru.ifru_newname   /* New name     */  
  47. #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通常用来保存所有接口信息
[cpp]  view plain copy
  1. // if.h  
  2. /* 
  3.  * Structure used in SIOCGIFCONF request. 
  4.  * Used to retrieve interface configuration 
  5.  * for machine (useful for programs which 
  6.  * must know all networks accessible). 
  7.  */  
  8. struct ifconf  {  
  9.     int ifc_len;            /* size of buffer   */  
  10.     union {  
  11.         char __user *ifcu_buf;  
  12.         struct ifreq __user *ifcu_req;  
  13.     } ifc_ifcu;  
  14. };  
  15. #define ifc_buf ifc_ifcu.ifcu_buf       /* buffer address   */  
  16. #define ifc_req ifc_ifcu.ifcu_req       /* array of structures  */  


该结构体可以用来获取所哟网络接口的名字和信息(不是全部信息,是ip地址)
(图片来自: http://tech.sunplusedu.com/space/post-4064.aspx



Example:

[cpp]  view plain copy
  1. #include <sys/types.h>    
  2. #include <sys/ioctl.h>    
  3. #include <sys/socket.h>    
  4. #include <net/if.h>    
  5. #include <stdio.h>    
  6. #include <stdlib.h>    
  7. #include <unistd.h>    
  8. #include <netdb.h>    
  9. #include <string.h>    
  10. #include <fcntl.h>    
  11. #include <string.h>    
  12. #include <errno.h>  
  13. typedef uint32_t uint32;   
  14. #define MAX_IF 10  
  15. int   
  16. main()  
  17. {  
  18.     struct ifreq ifVec[MAX_IF];//用来保存所有接口  
  19.       
  20.     int sock = -1;  
  21.     if ( (sock = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )  
  22.         fprintf(stderr, "Error:%d, cannot open RAM;\n");  
  23.       
  24.     // get if vector  
  25.     struct ifconf ioIfConf;  
  26.         ioIfConf.ifc_buf = (void *)ifVec;  
  27.         ioIfConf.ifc_len = sizeof(ifVec);  
  28.      printf("Len:%d\n", ioIfConf.ifc_len);       
  29.           
  30.     if (ioctl(sock, SIOCGIFCONF, &ioIfConf) < 0 )//获取所有网络接口信息  
  31.         fprintf(stderr, "Error:%d   ioctl IFCONF\n");  
  32.       
  33.      printf("Len:%d\n", ioIfConf.ifc_len);// 和前面到len对比,发现ioctl修改里len到大小       
  34.     //循环打印每个网络接口到信息  
  35.     {  
  36.         struct ifreq *ifPt;  
  37.         struct ifreq *ifEndPt;  
  38.         ifPt = ifVec;  
  39.         ifEndPt = (void *)((char *)ifVec + ioIfConf.ifc_len);  
  40.         for (ifPt = ifVec; ifPt < ifEndPt; ifPt++)  
  41.         {  
  42.             struct ifreq ifReq;  
  43.             if ( ifPt->ifr_addr.sa_family != AF_INET ) {  
  44.                         continue;  
  45.                     }  
  46.               
  47.             // Temp keepers of interface params...  
  48.             uint32 u32_addr, u32_mask;  
  49.               
  50.             /*  打印ip地址  */  
  51.             char ipDotBuf[16], subnetDotBuf[16], maskDotBuf[16]; // 保存点分十进制到ip地址  
  52.             u32_addr = ((struct sockaddr_in *)&ifPt->ifr_addr)->sin_addr.s_addr;  
  53.             inet_ntop(AF_INET, &u32_addr, ipDotBuf, (socklen_t )sizeof(ipDotBuf));   
  54.             printf("IP Address: %s\n", ipDotBuf);  
  55.               
  56.             /*    打印地址掩码    */  
  57.             bzero(&ifReq,sizeof(struct ifreq));    
  58.             memcpy(ifReq.ifr_name, ifPt->ifr_name, sizeof(ifReq.ifr_name));    
  59.             if (ioctl(sock, SIOCGIFNETMASK, &ifReq ) < 0){  
  60.                 fprintf(stderr, "Error: %d, cannot get mask\n", errno);  
  61.             }  
  62.             else{  
  63.                 u32_mask = ((struct sockaddr_in *)&ifReq.ifr_addr)->sin_addr.s_addr;  
  64.                 inet_ntop(AF_INET, &u32_mask, maskDotBuf, (socklen_t )sizeof(maskDotBuf));  
  65.                 printf("Mask: %s\n", maskDotBuf);  
  66.             }     
  67.             /*    打印MTU    */  
  68.             bzero(&ifReq,sizeof(struct ifreq));    
  69.             memcpy(ifReq.ifr_name, ifPt->ifr_name, sizeof(ifReq.ifr_name));    
  70.             if (ioctl(sock, SIOCGIFMTU, &ifReq ) < 0){  
  71.                 fprintf(stderr, "Error: %d, cannot get MTU\n", errno);  
  72.             }  
  73.             else{  
  74.                 printf("SIOCGIFMTU:%d\n", ifReq.ifr_mtu);   
  75.             }  
  76.                /*    其他信息的打印方式与掩码和MTU相同    */  
  77.         }  
  78.     }  
  79.       
  80.       
  81.       
  82. }  



运行结果:
[cpp]  view plain copy
  1. windeal@ubuntu:~/Windeal/apue$ ./exe   
  2. Len:320  
  3. Len:64  
  4. IP Address: 127.0.0.1  
  5. Mask: 255.0.0.0  
  6. SIOCGIFMTU:16436  
  7. IP Address: 172.17.92.198  
  8. Mask: 255.255.254.0  
  9. SIOCGIFMTU:1500  
  10. windeal@ubuntu:~/Windeal/apue$   

这篇关于获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/538325

相关文章

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

MySQL中的索引结构和分类实战案例详解

《MySQL中的索引结构和分类实战案例详解》本文详解MySQL索引结构与分类,涵盖B树、B+树、哈希及全文索引,分析其原理与优劣势,并结合实战案例探讨创建、管理及优化技巧,助力提升查询性能,感兴趣的朋... 目录一、索引概述1.1 索引的定义与作用1.2 索引的基本原理二、索引结构详解2.1 B树索引2.2

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat