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/>.
20 * Testcase for STUN server resolution
22 * @file nat/test_stun.c
23 * @brief Testcase for STUN library
24 * @author Bruno Souza Cabral
25 * @author Christian Grothoff
30 #include "gnunet_util_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_nat_lib.h"
37 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
40 * Time to wait before stopping NAT, in seconds
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46 * The port the test service is running on (default 7895)
48 static unsigned long port = 7895;
52 static const char *stun_server = "stun.gnunet.org";
54 static int stun_port = 3478;
57 * The listen socket of the service for IPv4
59 static struct GNUNET_NETWORK_Handle *lsock4;
62 * The listen task ID for IPv4
64 static struct GNUNET_SCHEDULER_Task *ltask4;
67 * Handle for the STUN request.
69 static struct GNUNET_NAT_STUN_Handle *rh;
73 print_answer(struct sockaddr_in* answer)
75 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
76 "External IP is: %s , with port %d\n",
77 inet_ntoa (answer->sin_addr),
78 ntohs (answer->sin_port));
83 * Function that terminates the test.
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
89 "Stopping NAT and quitting...\n");
92 GNUNET_SCHEDULER_cancel (ltask4);
97 GNUNET_NETWORK_socket_close(lsock4);
102 GNUNET_NAT_stun_make_request_cancel (rh);
109 * Activity on our incoming socket. Read data from the
110 * incoming connection.
115 do_udp_read (void *cls)
117 //struct GNUNET_NAT_Test *tst = cls;
118 unsigned char reply_buf[1024];
120 struct sockaddr_in answer;
121 const struct GNUNET_SCHEDULER_TaskContext *tc;
124 tc = GNUNET_SCHEDULER_get_task_context ();
125 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
126 (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
130 "Timeout waiting for STUN response\n");
133 rlen = GNUNET_NETWORK_socket_recv (lsock4,
138 sizeof(struct sockaddr_in));
140 GNUNET_NAT_stun_handle_packet (reply_buf,
145 "Unexpected UDP packet, trying to read more\n");
146 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
152 print_answer (&answer);
158 * Create an IPv4 listen socket bound to our port.
160 * @return NULL on error
162 static struct GNUNET_NETWORK_Handle *
165 struct GNUNET_NETWORK_Handle *ls;
166 struct sockaddr_in sa4;
169 memset (&sa4, 0, sizeof (sa4));
170 sa4.sin_family = AF_INET;
171 sa4.sin_port = htons (port);
172 #if HAVE_SOCKADDR_IN_SIN_LEN
173 sa4.sin_len = sizeof (sa4);
175 ls = GNUNET_NETWORK_socket_create (AF_INET,
181 GNUNET_NETWORK_socket_bind (ls,
182 (const struct sockaddr *) &sa4,
186 GNUNET_NETWORK_socket_close (ls);
195 * Function called with the result of the STUN request transmission attempt.
198 * @param error status code from STUN
201 request_callback (void *cls,
202 enum GNUNET_NAT_StatusCode error)
205 if (GNUNET_NAT_ERROR_SUCCESS == error)
207 /* all good, start to receive */
208 ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
214 if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
216 ret = 77; /* report 'skip' */
218 "System is offline, cannot test STUN request.\n");
229 * Main function run with scheduler.
235 const struct GNUNET_CONFIGURATION_Handle *cfg)
237 //Lets create the socket
241 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
243 GNUNET_SCHEDULER_shutdown ();
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
247 "Service listens on port %u\n",
248 (unsigned int) port);
249 rh = GNUNET_NAT_stun_make_request (stun_server,
252 &request_callback, NULL);
253 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
259 main (int argc, char *const argv[])
261 struct GNUNET_GETOPT_CommandLineOption options[] = {
262 GNUNET_GETOPT_OPTION_END
264 char *const argv_prog[] = {
271 struct GNUNET_OS_Process *proc;
273 GNUNET_log_setup ("test-stun",
277 /* Lets start resolver */
278 fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
279 proc = GNUNET_OS_start_process (GNUNET_YES,
280 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
283 "gnunet-service-resolver",
284 "-c", "test_stun.conf", NULL);
288 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
289 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
293 GNUNET_PROGRAM_run (3, argv_prog,
294 "test-stun", "nohelp",
298 /* Now kill the resolver */
299 if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
301 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
303 GNUNET_OS_process_wait (proc);
304 GNUNET_OS_process_destroy (proc);
311 /* end of test_stun.c */