- fix multiple gnunet-nat crashes
[oweals/gnunet.git] / src / nat / test_stun.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2015 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 STUN server resolution
23  *
24  * @file nat/test_stun.c
25  * @brief Testcase for STUN library
26  * @author Bruno Souza Cabral
27
28  *
29  */
30
31
32 #include "platform.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
40 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
41
42 /**
43  * Time to wait before stopping NAT, in seconds
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46
47
48 /**
49  * The port the test service is running on (default 7895)
50  */
51 static unsigned long port = 7895;
52 static int ret = 1;
53
54 static char *stun_server = "stun.ekiga.net";
55 static int stun_port = 3478;
56
57 /**
58  * The listen socket of the service for IPv4
59  */
60 static struct GNUNET_NETWORK_Handle *lsock4;
61
62
63 /**
64  * The listen task ID for IPv4
65  */
66 static struct GNUNET_SCHEDULER_Task * ltask4;
67
68
69
70
71 static void
72 print_answer(struct sockaddr_in* answer)
73 {
74     GNUNET_log (GNUNET_ERROR_TYPE_INFO,"External IP is: %s , with port %d\n", inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
75 }
76
77
78
79 /**
80  * Function that terminates the test.
81  */
82 static void
83 stop ()
84 {
85   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
86
87   //Clean task
88   if(NULL != ltask4)
89   {
90     GNUNET_SCHEDULER_cancel (ltask4);
91     ltask4 = NULL;
92   }
93
94   //Clean socket
95   if(NULL != lsock4)
96   {
97     GNUNET_NETWORK_socket_close(lsock4);
98     lsock4 = NULL;
99   }
100
101 }
102
103
104 /**
105  * Activity on our incoming socket.  Read data from the
106  * incoming connection.
107  *
108  * @param cls
109  * @param tc scheduler context
110  */
111 static void
112 do_udp_read (void *cls,
113              const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
115     //struct GNUNET_NAT_Test *tst = cls;
116         unsigned char reply_buf[1024];
117         ssize_t rlen;
118         struct sockaddr_in answer;
119
120   if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
121       (GNUNET_NETWORK_fdset_isset (tc->read_ready,
122                                    lsock4)))
123         {
124                 rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));
125
126
127                 //Lets handle the packet
128                 memset(&answer, 0, sizeof(struct sockaddr_in));
129
130     if(GNUNET_OK == GNUNET_NAT_stun_handle_packet(reply_buf, rlen, &answer))
131     {
132       //Print the answer
133       ret = 0;
134       print_answer(&answer);
135
136       //Destroy the connection
137       GNUNET_NETWORK_socket_close(lsock4);
138       lsock4 = NULL;
139
140     }
141     else
142     {
143       //Lets try again, its a invalid message
144       ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
145                                               lsock4, &do_udp_read, NULL);
146     }
147
148         }
149   else
150   {
151     //We got a timeout
152     ltask4 = NULL;
153     stop();
154   }
155
156   ltask4 = NULL;
157 }
158
159
160 /**
161  * Create an IPv4 listen socket bound to our port.
162  *
163  * @return NULL on error
164  */
165 static struct GNUNET_NETWORK_Handle *
166 bind_v4 ()
167 {
168     struct GNUNET_NETWORK_Handle *ls;
169     struct sockaddr_in sa4;
170     int eno;
171
172     memset (&sa4, 0, sizeof (sa4));
173     sa4.sin_family = AF_INET;
174     sa4.sin_port = htons (port);
175 #if HAVE_SOCKADDR_IN_SIN_LEN
176     sa4.sin_len = sizeof (sa4);
177 #endif
178     ls = GNUNET_NETWORK_socket_create (AF_INET,
179                                        SOCK_DGRAM,
180                                        0);
181     if (NULL == ls)
182         return NULL;
183     if (GNUNET_OK !=
184             GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
185                                         sizeof (sa4)))
186     {
187         eno = errno;
188         GNUNET_NETWORK_socket_close (ls);
189         errno = eno;
190         return NULL;
191     }
192     return ls;
193 }
194
195
196
197 static void request_callback(void *cls,
198 enum GNUNET_NAT_StatusCode error)
199 {
200   if(error == GNUNET_NAT_ERROR_NOT_ONLINE)
201   {
202     //If we are not online, mark the test as success
203     ret = 0;
204     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205                 "test-stun detected as offline, cant make STUN request.\n"
206                 );
207   }
208   else
209   {
210     ret = error;
211   }
212   stop();
213 };
214
215
216 /**
217  * Main function run with scheduler.
218  */
219 static void
220 run (void *cls, char *const *args, const char *cfgfile,
221      const struct GNUNET_CONFIGURATION_Handle *cfg)
222 {
223
224   //Lets create the socket
225   lsock4 = bind_v4 ();
226   ltask4 = NULL;
227   if (NULL == lsock4)
228   {
229       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
230       GNUNET_SCHEDULER_shutdown ();
231       return;
232   }
233   else
234   {
235       //Lets call our function now when it accepts
236       ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
237                                               lsock4, &do_udp_read, NULL );
238
239   }
240
241   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
242               "Service listens on port %u\n",
243               port);
244   GNUNET_NAT_stun_make_request (stun_server, stun_port, lsock4,
245                                 &request_callback, NULL);
246
247   GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL);
248
249 }
250
251
252 int
253 main (int argc, char *const argv[])
254 {
255   struct GNUNET_GETOPT_CommandLineOption options[] = {
256       GNUNET_GETOPT_OPTION_END
257   };
258
259   char *const argv_prog[] = {
260       "test-stun",
261       "-c",
262       "test_stun.conf",
263       NULL
264   };
265   GNUNET_log_setup ("test-stun",
266                     "WARNING",
267                     NULL);
268
269   /* Lets start resolver */
270   char *fn;
271   struct GNUNET_OS_Process *proc;
272
273   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
274   proc = GNUNET_OS_start_process (GNUNET_YES,
275                                   GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
276                                   NULL, NULL, NULL,
277                                   fn,
278                                   "gnunet-service-resolver",
279                                   "-c", "test_stun.conf", NULL);
280
281   if (NULL == proc)
282   {
283     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
284     exit(1);
285   }
286
287   GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, NULL);
288
289
290   /* Now kill the resolver */
291   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
292   {
293       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
294   }
295   GNUNET_OS_process_wait (proc);
296   GNUNET_OS_process_destroy (proc);
297   proc = NULL;
298   GNUNET_free (fn);
299
300
301         return ret;
302 }
303
304 /* end of test_stun.c */