Cyberithub

Useful C Program to List Network Interfaces using only 30 Lines of Code

In this tutorial, I will take you through a C Program to list network interfaces and IP address associated with it in Linux. Network Interface is a medium which allows a System or Computer to be connected over a network.

Why C Programming ?

You can easily write a program in c using inbuilt C Library functions to list network interfaces and ip address details associated with it.

Client and Server Model

In this model, a server listens for connections. The client, knowing the address and port number that the server is listening on, establishes the connection by sending the first packet.

For example, the web server at example.com listens on port 80 (HTTP) and port 443 (HTTPS). A web browser (client) must establish the connection by sending the first packet to the web server address and port.

sizeof() function in C

Using sizeof() function, you can create memory space for any objects at compile time. To know more about sizeof function in C, you can visit 10 Useful Examples of sizeof function in C.

Address Family in C

C contains address family for different protocols and different interfaces. For ex: AF_INET is the address family for IPv4 addresses and AF_INET6 is the address family for IPv6 addresses. You can use these family in your program to list network interfaces and IP associated with it.

AF_UNIX, AF_LOCAL: Local communication
AF_INET: IPv4 Internet protocols
AF_INET6: IPv6 Internet protocols
AF_IPX: IPX – Novell protocols
AF_NETLINK: Kernel user interface device
AF_X25: ITU-T X.25 / ISO-8208 protocol
AF_AX25: Amateur radio AX.25 protocol
AF_ATMPVC: Access to raw ATM PVCs
AF_APPLETALK: Appletalk
AF_PACKET: Low level packet interface

Useful C Program to List Network Interfaces using only 30 Lines of Code 1

struct sockaddr_in

An IP socket address is defined as a combination of an IP interface address and a 16-bit port number. The basic IP protocol does not supply port numbers, they are implemented by higher level protocols like udp and tcp. On raw sockets sin_port is set to the IP protocol. To know more about IP, You can visit IP Man Page.

struct sockaddr_in {
short int sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
};

struct sockaddr_in6

The address notation for IPv6 is a group of 8 4-digit hexadecimal numbers, separated with a ‘:’. “::” stands for a string of 0 bits. Special addresses are ::1 for loopback and ::FFFF:<IPv4 address> for IPv4-mapped-on-IPv6. To know more about IPv6, you can visit IPv6 Man Page.

struct sockaddr_in6 {
sa_family_t sin6_family; 
in_port_t sin6_port; 
uint32_t sin6_flowinfo; 
struct in6_addr sin6_addr;
uint32_t sin6_scope_id; 
};

struct ifaddrs

The getifaddrs() function creates a linked list of structures describing the network interfaces of the local system, and stores the address of the first item of the list in *ifap. The list consists of ifaddrs structures, defined as follows. To know more about getifaddrs, you can visit getifaddres Man Page.

struct ifaddrs {
struct ifaddrs *ifa_next; 
char *ifa_name; 
unsigned int ifa_flags; 
struct sockaddr *ifa_addr; 
struct sockaddr *ifa_netmask; 
union {
struct sockaddr *ifu_broadaddr;
struct sockaddr *ifu_dstaddr;
} ifa_ifu;
#define ifa_broadaddr ifa_ifu.ifu_broadaddr
#define ifa_dstaddr ifa_ifu.ifu_dstaddr
void *ifa_data; 
};

List Network Interfaces

Also Read: Easy Steps to Install GCC On CentOS 7

#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
struct ifaddrs *addresses;
if (getifaddrs(&addresses) == -1)
{
printf("getifaddrs call failed\n");
return -1;
}

struct ifaddrs *address = addresses;
while(address)
{
int family = address->ifa_addr->sa_family;
if (family == AF_INET || family == AF_INET6)
{
printf("%s\t", address->ifa_name);
printf("%s\t", family == AF_INET ? "IPv4" : "IPv6");
char ap[100];
const int family_size = family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
getnameinfo(address->ifa_addr,family_size, ap, sizeof(ap), 0, 0, NI_NUMERICHOST);
printf("\t%s\n", ap);
}
address = address->ifa_next;
}
freeifaddrs(addresses);
return 0;
}

Compile Your Program

[root@localhost ~]# gcc -o network network.c

Run Your Program

[root@localhost ~]# ./network

Output

lo      IPv4 127.0.0.1
enp0s3  IPv4 192.168.0.110
docker0 IPv4 172.17.0.1
lo      IPv6 ::1
enp0s3  IPv6 fe80::4e2c:5835:897b:6ec7%enp0s3

 

 

Also Read: Hands on Network Programming with C

Leave a Comment