Linxu的struct ifaddrs 与getifaddrs()函数
2015-08-06 15:17
351 查看
来自man函数手册中的介绍:
1,关于struct ifaddrs的说明:
[cpp] view plaincopy
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, man中的实例代码:
[cpp] view plaincopy
#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);
}
1,关于struct ifaddrs的说明:
[cpp] view plaincopy
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, man中的实例代码:
[cpp] view plaincopy
#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);
}
相关文章推荐
- Reachability判断网络是否连接
- InterfaceConnect
- 用CocoaPods做iOS程序的依赖管理
- JS实现从网页顶部掉下弹出层效果的方法
- Oracle异常整理
- 推荐算法工程师
- 第一章:taglib 原理和实现
- 数据结构基础(7) --循环队列的设计与实现
- 酷炫图片展示特效
- 【hibernate】spring+ jpa + hibername 配置过程遇到的问题
- DZY Loves Chemistry
- nginx 添加404页面后,仍然提示file not found
- iOS开发系列--并行开发其实很容易
- 20个数据可视化(数据视觉化)工具
- Sublime 3新建文件快速生成Html头部信息
- JS判断设备屏幕方向
- Dijkstra 最短路径算法详解 无向图
- MyBatis学习总结一
- Oracle下一条SQL语句的优化
- TabHost实现底部菜单