test_nat_mini.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2011 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. * Testcase for port redirection and public IP address retrieval.
  18. * This test never fails, because there need to be a NAT box set up for tha *
  19. * @file nat/test_nat_mini.c
  20. * @brief Testcase for NAT library - mini
  21. * @author Christian Grothoff
  22. *
  23. * TODO: actually use ARM to start resolver service to make DNS work!
  24. */
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_program_lib.h"
  28. #include "gnunet_scheduler_lib.h"
  29. #include "gnunet_nat_lib.h"
  30. /* Time to wait before stopping NAT, in seconds */
  31. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  32. /**
  33. * Function called on each address that the NAT service
  34. * believes to be valid for the transport.
  35. */
  36. static void
  37. addr_callback (void *cls, int add_remove,
  38. const struct sockaddr *addr,
  39. socklen_t addrlen,
  40. enum GNUNET_NAT_StatusCode ret)
  41. {
  42. if (GNUNET_NAT_ERROR_SUCCESS == ret)
  43. {
  44. fprintf (stderr,
  45. "Address changed: %s `%s' (%u bytes)\n",
  46. add_remove == GNUNET_YES
  47. ? "added" : "removed",
  48. GNUNET_a2s (addr,
  49. addrlen),
  50. (unsigned int) addrlen);
  51. }
  52. else
  53. ;
  54. //TODO: proper error handling!
  55. }
  56. /**
  57. * Function that terminates the test.
  58. */
  59. static void
  60. stop (void *cls)
  61. {
  62. struct GNUNET_NAT_MiniHandle *mini = cls;
  63. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
  64. GNUNET_NAT_mini_map_stop (mini);
  65. }
  66. #define PORT 10000
  67. /**
  68. * Main function run with scheduler.
  69. */
  70. static void
  71. run (void *cls, char *const *args, const char *cfgfile,
  72. const struct GNUNET_CONFIGURATION_Handle *cfg)
  73. {
  74. struct GNUNET_NAT_MiniHandle *mini;
  75. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  76. "Requesting NAT redirection for port %u...\n",
  77. PORT);
  78. mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */ ,
  79. &addr_callback, NULL);
  80. if (NULL == mini)
  81. {
  82. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n");
  83. return;
  84. }
  85. GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini);
  86. }
  87. int
  88. main (int argc, char *const argv[])
  89. {
  90. struct GNUNET_GETOPT_CommandLineOption options[] = {
  91. GNUNET_GETOPT_OPTION_END
  92. };
  93. char *const argv_prog[] = {
  94. "test-nat-mini",
  95. "-c",
  96. "test_nat_data.conf",
  97. "-L",
  98. "WARNING",
  99. NULL
  100. };
  101. GNUNET_log_setup ("test-nat-mini",
  102. "WARNING",
  103. NULL);
  104. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  105. "UPnP test for NAT library, timeout set to %s\n",
  106. GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
  107. GNUNET_YES));
  108. GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run,
  109. NULL);
  110. return 0;
  111. }
  112. /* end of test_nat_mini.c */