lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* 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 | |
| 12 | static char ifname[IF_NAMESIZE]; |
| 13 | |
| 14 | static 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 | |
| 33 | static 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 | |
| 43 | static 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 | |
| 55 | int main(void) |
| 56 | { |
| 57 | test_if_nameindex(); |
| 58 | test_if_indextoname(); |
| 59 | test_if_nametoindex(); |
| 60 | return 0; |
| 61 | } |