blob: 126c5bab4dfc843b8c69b56d951f0f34cd082191 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* if_nameindex.c: test the if_nameindex() function
2 *
3 * Copyright (C) 2006 Erik Andersen <andersen@uclibc.org>
4 *
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <net/if.h>
11
12static char ifname[IF_NAMESIZE];
13
14static void test_if_nameindex(void)
15{
16 size_t i;
17 struct if_nameindex *ret;
18
19 ret = if_nameindex();
20
21 if (ret == NULL) {
22 perror("if_nameindex()");
23 exit(1);
24 }
25
26 printf("--- if_nameindex()\n");
27 for (i=0; ret[i].if_name; ++i)
28 printf("%i: %s\n", ret[i].if_index, ret[i].if_name);
29
30 if_freenameindex(ret);
31}
32
33static void test_if_indextoname(void)
34{
35 if (if_indextoname(1, ifname) == NULL) {
36 perror("if_nameindex()");
37 exit(1);
38 }
39
40 printf("if_indextoname(1) = %s\n", ifname);
41}
42
43static void test_if_nametoindex(void)
44{
45 int ifindex = if_nametoindex(ifname);
46
47 if (ifindex == 0) {
48 perror("if_nametoindex()");
49 exit(1);
50 }
51
52 printf("if_nametoindex(%s) = %i\n", ifname, ifindex);
53}
54
55int main(void)
56{
57 test_if_nameindex();
58 test_if_indextoname();
59 test_if_nametoindex();
60 return 0;
61}