*BSD News Article 6146


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel.anu.edu.au!munnari.oz.au!labtam!labtam!mark
From: mark@labtam.labtam.oz.au (Mark Treacy)
Subject: Re: getting net config with SIOCGIFCONF ioctl call
Organization: Labtam Australia Pty. Ltd., Melbourne, Australia
Date: Thu, 8 Oct 1992 11:20:44 GMT
Message-ID: <mark.718543244@labtam>
References: <7519@vtserf.cc.vt.edu>
Keywords: ioctl ifconf ifreq
Lines: 60

linkt@GroupW.cns.vt.edu (Tim Link) writes:
>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???

I haven't seen any replies to this, so either you're still in the dark
or our news feed is dropping articles.

In the 4.3bsd-reno and net2 release the ifreq's returned by SIOCGIFCONF
are not fixed length.  See ifconf() in usr/src/sys/net/if.c .

You need something similar to the code in inet.c in tcpdump.
i.e. (fragment taken from tcpdump)

	ifc.ifc_len = sizeof ibuf;
	ifc.ifc_buf = (caddr_t)ibuf;

	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
	    ifc.ifc_len < sizeof(struct ifreq)) {
		perror("tcpdump: SIOCGIFCONF: ");
		exit(1);
	}
	...

	ifrp = ibuf;
	ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
	
	while (ifrp < ifend) {
		....
#if BSD >= 199006
		n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
		if (n < sizeof(*ifrp))
			++ifrp;
		else
			ifrp = (struct ifreq *)((char *)ifrp + n);
#else
		++ifrp;
#endif
	}
	...


You're probably only interested in AF_INET addresses, so skip
over the ones that aren't.  if_attach() creates an AF_LINK
address for an interface which isn't terribly usefull
since it only gets filled in with the interface name.
The function in libc to handle AF_LINK addresses wouldn't
display the address correctly anyway.

>As always, any and all help appreciated.  
>  Tim

 - Mark.