#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main() {
int i = 0;
int sockfd;
struct ifconf ifc;
char buf[512] = {0};
struct ifreq *ifr;
ifc.ifc_len = 512;
ifc.ifc_buf = buf;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
return -1;
}
ioctl(sockfd, SIOCGIFCONF, &ifc);
ifr = (struct ifreq*)buf;
for (i = (ifc.ifc_len /sizeof(struct ifreq)); i > 0; i--) {
printf("%s: %s",ifr->ifr_name,
inet_ntoa(((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr));
ifr++;
}
}