-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / nat / test_nat_mini.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2011 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * Testcase for port redirection and public IP address retrieval.
21  * This test never fails, because there need to be a NAT box set up for tha *
22  * @file nat/test_nat_mini.c
23  * @brief Testcase for NAT library - mini
24  * @author Christian Grothoff
25  *
26  * TODO: actually use ARM to start resolver service to make DNS work!
27  */
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_nat_lib.h"
34
35 /* Time to wait before stopping NAT, in seconds */
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
37
38 /**
39  * Function called on each address that the NAT service
40  * believes to be valid for the transport.
41  */
42 static void
43 addr_callback (void *cls, int add_remove,
44                const struct sockaddr *addr,
45                socklen_t addrlen,
46                enum GNUNET_NAT_StatusCode ret)
47 {
48   if (GNUNET_NAT_ERROR_SUCCESS == ret)
49   {
50     fprintf (stderr,
51            "Address changed: %s `%s' (%u bytes)\n",
52            add_remove == GNUNET_YES
53            ? "added" : "removed",
54            GNUNET_a2s (addr,
55                        addrlen),
56            (unsigned int) addrlen);
57   }
58   else
59     ;
60     //TODO: proper error handling!
61 }
62
63
64 /**
65  * Function that terminates the test.
66  */
67 static void
68 stop (void *cls)
69 {
70   struct GNUNET_NAT_MiniHandle *mini = cls;
71
72   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
73   GNUNET_NAT_mini_map_stop (mini);
74 }
75
76 #define PORT 10000
77
78 /**
79  * Main function run with scheduler.
80  */
81 static void
82 run (void *cls, char *const *args, const char *cfgfile,
83      const struct GNUNET_CONFIGURATION_Handle *cfg)
84 {
85   struct GNUNET_NAT_MiniHandle *mini;
86
87   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
88               "Requesting NAT redirection for port %u...\n",
89               PORT);
90   mini = GNUNET_NAT_mini_map_start (PORT, GNUNET_YES /* tcp */ ,
91                                     &addr_callback, NULL);
92   if (NULL == mini)
93   {
94     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Could not start UPnP interaction\n");
95     return;
96   }
97   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, mini);
98 }
99
100
101 int
102 main (int argc, char *const argv[])
103 {
104   struct GNUNET_GETOPT_CommandLineOption options[] = {
105     GNUNET_GETOPT_OPTION_END
106   };
107
108   char *const argv_prog[] = {
109     "test-nat-mini",
110     "-c",
111     "test_nat_data.conf",
112     "-L",
113     "WARNING",
114     NULL
115   };
116
117   GNUNET_log_setup ("test-nat-mini",
118                     "WARNING",
119                     NULL);
120
121   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
122               "UPnP test for NAT library, timeout set to %s\n",
123               GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
124                                                       GNUNET_YES));
125   GNUNET_PROGRAM_run (5, argv_prog, "test-nat-mini", "nohelp", options, &run,
126                       NULL);
127   return 0;
128 }
129
130 /* end of test_nat_mini.c */