checkpoint save
[oweals/gnunet.git] / src / nat / test_nat_mini.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 tha *
24  * @file nat/test_nat_mini.c
25  * @brief Testcase for NAT library - mini
26  * @author Christian Grothoff
27  *
28  * TODO: actually use ARM to start resolver service to make DNS work!
29  */
30
31 #include "platform.h"
32 #include "gnunet_common.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_nat_lib.h"
37
38
39 #define VERBOSE GNUNET_NO
40
41 /* Time to wait before stopping NAT, in seconds */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43
44 /**
45  * Function called on each address that the NAT service
46  * believes to be valid for the transport.
47  */
48 static void
49 addr_callback (void *cls, int add_remove, const struct sockaddr *addr,
50                socklen_t addrlen)
51 {
52   fprintf (stderr, "Address changed: %s `%s' (%u bytes)\n",
53            add_remove == GNUNET_YES ? "added" : "removed", GNUNET_a2s (addr,
54                                                                        addrlen),
55            (unsigned int) addrlen);
56 }
57
58
59 /**
60  * Function that terminates the test.
61  */
62 static void
63 stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   struct GNUNET_NAT_MiniHandle *mini = cls;
66
67   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
68   GNUNET_NAT_mini_map_stop (mini);
69 }
70
71 #define PORT 10000
72
73 /**
74  * Main function run with scheduler.
75  */
76 static void
77 run (void *cls, char *const *args, const char *cfgfile,
78      const struct GNUNET_CONFIGURATION_Handle *cfg)
79 {
80   struct GNUNET_NAT_MiniHandle *mini;
81
82   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
83               "Requesting NAT redirection for port %u...\n", PORT);
84   mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */ ,
85                                     &addr_callback, NULL);
86   if (NULL == mini)
87   {
88     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n");
89     return;
90   }
91   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini);
92 }
93
94
95 int
96 main (int argc, char *const argv[])
97 {
98   struct GNUNET_GETOPT_CommandLineOption options[] = {
99     GNUNET_GETOPT_OPTION_END
100   };
101
102   char *const argv_prog[] = {
103     "test-nat-mini",
104     "-c",
105     "test_nat_data.conf",
106     "-L",
107 #if VERBOSE
108     "DEBUG",
109 #else
110     "WARNING",
111 #endif
112     NULL
113   };
114
115   GNUNET_log_setup ("test-nat-mini",
116 #if VERBOSE
117                     "DEBUG",
118 #else
119                     "WARNING",
120 #endif
121                     NULL);
122
123   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
124               "UPnP test for NAT library, timeout set to %d seconds\n",
125               TIMEOUT);
126   GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run,
127                       NULL);
128   return 0;
129 }
130
131 /* end of test_nat_mini.c */