Add missing file
[oweals/gnunet.git] / src / nat / test_nat.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_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, "Address changed: %s `%s' (%u bytes)\n",
58               add_remove == GNUNET_YES ? "added" : "removed", 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   struct GNUNET_NAT_Handle *nat = cls;
71
72   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
73   GNUNET_NAT_unregister (nat);
74 }
75
76
77 struct addr_cls
78 {
79   struct sockaddr *addr;
80   socklen_t addrlen;
81 };
82
83
84 /**
85  * Return the address of the default interface,
86  * or any interface with a valid address if the default is not valid
87  *
88  * @param cls the 'struct addr_cls'
89  * @param name name of the interface
90  * @param isDefault do we think this may be our default interface
91  * @param addr address of the interface
92  * @param addrlen number of bytes in addr
93  * @return GNUNET_OK to continue iterating
94  */
95 static int
96 process_if (void *cls, const char *name, int isDefault,
97             const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
98             const struct sockaddr *netmask, socklen_t addrlen)
99 {
100   struct addr_cls *data = cls;
101
102   if (addr == NULL)
103     return GNUNET_OK;
104   GNUNET_free_non_null (data->addr);
105   data->addr = GNUNET_malloc (addrlen);
106   memcpy (data->addr, addr, addrlen);
107   data->addrlen = addrlen;
108   if (isDefault)
109     return GNUNET_SYSERR;
110   return GNUNET_OK;
111 }
112
113
114 /**
115  * Main function run with scheduler.
116  */
117 static void
118 run (void *cls, char *const *args, const char *cfgfile,
119      const struct GNUNET_CONFIGURATION_Handle *cfg)
120 {
121   struct GNUNET_NAT_Handle *nat;
122   struct addr_cls data;
123   struct sockaddr *addr;
124
125   data.addr = NULL;
126   GNUNET_OS_network_interfaces_list (process_if, &data);
127   if (NULL == data.addr)
128   {
129     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130                 "Could not find a valid interface address!\n");
131     exit (GNUNET_SYSERR);
132   }
133   addr = data.addr;
134   GNUNET_assert (addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
135   if (addr->sa_family == AF_INET)
136     ((struct sockaddr_in *) addr)->sin_port = htons (2086);
137   else
138     ((struct sockaddr_in6 *) addr)->sin6_port = htons (2086);
139
140   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
141               "Requesting NAT redirection from address %s...\n",
142               GNUNET_a2s (addr, data.addrlen));
143
144   nat = GNUNET_NAT_register (cfg, GNUNET_YES /* tcp */ ,
145                              2086, 1, (const struct sockaddr **) &addr,
146                              &data.addrlen, &addr_callback, NULL, NULL, NULL);
147   GNUNET_free (addr);
148   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, nat);
149 }
150
151
152 int
153 main (int argc, char *const argv[])
154 {
155   struct GNUNET_GETOPT_CommandLineOption options[] = {
156     GNUNET_GETOPT_OPTION_END
157   };
158
159   char *const argv_prog[] = {
160     "test-nat",
161     "-c",
162     "test_nat_data.conf",
163     NULL
164   };
165   GNUNET_log_setup ("test-nat",
166                     "WARNING",
167                     NULL);
168   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169               "Testing NAT library, timeout set to %d seconds\n", TIMEOUT);
170   GNUNET_PROGRAM_run (3, argv_prog, "test-nat", "nohelp", options, &run, NULL);
171   return 0;
172 }
173
174 /* end of test_nat.c */