630538162babc84421980957a2d0a7987c5f937b
[oweals/gnunet.git] / src / nat / test_stun.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2015 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14     
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/>.
17 */
18
19 /**
20  * Testcase for STUN server resolution
21  *
22  * @file nat/test_stun.c
23  * @brief Testcase for STUN library
24  * @author Bruno Souza Cabral
25  * @author Christian Grothoff
26  */
27
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_nat_lib.h"
34
35
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
38
39 /**
40  * Time to wait before stopping NAT, in seconds
41  */
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43
44
45 /**
46  * The port the test service is running on (default 7895)
47  */
48 static unsigned long port = 7895;
49
50 static int ret = 1;
51
52 static const char *stun_server = "stun.gnunet.org";
53
54 static int stun_port = 3478;
55
56 /**
57  * The listen socket of the service for IPv4
58  */
59 static struct GNUNET_NETWORK_Handle *lsock4;
60
61 /**
62  * The listen task ID for IPv4
63  */
64 static struct GNUNET_SCHEDULER_Task *ltask4;
65
66 /**
67  * Handle for the STUN request.
68  */
69 static struct GNUNET_NAT_STUN_Handle *rh;
70
71
72 static void
73 print_answer(struct sockaddr_in* answer)
74 {
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));
79 }
80
81
82 /**
83  * Function that terminates the test.
84  */
85 static void
86 stop ()
87 {
88   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
89               "Stopping NAT and quitting...\n");
90   if (NULL != ltask4)
91   {
92     GNUNET_SCHEDULER_cancel (ltask4);
93     ltask4 = NULL;
94   }
95   if(NULL != lsock4)
96   {
97     GNUNET_NETWORK_socket_close(lsock4);
98     lsock4 = NULL;
99   }
100   if (NULL != rh)
101   {
102     GNUNET_NAT_stun_make_request_cancel (rh);
103     rh = NULL;
104   }
105 }
106
107
108 /**
109  * Activity on our incoming socket.  Read data from the
110  * incoming connection.
111  *
112  * @param cls
113  */
114 static void
115 do_udp_read (void *cls)
116 {
117   //struct GNUNET_NAT_Test *tst = cls;
118   unsigned char reply_buf[1024];
119   ssize_t rlen;
120   struct sockaddr_in answer;
121   const struct GNUNET_SCHEDULER_TaskContext *tc;
122
123   ltask4 = NULL;
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,
127                                       lsock4)) )
128   {
129     fprintf (stderr,
130              "Timeout waiting for STUN response\n");
131     stop();
132   }
133   rlen = GNUNET_NETWORK_socket_recv (lsock4,
134                                      reply_buf,
135                                      sizeof (reply_buf));
136   memset (&answer,
137           0,
138           sizeof(struct sockaddr_in));
139   if (GNUNET_OK !=
140       GNUNET_NAT_stun_handle_packet (reply_buf,
141                                      rlen,
142                                      &answer))
143   {
144     fprintf (stderr,
145              "Unexpected UDP packet, trying to read more\n");
146     ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
147                                             lsock4,
148                                             &do_udp_read, NULL);
149     return;
150   }
151   ret = 0;
152   print_answer (&answer);
153   stop ();
154 }
155
156
157 /**
158  * Create an IPv4 listen socket bound to our port.
159  *
160  * @return NULL on error
161  */
162 static struct GNUNET_NETWORK_Handle *
163 bind_v4 ()
164 {
165   struct GNUNET_NETWORK_Handle *ls;
166   struct sockaddr_in sa4;
167   int eno;
168
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);
174 #endif
175   ls = GNUNET_NETWORK_socket_create (AF_INET,
176                                      SOCK_DGRAM,
177                                      0);
178   if (NULL == ls)
179     return NULL;
180   if (GNUNET_OK !=
181       GNUNET_NETWORK_socket_bind (ls,
182                                   (const struct sockaddr *) &sa4,
183                                   sizeof (sa4)))
184   {
185     eno = errno;
186     GNUNET_NETWORK_socket_close (ls);
187     errno = eno;
188     return NULL;
189   }
190   return ls;
191 }
192
193
194 /**
195  * Function called with the result of the STUN request transmission attempt.
196  *
197  * @param cls unused
198  * @param error status code from STUN
199  */
200 static void
201 request_callback (void *cls,
202                   enum GNUNET_NAT_StatusCode error)
203 {
204   rh = NULL;
205   if (GNUNET_NAT_ERROR_SUCCESS == error)
206   {
207     /* all good, start to receive */
208     ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
209                                             lsock4,
210                                             &do_udp_read,
211                                             NULL);
212     return;
213   }
214   if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
215   {
216     ret = 77; /* report 'skip' */
217     fprintf (stderr,
218              "System is offline, cannot test STUN request.\n");
219   }
220   else
221   {
222     ret = error;
223   }
224   stop();
225 }
226
227
228 /**
229  * Main function run with scheduler.
230  */
231 static void
232 run (void *cls,
233      char *const *args,
234      const char *cfgfile,
235      const struct GNUNET_CONFIGURATION_Handle *cfg)
236 {
237   //Lets create the socket
238   lsock4 = bind_v4 ();
239   if (NULL == lsock4)
240   {
241     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
242                          "bind");
243     GNUNET_SCHEDULER_shutdown ();
244     return;
245   }
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,
250                                      stun_port,
251                                      lsock4,
252                                      &request_callback, NULL);
253   GNUNET_SCHEDULER_add_delayed (TIMEOUT,
254                                 &stop, NULL);
255 }
256
257
258 int
259 main (int argc, char *const argv[])
260 {
261   struct GNUNET_GETOPT_CommandLineOption options[] = {
262       GNUNET_GETOPT_OPTION_END
263   };
264   char *const argv_prog[] = {
265       "test-stun",
266       "-c",
267       "test_stun.conf",
268       NULL
269   };
270   char *fn;
271   struct GNUNET_OS_Process *proc;
272
273   GNUNET_log_setup ("test-stun",
274                     "WARNING",
275                     NULL);
276
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,
281                                   NULL, NULL, NULL,
282                                   fn,
283                                   "gnunet-service-resolver",
284                                   "-c", "test_stun.conf", NULL);
285
286   if (NULL == proc)
287   {
288     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
289                 "This test was unable to start gnunet-service-resolver, and it is required to run ...\n");
290     exit(1);
291   }
292
293   GNUNET_PROGRAM_run (3, argv_prog,
294                       "test-stun", "nohelp",
295                       options,
296                       &run, NULL);
297
298   /* Now kill the resolver */
299   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
300   {
301     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
302   }
303   GNUNET_OS_process_wait (proc);
304   GNUNET_OS_process_destroy (proc);
305   proc = NULL;
306   GNUNET_free (fn);
307
308   return ret;
309 }
310
311 /* end of test_stun.c */