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