Re: IP address without hostname
On Friday 16 August 2002 20:41, Peter Bartosch wrote:
> hello
>
>
> i've got a little problem with a c program:
>
> i'm searching for a function to gather the IP adresses from the
> interfaces of a computer - without having the hostname nor any other
> information ...
I found this somewhere on another maillist. It uses ioctl() to poll some
information.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define MAX_NUM_IFREQ (512)
main() {
struct ifconf Ifc;
struct ifreq IfcBuf[MAX_NUM_IFREQ];
struct ifreq *pIfr;
struct ifreq single;
int num_ifreq;
int i;
int fd;
Ifc.ifc_len = sizeof(IfcBuf);
Ifc.ifc_buf = (char *) IfcBuf;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket()");
exit(1);
}
if ( ioctl(fd, SIOCGIFCONF, &Ifc) < 0) {
perror("ioctl SIOCGIFCONF");
exit(1);
}
num_ifreq = Ifc.ifc_len / sizeof(struct ifreq);
for ( pIfr = Ifc.ifc_req, i = 0 ; i < num_ifreq; pIfr++, i++ ) {
memcpy(single.ifr_name,pIfr->ifr_name,IF_NAMESIZE);
if( ioctl(fd, SIOCGIFADDR, &single) < 0 ) {
perror("ioctl SIOCGIFADDR");
exit(1);
}
printf("%d: %s<= is: %s\n", i, pIfr->ifr_name, inet_ntoa(((struct
sockaddr_in*)&single.ifr_addr)->sin_addr));
}
}
--
Embedded Linux -- True multitasking!
TWO TOASTS AT THE SAME TIME!
Reply to: