e75d55bd0276719ce43fd0b32c825b822900a36a
[oweals/gnunet.git] / src / nat / test_stun.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2015 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * Testcase for STUN server resolution
23  *
24  * @file nat/test_stun.c
25  * @brief Testcase for STUN library
26  * @author Bruno Souza Cabral
27
28  *
29  */
30
31
32 #include "platform.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_nat_lib.h"
37
38
39
40 #define LOG(kind,...) GNUNET_log_from (kind, "test-stun", __VA_ARGS__)
41
42 /**
43  * Time to wait before stopping NAT, in seconds
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
46
47
48 /**
49  * The port the test service is running on (default 7895)
50  */
51 static unsigned long port = 7895;
52 static int ret = 1;
53
54 static char *stun_server = "stun2.l.google.com";
55 static int stun_port = 19302;
56
57 /**
58  * The listen socket of the service for IPv4
59  */
60 static struct GNUNET_NETWORK_Handle *lsock4;
61
62
63 /**
64  * The listen task ID for IPv4
65  */
66 static struct GNUNET_SCHEDULER_Task * ltask4;
67
68
69
70
71 static void
72 print_answer(struct sockaddr_in* answer)
73 {
74         printf("External IP is: %s , with port %d\n", inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
75 }
76
77
78 /**
79  * Activity on our incoming socket.  Read data from the
80  * incoming connection.
81  *
82  * @param cls 
83  * @param tc scheduler context
84  */
85 static void
86 do_udp_read (void *cls,
87              const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89     //struct GNUNET_NAT_Test *tst = cls;
90         unsigned char reply_buf[1024];
91         ssize_t rlen;
92         struct sockaddr_in answer;
93
94     printf("UDP READ\n");
95
96
97     if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
98       (GNUNET_NETWORK_fdset_isset (tc->read_ready,
99                                    lsock4)))
100         {
101                 rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof (reply_buf));
102
103                 
104                 //Lets handle the packet
105                 memset(&answer, 0, sizeof(struct sockaddr_in));
106         GNUNET_NAT_stun_handle_packet(reply_buf,rlen, &answer);
107
108                 //Print the answer
109                 ret = 0;
110                 print_answer(&answer);
111
112         //Destroy the connection
113         GNUNET_NETWORK_socket_close(lsock4);
114                 
115         }
116
117
118 }
119
120
121 /**
122  * Create an IPv4 listen socket bound to our port.
123  *
124  * @return NULL on error
125  */
126 static struct GNUNET_NETWORK_Handle *
127 bind_v4 ()
128 {
129     struct GNUNET_NETWORK_Handle *ls;
130     struct sockaddr_in sa4;
131     int eno;
132
133     memset (&sa4, 0, sizeof (sa4));
134     sa4.sin_family = AF_INET;
135     sa4.sin_port = htons (port);
136 #if HAVE_SOCKADDR_IN_SIN_LEN
137     sa4.sin_len = sizeof (sa4);
138 #endif 
139     ls = GNUNET_NETWORK_socket_create (AF_INET,
140                                        SOCK_DGRAM,
141                                        0);
142     if (NULL == ls)
143         return NULL;
144     if (GNUNET_OK !=
145             GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
146                                         sizeof (sa4)))
147     {
148         eno = errno;
149         GNUNET_NETWORK_socket_close (ls);
150         errno = eno;
151         return NULL;
152     }
153     return ls;
154 }
155
156 /**
157  * Function that terminates the test.
158  */
159 static void
160 stop ()
161 {
162     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
163
164     printf("Stopped !!\n");
165     //Clean task
166     if(NULL != ltask4)
167         GNUNET_SCHEDULER_cancel (ltask4);
168
169     //Clean socket
170     if(NULL != ltask4)
171         GNUNET_NETWORK_socket_close (lsock4);
172
173 }
174
175
176 static void request_callback(void *cls,
177 enum GNUNET_NAT_StatusCode result)
178 {
179     ret = result;
180     stop();
181     printf("Called back\n");
182 };
183
184
185 /**
186  * Main function run with scheduler.
187  */
188 static void
189 run (void *cls, char *const *args, const char *cfgfile,
190      const struct GNUNET_CONFIGURATION_Handle *cfg)
191 {
192
193
194     //Lets create the socket
195     lsock4 = bind_v4 ();
196     if (NULL == lsock4)
197     {
198         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
199         GNUNET_SCHEDULER_shutdown ();
200         return;
201     }
202     else
203     {
204         //Lets call our function now when it accepts
205         ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
206                                                 lsock4, &do_udp_read, NULL);
207         /* So you read once and what will happen if you get an irregular message? Repeat and add timeout */
208
209     }
210
211     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
212                 "Service listens on port %u\n",
213                 port);
214         printf("Start main event\n");
215     GNUNET_NAT_stun_make_request(stun_server, stun_port, lsock4, &request_callback, NULL);
216     printf("Made the requeest\n");
217
218     //GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL);
219
220 }
221
222
223 int
224 main (int argc, char *const argv[])
225 {
226     struct GNUNET_GETOPT_CommandLineOption options[] = {
227         GNUNET_GETOPT_OPTION_END
228     };
229
230     char *const argv_prog[] = {
231         "test-stun",
232         "-c",
233         "test_stun.conf",
234         NULL
235     };
236     GNUNET_log_setup ("test-stun",
237                       "WARNING",
238                       NULL);
239
240     /* Lets start resolver */
241     char *fn;
242     struct GNUNET_OS_Process *proc;
243
244     fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
245     proc = GNUNET_OS_start_process (GNUNET_YES,
246                                     GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
247                                     NULL, NULL, NULL,
248                                     fn,
249                                     "gnunet-service-resolver",
250                                     "-c", "test_stun.conf", NULL);
251     GNUNET_assert (NULL != proc);
252
253     GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, NULL);
254
255     /* Now kill the resolver */
256     if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
257     {
258         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
259     }
260     GNUNET_OS_process_wait (proc);
261     GNUNET_OS_process_destroy (proc);
262     proc = NULL;
263
264     
265         return ret;
266 }
267
268 /* end of test_stun.c */