2 This file is part of GNUnet.
3 Copyright (C) 2009, 2015 GNUnet e.V.
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.
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.
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.
22 * Testcase for STUN server resolution
24 * @file nat/test_stun.c
25 * @brief Testcase for STUN library
26 * @author Bruno Souza Cabral
27 * @author Christian Grothoff
32 #include "gnunet_util_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_nat_lib.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
42 * Time to wait before stopping NAT, in seconds
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
48 * The port the test service is running on (default 7895)
50 static unsigned long port = 7895;
54 static const char *stun_server = "stun.gnunet.org";
56 static int stun_port = 3478;
59 * The listen socket of the service for IPv4
61 static struct GNUNET_NETWORK_Handle *lsock4;
64 * The listen task ID for IPv4
66 static struct GNUNET_SCHEDULER_Task *ltask4;
69 * Handle for the STUN request.
71 static struct GNUNET_NAT_STUN_Handle *rh;
75 print_answer(struct sockaddr_in* answer)
77 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
78 "External IP is: %s , with port %d\n",
79 inet_ntoa (answer->sin_addr),
80 ntohs (answer->sin_port));
85 * Function that terminates the test.
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91 "Stopping NAT and quitting...\n");
94 GNUNET_SCHEDULER_cancel (ltask4);
99 GNUNET_NETWORK_socket_close(lsock4);
104 GNUNET_NAT_stun_make_request_cancel (rh);
111 * Activity on our incoming socket. Read data from the
112 * incoming connection.
117 do_udp_read (void *cls)
119 //struct GNUNET_NAT_Test *tst = cls;
120 unsigned char reply_buf[1024];
122 struct sockaddr_in answer;
123 const struct GNUNET_SCHEDULER_TaskContext *tc;
126 tc = GNUNET_SCHEDULER_get_task_context ();
127 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
128 (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
132 "Timeout waiting for STUN response\n");
135 rlen = GNUNET_NETWORK_socket_recv (lsock4,
140 sizeof(struct sockaddr_in));
142 GNUNET_NAT_stun_handle_packet (reply_buf,
147 "Unexpected UDP packet, trying to read more\n");
148 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
154 print_answer (&answer);
160 * Create an IPv4 listen socket bound to our port.
162 * @return NULL on error
164 static struct GNUNET_NETWORK_Handle *
167 struct GNUNET_NETWORK_Handle *ls;
168 struct sockaddr_in sa4;
171 memset (&sa4, 0, sizeof (sa4));
172 sa4.sin_family = AF_INET;
173 sa4.sin_port = htons (port);
174 #if HAVE_SOCKADDR_IN_SIN_LEN
175 sa4.sin_len = sizeof (sa4);
177 ls = GNUNET_NETWORK_socket_create (AF_INET,
183 GNUNET_NETWORK_socket_bind (ls,
184 (const struct sockaddr *) &sa4,
188 GNUNET_NETWORK_socket_close (ls);
197 * Function called with the result of the STUN request transmission attempt.
200 * @param error status code from STUN
203 request_callback (void *cls,
204 enum GNUNET_NAT_StatusCode error)
207 if (GNUNET_NAT_ERROR_SUCCESS == error)
209 /* all good, start to receive */
210 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
216 if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
218 ret = 77; /* report 'skip' */
220 "System is offline, cannot test STUN request.\n");
231 * Main function run with scheduler.
237 const struct GNUNET_CONFIGURATION_Handle *cfg)
239 //Lets create the socket
243 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
245 GNUNET_SCHEDULER_shutdown ();
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 "Service listens on port %u\n",
250 (unsigned int) port);
251 rh = GNUNET_NAT_stun_make_request (stun_server,
254 &request_callback, NULL);
255 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
261 main (int argc, char *const argv[])
263 struct GNUNET_GETOPT_CommandLineOption options[] = {
264 GNUNET_GETOPT_OPTION_END
266 char *const argv_prog[] = {
273 struct GNUNET_OS_Process *proc;
275 GNUNET_log_setup ("test-stun",
279 /* Lets start resolver */
280 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
281 proc = GNUNET_OS_start_process (GNUNET_YES,
282 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
285 "gnunet-service-resolver",
286 "-c", "test_stun.conf", NULL);
290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
295 GNUNET_PROGRAM_run (3, argv_prog,
296 "test-stun", "nohelp",
300 /* Now kill the resolver */
301 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
303 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
305 GNUNET_OS_process_wait (proc);
306 GNUNET_OS_process_destroy (proc);
313 /* end of test_stun.c */