src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / nat / test_nat.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 that.
24  * So we only get IP address and open the 2086 port using any NAT traversal
25  * method available, wait for 30s, close ports and return.
26  * Have a look at the logs and use NMAP to check that it works with your box.
27  *
28  * @file nat/test_nat.c
29  * @brief Testcase for NAT library
30  * @author Milan Bouchet-Valat
31  * @author Christian Grothoff
32  *
33  * TODO: actually use ARM to start resolver service to make DNS work!
34  */
35
36 #include "platform.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_program_lib.h"
39 #include "gnunet_scheduler_lib.h"
40 #include "gnunet_nat_lib.h"
41
42
43 /**
44  * Time to wait before stopping NAT, in seconds
45  */
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
47
48
49 /**
50  * Function called on each address that the NAT service
51  * believes to be valid for the transport.
52  */
53 static void
54 addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
55                socklen_t addrlen)
56 {
57   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
58               "Address changed: %s `%s' (%u bytes)\n",
59               add_remove == GNUNET_YES ? "added" : "removed",
60               GNUNET_a2s (addr,
61                           addrlen),
62               (unsigned int) addrlen);
63 }
64
65
66 /**
67  * Function that terminates the test.
68  */
69 static void
70 stop (void *cls)
71 {
72   struct GNUNET_NAT_Handle *nat = cls;
73
74   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
75               "Stopping NAT and quitting...\n");
76   GNUNET_NAT_unregister (nat);
77 }
78
79
80 struct addr_cls
81 {
82   struct sockaddr *addr;
83   socklen_t addrlen;
84 };
85
86
87 /**
88  * Return the address of the default interface,
89  * or any interface with a valid address if the default is not valid
90  *
91  * @param cls the 'struct addr_cls'
92  * @param name name of the interface
93  * @param isDefault do we think this may be our default interface
94  * @param addr address of the interface
95  * @param addrlen number of bytes in @a addr
96  * @return #GNUNET_OK to continue iterating
97  */
98 static int
99 process_if (void *cls,
100             const char *name,
101             int isDefault,
102             const struct sockaddr *addr,
103             const struct sockaddr *broadcast_addr,
104             const struct sockaddr *netmask,
105             socklen_t addrlen)
106 {
107   struct addr_cls *data = cls;
108
109   if (addr == NULL)
110     return GNUNET_OK;
111   GNUNET_free_non_null (data->addr);
112   data->addr = GNUNET_malloc (addrlen);
113   GNUNET_memcpy (data->addr, addr, addrlen);
114   data->addrlen = addrlen;
115   if (isDefault)
116     return GNUNET_SYSERR;
117   return GNUNET_OK;
118 }
119
120
121 /**
122  * Main function run with scheduler.
123  */
124 static void
125 run (void *cls,
126      char *const *args,
127      const char *cfgfile,
128      const struct GNUNET_CONFIGURATION_Handle *cfg)
129 {
130   struct GNUNET_NAT_Handle *nat;
131   struct addr_cls data;
132   struct sockaddr *addr;
133
134   data.addr = NULL;
135   GNUNET_OS_network_interfaces_list (process_if, &data);
136   if (NULL == data.addr)
137   {
138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139                 "Could not find a valid interface address!\n");
140     exit (77); /* marks test as skipped */
141   }
142   addr = data.addr;
143   GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
144   if (addr->sa_family == AF_INET)
145     ((struct sockaddr_in *) addr)->sin_port = htons (2086);
146   else
147     ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086);
148
149   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
150               "Requesting NAT redirection from address %s...\n",
151               GNUNET_a2s (addr, data.addrlen));
152
153   nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ ,
154                              2086, 1, (const struct sockaddr **) &addr,
155                              &data.addrlen, &addr_callback, NULL, NULL, NULL);
156   GNUNET_free (addr);
157   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
158 }
159
160
161 int
162 main (int argc, char *const argv[])
163 {
164   struct GNUNET_GETOPT_CommandLineOption options[] = {
165     GNUNET_GETOPT_OPTION_END
166   };
167
168   char *const argv_prog[] = {
169     "test-nat",
170     "-c",
171     "test_nat_data.conf",
172     NULL
173   };
174   GNUNET_log_setup ("test-nat",
175                     "WARNING",
176                     NULL);
177   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
178               "Testing NAT library, timeout set to %s\n",
179               GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
180                                                       GNUNET_YES));
181   GNUNET_PROGRAM_run (3,
182                       argv_prog,
183                       "test-nat",
184                       "nohelp",
185                       options,
186                       &run, NULL);
187   return 0;
188 }
189
190 /* end of test_nat.c */