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 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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
38 #define LOG(kind, ...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
41 * Time to wait before stopping NAT, in seconds
43 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
47 * The port the test service is running on (default 7895)
49 static unsigned long port = 7895;
53 static const char *stun_server = "stun.gnunet.org";
55 static int stun_port = 3478;
58 * The listen socket of the service for IPv4
60 static struct GNUNET_NETWORK_Handle *lsock4;
63 * The listen task ID for IPv4
65 static struct GNUNET_SCHEDULER_Task *ltask4;
68 * Handle for the STUN request.
70 static struct GNUNET_NAT_STUN_Handle *rh;
74 print_answer (struct sockaddr_in*answer)
76 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
77 "External IP is: %s , with port %d\n",
78 inet_ntoa (answer->sin_addr),
79 ntohs (answer->sin_port));
84 * Function that terminates the test.
89 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
90 "Stopping NAT and quitting...\n");
93 GNUNET_SCHEDULER_cancel (ltask4);
98 GNUNET_NETWORK_socket_close (lsock4);
103 GNUNET_NAT_stun_make_request_cancel (rh);
110 * Activity on our incoming socket. Read data from the
111 * incoming connection.
116 do_udp_read (void *cls)
118 // struct GNUNET_NAT_Test *tst = cls;
119 unsigned char reply_buf[1024];
121 struct sockaddr_in answer;
122 const struct GNUNET_SCHEDULER_TaskContext *tc;
125 tc = GNUNET_SCHEDULER_get_task_context ();
126 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
127 (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
131 "Timeout waiting for STUN response\n");
134 rlen = GNUNET_NETWORK_socket_recv (lsock4,
139 sizeof(struct sockaddr_in));
141 GNUNET_NAT_stun_handle_packet (reply_buf,
146 "Unexpected UDP packet, trying to read more\n");
147 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
153 print_answer (&answer);
159 * Create an IPv4 listen socket bound to our port.
161 * @return NULL on error
163 static struct GNUNET_NETWORK_Handle *
166 struct GNUNET_NETWORK_Handle *ls;
167 struct sockaddr_in sa4;
170 memset (&sa4, 0, sizeof(sa4));
171 sa4.sin_family = AF_INET;
172 sa4.sin_port = htons (port);
173 #if HAVE_SOCKADDR_IN_SIN_LEN
174 sa4.sin_len = sizeof(sa4);
176 ls = GNUNET_NETWORK_socket_create (AF_INET,
182 GNUNET_NETWORK_socket_bind (ls,
183 (const struct sockaddr *) &sa4,
187 GNUNET_NETWORK_socket_close (ls);
196 * Function called with the result of the STUN request transmission attempt.
199 * @param error status code from STUN
202 request_callback (void *cls,
203 enum GNUNET_NAT_StatusCode error)
206 if (GNUNET_NAT_ERROR_SUCCESS == error)
208 /* all good, start to receive */
209 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
215 if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
217 ret = 77; /* report 'skip' */
219 "System is offline, cannot test STUN request.\n");
230 * Main function run with scheduler.
236 const struct GNUNET_CONFIGURATION_Handle *cfg)
238 // Lets create the socket
242 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
244 GNUNET_SCHEDULER_shutdown ();
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248 "Service listens on port %u\n",
249 (unsigned int) port);
250 rh = GNUNET_NAT_stun_make_request (stun_server,
253 &request_callback, NULL);
254 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
260 main (int argc, char *const argv[])
262 struct GNUNET_GETOPT_CommandLineOption options[] = {
263 GNUNET_GETOPT_OPTION_END
265 char *const argv_prog[] = {
272 struct GNUNET_OS_Process *proc;
274 GNUNET_log_setup ("test-stun",
278 /* Lets start resolver */
279 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
280 proc = GNUNET_OS_start_process (GNUNET_YES,
281 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
284 "gnunet-service-resolver",
285 "-c", "test_stun.conf", NULL);
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
290 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
294 GNUNET_PROGRAM_run (3, argv_prog,
295 "test-stun", "nohelp",
299 /* Now kill the resolver */
300 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
302 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
304 GNUNET_OS_process_wait (proc);
305 GNUNET_OS_process_destroy (proc);
313 /* end of test_stun.c */