57abca3d0ca94cec90edc898241eb33b2248695e
[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_YES
40
41
42 /* Time to wait before stopping NAT, in seconds */
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44
45
46
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,
55                const struct sockaddr *addr, socklen_t addrlen)
56 {
57   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
58               "Address changed: %s `%s' (%u bytes)\n",
59               add_remove == GNUNET_YES ? "added" : "removed",
60               GNUNET_a2s (addr, addrlen),
61               (unsigned int) addrlen);
62 }
63
64
65 /**
66  * Function that terminates the test.
67  */
68 static void
69 stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   struct GNUNET_NAT_MiniHandle *mini = cls;
72
73   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
74               "Stopping NAT and quitting...\n");
75   GNUNET_NAT_mini_map_stop (mini);
76 }
77
78 #define PORT 10000
79
80 /**
81  * Main function run with scheduler.
82  */
83 static void
84 run (void *cls,
85      char *const *args,
86      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
87 {
88   struct GNUNET_NAT_MiniHandle *mini;
89
90   GNUNET_log_setup ("test-nat-mini", "DEBUG", NULL);
91   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
92               "Requesting NAT redirection for port %u...\n",
93               PORT);
94   mini = GNUNET_NAT_mini_map_start (PORT,
95                                     GNUNET_YES /* tcp */,
96                                     &addr_callback, NULL);
97   if (NULL == mini)
98     {
99       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
100                   "Could not start UPnP interaction\n");
101       return;
102     }
103   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini);
104 }
105
106
107 int
108 main (int argc, char *const argv[])
109 {
110   struct GNUNET_GETOPT_CommandLineOption options[] = {
111     GNUNET_GETOPT_OPTION_END
112   };
113
114   char *const argv_prog[] = {
115     "test-nat-mini",
116     "-c",
117     "test_nat_data.conf",
118     "-L",
119 #if VERBOSE
120     "DEBUG",
121 #else
122     "WARNING",
123 #endif
124     NULL
125   };
126
127   GNUNET_log_setup ("test-nat-mini",
128 #if VERBOSE
129                     "DEBUG",
130 #else
131                     "WARNING",
132 #endif
133                     NULL);
134
135   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
136               "UPnP test for NAT library, timeout set to %d seconds\n", TIMEOUT);
137   GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini",
138                       "nohelp", options, &run, NULL);
139   return 0;
140 }
141
142 /* end of test_nat_mini.c */