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. ASCII码表(常用)

       

  2. WinCE系统声音定制

    作者:ARM-WinCE 2010的第一篇Blog,介绍一下WinCE系统声音的定制.说白了,就是设置注册表.WinCE系统启动的开机音乐,点击触摸屏以及键盘输入的按键音,还有系统运行过程中的各种声音 ...

  3. 客户全局信用控制&非全局信用控制

    看个简单的例子 客户信用限额 非全局信用控制 非全局信用控制比较简单,我们看一下全局信用控制 设置: 实现结果:全局&非全局对比

  4. SharePoint 添加BCD菜单

    前言:在SharePoint中,我们常见的操作就是添加我们的自定义BCD菜单,下面,简单介绍下添加自定义BCD菜单的操作.主要介绍两种熟悉的方法,一种通过xml方式,另一种是通过js的方式. 环境:S ...

  5. PHP获取指定地区的天气

    在开发网站的时候用到天气查询,由于是基于Wordpress的 所以有很多限制,先建一个[weather.php]的文件,然后看代码: <?php //获取天气 $url = 'http://m. ...

  6. LeetCode(45)-Bulls and Cows

    题目: You are playing the following Bulls and Cows game with your friend: You write down a number and ...

  7. WebStorm常用快捷键总结

    在使用WebStorm的过程中,常用快捷键整理: 1.  必备快捷键 Ctrl+/:注释当前行 Ctrl+Shift+/:当前位置插入注释 Ctrl+Alt+/:块注释,并Focus到首行,写注释说明 ...

  8. Robot Framework + Pywinauto 框架实现Windows GUI Automation

    Robot Framework is a generic test automation framework for acceptance testing and acceptance test-dr ...

  9. jQuery选择器面试题

    $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...

  10. oracle 查看表的索引信息

    1.select * from user_indexes where table_name='PAMSODT0P02' 2.select * from user_ind_columns  where ...