Linux获取网络接口信息
linux获取网络接口信息需要用到的函数为ioctl(),结构体struct ifreq,struct ifconf
1.ioctl()函数原型及作用
#include <sys/ioctl.h> int ioctl(int d, int request, ...); //参数
//int d:是一个文件描述符
//int request :表示要请求的信息。如IP地址、网络掩码等
//......:可变参数,根据request而定
下面是ioctl请求的request参数以及arg地址必须指向的数据类型:

2.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
# 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),,,)
# define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),,,)
# define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),,,)
3.struct ifconf
ifreq包含在ifconf结构中。而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),,,,,) /* not right */
#endif /* Misc. */
下面程序是获取本机的IP地址
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h> #ifndef IFNAMESZ
#define IFNAMESZ 16
#endif int main(int argc, char *argv[])
{
if (argc != ) {
printf("using %s <ETH_NAME>\n", argv[]);
return ;
} int sock = ;
struct sockaddr_in sin;
struct ifreq ifr;//用来保存接口的信息 sock = socket(AF_INET, SOCK_DGRAM, );
if (sock == -) {
perror("socket");
return ;
}
memcpy(ifr.ifr_name, argv[], IFNAMESZ);
ifr.ifr_name[IFNAMESZ - ] = '\0'; if (ioctl(sock, SIOCGIFADDR, &ifr) == ) { //SIOCGIFADDR 获取接口地址
memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); //将获得地址拷贝到 sockaddr_in 结构体中
printf("Ip: %s\n", inet_ntoa(sin.sin_addr));
} if (ioctl(sock, SIOCGIFNETMASK, &ifr) == ) {
memcpy(&sin, &ifr.ifr_netmask, sizeof(sin));
printf("netmask: %s\n", inet_ntoa(sin.sin_addr));
}
close(sock); return ;
}
运行结果如下:

Linux获取网络接口信息的更多相关文章
- 自己动手写路由器之ioctl获取网络接口信息
最近打算写一个简单路由器,里面有用到ioctl获取网络接口信息,那就先把这部分单独拿出来说一说吧! ioctl这个函数,可以用来对特殊文件的基础设备参数进行操作,它们可以完成与打开文件描述符相关联的控 ...
- 获取网络接口信息——ioctl()函数与结构体struct ifreq、 struct ifconf
转载请注明出处:windeal专栏 Linux 下 可以使用ioctl()函数 以及 结构体 struct ifreq 结构体struct ifconf来获取网络接口的各种信息. ioctl 首先看 ...
- LINUX获取文件信息
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- [转]使用GetIfTable获取MIB_IFTABLE和MIB_IFROW获取网络接口信息
#include <iphlpapi.h> #pragma comment ( lib, "iphlpapi.lib") 使用GetIfTable()获取各个端口信息的 ...
- linux的帮助信息获取以及man章节的划分
linux的帮助信息获取以及man章节的划分 linux 帮助 man 章节 linux 获取帮助的途径 (1)help (2)man (3)info command在线获取 (4)程序自带帮助文档 ...
- Linux下Wireshark普通用户不能获取网络接口问题
Linux下Wireshark普通用户不能获取网络接口问题 1.安装setcap, setcap 是libcap2-bin包的一部分,一般来说,这个包默认会已经装好. sudo apt-get ins ...
- 封装获取网络信息Linux—API类
封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...
- Linux 网络编程基础(2)-- 获取主机信息
前一篇已经介绍了最基本的网络数据结构.这篇介绍一下获取主机信息的函数 举个例子,想要通过代码的方式从百度获取当前的时间,怎么做?我们不知道百度的IP地址啊,这代码怎么写?还好,Linux提供了一些AP ...
- C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码
项目开始设计的是运行在windows下,所以一开始采用的是windows服务模式来获取多媒体文件信息,后来要求调整为可以在Linux下运行,经过这两天的资料查找,实现了Linux下通过.NET来获取多 ...
随机推荐
- AngularJS进阶(十九)在AngularJS应用中集成百度地图实现定位功能
在AngularJS应用中集成百度地图实现定位功能 注:请点击此处进行充电! 前言 根据项目需求,需要实现手机定位功能,考虑到百度业务的强大能力,遂决定使用百度地图第三方服务. 添加第三方模块的步骤与 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Netflix Recommendations
by Xavier Amatriain and Justin Basilico (Personalization Science and Engineering) In part one of thi ...
- android的Devices窗口中Online显示成Offline
这种情况几率很低,如果出现,点击Reset adb就好了.
- ORACLE中用rownum分页并排序的SQL语句
ORACLE中用rownum分页并排序的SQL语句 以前分页习惯用这样的SQL语句: select * from (selectt.*,rownum row_num frommytable t ord ...
- myBatis源码之XMLConfigBuilder
XMLConfigBuilder是对mybatis的配置文件进行解析的类,会对myabtis解析后的信息存放在Configuration对象中,Configuration对象会贯穿整个mybatis的 ...
- java基础语法(一)
java基础语法(一) 1.类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成 对象(Object).对象又叫做实例(Instance). 2.类由两大部分构成:属性 ...
- Web 前台优化
大型网站--前端性能优化和规范 2013-10-28 09:00 by 贤达, 2769 阅读, 10 评论, 收藏, 编辑 Web性能涉及的范围太广,但一般web开发者在程序上线以后很多都曾遇到 ...
- SQL遇到的问题
1.问题描述:拼接sql字符串涉及到表变量时报错. 解决办法:把表变量的定义一同放在字符串中. 2.问题描述:EF添加实体后,调用存储过程调用不到 解决办法:必须先db.SaveChanges()后 ...
- IDEA: 遇到问题Error during artifact deployment. See server log for details解决方法
1.检查tomcat是否配置正确. 2.检查配置文件是否配置正确,web.xml.等. 3. 4.