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