if_nameindex.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <net/if.h>
  10. static char ifname[IF_NAMESIZE];
  11. void test_if_nameindex(void)
  12. {
  13. size_t i;
  14. struct if_nameindex *ret;
  15. ret = if_nameindex();
  16. if (ret == NULL) {
  17. perror("if_nameindex()");
  18. exit(1);
  19. }
  20. printf("--- if_nameindex()\n");
  21. for (i=0; ret[i].if_name; ++i)
  22. printf("%i: %s\n", ret[i].if_index, ret[i].if_name);
  23. if_freenameindex(ret);
  24. }
  25. void test_if_indextoname(void)
  26. {
  27. if (if_indextoname(1, ifname) == NULL) {
  28. perror("if_nameindex()");
  29. exit(1);
  30. }
  31. printf("if_indextoname(1) = %s\n", ifname);
  32. }
  33. void test_if_nametoindex(void)
  34. {
  35. int ifindex = if_nametoindex(ifname);
  36. if (ifindex == 0) {
  37. perror("if_nametoindex()");
  38. exit(1);
  39. }
  40. printf("if_nametoindex(%s) = %i\n", ifname, ifindex);
  41. }
  42. int main()
  43. {
  44. test_if_nameindex();
  45. test_if_indextoname();
  46. test_if_nametoindex();
  47. return 0;
  48. }