WiP
[oweals/gnunet.git] / src / nat / test_nat.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file nat/test_nat.c
23  * @brief Testcase for NAT library
24  * @author Milan Bouchet-Valat
25  */
26
27 /**
28  * Testcase for port redirection and public IP address retrieval.
29  * This test never fails, because there need to be a NAT box set up for that.
30  * So we only get IP address and open the 2086 port using any UPnP and NAT-PMP
31  * routers found, wait for 30s, close ports and return.
32  * Have a look at the logs and use NMAP to check that it works with your box.
33  */
34
35
36 #include "platform.h"
37 #include "gnunet_common.h"
38 #include "gnunet_util_lib.h"
39 #include "gnunet_program_lib.h"
40 #include "gnunet_scheduler_lib.h"
41 #include "gnunet_nat_lib.h"
42
43 /* Time to wait before stopping NAT, in seconds */
44 #define TIMEOUT 60
45
46 struct addr_cls
47 {
48   struct sockaddr *addr;
49   socklen_t addrlen;
50 };
51
52 static void
53 addr_callback (void *cls, int add_remove,
54                const struct sockaddr *addr, socklen_t addrlen)
55 {
56   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "External address changed: %s %s\n",
57               add_remove == GNUNET_YES ? "added" : "removed",
58               GNUNET_a2s (addr, addrlen));
59 }
60
61 static void
62 stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 {
64   struct GNUNET_NAT_Handle *nat = cls;
65
66   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
67   GNUNET_NAT_unregister (nat);
68 }
69
70 /* Return the address of the default interface,
71  * or any interface with a valid address if the default is not valid */
72 static int
73 process_if (void *cls,
74             const char *name,
75             int isDefault, const struct sockaddr *addr, socklen_t addrlen)
76 {
77   struct addr_cls *data = cls;
78
79   if (addr && addrlen > 0)
80     {
81       if (data->addr)
82         GNUNET_free (data->addr);
83       data->addr = memcpy (GNUNET_malloc (addrlen), addr, addrlen);
84       data->addrlen = addrlen;
85       if (isDefault)
86         return GNUNET_SYSERR;
87     }
88   return GNUNET_OK;
89 }
90
91 static void
92 run (void *cls,
93      char *const *args,
94      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
95 {
96   struct GNUNET_NAT_Handle *nat;
97   struct addr_cls data;
98   struct sockaddr *addr;
99
100   GNUNET_log_setup ("test-nat", "DEBUG", NULL);
101
102   data.addr = NULL;
103   GNUNET_OS_network_interfaces_list (process_if, &data);
104   if (!data.addr)
105     {
106       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
107                   "Could not find a valid interface address!\n");
108       exit (GNUNET_SYSERR);
109     }
110
111   addr = GNUNET_malloc (data.addrlen);
112   memcpy (addr, data.addr, data.addrlen);
113
114   GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
115   if (addr->sa_family == AF_INET)
116     ((struct sockaddr_in *) addr)->sin_port = htons (2086);
117   else
118     ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086);
119
120   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
121               "Requesting NAT redirection from address %s...\n",
122               GNUNET_a2s (addr, data.addrlen));
123
124   nat = GNUNET_NAT_register (addr, data.addrlen, addr_callback, NULL);
125   GNUNET_free (addr);
126
127   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
128                                 (GNUNET_TIME_UNIT_SECONDS, TIMEOUT), stop,
129                                 nat);
130 }
131
132 int
133 main (int argc, char *const argv[])
134 {
135   struct GNUNET_GETOPT_CommandLineOption options[] = {
136     GNUNET_GETOPT_OPTION_END
137   };
138
139   char *const argv_prog[] = {
140     "test-nat",
141     "-c",
142     "test-nat.conf",
143     "-L",
144 #if VERBOSE
145     "DEBUG",
146 #else
147     "WARNING",
148 #endif
149     NULL
150   };
151
152   GNUNET_log_setup ("test-nat",
153 #if VERBOSE
154                     "DEBUG",
155 #else
156                     "WARNING",
157 #endif
158                     NULL);
159
160   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
161               "Testing NAT library, timeout set to %d seconds\n", TIMEOUT);
162
163   GNUNET_PROGRAM_run (5, argv_prog, "test-nat", "nohelp", options, &run, NULL);
164
165   return 0;
166 }
167
168 /* end of test_nat.c */