linux subagent - Net.InterfaceList

From: Alex Kirhenshtein <alk_at_DOMAIN_REMOVED>
Date: Fri, 02 Sep 2005 14:36:58 +0300

Hello.

As were discovered earlier, function if_nameindex() do not show aliases,
so now i use alternative way. test application attached, and it would be
nice to get a responce from users with different kernels before adding.

build: gcc iflist.c -o iflist -Wall

tested on:
Linux kenny 2.6.9-gentoo-r6 #1 Wed Nov 24 22:11:27 EEST 2005
Linux cartman 2.6.12-gentoo-r1 #3 SMP Mon Jun 27 23:56:19 EEST 2005

wbr, alex.

-- 
Alex Kirhenshtein
C.T.Co
Cellular: +371-9145688

/* $Id$ */

// vim: ts=3 sw=3

#include <stdio.h>
#include <stdlib.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

#ifdef USE_NAMETOINDEX
// if_nameindex()
int main(void)
{
        struct if_nameindex *pIndex;
        int i;

        pIndex = if_nameindex();
        if (pIndex != NULL)
        {
                for (i = 0; pIndex[i].if_index != 0 && pIndex[i].if_name != NULL; i++)
                {
                        printf("[%2d] %-8s\n",
                                        pIndex[i].if_index,
                                        pIndex[i].if_name);
                }
      if_freenameindex(pIndex);
        }

        return 0;
}

#else

// SIOCGIFCONF
int main(void)
{
        int s;
        char *buff;
        struct ifconf ifc;
        int i;

        s = socket(PF_INET, SOCK_DGRAM, 0);
        if (s > 0)
        {
                ifc.ifc_len = 0;
                ifc.ifc_buf = NULL;
                if (ioctl(s, SIOCGIFCONF, (caddr_t)&ifc) == 0)
                {
                        buff = malloc(ifc.ifc_len);
                        if (buff != NULL)
                        {
                                ifc.ifc_buf = buff;

                                if (ioctl(s, SIOCGIFCONF, (caddr_t)&ifc) == 0)
                                {
                                        for (i = 0; i < ifc.ifc_len / sizeof(struct ifreq); i++)
                                        {
                                                if( ifc.ifc_req[i].ifr_addr.sa_family == AF_INET )
                                                {
                                                        struct sockaddr_in *addr;
                                                        struct ifreq irq;
                                                        int mask = 0;
                                                        int index = if_nametoindex(ifc.ifc_req[i].ifr_name);

                                                        strcpy(irq.ifr_name, ifc.ifc_req[i].ifr_name);

                                                        if (ioctl(s, SIOCGIFNETMASK, &irq) == 0)
                                                        {
                                                                mask = 33 - ffs(htonl(((struct sockaddr_in *)
                                                                                                        &irq.ifr_addr)->sin_addr.s_addr));
                                                        }

                                                        addr = (struct sockaddr_in *)&(ifc.ifc_req[i].ifr_addr);
                                                        printf("[%2d] %-8s %s/%d\n",
                                                                        index,
                                                                        ifc.ifc_req[i].ifr_name,
                                                                        inet_ntoa(addr->sin_addr),
                                                                        mask);
                                                }
                                        }
                                }
                                else
                                {
                                        perror("sysctl-2()");
                                }

                                free(buff);
                        }
                        else
                        {
                                perror("malloc()");
                        }
                }
                else
                {
                        perror("sysctl-1()");
                        return 0;
                }
        }
        else
        {
                perror("socket()");
        }

        return 0;
}

#endif

///////////////////////////////////////////////////////////////////////////////
/*

$Log$

*/
Received on Fri Sep 02 2005 - 14:36:58 EEST

This archive was generated by hypermail 2.2.0 : Fri Sep 02 2005 - 14:45:14 EEST