tighten formatting rules
[oweals/gnunet.git] / src / nat / test_nat_mini.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * Testcase for port redirection and public IP address retrieval.
23  * This test never fails, because there need to be a NAT box set up for tha *
24  * @file nat/test_nat_mini.c
25  * @brief Testcase for NAT library - mini
26  * @author Christian Grothoff
27  *
28  * TODO: actually use ARM to start resolver service to make DNS work!
29  */
30
31 #include "platform.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_nat_lib.h"
36
37 /* Time to wait before stopping NAT, in seconds */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39
40 /**
41  * Function called on each address that the NAT service
42  * believes to be valid for the transport.
43  */
44 static void
45 addr_callback (void *cls, int add_remove,
46                const struct sockaddr *addr,
47                socklen_t addrlen,
48                enum GNUNET_NAT_StatusCode ret)
49 {
50   if (GNUNET_NAT_ERROR_SUCCESS == ret)
51   {
52     fprintf (stderr,
53              "Address changed: %s `%s' (%u bytes)\n",
54              add_remove == GNUNET_YES
55              ? "added" : "removed",
56              GNUNET_a2s (addr,
57                          addrlen),
58              (unsigned int) addrlen);
59   }
60   else
61     ;
62   // TODO: proper error handling!
63 }
64
65
66 /**
67  * Function that terminates the test.
68  */
69 static void
70 stop (void *cls)
71 {
72   struct GNUNET_NAT_MiniHandle *mini = cls;
73
74   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
75   GNUNET_NAT_mini_map_stop (mini);
76 }
77
78
79 #define PORT 10000
80
81 /**
82  * Main function run with scheduler.
83  */
84 static void
85 run (void *cls, char *const *args, const char *cfgfile,
86      const struct GNUNET_CONFIGURATION_Handle *cfg)
87 {
88   struct GNUNET_NAT_MiniHandle *mini;
89
90   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91               "Requesting NAT redirection for port %u...\n",
92               PORT);
93   mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */,
94                                     &addr_callback, NULL);
95   if (NULL == mini)
96   {
97     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n");
98     return;
99   }
100   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini);
101 }
102
103
104 int
105 main (int argc, char *const argv[])
106 {
107   struct GNUNET_GETOPT_CommandLineOption options[] = {
108     GNUNET_GETOPT_OPTION_END
109   };
110
111   char *const argv_prog[] = {
112     "test-nat-mini",
113     "-c",
114     "test_nat_data.conf",
115     "-L",
116     "WARNING",
117     NULL
118   };
119
120   GNUNET_log_setup ("test-nat-mini",
121                     "WARNING",
122                     NULL);
123
124   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
125               "UPnP test for NAT library, timeout set to %s\n",
126               GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
127                                                       GNUNET_YES));
128   GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run,
129                       NULL);
130   return 0;
131 }
132
133
134 /* end of test_nat_mini.c */