getifaddrs

获取本地网络接口的信息。在路由器上可以用这个接口来获取wan/lan等接口当前的ip地址,广播地址等信息。

       #include <sys/types.h>
#include <ifaddrs.h> int getifaddrs(struct ifaddrs **ifap); void freeifaddrs(struct ifaddrs *ifa);

getifaddrs创建一个链表,链表上的每个节点都是一个struct ifaddrs结构,getifaddrs()返回链表第一个元素的指针。成功返回0, 失败返回-1,同时errno会被赋允相应错误码。

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 */
};

ifa_next 指向链表中下一个struct ifaddr结构

ifa_name 网络接口名

ifa_flags 网络接口标志,这些标志见下面描述。

ifa_addr 指向一个包含网络地址的sockaddr结构

ifa_netmask 指向一个包含网络掩码的结构

ifu_broadaddr 如果(ifa_flags&IFF_BROADCAST)有效,ifu_broadaddr指向一个包含广播地址的结构。

ifu_dstaddr 如果(ifa_flags&IFF_POINTOPOINT)有效,ifu_dstaddr指向一个包含p2p目的地址的结构。

ifa_addr 指向一个缓冲区,其中包含地址族私有数据。没有私有数据则为NULL。

ifa_flags 标志位:

                                      Device flags
IFF_UP Interface is running.
IFF_BROADCAST Valid broadcast address set. IFF_DEBUG Internal debugging flag.
IFF_LOOPBACK Interface is a loopback interface.
IFF_POINTOPOINT Interface is a point-to-point link.
IFF_RUNNING Resources allocated.
IFF_NOARP No arp protocol, L2 destination address not
set.
IFF_PROMISC Interface is in promiscuous mode.
IFF_NOTRAILERS Avoid use of trailers.
IFF_ALLMULTI Receive all multicast packets.
IFF_MASTER Master of a load balancing bundle.
IFF_SLAVE Slave of a load balancing bundle.
IFF_MULTICAST Supports multicast
IFF_PORTSEL Is able to select media type via ifmap.
IFF_AUTOMEDIA Auto media selection active.
IFF_DYNAMIC The addresses are lost when the interface
goes down.
IFF_LOWER_UP Driver signals L1 up (since Linux 2.6.17)
IFF_DORMANT Driver signals dormant (since Linux 2.6.17)
IFF_ECHO Echo sent packets (since Linux 2.6.25)

使用实例,在man 3 getifaddrs的例子基础上加了一个broadcast address:

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <net/if.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)" : ""); if (ifa->ifa_flags & IFF_BROADCAST) /* 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); if (ifa->ifa_flags & IFF_BROADCAST) {
s = getnameinfo(ifa->ifa_broadaddr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
printf("\tbroadcast address: <%s>\n", host);
}
if (ifa->ifa_flags & IFF_POINTOPOINT) {
s = getnameinfo(ifa->ifa_dstaddr,
(family == AF_INET) ? sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
printf("\tbroadcast address: <%s>\n", host);
}
}
} freeifaddrs(ifaddr);
exit(EXIT_SUCCESS);
}

getifaddrs的更多相关文章

  1. iOS 网络流量统计

    在开发中,有时候需要获取流量统计信息.研究发现:通过函数getifaddrs来得到系统网络接口的信息,网络接口的信息,包含在if_data字段中, 有很多信息, 但我现在只关心ifi_ibytes,  ...

  2. iOS常用系统信息获取方法

    一.手机电量获取,方法二需要导入头文件#import<objc/runtime.h> 方法一.获取电池电量(一般用百分数表示,大家自行处理就好) -(CGFloat)getBatteryQ ...

  3. ios 获取手机的IP地址

    - (NSString *)getIPAddress:(BOOL)preferIPv4{ NSArray *searchArray = preferIPv4 ? @[ IOS_VPN @"/ ...

  4. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  5. iOS 直播-网速监控

    iOS 直播-网速监控 CXNetworkSpeed.h // // CXNetworkSpeed.h // CXNetworkSpeedDemo // // Created by xubaoaich ...

  6. ClearContainer 网络部分源码分析

    // cc-oci-runtime/src/oci.c /*! * Create the state file, apply mounts and run hooks, but do not star ...

  7. iOS获取本机IP地址

    #import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { ...

  8. iOS - YYAdd对UIDevice的拓展

    YYKit中对UIDevice的拓展,accumulate knowledge !!! 首先 #include <sys/socket.h> #include <sys/sysctl ...

  9. iOS开发中获取WiFi相关信息

    iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiFi相关的. 1.Ping域名.Ping某IP 有 时候可能会遇到ping 某个域名或者ip通不通,再做下一步操作.这里的p ...

随机推荐

  1. CF666E Forensic Examination SAM+倍增,线段树和并

    题面: 给你一个串S以及一个字符串数组T[1..m],q次询问,每次问S的子串S[p_l..p_r]在T[l..r]中的哪个串里的出现次数最多,并输出出现次数.如有多解输出最靠前的那一个. 分析: 第 ...

  2. hdu3094 A tree game

    题目描述 题解: 树上删边. $SG[u]$^=$SG[son[u]]+1$ 代码: #include<cstdio> #include<cstring> ; template ...

  3. 网络设置命令--ifconfig.setup

    ifconfig命令 作用:用于显示以及设置当前活动网卡信息 一.  显示当前活动网卡信息 ifconfig 从上面可以看到当前主要有2块活动网卡,eth0:代表当前本地真实网卡 lo:代表回访网卡, ...

  4. mysql 5.7 windows zip安装

    mysql 官网下载windows zip 安装包 并解压 (D:wampmysql-56-winx64) 添加path D:wampmysql-5722-winx64bin 创建data目录 D:\ ...

  5. SSRS ( .rdl文件)如何动态的设置导出Excel文件中的工作表标签名

    要实现以上效果,则在Tablix属性里设置 参考:https://dotblogs.com.tw/ricochen/archive/2012/06/14/72798.aspx

  6. Relocation(状压DP)

    Description Emma and Eric are moving to their new house they bought after returning from their honey ...

  7. Food Delivery (区间DP)

    When we are focusing on solving problems, we usually prefer to stay in front of computers rather tha ...

  8. [luoguP2948] [USACO09OPEN]滑雪课Ski Lessons(DP)

    传送门 f[i][j]表示i时刻能力值为j的最大滑雪数 显然f[0][1]=0,开始搜索 三种转移: ①美美的喝上一杯**:f[i+1][j]=max(f[i+1][j],f[i][j]) ②滑雪,f ...

  9. BZOJ1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机

    n<=200个点m<=40000条边无向图,求   t次走不经过同条边的路径从1到n的经过的边的最大值   的最小值. 最大值最小--二分,t次不重边路径--边权1的最大流. #inclu ...

  10. Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device

    Linux MTD (Memory Technology Device) subsystem analysis For Atheros char device 读了Linux MTD 源代码分析 对这 ...