Return to BSD News archive
Path: sserve!manuel!munnari.oz.au!spool.mu.edu!yale.edu!qt.cs.utexas.edu!news.Brown.EDU!noc.near.net!uunet!vtserf!GroupW.cns.vt.edu!linkt From: linkt@GroupW.cns.vt.edu (Tim Link) Newsgroups: comp.unix.bsd Subject: getting net config with SIOCGIFCONF ioctl call Keywords: ioctl ifconf ifreq Message-ID: <7519@vtserf.cc.vt.edu> Date: 29 Sep 92 05:19:30 GMT Sender: usenet@vtserf.cc.vt.edu Followup-To: linkt@Groupw.CNS.VT.EDU Organization: Va Tech Communications Resources Lines: 94 I'm working on a program that needs to determine the network configuration under 386BSD. I'm using the SIOCGIFCONF IOCTL function which *I THINK* should return to me an array of ifreq structures which have the name of the interface and it's ip address. This ain't what I'm gettin'. I've poked and prodded and looked at if.c to try to figure out what's going on, but I can't see where the problem is. Just as a test I compiled the program on an Ultrix machine and the results were close to what I expected, but still not exact. Can anybody look at this and tell me if I'm completely wrong in what I'm doing of if SIOCGIFCONF isn't returning what it should be??? As always, any and all help appreciated. Tim Here's the output with 386BSD: (The addresses are completely wrong, and why are the "if" names spaced?) if:we0 addr:0.3.0.0 if: addr:4.5.0.0 if: addr:12.3.45.35 if: addr:7.0.0.1 if: addr:0.0.3.0 if: addr:0.0.0.0 if:sl0 addr:24.0.0.2 if: addr:0.0.0.0 if: addr:0.0.0.0 if: addr:0.0.0.0 Here's the output on the Ultrix system: if:ln0 addr:0.0.0.0 <---+-- What are these??? if:ln0 addr:4.5.0.2 <---+ if:ln0 addr:128.173.5.9<-----+- These are correct. if:lo0 addr:127.0.0.1 <-----+ if: addr:0.0.0.0 if: addr:0.0.0.0 if: addr:0.0.0.0 if: addr:0.0.0.0 if: addr:0.0.0.0 if: addr:0.0.0.0 And here's the program: #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <stdio.h> struct ifreq ifreq[10]; /* Holds interface configuration */ struct ifconf ifconf; /* Int. config ioctl block (pnts to ifreq) */ int s; int i, y; struct in_addr ipa; struct ifreq iftest; struct in_addr test2; main() { printf("\n"); /* * Get a socket. */ if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { printf( "socket error\n" ); exit(1); } /* * Get network configuration. */ ifconf.ifc_len = sizeof(ifreq); ifconf.ifc_req = ifreq; if ((ioctl(s, SIOCGIFCONF, (caddr_t) &ifconf) < 0) || (ifconf.ifc_len <= 0)) { printf("ioctl error\n"); exit(1); } printf( "\n" ); for ( i = 0; i < 10; i++ ) { printf( "if:%s \t addr:%s\n", &ifreq[i].ifr_name, inet_ntoa( ((struct sockaddr_in *)(&ifreq[i].ifr_addr))->sin_addr ) ); } } QUIT name, inet_ntoa( ((struct sockaddr_in *)(&ifreq[i].ifr_addr))->sin_addr ) ); } }