ioctl struct ifreq

ioctl获取本地IP详解
本文介绍如何使用ioctl函数配合ifconf及ifreq结构体来获取本地IP地址。通过实例代码展示了初始化ifconf结构体并调用ioctl获取所有网络接口信息的过程,最后解析ifreq结构体以提取各接口的IP地址。

用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人
来说都是比较陌生的,这里给大家一种比较简单的理解方法,当然只一种帮助
理解的方法,在描述中可能会有一些地方与真实定义有所出入,仅供参考.

首先先认识一下ifconf和ifreq:

//ifconf通常是用来保存所有接口信息的
//if.h
struct ifconf
{
int ifc_len; /* size of buffer */
union
{
char *ifcu_buf; /* input from user->kernel*/
struct ifreq *ifcu_req; /* return from kernel->user*/
} ifc_ifcu;
};
#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
#define ifc_req ifc_ifcu.ifcu_req /* array of structures */
 
//ifreq用来保存某个接口的信息
//if.h
struct ifreq 
{
char ifr_name[IFNAMSIZ];
union {
struct sockaddr ifru_addr;
struct sockaddr ifru_dstaddr;
struct sockaddr ifru_broadaddr;
short ifru_flags;
int ifru_metric;
caddr_t ifru_data;
} ifr_ifru;
};
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr

 

上边这两个结构看起来比较复杂,我们现在把它们简单化一些:
比如说现在我们向实现获得本地IP的功能。

我们的做法是:
1. 先通过ioctl获得本地所有接口的信息,并保存在ifconf中
2. 再从ifconf中取出每一个ifreq中表示ip地址的信息

具体使用时我们可以认为ifconf就有两个成员:
ifc_len 和 ifc_buf,
 如图一所示:    

  

ifc_len:表示用来存放所有接口信息的缓冲区长度
ifc_buf:表示存放接口信息的缓冲区

所以我们需要在程序开始时对ifconf的ifc_len和ifc_buf进行初始化 
接下来使用ioctl获取所有接口信息,完成后ifc_len内存放实际获得的借口信息总长度
并且信息被存放在ifc_buf中。
 
如下图示:(假设读到两个接口信息)

 

 

接下来我们只需要从一个一个的接口信息获取ip地址信息即可。

下面有一个简单的参考:

#include
#include
#include
#include
#include in .h>
#include <string .h>
#include if .h>
#include
 
int main()
{
int i=0;
int sockfd;
struct ifconf ifconf;
unsigned char buf[512];
struct ifreq *ifreq;

//初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf;

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
{
perror("socket" );
exit(1);
}
ioctl(sockfd, SIOCGIFCONF, &ifconf); //获取所有接口信息

//接下来一个一个的获取IP地址
ifreq = (struct ifreq*)buf;
for (i=(ifconf.ifc_len/sizeof (struct ifreq)); i>0; i--)
{
// if(ifreq->ifr_flags == AF_INET){ //for ipv4
printf("name = [%s]/n" , ifreq->ifr_name);
printf("local addr = [%s]/n" ,inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
ifreq++;
// }
}
return 0;
}

### `struct ifreq` 的用途和使用方法 `struct ifreq` 是用于与网络接口进行交互的重要数据结构,常用于通过 `ioctl()` 系统调用来获取或设置网络接口的配置信息。该结构体定义在 `<net/if.h>` 头文件中,适用于 Linux 网络编程环境。其主要字段包括接口名称、索引、IP 地址、子网掩码、MTU 等网络相关参数[^3]。 #### 获取网络接口的 MTU 通过 `ioctl()` 调用 `SIOCGIFMTU` 可以获取指定网络接口的最大传输单元(MTU)。该值存储在 `ifr.ifr_mtu` 字段中,示例如下: ```c #include <sys/ioctl.h> #include <net/if.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <unistd.h> int main() { int fd = socket(AF_INET, SOCK_DGRAM, 0); struct ifreq ifr; strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1); if (ioctl(fd, SIOCGIFMTU, &ifr) == 0) { printf("MTU of eth0: %d\n", ifr.ifr_mtu); } else { perror("ioctl"); } close(fd); return 0; } ``` #### 获取网络接口的名称 当已知接口索引时,可以通过 `SIOCGIFNAME` 获取对应的接口名称。该操作常用于 CAN 接口或数据包捕获等场景中,示例如下: ```c #include <sys/ioctl.h> #include <net/if.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <linux/can.h> int main() { int s = socket(PF_CAN, SOCK_DGRAM, CAN_RAW); struct sockaddr_can addr; struct ifreq ifr; socklen_t len = sizeof(addr); struct can_frame frame; // 接收 CAN 帧并获取来源接口索引 int nbytes = recvfrom(s, &frame, sizeof(struct can_frame), 0, (struct sockaddr *)&addr, &len); ifr.ifr_ifindex = addr.can_ifindex; // 获取接口名称 ioctl(s, SIOCGIFNAME, &ifr); printf("Received a CAN frame from interface %s\n", ifr.ifr_name); close(s); return 0; } ``` #### 获取或设置网络接口的 IP 地址 通过 `SIOCGIFADDR` 和 `SIOCSIFADDR` 可以分别获取和设置网络接口的 IP 地址。以下代码展示了如何获取接口的 IP 地址: ```c #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> int main() { int sockfd = socket(AF_INET, SOCK_DGRAM, 0); struct ifreq ifr; strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1); if (ioctl(sockfd, SIOCGIFADDR, &ifr) == 0) { struct sockaddr_in *ipaddr = (struct sockaddr_in *)&ifr.ifr_addr; printf("IP Address of eth0: %s\n", inet_ntoa(ipaddr->sin_addr)); } else { perror("ioctl"); } close(sockfd); return 0; } ``` #### 获取网络接口的 MAC 地址 通过 `SIOCGIFHWADDR` 可以获取网络接口的硬件地址(即 MAC 地址),示例如下: ```c #include <sys/ioctl.h> #include <net/if.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> int main() { int sockfd = socket(AF_INET, SOCK_DGRAM, 0); struct ifreq ifr; strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1); if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) == 0) { unsigned char *mac = (unsigned char *)ifr.ifr_hwaddr.sa_data; printf("MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } else { perror("ioctl"); } close(sockfd); return 0; } ``` ### 注意事项 - `struct ifreq` 的大小限制为 `IFNAMSIZ`(通常是 16 字节),因此接口名称不能超过该长度。 - 使用 `ioctl()` 时需确保调用的权限,某些操作需要 root 权限。 - 不同的 `ioctl()` 命令对 `struct ifreq` 的不同字段进行操作,需根据具体需求选择合适的命令。 - 在 CAN 网络编程中,`ifr.ifr_ifindex` 和 `SIOCGIFNAME` 被广泛用于识别数据帧的来源接口[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值