dc8255a8e40977edb2c5b72d286f9e58a4d70490
[oweals/gnunet.git] / src / nat / nat_test.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file nat/nat_test.c
23  * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server)
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_lib.h"
29 #include "nat.h"
30
31
32 /**
33  * Entry we keep for each incoming connection.
34  */
35 struct NatActivity
36 {
37   /**
38    * This is a doubly-linked list.
39    */
40   struct NatActivity *next;
41
42   /**
43    * This is a doubly-linked list.
44    */
45   struct NatActivity *prev;
46
47   /**
48    * Socket of the incoming connection.
49    */
50   struct GNUNET_NETWORK_Handle *sock;
51
52   /**
53    * Handle of the master context.
54    */
55   struct GNUNET_NAT_Test *h;
56
57   /**
58    * Task reading from the incoming connection.
59    */
60   GNUNET_SCHEDULER_TaskIdentifier rtask;
61 };
62
63
64 /**
65  * Handle to a NAT test.
66  */
67 struct GNUNET_NAT_Test
68 {
69
70   /**
71    * Configuration used
72    */
73   const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75   /**
76    * Function to call with success report
77    */
78   GNUNET_NAT_TestCallback report;
79   
80   /**
81    * Closure for 'report'.
82    */
83   void *report_cls;
84
85   /**
86    * Handle to NAT traversal in use
87    */
88   struct GNUNET_NAT_Handle *nat;
89
90   /**
91    * Handle to listen socket, or NULL
92    */
93   struct GNUNET_NETWORK_Handle *lsock;
94
95   /**
96    * Head of list of nat activities.
97    */
98   struct NatActivity *head;
99
100   /**
101    * Tail of list of nat activities.
102    */
103   struct NatActivity *tail;
104
105   /**
106    * Identity of task for the listen socket (if any)
107    */
108   GNUNET_SCHEDULER_TaskIdentifier ltask;
109
110   /**
111    * GNUNET_YES if we're testing TCP
112    */
113   int is_tcp;
114
115   /**
116    * Data that should be transmitted or source-port.
117    */
118   uint16_t data;
119
120   /**
121    * Advertised port to the other peer.
122    */
123   uint16_t adv_port;
124
125 };
126
127
128 /**
129  * Function called from GNUNET_NAT_register
130  * whenever someone asks us to do connection
131  * reversal.
132  *
133  * @param cls closure, our 'struct GNUNET_NAT_Handle'
134  * @param addr public IP address of the other peer
135  * @param addrlen actual lenght of the address
136  */
137 static void
138 reversal_cb (void *cls, 
139              const struct sockaddr *addr,
140              socklen_t addrlen)
141 {
142   struct GNUNET_NAT_Test *h = cls;
143   const struct sockaddr_in *sa;
144
145   if (addrlen != sizeof (struct sockaddr_in))
146     return;
147   sa = (const struct sockaddr_in *) addr;
148   if (h->data != sa->sin_port)
149     {
150       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151                   "Received connection reversal request for wrong port\n");
152       return; /* wrong port */
153     }
154   /* report success */
155   h->report (h->report_cls, GNUNET_OK);
156 }
157
158
159 /**
160  * Activity on our incoming socket.  Read data from the
161  * incoming connection.
162  *
163  * @param cls the 'struct NatActivity'
164  * @param tc scheduler context
165  */
166 static void
167 do_read (void *cls,
168          const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   struct NatActivity *na = cls;
171   struct GNUNET_NAT_Test *tst;
172   uint16_t data;
173
174   na->rtask = GNUNET_SCHEDULER_NO_TASK;
175   tst = na->h;
176   GNUNET_CONTAINER_DLL_remove (tst->head,
177                                tst->tail,
178                                na);
179   if ( (NULL != tc->write_ready) &&
180        (GNUNET_NETWORK_fdset_isset (tc->read_ready, 
181                                     na->sock)) &&
182        (sizeof (data) ==
183         GNUNET_NETWORK_socket_recv (na->sock,
184                                     &data,
185                                     sizeof (data))) )
186     {
187       if (data == tst->data)
188         tst->report (tst->report_cls, GNUNET_OK);
189       else
190         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191                     "Received data mismatches expected value\n");
192     }
193   else
194     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195                 "Failed to receive data from inbound connection\n");
196   GNUNET_NETWORK_socket_close (na->sock);
197   GNUNET_free (na);
198 }
199
200
201 /**
202  * Activity on our listen socket. Accept the
203  * incoming connection.
204  *
205  * @param cls the 'struct GNUNET_NAT_Test'
206  * @param tc scheduler context
207  */
208 static void
209 do_accept (void *cls,
210            const struct GNUNET_SCHEDULER_TaskContext *tc)
211 {
212   struct GNUNET_NAT_Test *tst = cls;
213   struct GNUNET_NETWORK_Handle *s;
214   struct NatActivity *wl;
215
216   tst->ltask = GNUNET_SCHEDULER_NO_TASK;
217   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
218     return; 
219   tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
220                                               tst->lsock,
221                                               &do_accept,
222                                               tst);
223   s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
224   if (NULL == s)
225     {
226       GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
227       return; /* odd error */
228     }
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230               "Got an inbound connection, waiting for data\n");
231   wl = GNUNET_malloc (sizeof (struct NatActivity));
232   wl->sock = s;
233   wl->h = tst;
234   wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
235                                              wl->sock,
236                                              &do_read,
237                                              wl);
238   GNUNET_CONTAINER_DLL_insert (tst->head,
239                                tst->tail,
240                                wl);
241 }
242
243
244 /**
245  * Address-callback, used to send message to gnunet-nat-server.
246  *
247  * @param cls closure
248  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
249  *     the previous (now invalid) one
250  * @param addr either the previous or the new public IP address
251  * @param addrlen actual lenght of the address
252  */
253 static void 
254 addr_cb (void *cls,
255          int add_remove,
256          const struct sockaddr *addr,
257          socklen_t addrlen)
258 {
259   struct GNUNET_NAT_Test *h = cls;
260   struct GNUNET_CLIENT_Connection *client;
261   struct GNUNET_NAT_TestMessage msg;
262   const struct sockaddr_in *sa;
263
264   if (GNUNET_YES != add_remove)
265     return;
266   if (addrlen != sizeof (struct sockaddr_in))
267     return; /* ignore IPv6 here */
268   sa = (const struct sockaddr_in*) addr;
269   msg.header.size = htons (sizeof(struct GNUNET_NAT_TestMessage));
270   msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
271   msg.dst_ipv4 = sa->sin_addr.s_addr;
272   msg.dport = sa->sin_port;
273   msg.data = h->data;
274   msg.is_tcp = htonl ((uint32_t) h->is_tcp);
275
276   client = GNUNET_CLIENT_connect ("gnunet-nat-server",
277                                   h->cfg);
278   GNUNET_break (GNUNET_OK ==
279                 GNUNET_CLIENT_transmit_and_get_response (client,
280                                                          &msg.header,
281                                                          GNUNET_TIME_UNIT_SECONDS,
282                                                          GNUNET_YES,
283                                                          NULL, NULL));
284   GNUNET_CLIENT_disconnect (client, GNUNET_YES);  
285 }
286
287
288 /**
289  * Start testing if NAT traversal works using the
290  * given configuration (IPv4-only).
291  *
292  * @param cfg configuration for the NAT traversal
293  * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
294  * @param bnd_port port to bind to, 0 for connection reversal
295  * @param adv_port externally advertised port to use
296  * @param report function to call with the result of the test
297  * @param report_cls closure for report
298  * @return handle to cancel NAT test
299  */
300 struct GNUNET_NAT_Test *
301 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
302                        int is_tcp,
303                        uint16_t bnd_port,
304                        uint16_t adv_port,
305                        GNUNET_NAT_TestCallback report,
306                        void *report_cls)
307 {
308   struct GNUNET_NAT_Test *ret;
309   struct sockaddr_in sa;
310   const struct sockaddr *addrs[] = { (const struct sockaddr*) &sa };
311   const socklen_t addrlens[] = { sizeof (sa) };
312
313   memset (&sa, 0, sizeof (sa));
314   sa.sin_port = htons (bnd_port);
315 #if HAVE_SOCKADDR_IN_SIN_LEN
316   sa.sin_len = sizeof (sa);
317 #endif
318   
319   ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
320   ret->cfg = cfg;
321   ret->is_tcp = is_tcp;
322   ret->data = bnd_port;
323   ret->adv_port = adv_port;
324   ret->report = report;
325   ret->report_cls = report_cls;
326   if (bnd_port == 0)
327     {      
328       ret->nat = GNUNET_NAT_register (cfg, is_tcp,
329                                       0, 
330                                       0, NULL, NULL,
331                                       &addr_cb, &reversal_cb, ret);
332     }
333   else
334     {
335       ret->lsock = GNUNET_NETWORK_socket_create (AF_INET, 
336                                                  (is_tcp==GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM, 0);
337       if ( (ret->lsock == NULL) ||
338            (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
339                                                      (const struct sockaddr*) &sa,
340                                                      sizeof (sa))) )
341         {
342           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
343                       _("Failed to create listen socket for NAT test\n"));
344           if (NULL != ret->lsock)
345             GNUNET_NETWORK_socket_close (ret->lsock);
346           GNUNET_free (ret);
347           return NULL;
348         }
349       GNUNET_break (GNUNET_OK ==
350                     GNUNET_NETWORK_socket_listen (ret->lsock, 5));
351       ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
352                                                   ret->lsock,
353                                                   &do_accept,
354                                                   ret);
355       ret->nat = GNUNET_NAT_register (cfg, is_tcp,
356                                       adv_port, 
357                                       1, addrs, addrlens,
358                                       &addr_cb, NULL, ret);
359     }
360   return ret;
361 }
362
363
364 /**
365  * Stop an active NAT test.
366  *
367  * @param tst test to stop.
368  */
369 void
370 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
371 {
372   struct NatActivity *pos;
373
374   while (NULL != (pos = tst->head))
375     {
376       GNUNET_CONTAINER_DLL_remove (tst->head,
377                                    tst->tail,
378                                    pos);
379       GNUNET_SCHEDULER_cancel (pos->rtask);
380       GNUNET_NETWORK_socket_close (pos->sock);
381       GNUNET_free (pos);
382     }
383   if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
384     GNUNET_SCHEDULER_cancel (tst->ltask);
385   if (NULL != tst->lsock)
386     GNUNET_NETWORK_socket_close (tst->lsock);
387   GNUNET_NAT_unregister (tst->nat);
388   GNUNET_free (tst);
389 }
390
391 /* end of nat_test.c */