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获取网络接口信息的更多相关文章

  1. 自己动手写路由器之ioctl获取网络接口信息

    最近打算写一个简单路由器,里面有用到ioctl获取网络接口信息,那就先把这部分单独拿出来说一说吧! ioctl这个函数,可以用来对特殊文件的基础设备参数进行操作,它们可以完成与打开文件描述符相关联的控 ...

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

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

  3. LINUX获取文件信息

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  4. [转]使用GetIfTable获取MIB_IFTABLE和MIB_IFROW获取网络接口信息

    #include <iphlpapi.h> #pragma comment ( lib, "iphlpapi.lib") 使用GetIfTable()获取各个端口信息的 ...

  5. linux的帮助信息获取以及man章节的划分

    linux的帮助信息获取以及man章节的划分 linux 帮助 man 章节 linux 获取帮助的途径 (1)help (2)man (3)info command在线获取 (4)程序自带帮助文档 ...

  6. Linux下Wireshark普通用户不能获取网络接口问题

    Linux下Wireshark普通用户不能获取网络接口问题 1.安装setcap, setcap 是libcap2-bin包的一部分,一般来说,这个包默认会已经装好. sudo apt-get ins ...

  7. 封装获取网络信息Linux—API类

    封装获取网络信息Linux—API类 封装好的库: #ifndef NETINFORMATION_H #define NETINFORMATION_H #include <netdb.h> ...

  8. Linux 网络编程基础(2)-- 获取主机信息

    前一篇已经介绍了最基本的网络数据结构.这篇介绍一下获取主机信息的函数 举个例子,想要通过代码的方式从百度获取当前的时间,怎么做?我们不知道百度的IP地址啊,这代码怎么写?还好,Linux提供了一些AP ...

  9. C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码

    项目开始设计的是运行在windows下,所以一开始采用的是windows服务模式来获取多媒体文件信息,后来要求调整为可以在Linux下运行,经过这两天的资料查找,实现了Linux下通过.NET来获取多 ...

随机推荐

  1. Android表格布局(Table Layout)

    Android表格布局(Table Layout) 先来看布局管理器之间继承关系图: 图1 可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器. 表格布局采用 ...

  2. JavaScript单线程的疑问与解答

    问: JavaScript是单线程的,有任务队列,比如使用setTimeou(func,secs)来在secs毫秒后向任务队列添加func.但是,setTimeout后面跟一个死循环,那么死循环导致任 ...

  3. Machine Learning 学习笔记

    点击标题可转到相关博客. 博客专栏:机器学习 PDF 文档下载地址:Machine Learning 学习笔记 机器学习 scikit-learn 图谱 人脸表情识别常用的几个数据库 机器学习 F1- ...

  4. HBase快照

    CDH是Cloudera的完全开源分布式Apache Hadoop及相关项目(包括Apache HBase).CDH的当前版本(4.2)引入的一个HBase新特性最近加入到了主干中,允许用户对指定表进 ...

  5. LeetCode之旅(20)-Power of Three

    题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...

  6. Spring Cloud 入门教程 - Eureka服务注册与发现

    简介 在微服务中,服务注册与发现对管理各个微服务子系统起着关键作用.随着系统水平扩展的越来越多,系统拆分为微服务的数量也会相应增加,那么管理和获取这些微服务的URL就会变得十分棘手,如果我们每新加一个 ...

  7. IT轮子系列(三)——如何显示方法名——Swagger的使用(三)

    前言 在上一篇文章IT轮子系列(三)——如何给返回类型添加注释——Swagger的使用(二) 介绍如何使用swashbuckle的时候忽略了一个问题,就是默认创建的API项目在生成文档的时候是没有显示 ...

  8. word break II(单词切分)

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  9. JavaScript中将对象数组中的某个属性值,批量替换成另一个数值

    原文链接 https://segmentfault.com/q/1010000010352622 希望将下列数组中的sh替换成沪,sz替换成深 var stooges = [ {label:1,val ...

  10. json.parseArray源码解析

    json.parseArray源码解析 public static <T> List<T> parseArray(String text, Class<T> cla ...