lo0 not in ioctl( SIOCGIFCONF )

Jens Rehsack rehsack at web.de
Mon Jul 21 21:31:03 UTC 2008


Brooks Davis wrote:
>> Hi,
>>
>> maybe this question is better asked in this list ...
>>
>> I was searching why ports/net/p5-Net-Interface was not working as
>> expected and found some reasons. Most of them I can answer by implementing
>> some test code as attached, but now I'm wondering why em0 is shown twice
>> and lo0 is not included.
>> The same situation on another machine ..
> 
> The attachment didn't make it through.
> 
> -- Brooks

Copy&Paste starts here ...
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <net/if.h>
#include <errno.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>

#ifndef _SIZEOF_ADDR_IFREQ
#define	_SIZEOF_ADDR_IFREQ(ifr) \
	((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
	 (sizeof(struct ifreq) - sizeof(struct sockaddr) + \
	  (ifr).ifr_addr.sa_len) : sizeof(struct ifreq))
#endif

int
main()
{
     struct ifconf ifc;
     struct ifreq *ifr, *lifr;
     int fd;
     unsigned int n;

     fd = socket( AF_INET, SOCK_STREAM, 0 );
     bzero(&ifc, sizeof(ifc));
         n = 3;
         ifr = calloc( ifc.ifc_len, sizeof(*ifr) );
         do
         {
             n *= 2;
             ifr = realloc( ifr, sizeof(*ifr) * n );
             bzero( ifr, sizeof(*ifr) * n );
             ifc.ifc_req = ifr;
             ifc.ifc_len = n * sizeof(*ifr);
         } while( ( ioctl( fd, SIOCGIFCONF, &ifc ) == -1 ) || ( ifc.ifc_len 
== n * sizeof(*ifr)) );

     lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];

     while (ifr < lifr)
     {
         printf( "%s\n", ifr->ifr_name );
         ifr = (struct ifreq *)(((char *)ifr) + _SIZEOF_ADDR_IFREQ(*ifr));
     }

     return 0;
}


More information about the freebsd-net mailing list