test_os_network.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2003, 2004, 2005, 2006, 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/test_os_network.c
  18. * @brief testcase for util/os_network.c
  19. */
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. /**
  23. * Check if the address we got is IPv4 or IPv6 loopback (which should
  24. * be present on all systems at all times); if so, set ok to 0
  25. * (success).
  26. */
  27. static int
  28. proc (void *cls,
  29. const char *name,
  30. int isDefault,
  31. const struct sockaddr *addr,
  32. const struct sockaddr *broadcast_addr,
  33. const struct sockaddr *netmask,
  34. socklen_t addrlen)
  35. {
  36. int *ok = cls;
  37. char buf[INET6_ADDRSTRLEN];
  38. const char *protocol;
  39. if (NULL == addr)
  40. return GNUNET_OK;
  41. if (addrlen == sizeof(struct sockaddr_in))
  42. protocol = "IPv4";
  43. else
  44. protocol = "IPv6";
  45. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  46. "%s Address `%s'\n",
  47. protocol,
  48. GNUNET_a2s ((const struct sockaddr *) addr,
  49. addrlen));
  50. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  51. "Netmask `%s'\n",
  52. GNUNET_a2s ((const struct sockaddr *) netmask,
  53. addrlen));
  54. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  55. "`%s'\n",
  56. GNUNET_a2s ((const struct sockaddr *) broadcast_addr,
  57. addrlen));
  58. inet_ntop (addr->sa_family,
  59. (addr->sa_family ==
  60. AF_INET) ? (void *) &((struct sockaddr_in *) addr)->sin_addr
  61. : (void *) &((struct sockaddr_in6 *) addr)->sin6_addr, buf,
  62. sizeof(buf));
  63. if ((0 == strcmp ("::1", buf)) || (0 == strcmp ("127.0.0.1", buf)))
  64. *ok = 0;
  65. return GNUNET_OK;
  66. }
  67. int
  68. main (int argc, char *argv[])
  69. {
  70. int ret;
  71. GNUNET_log_setup ("test-os-network",
  72. "WARNING",
  73. NULL);
  74. ret = 1;
  75. GNUNET_OS_network_interfaces_list (&proc,
  76. &ret);
  77. return ret;
  78. }
  79. /* end of test_os_network.c */