根据网卡名获取IP地址,以及掩码地址

简介:

nterface name is something like “eth0″ and the ip address of the interface can be retrieved using the ioctl function.
Here is a simple piece of code that demonstrates how :

Code

1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <sys/ioctl.h>
6 #include <netinet/in.h>
7 #include <net/if.h>
8 #include <unistd.h>
9 #include <arpa/inet.h>
10  
11 int main()
12 {
13     int fd;
14     struct ifreq ifr;
15      
16     char iface[] = "eth0";
17      
18     fd = socket(AF_INET, SOCK_DGRAM, 0);
19  
20     //Type of address to retrieve - IPv4 IP address
21     ifr.ifr_addr.sa_family = AF_INET;
22  
23     //Copy the interface name in the ifreq structure
24     strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
25  
26     ioctl(fd, SIOCGIFADDR, &ifr);
27  
28     close(fd);
29  
30     //display result
31     printf("%s - %s\n" , iface , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
32  
33     return 0;
34 }

Output

1 $ gcc ioctl.c && ./a.out
2 eth0 - 192.168.0.6

The socket used can be a TCP socket (SOCK_STREAM) as well.

If you also need the netmask then use the SIOCGIFNETMASK value in ioctl like this :

1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <sys/ioctl.h>
5 #include <net/if.h>
6 #include <unistd.h>
7 #include <arpa/inet.h>
8  
9 int main()
10 {
11     int fd;
12     struct ifreq ifr;
13      
14     char iface[] = "eth0";
15      
16     fd = socket(AF_INET, SOCK_DGRAM, 0);
17  
18     //Type of address to retrieve - IPv4 IP address
19     ifr.ifr_addr.sa_family = AF_INET;
20  
21     //Copy the interface name in the ifreq structure
22     strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
23      
24     //get the ip address
25     ioctl(fd, SIOCGIFADDR, &ifr);
26      
27     //display ip
28     printf("IP address of %s - %s\n" , iface , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
29      
30     //get the netmask ip
31     ioctl(fd, SIOCGIFNETMASK, &ifr);
32      
33     //display netmask
34     printf("Netmask of %s - %s\n" , iface , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
35      
36     close(fd);
37      
38     return 0;
39 }

Output :

1 $ gcc ioctl.c && ./a.out
2 IP address of eth0 - 192.168.0.6
3 Netmask of eth0 - 255.255.255.0
目录
相关文章
|
16天前
|
负载均衡 安全 Linux
为何一个网卡需要配置多个IP地址?🌐
在Linux环境中,一个网卡配置多个IP地址是一个常见且强大的网络管理策略🛠️。这种策略不仅增加了网络的灵活性和效率,还能满足特定的网络需求和应用场景🎯。让我们一探究竟,看看在哪些情况下,为什么一个网卡会需要配置多个IP地址,并探讨不配置多个IP地址的后果。
为何一个网卡需要配置多个IP地址?🌐
|
16天前
|
负载均衡 网络架构
IP地址:是给主机配置的,还是给网卡配置的?🤔
在探索网络的奥秘时,我们经常会遇到一个看似简单但又复杂的问题:IP地址到底是配置在主机上,还是配置在网卡上?为什么我们通常说的是“主机IP地址”呢?让我们一起深入探讨。
IP地址:是给主机配置的,还是给网卡配置的?🤔
|
2月前
|
安全 网络协议 测试技术
网络地址 0.0.0.0 与回环地址 127.0.0.1 的区别
网络地址 0.0.0.0 与回环地址 127.0.0.1 的区别
43 0
|
9月前
IP地址10.224.12.1的子网掩码为255.240.0.0,和该地址在同一子网中的地址是
IP地址10.224.12.1的子网掩码为255.240.0.0,和该地址在同一子网中的地址是
79 0
|
安全
为什么要区分不同的网段IP地址
为什么要区分不同的网段IP地址
92 0
|
小程序 Java 网络架构
自动获取计算机名、用户名、IP地址、子网掩码和默认网关
本单位有很多计算机设备(成千上万),现需要统计计算机名、用户名、MAC地址、IP地址、默认网关、子网掩码信息,咋办捏。
301 0
自动获取计算机名、用户名、IP地址、子网掩码和默认网关