getifaddrs
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_dstaddrvoid *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 flagsIFF_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 notset.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 multicastIFF_PORTSEL Is able to select media type via ifmap.IFF_AUTOMEDIA Auto media selection active.IFF_DYNAMIC The addresses are lost when the interfacegoes 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 wecan 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 symbolicform 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);
}
转载于:https://www.cnblogs.com/sammei/p/3955679.html
getifaddrs相关推荐
- Linux下正确使用getifaddrs()函数避免内存泄露
工作中使用valgrind检测内存泄露时,发现getifaddrs()很容易导致内存泄露,下面是正确的代码: //get local ip of network card //gcc -g get_a ...
- linux释放内存函数,LINUX上的getifaddrs()函数的内存释放有关问题
LINUX上的getifaddrs()函数的内存释放有关问题 LINUX上的getifaddrs()函数的内存释放有关问题 日期:2014-05-16 浏览次数:20386 次 LINUX下的geti ...
- Python.h: No such file or directory
安装python-devel包解决这个问题 # pip install netifaces DEPRECATION: Python 2.6 is no longer supported by ...
- windows linux C/C++获取操作系统、CPU、内存信息、硬盘、IP和MAC
Windows 操作系统和内存信息在windows下通过系统的API来获取,CPU信息则需要需要通过底层CPUID指令取得 代码: #include <iostream> #include ...
- 基于 linux 平台的 libpcap 源代码分析
libpcap 是 unix/linux 平台下的网络数据包捕获函数包,大多数网络监控软件都以它为基础.Libpcap 可以在绝大多数类 unix 平台下工作,本文分析了 libpcap 在 linu ...
- libpcap 源代码分析(二)
网络监控 绝大多数的现代操作系统都提供了对底层网络数据包捕获的机制,在捕获机制之上可以建立网络监控(Network Monitoring)应用软件.网络监控也常简称为sniffer,其最初的目的在于对 ...
- Qt5.5.1移植到freescale imx6
一.环境 HOST:ubuntu12.04-LTS Embedded:freescale imx6 linux-3.0.35 CROSS_COMPILE:freescale提供的gcc-4.6.2-g ...
- 入职一个多月了,谈谈感想
好久没写博客了,入职已经有一个多月了,今天我谈谈我工作的感想吧. 昨天一个同事离职了,跟我一样是出来实习的,工作经验比我多,思维很活跃,离职原因很简单:想从事底层开发.人各有志吧,跟我相同的是大学都不 ...
- Linux编程获取网络信息总结
Linux下C获取所有可用网卡信息 在Linux下开发网络程序时,经常会遇到需要取本地网络接口名.IP.广播地址 .子网掩码或者MAC地址等信息的需求,最常见的办法是配合宏SIOCGIFHWADDR. ...
- getpeername函数与getsockname函数的介绍
getpeername函数用于获取与某个套接字关联的外地协议地址 getsockname函数用于获取与某个套接字关联的本地协议地址 函数定义如下: #include<sys/socket.h&g ...
最新文章
- layer.msg();怎么关闭
- js变量前有 var 与没有的区别
- C语言二进制标识符,C语言入门基础大全,自学C语言必备知识!
- 【面试题视频讲解】TreeSet使用示例
- 2.linux换源问题
- 浅谈JSP表单中的form传值
- 2021十大金融科技趋势
- mongodb对数组元素及内嵌文档进行增删改查操作(转)
- 两个整数集合的交集 ———— 腾讯2014软件开发笔试题目
- java nio connect_服务器或客户端上的Java NIO套接字在什么时...
- php mvc 实现,php mvc的简单实现
- Linux下使用fdisk扩大分区容量
- 【POJ 1456】Supermarket【两种做法】【二叉堆贪心】【并查集】
- 怎样复制秀米html码,秀米微信图文编辑器如何复制?
- EXCEL里如何识别AB和BA并去重?EXCEL里如何对多列同一值不区分顺序去重?
- 欧姆龙plc学习笔记(七)
- free源码分析---1
- chrome浏览器主页被劫持为hao123
- Mobilenet_v2的参数alpha和depth_multiplier
- 桌面计算机图标管理打不开怎么回事,电脑桌面计算机图标打不开怎么办
热门文章
- 脱口秀在尝试处理一件难事
- php session不可用,php session 使用与安全
- 扇贝python课程免费_扇贝新推出的python课程值得买吗?
- sublime text3 python找不到文件路径_如何在sublime3 项目设置中设置 python 解释器的路径?...
- top 100 liked Q (26-)
- 32位x86处理器操作模式和寄存器简介
- 9-3-斐波那契查找-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
- Base64 的 JavaScript 实现 js-base64
- MapReduce运行流程分析
- 用栈实现队列,实现Enqueue和Dequeue方法