1 /* Test interface name <-> index conversions.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <net/if.h>
24
25 static int
do_test(void)26 do_test (void)
27 {
28 int failures = 0;
29 struct if_nameindex *idx = if_nameindex (), *p;
30 if (idx == NULL)
31 {
32 if (errno != ENOSYS)
33 {
34 printf ("Couldn't get any interfaces.\n");
35 exit (1);
36 }
37 /* The function is simply not implemented. */
38 exit (0);
39 }
40
41 printf ("Idx Name | Idx Name\n");
42
43 for (p = idx; p->if_index || p->if_name; ++p)
44 {
45 char buf[IFNAMSIZ];
46 unsigned int ni;
47 int result;
48 printf ("%3d %15s | ", p->if_index, p->if_name);
49 printf ("%3d", ni = if_nametoindex (p->if_name));
50 printf ("%15s", if_indextoname (p->if_index, buf));
51 result = (ni != p->if_index || (strcmp (buf, p->if_name)));
52 if (ni == p->if_index)
53 /* We have to make sure that this is not an alias with the
54 same interface number. */
55 if (p->if_index == if_nametoindex (buf))
56 result = 0;
57 printf ("%10s", result ? "fail" : "okay");
58 printf ("\n");
59 failures += result;
60 }
61 if_freenameindex (idx);
62 return failures ? 1 : 0;
63 }
64
65 #define TEST_FUNCTION do_test ()
66 #include "../test-skeleton.c"
67