/***************************************************************************** * This Linux code came from a Usenet posting: * * http://groups.google.com/groups?selm=u0ndla.t9r.ln%40charon.heiming.de&oe=UTF-8&output=gplain * Michael Heiming * From: Michael Heiming (michael+USENET@heiming.de) * Subject: Re: Code to discover interfaces and IP address * Newsgroups: alt.os.linux * Date: 2002-09-07 13:25:04 PST *****************************************************************************/ #include #include #include #include #include #include int main( int argc, char *argv[]) { FILE *f=fopen("/proc/net/dev","r"); char buf[4096]; char *p; struct ifreq ifr; int fd; if (!f) { perror("/proc/net/dev"); exit(1); } fd = socket(AF_INET,SOCK_DGRAM, 0); if (fd==-1) { perror("socket"); exit(1); } while (fgets(buf,4096,f)) { if ((p=strchr(buf,':'))) { *p=0; for (p=buf;*p == ' ';++p); strcpy(ifr.ifr_name, p); if (ioctl(fd, SIOCGIFADDR, &ifr)) { perror(p); } else { printf( "%s: %s\n", p,inet_ntoa(*(struct in_addr*)(ifr.ifr_ifru.ifru_addr.sa_data+2))); } } } return 0; }