2 This file is part of GNUnet.
3 Copyright (C) 2009, 2011 GNUnet e.V.
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.
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.
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/>.
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.
26 * @file nat/test_nat.c
27 * @brief Testcase for NAT library
28 * @author Milan Bouchet-Valat
29 * @author Christian Grothoff
31 * TODO: actually use ARM to start resolver service to make DNS work!
35 #include "gnunet_util_lib.h"
36 #include "gnunet_program_lib.h"
37 #include "gnunet_scheduler_lib.h"
38 #include "gnunet_nat_lib.h"
42 * Time to wait before stopping NAT, in seconds
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
48 * Function called on each address that the NAT service
49 * believes to be valid for the transport.
52 addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
55 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
56 "Address changed: %s `%s' (%u bytes)\n",
57 add_remove == GNUNET_YES ? "added" : "removed",
60 (unsigned int) addrlen);
65 * Function that terminates the test.
70 struct GNUNET_NAT_Handle *nat = cls;
72 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
73 "Stopping NAT and quitting...\n");
74 GNUNET_NAT_unregister (nat);
80 struct sockaddr *addr;
86 * Return the address of the default interface,
87 * or any interface with a valid address if the default is not valid
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
97 process_if (void *cls,
100 const struct sockaddr *addr,
101 const struct sockaddr *broadcast_addr,
102 const struct sockaddr *netmask,
105 struct addr_cls *data = cls;
109 GNUNET_free_non_null (data->addr);
110 data->addr = GNUNET_malloc (addrlen);
111 GNUNET_memcpy (data->addr, addr, addrlen);
112 data->addrlen = addrlen;
114 return GNUNET_SYSERR;
120 * Main function run with scheduler.
126 const struct GNUNET_CONFIGURATION_Handle *cfg)
128 struct GNUNET_NAT_Handle *nat;
129 struct addr_cls data;
130 struct sockaddr *addr;
133 GNUNET_OS_network_interfaces_list (process_if, &data);
134 if (NULL == data.addr)
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
137 "Could not find a valid interface address!\n");
138 exit (77); /* marks test as skipped */
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);
145 ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086);
147 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
148 "Requesting NAT redirection from address %s...\n",
149 GNUNET_a2s (addr, data.addrlen));
151 nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ ,
152 2086, 1, (const struct sockaddr **) &addr,
153 &data.addrlen, &addr_callback, NULL, NULL, NULL);
155 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
160 main (int argc, char *const argv[])
162 struct GNUNET_GETOPT_CommandLineOption options[] = {
163 GNUNET_GETOPT_OPTION_END
166 char *const argv_prog[] = {
169 "test_nat_data.conf",
172 GNUNET_log_setup ("test-nat",
175 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
176 "Testing NAT library, timeout set to %s\n",
177 GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
179 GNUNET_PROGRAM_run (3,
188 /* end of test_nat.c */