linux内核完全剖析下当前的IP地址存在哪个数组里

C/C++/Go Linux/Web 全栈之路
linux C如何获取服务器节点上所有网口的ip地址
之前项目原因,需要获取当前服务器节点上所有网口的ip地址,但是当时由于时间比较紧,一直没搞出来,最近没那么忙了,又在网上找了一下,终于实现了这一个功能,因此记录下来,以备不时之需。
这种获取所有ip的方法主要是通过遍历所有网口信息而获取的,即首先通过函数ioctl获取所有网口的信息,然后再逐一遍历每个网口,解析出网口对应的ip地址。下面是实现代码是经过验证的。
#include &stdio.h&
#include &stdlib.h&
#include &unistd.h&
#include &linux/types.h&
#include &string.h&
#include &sys/socket.h&
#include &netinet/in.h&
#include &net/if.h&
#include &netdb.h&
#include &arpa/inet.h&
#include &sys/ioctl.h&
//获取地址
//返回IP地址字符串
//返回:0=成功,-1=失败
int get_all_localip()
int i = 0;
char buf[512];
struct ifreq *
//初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf =
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) & 0)
return -1;
ioctl(sockfd, SIOCGIFCONF, &ifconf);
//获取所有接口信息
close(sockfd);
//接下来一个一个的获取IP地址
ifreq = (struct ifreq*)
for(i = (ifconf.ifc_len / sizeof(struct ifreq)); i & 0; i--)
ip = inet_ntoa(((struct sockaddr_in*)&(ifreq-&ifr_addr))-&sin_addr);
if(strcmp(ip,"127.0.0.1") == 0)
//排除127.0.0.1,继续下一个
printf("IP地址: %s\n", ip);
ip = NULL;
int main() {
get_all_localip();
以上参考:
rhel5.4-i386
#include &net/if.h&其实你可以&find /usr/include/ -name "*.h" -exec grep -l "ifconf" {} \;
在linux下c语言获取本机ip地址的函数,解除了只能获取127.0.0.1的bug.获得的是大端的4字节的ip地址,不是17字节的字符串地址.本人项目中已经在使用了
#include &stdio.h&
#include &stdlib.h&
#include &sys/types.h&
#include &sys/socket.h&
#include &netinet/in.h&
#include &netdb.h&
#include &arpa/inet.h&
#include &signal.h&
#include &sys/ioctl.h&
#include &linux/if.h&
long getlocalhostip(){
MAXINTERFACES=16;
int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES]; ///if.h
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) &= 0) //socket.h
ifc.ifc_len =
ifc.ifc_buf = (caddr_t)
if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) //ioctl.h
intrface = ifc.ifc_len / sizeof (struct ifreq);
while (intrface-- & 0)
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
ip=inet_addr( inet_ntoa( ((struct sockaddr_in*)(&buf[intrface].ifr_addr))-&sin_addr) );//types
close (fd);
union ipu{
unsigned char ipchar[4];
int main(int argc, char **argv){
iptest.ip = getlocalhostip();
printf("local ip:%x :%3u.%3u.%3u.%3u \n",iptest.ip, iptest.ipchar[0],iptest.ipchar[1],iptest.ipchar[2],iptest.ipchar[3]);
用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人
来说都是比较陌生的,这里给大家一种比较简单的理解方法,当然只一种帮助
理解的方法,在描述中可能会有一些地方与真实定义有所出入,仅供参考.
首先先认识一下ifconf和ifreq:
//ifconf通常是用来保存所有接口信息的
/* size of buffer
/* input from user-&kernel*/
ifreq *ifcu_
/* return from kernel-&user*/
} ifc_};#define
ifc_ifcu.ifcu_buf
/* buffer address
ifc_ifcu.ifcu_req
/* array of structures
//ifreq用来保存某个接口的信息
ifr_name[IFNAMSIZ];
sockaddr ifru_
sockaddr ifru_
sockaddr ifru_
caddr_t ifru_
} ifr_};#define
ifr_addr ifr_ifru.ifru_addr#define
ifr_dstaddr ifr_ifru.ifru_dstaddr#define
ifr_broadaddr ifr_ifru.ifru_broadaddr
上边这两个结构看起来比较复杂,我们现在把它们简单化一些:
比如说现在我们向实现获得本地IP的功能。
我们的做法是:
1. 先通过ioctl获得本地所有接口的信息,并保存在ifconf中
2. 再从ifconf中取出每一个ifreq中表示ip地址的信息
具体使用时我们可以认为ifconf就有两个成员:
ifc_len 和 ifc_buf, 如图一所示:    
ifc_len:表示用来存放所有接口信息的缓冲区长度
ifc_buf:表示存放接口信息的缓冲区
所以我们需要在程序开始时对ifconf的ifc_len和ifc_buf进行初始化
接下来使用ioctl获取所有接口信息,完成后ifc_len内存放实际获得的借口信息总长度
并且信息被存放在ifc_buf中。
如下图示:(假设读到两个接口信息)
接下来我们只需要从一个一个的接口信息获取ip地址信息即可。
下面有一个简单的参考:
#include #include #include #include #include in
.h&#include &string
.h&#include if
.h&#include int
unsigned char
buf[512]; struct
//初始化ifconf
ifconf.ifc_len = 512; ifconf.ifc_buf =
((sockfd = socket(AF_INET, SOCK_DGRAM, 0))&0)
perror("socket"
ioctl(sockfd, SIOCGIFCONF, &ifconf);
//获取所有接口信息
//接下来一个一个的获取IP地址
ifreq = (struct
(i=(ifconf.ifc_len/sizeof
ifreq)); i&0; i--) {//
if(ifreq-&ifr_flags == AF_INET){
//for ipv4
printf("name = [%s]\n"
, ifreq-&ifr_name);
printf("local addr = [%s]\n"
,inet_ntoa(((struct
sockaddr_in*)&(ifreq-&ifr_addr))-&sin_addr));
ifreq++;// }
---------------------------------------------------
Linux C 获取本机IP地址的方法,排除127.0.0.1
#include &stdio.h&
#include &stdlib.h&
#include &unistd.h&
#include &linux/types.h&
#include &string.h&
#include &sys/socket.h&
#include &netinet/in.h&
#include &net/if.h&
#include &netdb.h&
#include &arpa/inet.h&
#include &sys/ioctl.h&
//获取地址
//返回IP地址字符串
//返回:0=成功,-1=失败
int getlocalip(char* outip)
char buf[512];
struct ifreq *
//初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf =
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))&0)
return -1;
ioctl(sockfd, SIOCGIFCONF, &ifconf);
//获取所有接口信息
close(sockfd);
//接下来一个一个的获取IP地址
ifreq = (struct ifreq*)
for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i&0; i–)
ip = inet_ntoa(((struct sockaddr_in*)&(ifreq-&ifr_addr))-&sin_addr);
if(strcmp(ip,”127.0.0.1″)==0)
//排除127.0.0.1,继续下一个
strcpy(outip,ip);
return -1;
//——————————-函数的调用方式————————————-
char ip[20];
if ( getlocalip( ip ) == 0 )
printf( “ 本机IP地址是: %s\n”, ip );
printf( ” 无法获取本机IP地址
----------------------------
ifconf和ifreq
用ioctl获得本地ip地址时要用到两个结构体ifconf和ifreq,它们对于大多数人来说都是比较陌生的,这里给一种比较简单的理解方法, 仅供参考.
首先先认识一下ifconf和ifreq:
//ifconf通常是用来保存所有接口信息的 //ifreq用来保存某个接口的信息
struct ifreq {
char ifr_name[IFNAMSIZ];
struct sockaddr ifru_
struct sockaddr ifru_
struct sockaddr ifru_
short ifru_
caddr_t ifru_
#define ifr_addr ifr_ifru.ifru_addr
#define ifr_dstaddr ifr_ifru.ifru_dstaddr
#define ifr_broadaddr ifr_ifru.ifru_broadaddr
上边这两个结构看起来比较复杂,我们现在把它们简单化一些:比如说现在我们向实现获得本地IP的功能。
我们的做法是: 1. 先通过ioctl获得本地所有接口的信息,并保存在ifconf中 2. 再从ifconf中取出每一个ifreq中表示ip地址的信息
具体使用时我们可以认为ifconf就有两个成员:ifc_len 和 ifc_buf,如图一所示:    
ifc_len:表示用来存放所有接口信息的缓冲区长度 ifc_buf:表示存放接口信息的缓冲区
所以我们需要在程序开始时对ifconf的ifc_len和ifc_buf进行初始化接下来使用ioctl获取所有接口信息,完成后ifc_len内存放实际获得的接口信息总长度,并且信息被存放在ifc_buf中。如下图示:(假设读到两个接口信息)
接下来我们只需要从一个一个的接口信息获取ip地址信息即可。
下面有一个简单的参考:
int main(void)
unsigned char buf[512];
struct ifreq * //初始化ifconf
ifconf.ifc_len = 512;
ifconf.ifc_buf =
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))&0)
perror("socket");
ioctl(sockfd, SIOCGIFCONF, &ifconf); //获取所有接口信息
//逐个获取IP地址
ifreq = (struct ifreq*)
for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i&0; i--)
//if(ifreq-&ifr_flags == AF_INET){
//for ipv4
printf("name = [%s]n", ifreq-&ifr_name);
printf("local addr = [%s]n",inet_ntoa(((struct sockaddr_in*)&(ifreq-&ifr_addr))-&sin_addr));
---------------------------------------------------------------------------
获得Unix/Linux系统中的IP、MAC地址等信息
实际环境和特殊需求往往会将简单问题复杂化,比如计算机IP地址,对于一个连接中socket,可以直接获得本端和对端的IP、端口信息。但在一些特殊场合我们可能需要更多的信息,比如系统中有几块网卡,他们的Mac地址是多少,每块网卡分配了几个IP(一个网卡对应多个IP)等等。
这些信息往往需要通过ifconfig指令来获得,对于程序员来说,在代码中调用外部的shell指令可不是个最佳方案,因为没人能保障不同平台、不同版本的ifconfig指令输出的格式是一致的。本篇文章中将介绍通过ioctl函数实现上述需求。
#include &sys/ioctl.h&int ioctl(int fd, int request, … /* void *arg */);
返回:成功返回0,失败返回-1
ioctl函数的参数只有3个,但却是Unix中少有的几个“家族类”复杂函数,这里摘录一段《Unix网络编程》一书中对ioctl函数的描述:
在传统上ioctl函数是用于那些普遍使用、但不适合归入其他类别的任何特殊的系统接口……网络程序(一般是服务器程序)中ioctl常用于在程序启动时获得主机上所有接口的信息:接口的地址、接口是否支持广播、是否支持多播,等等。
ioctl函数的第一个参数fd,可以表示一个打开的文件(文件句柄)或网络套接字,第二个和第三个参数体现了函数的家族特色,参数二request根据函数功能分类定义了多组宏,而参数三总是一个指针,指针的类型依赖于参数二request。因为ioctl的种类实在太多,这里只列出和本文相关的几个参数定义:
参数二(宏)
SIOCGIFCONF
struct ifconf
获得所有接口列表
SIOCGIFADDR
struct ifreq
获得接口地址
SIOCGIFFLAGS
struct ifreq
获得接口标志
SIOCGIFBRDADDR
struct ifreq
获得广播地址
SIOCGIFNETMASK
struct ifreq
获得子网掩码
上表中列出了两个相关的结构体:struct ifconf 和 struct ifreq,要了解ioctl函数的具体运用,首先要了解这两个结构:
/* net/if.h */
struct ifconf
/* Size of buffer.
__caddr_t ifcu_
struct ifreq *ifcu_
struct ifreq
# define IFHWADDRLEN
# define IFNAMSIZ
IF_NAMESIZE
char ifrn_name[IFNAMSIZ];
/* Interface name, e.g. "en0".
struct sockaddr ifru_
struct sockaddr ifru_
struct sockaddr ifru_
struct sockaddr ifru_
struct sockaddr ifru_
short int ifru_
struct ifmap ifru_
char ifru_slave[IFNAMSIZ];
/* Just fits the size */
char ifru_newname[IFNAMSIZ];
__caddr_t ifru_
struct ifconf的第二个元素ifc_ifcu是一个联合,是指向struct ifreq结构的地址,通常是一组struct ifreq结构空间(每一个描述一个接口),struct ifconf的第一个元素ifc_len描述了struct ifreq结构空间的大小;结构struct ifreq也有两个元素,第一个元素ifr_ifrn内含一个字符串,用来描述接口的名称,比如“eth0″、”wlan0”等,第二个元素是联合,比较复杂,用来描述套接口的地址结构。
struct ifconf 和 struct ifreq的关系可以参考下图:
ioctl函数中的struct ifconf 和 struct ifreq结构关系
通常运用ioctl函数的第一步是从内核获取系统的所有接口,然后再针对每个接口获取其地址信息。获取所有接口通过SIOCGIFCONF请求来实现:
/* ifconf结构 */
struct ifreq ifrs[16];
ifreq结构数组(这里估计了接口的最大数量16) */
/* 初始化ifconf结构 */
ifc.ifc_len = sizeof(ifrs);
ifc.ifc_buf = (caddr_t)
/* 获得接口列表 */
ioctl(fd, SIOCGIFCONF, (char *) &ifc);
获得了接口列表,就可以通过struct ifconf结构中*ifcu_req的指针得到struct ifreq结构数组的地址,通过遍历获得每隔接口的详细地址信息:
("接口名称:%s/n",
ifrs[n].ifr_name); /* 接口名称 */
/* 获得IP地址 */
ioctl(fd, SIOCGIFADDR, (char *) &ifrs[n]);
("IP地址:%s/n",
(char*)inet_ntoa(((struct sockaddr_in*) (&ifrs[n].ifr_addr))-&sin_addr));
/* 获得子网掩码 */
ioctl(fd, SIOCGIFNETMASK, (char *) &ifrs[n]);
("子网掩码:%s/n",
(char*)inet_ntoa(((struct sockaddr_in*) (&ifrs[n].ifr_addr))-&sin_addr));
/* 获得广播地址 */
ioctl(fd, SIOCGIFBRDADDR, (char *) &ifrs[n]);
("广播地址:%s/n",
(char*)inet_ntoa(((struct sockaddr_in*) (&ifrs[n].ifr_addr))-&sin_addr));
/* 获得MAC地址 */
ioctl(fd, SIOCGIFHWADDR, (char *) &ifrs[n]);
("MAC地址:%02x:%02x:%02x:%02x:%02x:%02x/n",
(unsigned char) ifrs[n].ifr_hwaddr.sa_data[0],
(unsigned char) ifrs[n].ifr_hwaddr.sa_data[1],
(unsigned char) ifrs[n].ifr_hwaddr.sa_data[2],
(unsigned char) ifrs[n].ifr_hwaddr.sa_data[3],
(unsigned char) ifrs[n].ifr_hwaddr.sa_data[4],
(unsigned char) ifrs[n].ifr_hwaddr.sa_data[5]);
最后,给出一个参考程序代码。
ioctl函数没有纳入POXIS规范,各系统对ioctl的实现也不尽相同,下面的代码在我的Ubuntu10.04 linux上可执行通过,但在其他Unix系统上不一定能够通过编译,例如在Power AIX 5.3上需要将获得MAC地址的那段代码注释掉。
#include &arpa/inet.h&
#include &net/if.h&
#include &net/if_arp.h&
#include &netinet/in.h&
#include &stdio.h&
#include &sys/ioctl.h&
#include &sys/socket.h&
#include &unistd.h&
#define MAXINTERFACES 16
/* 最大接口数 */
/* 套接字 */
/* 接口数量 */
struct ifreq buf[MAXINTERFACES];
/* ifreq结构数组 */
/* ifconf结构 */
int main(argc, argv)
/* 建立IPv4的UDP套接字fd */
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
perror("socket(AF_INET, SOCK_DGRAM, 0)");
return -1;
/* 初始化ifconf结构 */
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)
/* 获得接口列表 */
if (ioctl(fd, SIOCGIFCONF, (char *) &ifc) == -1)
perror("SIOCGIFCONF ioctl");
return -1;
if_len = ifc.ifc_len / sizeof(struct ifreq); /*
接口数量 */
("接口数量:%d/n/n",
while (if_len– & 0) /*
遍历每个接口 */
("接口:%s/n",
buf[if_len].ifr_name); /* 接口名称 */
/* 获得接口标志 */
if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[if_len])))
/* 接口状态 */
if (buf[if_len].ifr_flags & IFF_UP)
("接口状态:
("接口状态:
char str[256];
sprintf(str, "SIOCGIFFLAGS ioctl %s", buf[if_len].ifr_name);
perror(str);
/* IP地址 */
if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[if_len])))
("IP地址:%s/n",
(char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))-&sin_addr));
char str[256];
sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
/* 子网掩码 */
if (!(ioctl(fd, SIOCGIFNETMASK, (char *) &buf[if_len])))
("子网掩码:%s/n",
(char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))-&sin_addr));
char str[256];
sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
/* 广播地址 */
if (!(ioctl(fd, SIOCGIFBRDADDR, (char *) &buf[if_len])))
("广播地址:%s/n",
(char*)inet_ntoa(((struct sockaddr_in*) (&buf[if_len].ifr_addr))-&sin_addr));
char str[256];
sprintf(str, "SIOCGIFADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
/*MAC地址 */
if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[if_len])))
("MAC地址:%02x:%02x:%02x:%02x:%02x:%02x/n/n",
(unsigned char) buf[if_len].ifr_hwaddr.sa_data[0],
(unsigned char) buf[if_len].ifr_hwaddr.sa_data[1],
(unsigned char) buf[if_len].ifr_hwaddr.sa_data[2],
(unsigned char) buf[if_len].ifr_hwaddr.sa_data[3],
(unsigned char) buf[if_len].ifr_hwaddr.sa_data[4],
(unsigned char) buf[if_len].ifr_hwaddr.sa_data[5]);
char str[256];
sprintf(str, "SIOCGIFHWADDR ioctl %s", buf[if_len].ifr_name);
perror(str);
}//–while end
//关闭socket
close(fd);
在我的系统上,程序输出:
接口数量:4
接口:wlan0
接口状态: UP
IP地址:192.168.1.142
子网掩码:255.255.255.0
广播地址:192.168.1.255
MAC地址:00:14:a5:65:47:57
接口:eth0:0
接口状态: UP
IP地址:192.168.4.113
子网掩码:255.255.255.0
广播地址:192.168.4.255
MAC地址:00:14:c2:e5:45:57
接口:eth0
接口状态: UP
IP地址:192.168.4.111
子网掩码:255.255.255.0
广播地址:192.168.4.255
MAC地址:00:14:c2:e5:45:57
接口状态: UP
IP地址:127.0.0.1
子网掩码:255.0.0.0
广播地址:0.0.0.0
MAC地址:00:00:00:00:00:00
从输出可以看出,系统有4个接口,”wlan0″表示第一块无线网卡接口,”eth0″(IP地址:192.168.4.111)表示第一块连线网卡接口(我们最长用的RJ45连接口网卡),”lo”是回路地址接口(我们常用的127.0.0.1)。
注意:”eth0:0″(IP地址:192.168.4.113)是有线网卡的别名(单网卡绑定多个IP),这是为了测试这个参考程序特意在eth0上添加的一个IP地址。
参考资料:《Unix网络编程》第16章 ioctl操作
没有更多推荐了,&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
获取Linux下的IP地址
摘要:/***获取Linux下的IP地址**@returnIP地址*@throwsSocketException*/publicstaticStringgetLinuxLocalIp()throwsSocketException{Stringip=&&;try{for(Enumeration&NetworkInterface&en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements()
/** * 获取Linux下的IP地址 * * @return IP地址 * @throws SocketException */public static String getLinuxLocalIp() throws SocketException { String ip = &&; try { for (Enumeration&NetworkInterface& en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); String name = intf.getName(); if (!name.contains(&docker&) &;&; !name.contains(&lo&)) { for (Enumeration&InetAddress& enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { String ipaddress = inetAddress.getHostAddress().toString(); if (!ipaddress.contains(&::&) &;&; !ipaddress.contains(&0:0:&) &;&; !ipaddress.contains(&fe80&)) { ip = } } } } } } catch (SocketException ex) { System.out.println(&获取ip地址异常&); ex.printStackTrace(); } System.out.println(&IP:& + ip);}
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
邮箱低至5折
推荐购买再奖现金,最高25%
&200元/3月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
获取Linux下的IP地址相关信息,包括
的信息,所有获取Linux下的IP地址相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
InternationalLinux Server
Linux C语言 获取本机(所有网卡)IP地址(IPV4)
1、根据ioctl机制打印当前所有网卡
#include &sys/ioctl.h&
#include &net/if.h&
#include &netinet/in.h&
#include &arpa/inet.h&
#include &string.h&
#include &stdio.h&
int get_local_ip(char *ip)
int fd, intrface, retn = 0;
struct ifreq buf[INET_ADDRSTRLEN];
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) &= 0)
ifc.ifc_len = sizeof(buf);
// caddr_t,linux内核源码里定义的:typedef void *caddr_t;
ifc.ifc_buf = (caddr_t)
if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
intrface = ifc.ifc_len/sizeof(struct ifreq);
while (intrface-- & 0)
if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[intrface])))
ip=(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))-&sin_addr));
printf("IP:%s\n", ip);
close(fd);
int main()
char ip[64];
memset(ip, 0, sizeof(ip));
get_local_ip(ip);
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
IP:192.168.2.53
IP:192.168.2.55
IP:127.0.0.1
代码解析:
&netinet/in.h&中定义了 #define INET_ADDRSTRLEN 16
ifreq、ifconf 、ioctl参考:
2、通过枚举网卡打印当前所有网卡
#include &stdio.h&
#include &ifaddrs.h&
#include &netinet/in.h&
#include &string.h&
#include &arpa/inet.h&
int get_local_ip(char *ip) {
struct ifaddrs *ifAddrS
void *tmpAddrPtr=NULL;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct != NULL) {
if (ifAddrStruct-&ifa_addr-&sa_family==AF_INET) {
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct-&ifa_addr)-&sin_
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
printf("%s IP Address:%s\n", ifAddrStruct-&ifa_name, ip);
ifAddrStruct=ifAddrStruct-&ifa_
//free ifaddrs
freeifaddrs(ifAddrStruct);
int main()
char ip[16];
memset(ip, 0, sizeof(ip));
get_local_ip(ip);
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
lo IP Address:127.0.0.1
eth0 IP Address:192.168.2.55
eth1 IP Address:192.168.2.53
代码解析:
API接口可查看man 7 netdevice
3、多网卡情况下,将所有IP地址以字符串形式打印出,用逗号(“,”)隔开,形式如:“127.0.0.1,192.168.2.55,192.168.2.53”
#include &stdio.h&
#include &ifaddrs.h&
#include &netinet/in.h&
#include &string.h&
#include &arpa/inet.h&
int get_local_ip(char *ips) {
struct ifaddrs *ifAddrS
void *tmpAddrPtr=NULL;
char ip[INET_ADDRSTRLEN];
int n = 0;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct != NULL) {
if (ifAddrStruct-&ifa_addr-&sa_family==AF_INET) {
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct-&ifa_addr)-&sin_
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
printf("%s IP Address:%s\n", ifAddrStruct-&ifa_name, ip);
if (n == 0){
strncat(ips, ip, INET_ADDRSTRLEN);
strncat(ips, ",", 1);
strncat(ips, ip, INET_ADDRSTRLEN);
ifAddrStruct=ifAddrStruct-&ifa_
//free ifaddrs
freeifaddrs(ifAddrStruct);
int main()
char ip[64];
memset(ip, 0, sizeof(ip));
get_local_ip(ip);
printf("IP:%s\n", ip);
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
lo IP Address:127.0.0.1
eth0 IP Address:192.168.2.55
eth1 IP Address:192.168.2.53
IP:127.0.0.1,192.168.2.55,192.168.2.53
4、多网卡情况下,将多个ip地址放入字符串数组中(去掉"127.0.0.1"地址)
#include &stdio.h&
#include &ifaddrs.h&
#include &netinet/in.h&
#include &string.h&
#include &arpa/inet.h&
int get_local_ip(char *ip_list) {
struct ifaddrs *ifAddrS
void *tmpAddrP
char ip[INET_ADDRSTRLEN];
int n = 0;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct != NULL) {
if (ifAddrStruct-&ifa_addr-&sa_family==AF_INET) {
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct-&ifa_addr)-&sin_
inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);
if (strcmp(ip, "127.0.0.1") != 0) {
printf("%s IP Address:%s\n", ifAddrStruct-&ifa_name, ip);
if (n == 0){
memcpy(ip_list, ip, INET_ADDRSTRLEN);
memcpy(ip_list+INET_ADDRSTRLEN, ip, INET_ADDRSTRLEN);
ifAddrStruct=ifAddrStruct-&ifa_
//free ifaddrs
freeifaddrs(ifAddrStruct);
int main()
char ip[3][INET_ADDRSTRLEN];
memset(ip, 0, sizeof(ip));
for (n=get_local_ip(*ip); n&0; n--) {
printf("IP:%s\n", ip[n-1]);
[root@localhost get_ip]# gcc -Wall -g ip.c
[root@localhost get_ip]# ./a.out
IP:192.168.2.53
IP:192.168.2.55
没有更多推荐了,

我要回帖

更多关于 linux内核完全剖析 的文章

 

随机推荐