1,关于struct ifaddrs的说明:
struct ifaddrs
{
struct ifaddrs *ifa_next; /* Next item in list */
char *ifa_name; /* Name of interface */
unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
struct sockaddr *ifa_addr; /* Address of interface */
struct sockaddr *ifa_netmask; /* Netmask of interface */
union
{
struct sockaddr *ifu_broadaddr; /* Broadcast address of interface */
struct sockaddr *ifu_dstaddr; /* Point-to-point destination address */
} ifa_ifu;
#define ifa_broadaddr ifa_ifu.ifu_broadaddr
#define ifa_dstaddr ifa_ifu.ifu_dstaddr
void *ifa_data; /* Address-specific data */
};
2,关于getifaddrs()
The getifaddrs() function creates a linked list of structures describing the network interfaces of the local system, and stores the address of the first
item of the list in *ifap.
The list consists of ifaddrs structures, defined as follows:
The ifa_next field contains a pointer to the next structure on the list, or
NULL if this is the last item of the list.
The ifa_name points to the null-terminated interface name.
The ifa_flags field contains the interface flags
The ifa_addr field points to a structure containing the interface address.
The ifa_netmask field points to a structure containing the netmask associated with ifa_addr, if applicable for the address family.
Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in ifa_flags (only one can be set at a time), either ifa_broadaddr will contain the broadcast address associated with ifa_addr (if applicable for the address family) or ifa_dstaddr will contain the destination address of the point-to-point interface.
The ifa_data field points to a buffer containing address-family-specific data;this field may be NULL if there is no such data for this interface.
返回值:
On success, getifaddrs() returns zero; on error, -1 is returned, and errno is set appropriately.
3,注意:
The data returned by getifaddrs() is dynamically allocated and should be freed using freeifaddrs() when no longer needed.
4,一个小例子:
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
exit(EXIT_FAILURE);
}
/* Walk through linked list, maintaining head pointer so we
can free list later */
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
/* Display interface name and family (including symbolic
form of the latter for the common families) */
printf("%s address family: %d%s\n",
ifa->ifa_name, family,
(family == AF_PACKET) ? " (AF_PACKET)" :
(family == AF_INET) ? " (AF_INET)" :
(family == AF_INET6) ? " (AF_INET6)" : "");
/* For an AF_INET* interface address, display the address */
if (family == AF_INET || family == AF_INET6)
{
s = getnameinfo(ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("\taddress: <%s>\n", host);
}
}
freeifaddrs(ifaddr); //一定要释放
exit(EXIT_SUCCESS);
}
输出:
引用
$ ./a.out
lo address family: 17 (AF_PACKET)
eth0 address family: 17 (AF_PACKET)
lo address family: 2 (AF_INET)
address: <127.0.0.1>
eth0 address family: 2 (AF_INET)
address: <10.1.1.4>
lo address family: 10 (AF_INET6)
address: <::1>
eth0 address family: 10 (AF_INET6)
address: <fe80::2d0:59ff:feda:eb51%eth0
分享到:
相关推荐
int getifaddrs(struct ifaddrs **ifap); ``` 参数`ifap`是一个指向指针的指针,调用后`*ifap`将指向一个链接列表,每个元素代表一个网络接口的`struct ifaddrs`结构。这个结构包含了一个`struct sockaddr`指针,...
`android-ifaddrs-master`这个压缩包可能就包含了针对Android平台的`getifaddrs`和`freeifaddrs`的实现。这个开源项目通常会提供一个跨平台的方式来获取网络接口信息。在项目中,找到源代码并将其集成到你的项目中。...
首先,`ifaddrs.h`是头文件,其中定义了`struct ifaddrs`结构体,它包含了关于网络接口的信息,如接口名称、IP地址、子网掩码、广播地址等。结构体的定义如下: ```c struct ifaddrs { struct ifaddrs *ifa_next; ...
描述中提到的解决方案是使用附件提供的`cifaddrs.c`和`cifaddrs.h`文件来替代标准的`ifaddrs`头文件,以解决不支持的函数问题。 `ifaddrs`结构体在C语言中用于获取操作系统网络接口的地址信息,包括IPv4、IPv6等。...
首先,定义一个`struct ifaddrs`指针,然后调用`getifaddrs`,它会填充所有网络接口的信息。接着,遍历返回的接口列表,检查每个接口的`ifa_addr`结构,如果是IPv4(`AF_INET`)或IPv6(`AF_INET6`),则可以通过`...
1. 包含必要的头文件,如`<sys/socket.h>`、`<netinet/in.h>`和`<ifaddrs.h>`,这些头文件包含了处理套接字和网络接口所需的所有定义和函数声明。 2. 定义一个函数,比如`get_device_ips`,该函数会调用`getifaddrs`...
void printIpAddresses(struct ifaddrs *ifap) { while (ifap) { if (ifap->ifa_addr && (ifap->ifa_addr->sa_family == AF_INET)) { struct sockaddr_in *addr = (struct sockaddr_in *)ifap->ifa_addr; char ...
void print_ipv6_addresses(struct ifaddrs *ifap) { for (; ifap != NULL; ifap = ifap->ifa_next) { if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET6) { char addr_str[INET6_ADDRSTRLEN]; ...
void print_mac_address(struct ifaddrs *ifap) { struct ifaddrs *ifa; for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr && (ifa->ifa_addr->sa_family == AF_LINK)) { struct ...
本文介绍了在 Linux 中获取 IPv4 和 IPv6 地址的方法,包括使用 `getifaddrs` 函数获取网络接口的信息,遍历链表,获取 IPv4 和 IPv6 地址,并使用 `inet_ntop` 函数将地址从二进制形式转换为字符串形式。
获取IP地址涉及到网络编程,可以使用`getaddrinfo`和`getifaddrs`函数。`getaddrinfo`用于获取域名的IP地址,而`getifaddrs`用于获取所有网络接口的地址信息。以下是一个简单的例子: ```c #include #include ...
void printMacAddress(struct ifaddrs* ifa) { if (ifa->ifa_addr && (ifa->ifa_addr->sa_family == AF_LINK)) { struct sockaddr_dl* sdl = (struct sockaddr_dl*)ifa->ifa_addr; char mac[6 * 3 + 5]; for ...
这个函数会填充一个`ifaddrs`结构体链表,其中包含了每个接口的IP地址(IPv4和IPv6)和其他信息。我们可以通过遍历这个链表,提取出每个接口的`ifa_addr`字段来得到IP地址。 对于MAC地址的获取,Linux提供了`ioctl...
不过,为了编程目的,我们可以使用套接字API(Socket API)和`ifaddrs`结构体来获取这些信息,而无需直接读取配置文件。 C语言源码中,我们需要引入必要的头文件: ```c #include #include #include #include ...
首先,我们需要引入两个系统框架头文件`<ifaddrs.h>`和`<arpa/inet.h>`。`ifaddrs.h`提供了获取网络接口地址的接口,而`arpa/inet.h`包含了IP地址转换的函数,如`inet_ntoa`。 接下来,定义一个方法`getIPAddress`...
每个接口信息由`struct ifaddrs`表示,其中的`ifa_addr`字段指向具体的IP地址(可以是IPv4的`struct sockaddr_in`或IPv6的`struct sockaddr_in6`)。 在C语言程序中,首先需要包含必要的头文件,然后根据操作系统...
struct ifaddrs *ifaddr, *ifa; getifaddrs(&ifaddr); for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { struct sockaddr_in *sin = ...
源码中应该包含了如何调用这些函数以及如何解析和显示结果的详细说明。通过阅读源码,你可以更好地理解如何在实际项目中应用这些技术。 总结,获取本机IP地址的C++方法涉及到网络接口和IP地址的基础知识,以及特定...
对于Unix-like系统,`getifaddrs()`返回的结构体`struct ifaddrs`中的`ifa_data`字段可以指向`sockaddr_ll`结构体,从中可以提取出MAC地址。而在Windows系统中,`IP_ADAPTER_ADDRESSES`结构体的`PhysicalAddress`...