在Linux下开发网络程序时,经常会遇到需要取本地网络接口名、IP、广播地址、子网掩码或者MAC地址等信息的需求,最常见的办法是配合宏SIOCGIFHWADDR、SIOCGIFADDR、SIOCGIFBRDADDR与SIOCGIFNETMASK作为参数调用函数ioctl分别获得MAC地址、IP地址、广播地址与子网掩码来实现。一次性获取此类信息的C语言代码实现如下。

 #include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <errno.h> int getLocalInfo(void)
{
int fd;
int interfaceNum = ;
struct ifreq buf[];
struct ifconf ifc;
struct ifreq ifrcopy;
char mac[] = {};
char ip[] = {};
char broadAddr[] = {};
char subnetMask[] = {}; if ((fd = socket(AF_INET, SOCK_DGRAM, )) < )
{
perror("socket"); close(fd);
return -;
} ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
printf("interface num = %dn", interfaceNum);
while (interfaceNum-- > )
{
printf("ndevice name: %sn", buf[interfaceNum].ifr_name); //ignore the interface that not up or not runing
ifrcopy = buf[interfaceNum];
if (ioctl(fd, SIOCGIFFLAGS, &ifrcopy))
{
printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__); close(fd);
return -;
} //get the mac of this interface
if (!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum])))
{
memset(mac, , sizeof(mac));
snprintf(mac, sizeof(mac), "%02x%02x%02x%02x%02x%02x",
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[], (unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[],
(unsigned char)buf[interfaceNum].ifr_hwaddr.sa_data[]);
printf("device mac: %sn", mac);
}
else
{
printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -;
} //get the IP of this interface if (!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
{
snprintf(ip, sizeof(ip), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr));
printf("device ip: %sn", ip);
}
else
{
printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -;
} //get the broad address of this interface if (!ioctl(fd, SIOCGIFBRDADDR, &buf[interfaceNum]))
{
snprintf(broadAddr, sizeof(broadAddr), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_broadaddr))->sin_addr));
printf("device broadAddr: %sn", broadAddr);
}
else
{
printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -;
} //get the subnet mask of this interface
if (!ioctl(fd, SIOCGIFNETMASK, &buf[interfaceNum]))
{
snprintf(subnetMask, sizeof(subnetMask), "%s",
(char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_netmask))->sin_addr));
printf("device subnetMask: %sn", subnetMask);
}
else
{
printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -; }
}
}
else
{
printf("ioctl: %s [%s:%d]n", strerror(errno), __FILE__, __LINE__);
close(fd);
return -;
} close(fd); return ;
} int main(void)
{
getLocalInfo(); return ;
}

使用ioctl函数虽然可以获取所有的信息,但是使用起来比较麻烦,如果不需要获取MAC地址,那么使用getifaddrs函数来获取更加方便与简洁。值得一提的是,在MacOS或iOS系统上(如iPhone程序开发),上述iotcl函数没法获得mac地址跟子网掩码,这个使用,使用getifaddrs函数便更有优势了。下面是使用getiaddrs函数获取网卡信息的C语言代码实现。

 #include <stdio.h>
#include <ifaddrs.h>
#include <arpa/inet.h> int getSubnetMask()
{
struct sockaddr_in *sin = NULL;
struct ifaddrs *ifa = NULL, *ifList; if (getifaddrs(&ifList) < )
{
return -;
} for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next)
{
if(ifa->ifa_addr->sa_family == AF_INET)
{
printf("n>>> interfaceName: %sn", ifa->ifa_name); sin = (struct sockaddr_in *)ifa->ifa_addr;
printf(">>> ipAddress: %sn", inet_ntoa(sin->sin_addr)); sin = (struct sockaddr_in *)ifa->ifa_dstaddr;
printf(">>> broadcast: %sn", inet_ntoa(sin->sin_addr)); sin = (struct sockaddr_in *)ifa->ifa_netmask;
printf(">>> subnetMask: %sn", inet_ntoa(sin->sin_addr));
}
} freeifaddrs(ifList); return ;
} int main(void)
{
getSubnetMask(); return ;
}

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指向链表的下一个成员;ifa_name是接口名称,以0结尾的字符串,比如eth0,lo;ifa_flags是接口的标识位(比如当IFF_BROADCAST或IFF_POINTOPOINT设置到此标识位时,影响联合体变量ifu_broadaddr存储广播地址或ifu_dstaddr记录点对点地址);ifa_netmask存储该接口的子网掩码;结构体变量存储广播地址或点对点地址(见括弧介绍ifa_flags);ifa_data存储了该接口协议族的特殊信息,它通常是NULL(一般不关注他)。

函数getifaddrs(int getifaddrs (struct ifaddrs **__ifap))获取本地网络接口信息,将之存储于链表中,链表头结点指针存储于__ifap中带回,函数执行成功返回0,失败返回-1,且为errno赋值。
    很显然,函数getifaddrs用于获取本机接口信息,比如最典型的获取本机IP地址。

Linux下C获取所有可用网卡信息的更多相关文章

  1. Linux下java获取CPU、内存、磁盘IO、网络带宽使用率

    一.CPU 使用proc文件系统,"proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为访问系统内核数据的操作提供接口.用户和应用程序可以通过proc得 ...

  2. 虚拟机Linux下一直获取不到ip怎么办

    虚拟机Linux下一直获取不到ip怎么办 Ifconfig -a 只显示了本地的ip127.0.0.1 和另一个eth1 但是找不到ip地址. 需要做的是: 申请ipdhclient eth1 另外释 ...

  3. linux下自动获取并安装软件包 apt-get 的命令介绍

    apt-cache search package    搜索包 apt-cache show package    获取包的相关信息,如说明.大小.版本等 sudo apt-get install p ...

  4. Linux下编程获取本地IP地址的常见方法

    转载于:http://blog.csdn.net/k346k346/article/details/48231933 在进行linux网络编程时,经常用到本机IP地址.本文罗列一下常见方法,以备不时之 ...

  5. Linux下"负载均衡+高可用"集群的考虑点 以及 高可用方案说明(Keepalive/Heartbeat)

    当下Linux运维技术越来越受到企业的关注和追捧, 在某些企业, 尤其是牵涉到电子商务和电子广告类的网站,通常会要求作负载均衡和高可用的Linux集群方案.那么如何实施Llinux集群架构,才能既有效 ...

  6. Linux下手动获取当前调用栈

    被问到如何手动获取当前的调用栈,之前碰到过一时没记起来,现在回头整理一下. 其原理是:使用backtrace()从栈中获取当前调用各层函数调用的返回地址,backtrace_symbols()将对应地 ...

  7. Linux下实现获取远程机器文件

    创建公钥秘钥实现无密码登录后即可获取到文件内容了!! A:xxx.xxx.6.xxx B:xxx.xxx.xxx.x 一.创建 A机器 ssh-keygen -t rsa 二.拷贝——将生成的公钥复制 ...

  8. Linux下Python获取IP地址

    <lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...

  9. Linux下wget获取ftp下目录下文件

    如果某个目录下有一个文件可以使用ftp命令: get xxx 如果是某个目录下有多个文件(且不需要获取目录下子文件夹下的内容): mget * 如果是某个目录下有子目录希望获取所有子目录: wget ...

随机推荐

  1. java EE :GenericServlet 抽象类、ServletConfig 接口

    ServletConfig 接口:当前 Servlet 在 web.xml 中相关配置信息 package javax.servlet; import java.util.Enumeration; p ...

  2. 【LOJ】#2012. 「SCOI2016」背单词

    题解 我们发现第一种操作肯定不可取,每个节点里它最近的点是它最长出现过的后缀,发现这就是AC自动机的fail节点,根据fail的关系这会是一棵树,而一个单词的前一个序号最大的后缀必定是它的父亲 然后我 ...

  3. bzoj 1108

    思路:水题, 将所有点按x轴对称反转,就变成了两堆点的坐标和的差.. #include<bits/stdc++.h> #define LL long long #define fi fir ...

  4. Codeforces Round 548 (Div. 2)

    layout: post title: Codeforces Round 548 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  5. JSP中动态INCLUDE与静态INCLUDE的区别?

    动态INCLUDE用jsp:include动作实现 它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数 静态INCLUDE用include伪码实现,定不会检查所含文件的变化,适用于包含 ...

  6. CSS 笔记——列表表格

    6. 列表表格 -> 列表 (1)list-style 基本语法 list-style : list-style-image || list-style-position || list-sty ...

  7. 「BZOJ4763」雪辉

    「BZOJ4763」天野雪辉 题目大意:有一棵 \(n\) 个点的树,树上每一个点有权值 \(a_i \leq 30000\) ,每次询问给出若干路径,求出这些路径的并上面的不同颜色数与 \(mex\ ...

  8. Every-SG 博弈论 mark定义和结论

    http://blog.sina.com.cn/s/blog_51cea4040100h3l9.html 这种类型,可以想成这样,有N组游戏,有N个穿红色衣服的人代表先手,有N个穿蓝色衣服的人代表后手 ...

  9. 69.广搜练习:  最少转弯问题(TURN)

    [问题描述] 给出一张地图,这张地图被分为n×m(n,m<=100)个方块,任何一个方块不是平地就是高山.平地可以通过,高山则不能.现在你处在地图的(x1,y1)这块平地,问:你至少需要拐几个弯 ...

  10. MySQL 之 Index Condition Pushdown(ICP)

    简介 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式. 当关闭ICP时,index 仅仅是data ...