Wednesday, February 2, 2011

Finding the IP address of network interfaces in C (linux)

The following program lists the 'up' network interfaces and their IP addresses. If you are behind a firewall, this will list the local IP address

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

int main(void)
{
int fd;
struct if_nameindex *curif, *ifs;
struct ifreq req;

if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1) {
ifs = if_nameindex();
if(ifs) {
for(curif = ifs; curif && curif->if_name; curif++) {
strncpy(req.ifr_name, curif->if_name, IFNAMSIZ);
req.ifr_name[IFNAMSIZ] = 0;
if (ioctl(fd, SIOCGIFADDR, &req) < 0)
perror("ioctl");
else
printf("%s: [%s]\n", curif->if_name,
inet_ntoa(((struct sockaddr_in*) &req.ifr_addr)->sin_addr));
}
if_freenameindex(ifs);
if(close(fd)!=0)
perror("close");
} else
perror("if_nameindex");
} else
perror("socket");
return 0;
}

No comments:

Post a Comment