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 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.
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.
17 * Testcase for STUN server resolution
19 * @file nat/test_stun.c
20 * @brief Testcase for STUN library
21 * @author Bruno Souza Cabral
22 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_nat_lib.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
37 * Time to wait before stopping NAT, in seconds
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43 * The port the test service is running on (default 7895)
45 static unsigned long port = 7895;
49 static const char *stun_server = "stun.gnunet.org";
51 static int stun_port = 3478;
54 * The listen socket of the service for IPv4
56 static struct GNUNET_NETWORK_Handle *lsock4;
59 * The listen task ID for IPv4
61 static struct GNUNET_SCHEDULER_Task *ltask4;
64 * Handle for the STUN request.
66 static struct GNUNET_NAT_STUN_Handle *rh;
70 print_answer(struct sockaddr_in* answer)
72 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
73 "External IP is: %s , with port %d\n",
74 inet_ntoa (answer->sin_addr),
75 ntohs (answer->sin_port));
80 * Function that terminates the test.
85 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
86 "Stopping NAT and quitting...\n");
89 GNUNET_SCHEDULER_cancel (ltask4);
94 GNUNET_NETWORK_socket_close(lsock4);
99 GNUNET_NAT_stun_make_request_cancel (rh);
106 * Activity on our incoming socket. Read data from the
107 * incoming connection.
112 do_udp_read (void *cls)
114 //struct GNUNET_NAT_Test *tst = cls;
115 unsigned char reply_buf[1024];
117 struct sockaddr_in answer;
118 const struct GNUNET_SCHEDULER_TaskContext *tc;
121 tc = GNUNET_SCHEDULER_get_task_context ();
122 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
123 (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
127 "Timeout waiting for STUN response\n");
130 rlen = GNUNET_NETWORK_socket_recv (lsock4,
135 sizeof(struct sockaddr_in));
137 GNUNET_NAT_stun_handle_packet (reply_buf,
142 "Unexpected UDP packet, trying to read more\n");
143 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
149 print_answer (&answer);
155 * Create an IPv4 listen socket bound to our port.
157 * @return NULL on error
159 static struct GNUNET_NETWORK_Handle *
162 struct GNUNET_NETWORK_Handle *ls;
163 struct sockaddr_in sa4;
166 memset (&sa4, 0, sizeof (sa4));
167 sa4.sin_family = AF_INET;
168 sa4.sin_port = htons (port);
169 #if HAVE_SOCKADDR_IN_SIN_LEN
170 sa4.sin_len = sizeof (sa4);
172 ls = GNUNET_NETWORK_socket_create (AF_INET,
178 GNUNET_NETWORK_socket_bind (ls,
179 (const struct sockaddr *) &sa4,
183 GNUNET_NETWORK_socket_close (ls);
192 * Function called with the result of the STUN request transmission attempt.
195 * @param error status code from STUN
198 request_callback (void *cls,
199 enum GNUNET_NAT_StatusCode error)
202 if (GNUNET_NAT_ERROR_SUCCESS == error)
204 /* all good, start to receive */
205 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
211 if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
213 ret = 77; /* report 'skip' */
215 "System is offline, cannot test STUN request.\n");
226 * Main function run with scheduler.
232 const struct GNUNET_CONFIGURATION_Handle *cfg)
234 //Lets create the socket
238 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
240 GNUNET_SCHEDULER_shutdown ();
243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
244 "Service listens on port %u\n",
245 (unsigned int) port);
246 rh = GNUNET_NAT_stun_make_request (stun_server,
249 &request_callback, NULL);
250 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
256 main (int argc, char *const argv[])
258 struct GNUNET_GETOPT_CommandLineOption options[] = {
259 GNUNET_GETOPT_OPTION_END
261 char *const argv_prog[] = {
268 struct GNUNET_OS_Process *proc;
270 GNUNET_log_setup ("test-stun",
274 /* Lets start resolver */
275 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
276 proc = GNUNET_OS_start_process (GNUNET_YES,
277 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
280 "gnunet-service-resolver",
281 "-c", "test_stun.conf", NULL);
285 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
286 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
290 GNUNET_PROGRAM_run (3, argv_prog,
291 "test-stun", "nohelp",
295 /* Now kill the resolver */
296 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
298 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
300 GNUNET_OS_process_wait (proc);
301 GNUNET_OS_process_destroy (proc);
308 /* end of test_stun.c */