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