Back to index

glibc  2.9
test_ifindex.c
Go to the documentation of this file.
00001 /* Test interface name <-> index conversions.
00002    Copyright (C) 1997, 2000, 2005 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Philip Blundell <Philip.Blundell@pobox.com>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <net/if.h>
00026 
00027 static int
00028 do_test (void)
00029 {
00030   int failures = 0;
00031   struct if_nameindex *idx = if_nameindex (), *p;
00032   if (idx == NULL)
00033     {
00034       if (errno != ENOSYS)
00035        {
00036          printf ("Couldn't get any interfaces.\n");
00037          exit (1);
00038        }
00039       /* The function is simply not implemented.  */
00040       exit (0);
00041     }
00042 
00043   printf ("Idx            Name | Idx           Name\n");
00044 
00045   for (p = idx; p->if_index || p->if_name; ++p)
00046     {
00047       char buf[IFNAMSIZ];
00048       unsigned int ni;
00049       int result;
00050       printf ("%3d %15s | ", p->if_index, p->if_name);
00051       printf ("%3d", ni = if_nametoindex (p->if_name));
00052       printf ("%15s", if_indextoname (p->if_index, buf));
00053       result = (ni != p->if_index || (strcmp (buf, p->if_name)));
00054       if (ni == p->if_index)
00055        /* We have to make sure that this is not an alias with the
00056           same interface number.  */
00057        if (p->if_index == if_nametoindex (buf))
00058          result = 0;
00059       printf ("%10s", result ? "fail" : "okay");
00060       printf ("\n");
00061       failures += result;
00062     }
00063   if_freenameindex (idx);
00064   return failures ? 1 : 0;
00065 }
00066 
00067 #define TEST_FUNCTION do_test ()
00068 #include "../test-skeleton.c"