#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
struct sockaddr_in *addr;
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET)
{
printf("Device name: %s\n", ifa->ifa_name);
addr = (struct sockaddr_in *)ifa->ifa_addr;
printf(" Addr: %s\n",
inet_ntop(AF_INET, &(addr->sin_addr), host, sizeof(host)));
addr = (struct sockaddr_in *)ifa->ifa_broadaddr;
printf(" Bcast address: %s\n",
inet_ntop(AF_INET, &(addr->sin_addr), host, sizeof(host)));
addr = (struct sockaddr_in *)ifa->ifa_netmask;
printf(" Mask address: %s\n",
inet_ntop(AF_INET, &addr->sin_addr, host, sizeof(host)));
}
}
}