Two Methods to Retrieve IPv4 Address of Network Interfaces in Linux Using C

Dec 03, 2025 · Programming · 10 views · 7.8

Keywords: Linux | C programming | IPv4 address | network interface | ioctl | getifaddrs

Abstract: This paper comprehensively explores two core methods for obtaining IPv4 addresses of network interfaces in Linux using C: the traditional approach based on ioctl system calls and the modern approach using the getifaddrs function. It analyzes data structures, implementation principles, and application scenarios, providing complete code examples to extract IP addresses from specific interfaces (e.g., eth0), and compares their advantages and disadvantages.

Introduction

In Linux system programming, retrieving the IPv4 address of a network interface is a common task, especially in network configuration, monitoring, and diagnostic applications. Based on high-scoring Q&A data from Stack Overflow, this paper systematically introduces two primary methods: using the ioctl system call and the getifaddrs function. These methods are applicable not only to Ethernet interfaces like eth0 but also extendable to other interface types.

Traditional Method Based on ioctl

ioctl (input/output control) is a system call in Unix/Linux systems for device control, commonly used in network programming to obtain interface information. The core steps of implementation are analyzed as follows:

First, necessary header files such as <sys/socket.h>, <net/if.h>, and <arpa/inet.h> must be included to access network-related data structures and functions. The key data structure is struct ifreq, which is used to pass interface request information between user space and the kernel. For example, the ifr.ifr_name field stores the interface name (e.g., "eth0"), while ifr.ifr_addr stores address information.

The code implementation starts by creating a socket: fd = socket(AF_INET, SOCK_DGRAM, 0);. Here, AF_INET (IPv4 protocol family) and SOCK_DGRAM (datagram socket) are used because ioctl operations typically do not depend on specific protocol types. Next, set ifr.ifr_addr.sa_family = AF_INET; to specify the IPv4 address, and use strncpy to safely copy the interface name to ifr.ifr_name.

The core call is ioctl(fd, SIOCGIFADDR, &ifr);, where SIOCGIFADDR is the command to get the interface address. After execution, ifr.ifr_addr will contain a struct sockaddr_in structure, with its sin_addr field storing the IPv4 address. Finally, use the inet_ntoa function to convert the binary address to a dotted-decimal string for output. For instance, for the eth0 interface, the output might be "192.168.1.100".

This method is straightforward but relies on older interfaces and may be limited in newer systems or complex network configurations. Additionally, it can only retrieve the address of a single interface, requiring multiple calls to handle multiple interfaces.

Modern Method Based on getifaddrs

As a supplement, the getifaddrs function provides a more modern and flexible way to obtain information about all network interfaces. It returns a linked list containing detailed information for each interface, such as name, address, and netmask.

The implementation involves calling getifaddrs(&ifaddr) to get the interface list, where ifaddr is a pointer to a struct ifaddrs linked list. Each node includes fields like ifa_name (interface name) and ifa_addr (address structure). By traversing the list, specific interfaces (e.g., eth0) and address families (AF_INET) can be filtered.

For example, use the getnameinfo function to convert the address to a readable string: s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);. Here, the NI_NUMERICHOST flag ensures the output is in numeric format (e.g., IPv4 address). If successful, the host buffer will store the address string.

This method has advantages in retrieving all interface information at once, supporting both IPv4 and IPv6, and being easier to extend. However, it requires more memory and processing overhead, which may be overly complex for simple tasks.

Comparison and Conclusion

Both methods have their applicable scenarios: the ioctl method is suitable for quickly obtaining the IPv4 address of a single interface, with concise code but poorer portability; the getifaddrs method is more comprehensive, supporting multiple interfaces and protocols, but with a slightly more complex implementation. In practical applications, the choice should be based on needs: if only the IPv4 address of eth0 is required, ioctl is an efficient choice; if monitoring all interfaces or handling IPv6 is needed, getifaddrs is more appropriate.

Through in-depth analysis of core data structures and code examples, this paper provides practical guidance for Linux network programming. Developers should understand underlying principles, such as the usage of struct ifreq and struct ifaddrs, to ensure code robustness and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.