tst-ifaddrs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Test listing of network interface addresses.
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ifaddrs.h>
  20. #include <netinet/in.h>
  21. #include <arpa/inet.h>
  22. static int failures;
  23. static const char *
  24. addr_string (struct sockaddr *sa, char *buf, size_t size)
  25. {
  26. if (sa == NULL)
  27. return "<none>";
  28. switch (sa->sa_family)
  29. {
  30. case AF_INET:
  31. return inet_ntop (AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
  32. buf, size);
  33. case AF_INET6:
  34. return inet_ntop (AF_INET6, &((struct sockaddr_in6 *) sa)->sin6_addr,
  35. buf, size);
  36. #ifdef AF_LINK
  37. case AF_LINK:
  38. return "<link>";
  39. #endif
  40. case AF_UNSPEC:
  41. return "---";
  42. #ifdef AF_PACKET
  43. case AF_PACKET:
  44. return "<packet>";
  45. #endif
  46. default:
  47. ++failures;
  48. printf ("sa_family=%d %08x\n", sa->sa_family,
  49. *(int*)&((struct sockaddr_in *) sa)->sin_addr.s_addr);
  50. return "<unexpected sockaddr family>";
  51. }
  52. }
  53. static int
  54. do_test (void)
  55. {
  56. struct ifaddrs *ifaces, *ifa;
  57. if (getifaddrs (&ifaces) < 0)
  58. {
  59. if (errno != ENOSYS)
  60. {
  61. printf ("Couldn't get any interfaces: %s.\n", strerror (errno));
  62. exit (1);
  63. }
  64. /* The function is simply not implemented. */
  65. exit (0);
  66. }
  67. puts ("\
  68. Name Flags Address Netmask Broadcast/Destination");
  69. for (ifa = ifaces; ifa != NULL; ifa = ifa->ifa_next)
  70. {
  71. char abuf[64], mbuf[64], dbuf[64];
  72. printf ("%-15s%#.4x %-15s %-15s %-15s\n",
  73. ifa->ifa_name, ifa->ifa_flags,
  74. addr_string (ifa->ifa_addr, abuf, sizeof (abuf)),
  75. addr_string (ifa->ifa_netmask, mbuf, sizeof (mbuf)),
  76. addr_string (ifa->ifa_broadaddr, dbuf, sizeof (dbuf)));
  77. }
  78. freeifaddrs (ifaces);
  79. return failures ? 1 : 0;
  80. }
  81. #define TEST_FUNCTION do_test ()
  82. #include "../test-skeleton.c"