1、根据ioctl机制打印当前所有网卡

代码:

#include

#include

#include

#include

#include

#include

int get_local_ip(char *ip)

{

int fd, intrface, retn = 0;

struct ifreq buf[INET_ADDRSTRLEN];

struct ifconf ifc;

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)buf;

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);

return 0;

}

}

int main()

{

char ip[64];

memset(ip, 0, sizeof(ip));

get_local_ip(ip);

return 0;

}

编译:

[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

代码解析:

中定义了#define INET_ADDRSTRLEN 16

ifreq、ifconf 、ioctl参考:http://blog.csdn.net/wl_haanel/article/details/4536236

2、通过枚举网卡打印当前所有网卡

代码:

#include

#include

#include

#include

#include

int get_local_ip(char *ip) {

struct ifaddrs *ifAddrStruct;

void *tmpAddrPtr=NULL;

getifaddrs(&ifAddrStruct);

while (ifAddrStruct != NULL) {

if (ifAddrStruct->ifa_addr->sa_family==AF_INET) {

tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;

inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN);

printf("%s IP Address:%s\n", ifAddrStruct->ifa_name, ip);

}

ifAddrStruct=ifAddrStruct->ifa_next;

}

//free ifaddrs

freeifaddrs(ifAddrStruct);

return 0;

}

int main()

{

char ip[16];

memset(ip, 0, sizeof(ip));

get_local_ip(ip);

return 0;

}

编译:

[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

#include

#include

#include

#include

int get_local_ip(char *ips) {

struct ifaddrs *ifAddrStruct;

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_addr;

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);

} else {

strncat(ips, ",", 1);

strncat(ips, ip, INET_ADDRSTRLEN);

}

n++;

}

ifAddrStruct=ifAddrStruct->ifa_next;

}

//free ifaddrs

freeifaddrs(ifAddrStruct);

return 0;

}

int main()

{

char ip[64];

memset(ip, 0, sizeof(ip));

get_local_ip(ip);

printf("IP:%s\n", ip);

return 0;

}

编译:

[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

#include

#include

#include

#include

int get_local_ip(char *ip_list) {

struct ifaddrs *ifAddrStruct;

void *tmpAddrPtr;

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_addr;

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);

} else {

memcpy(ip_list+INET_ADDRSTRLEN, ip, INET_ADDRSTRLEN);

}

n++;

}

}

ifAddrStruct=ifAddrStruct->ifa_next;

}

//free ifaddrs

freeifaddrs(ifAddrStruct);

return n;

}

int main()

{

char ip[3][INET_ADDRSTRLEN];

memset(ip, 0, sizeof(ip));

int n;

for (n=get_local_ip(*ip); n>0; n--) {

printf("IP:%s\n", ip[n-1]);

}

return 0;

}

编译:

[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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐