remove send on connect
[oweals/gnunet.git] / src / nat / test_nat.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2011 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  * 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_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
44 #define VERBOSE GNUNET_NO
45
46
47 /**
48  * Time to wait before stopping NAT, in seconds 
49  */
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
51
52
53 /**
54  * Function called on each address that the NAT service
55  * believes to be valid for the transport.
56  */
57 static void
58 addr_callback (void *cls, int add_remove,
59                const struct sockaddr *addr, socklen_t addrlen)
60 {
61   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
62               "Address changed: %s `%s' (%u bytes)\n",
63               add_remove == GNUNET_YES ? "added" : "removed",
64               GNUNET_a2s (addr, addrlen),
65               (unsigned int) addrlen);
66 }
67
68
69 /**
70  * Function that terminates the test.
71  */
72 static void
73 stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   struct GNUNET_NAT_Handle *nat = cls;
76
77   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
78               "Stopping NAT and quitting...\n");
79   GNUNET_NAT_unregister (nat);
80 }
81
82
83 struct addr_cls
84 {
85   struct sockaddr *addr;
86   socklen_t addrlen;
87 };
88
89
90 /**
91  * Return the address of the default interface,
92  * or any interface with a valid address if the default is not valid 
93  *
94  * @param cls the 'struct addr_cls'
95  * @param name name of the interface
96  * @param isDefault do we think this may be our default interface
97  * @param addr address of the interface
98  * @param addrlen number of bytes in addr
99  * @return GNUNET_OK to continue iterating
100  */
101 static int
102 process_if (void *cls,
103             const char *name,
104             int isDefault, const struct sockaddr *addr, socklen_t addrlen)
105 {
106   struct addr_cls *data = cls;
107
108   if (addr == NULL)
109     return GNUNET_OK;
110   GNUNET_free_non_null (data->addr);
111   data->addr = GNUNET_malloc (addrlen);
112   memcpy (data->addr, addr, addrlen);
113   data->addrlen = addrlen;
114   if (isDefault)
115     return GNUNET_SYSERR;
116   return GNUNET_OK;
117 }
118
119
120 /**
121  * Main function run with scheduler.
122  */
123 static void
124 run (void *cls,
125      char *const *args,
126      const char *cfgfile, 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 (GNUNET_SYSERR);
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, 
152                              GNUNET_YES /* tcp */,
153                              2086,
154                              1,
155                              (const struct sockaddr**) &addr, 
156                              &data.addrlen, 
157                              &addr_callback, NULL, NULL);
158   GNUNET_free (addr);
159   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
160 }
161
162
163 int
164 main (int argc, char *const argv[])
165 {
166   struct GNUNET_GETOPT_CommandLineOption options[] = {
167     GNUNET_GETOPT_OPTION_END
168   };
169
170   char *const argv_prog[] = {
171     "test-nat",
172     "-c",
173     "test_nat_data.conf",
174     "-L",
175 #if VERBOSE
176     "DEBUG",
177 #else
178     "WARNING",
179 #endif
180     NULL
181   };
182
183   GNUNET_log_setup ("test-nat",
184 #if VERBOSE
185                     "DEBUG",
186 #else
187                     "WARNING",
188 #endif
189                     NULL);
190
191   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
192               "Testing NAT library, timeout set to %d seconds\n", TIMEOUT);
193
194   GNUNET_PROGRAM_run (5, argv_prog, "test-nat", "nohelp", options, &run, NULL);
195
196   return 0;
197 }
198
199 /* end of test_nat.c */