indentation
[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  * Entry we keep for each connection to the gnunet-nat-service.
66  */
67 struct ClientActivity
68 {
69   /**
70    * This is a doubly-linked list.
71    */
72   struct ClientActivity *next;
73
74   /**
75    * This is a doubly-linked list.
76    */
77   struct ClientActivity *prev;
78
79   /**
80    * Socket of the incoming connection.
81    */
82   struct GNUNET_CLIENT_Connection *client;
83
84 };
85
86
87 /**
88  * Handle to a NAT test.
89  */
90 struct GNUNET_NAT_Test
91 {
92
93   /**
94    * Configuration used
95    */
96   const struct GNUNET_CONFIGURATION_Handle *cfg;
97
98   /**
99    * Function to call with success report
100    */
101   GNUNET_NAT_TestCallback report;
102
103   /**
104    * Closure for 'report'.
105    */
106   void *report_cls;
107
108   /**
109    * Handle to NAT traversal in use
110    */
111   struct GNUNET_NAT_Handle *nat;
112
113   /**
114    * Handle to listen socket, or NULL
115    */
116   struct GNUNET_NETWORK_Handle *lsock;
117
118   /**
119    * Head of list of nat activities.
120    */
121   struct NatActivity *na_head;
122
123   /**
124    * Tail of list of nat activities.
125    */
126   struct NatActivity *na_tail;
127
128   /**
129    * Head of list of client activities.
130    */
131   struct ClientActivity *ca_head;
132
133   /**
134    * Tail of list of client activities.
135    */
136   struct ClientActivity *ca_tail;
137
138   /**
139    * Identity of task for the listen socket (if any)
140    */
141   GNUNET_SCHEDULER_TaskIdentifier ltask;
142
143   /**
144    * GNUNET_YES if we're testing TCP
145    */
146   int is_tcp;
147
148   /**
149    * Data that should be transmitted or source-port.
150    */
151   uint16_t data;
152
153   /**
154    * Advertised port to the other peer.
155    */
156   uint16_t adv_port;
157
158 };
159
160
161 /**
162  * Function called from GNUNET_NAT_register whenever someone asks us
163  * to do connection reversal.
164  *
165  * @param cls closure, our 'struct GNUNET_NAT_Handle'
166  * @param addr public IP address of the other peer
167  * @param addrlen actual lenght of the address
168  */
169 static void
170 reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
171 {
172   struct GNUNET_NAT_Test *h = cls;
173   const struct sockaddr_in *sa;
174
175   if (addrlen != sizeof (struct sockaddr_in))
176     return;
177   sa = (const struct sockaddr_in *) addr;
178   if (h->data != sa->sin_port)
179   {
180 #if DEBUG_NAT
181     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
182                      "Received connection reversal request for wrong port\n");
183 #endif
184     return;                     /* wrong port */
185   }
186   /* report success */
187   h->report (h->report_cls, GNUNET_OK);
188 }
189
190
191 /**
192  * Activity on our incoming socket.  Read data from the
193  * incoming connection.
194  *
195  * @param cls the 'struct NatActivity'
196  * @param tc scheduler context
197  */
198 static void
199 do_udp_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 {
201   struct GNUNET_NAT_Test *tst = cls;
202   uint16_t data;
203
204   tst->ltask =
205       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
206                                      &do_udp_read, tst);
207   if ((NULL != tc->write_ready) &&
208       (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
209       (sizeof (data) ==
210        GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
211   {
212     if (data == tst->data)
213       tst->report (tst->report_cls, GNUNET_OK);
214 #if DEBUG_NAT
215     else
216       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
217                        "Received data mismatches expected value\n");
218 #endif
219   }
220 #if DEBUG_NAT
221   else
222     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
223                      "Failed to receive data from inbound connection\n");
224 #endif
225 }
226
227
228 /**
229  * Activity on our incoming socket.  Read data from the
230  * incoming connection.
231  *
232  * @param cls the 'struct NatActivity'
233  * @param tc scheduler context
234  */
235 static void
236 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   struct NatActivity *na = cls;
239   struct GNUNET_NAT_Test *tst;
240   uint16_t data;
241
242   na->rtask = GNUNET_SCHEDULER_NO_TASK;
243   tst = na->h;
244   GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
245   if ((NULL != tc->write_ready) &&
246       (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
247       (sizeof (data) ==
248        GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
249   {
250     if (data == tst->data)
251       tst->report (tst->report_cls, GNUNET_OK);
252 #if DEBUG_NAT
253     else
254       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
255                        "nat" "Received data mismatches expected value\n");
256 #endif
257   }
258 #if DEBUG_NAT
259   else
260     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
261                      "Failed to receive data from inbound connection\n");
262 #endif
263   GNUNET_NETWORK_socket_close (na->sock);
264   GNUNET_free (na);
265 }
266
267
268 /**
269  * Activity on our listen socket. Accept the
270  * incoming connection.
271  *
272  * @param cls the 'struct GNUNET_NAT_Test'
273  * @param tc scheduler context
274  */
275 static void
276 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
277 {
278   struct GNUNET_NAT_Test *tst = cls;
279   struct GNUNET_NETWORK_Handle *s;
280   struct NatActivity *wl;
281
282   tst->ltask = GNUNET_SCHEDULER_NO_TASK;
283   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
284     return;
285   tst->ltask =
286       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
287                                      &do_accept, tst);
288   s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
289   if (NULL == s)
290   {
291     GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
292     return;                     /* odd error */
293   }
294 #if DEBUG_NAT
295   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
296                    "Got an inbound connection, waiting for data\n");
297 #endif
298   wl = GNUNET_malloc (sizeof (struct NatActivity));
299   wl->sock = s;
300   wl->h = tst;
301   wl->rtask =
302       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, wl->sock,
303                                      &do_read, wl);
304   GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
305 }
306
307
308 /**
309  * Address-callback, used to send message to gnunet-nat-server.
310  *
311  * @param cls closure
312  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
313  *     the previous (now invalid) one
314  * @param addr either the previous or the new public IP address
315  * @param addrlen actual lenght of the address
316  */
317 static void
318 addr_cb (void *cls, int add_remove, const struct sockaddr *addr,
319          socklen_t addrlen)
320 {
321   struct GNUNET_NAT_Test *h = cls;
322   struct ClientActivity *ca;
323   struct GNUNET_CLIENT_Connection *client;
324   struct GNUNET_NAT_TestMessage msg;
325   const struct sockaddr_in *sa;
326
327   if (GNUNET_YES != add_remove)
328     return;
329   if (addrlen != sizeof (struct sockaddr_in))
330     return;                     /* ignore IPv6 here */
331 #if DEBUG_NAT
332   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
333                    "Asking gnunet-nat-server to connect to `%s'\n",
334                    GNUNET_a2s (addr, addrlen));
335 #endif
336   sa = (const struct sockaddr_in *) addr;
337   msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
338   msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
339   msg.dst_ipv4 = sa->sin_addr.s_addr;
340   msg.dport = sa->sin_port;
341   msg.data = h->data;
342   msg.is_tcp = htonl ((uint32_t) h->is_tcp);
343
344   client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg);
345   if (NULL == client)
346   {
347     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348                 _("Failed to connect to `gnunet-nat-server'\n"));
349     return;
350   }
351   ca = GNUNET_malloc (sizeof (struct ClientActivity));
352   ca->client = client;
353   GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
354   GNUNET_break (GNUNET_OK ==
355                 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
356                                                          GNUNET_TIME_UNIT_SECONDS,
357                                                          GNUNET_YES, NULL,
358                                                          NULL));
359 }
360
361
362 /**
363  * Start testing if NAT traversal works using the
364  * given configuration (IPv4-only).
365  *
366  * @param cfg configuration for the NAT traversal
367  * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
368  * @param bnd_port port to bind to, 0 for connection reversal
369  * @param adv_port externally advertised port to use
370  * @param report function to call with the result of the test
371  * @param report_cls closure for report
372  * @return handle to cancel NAT test
373  */
374 struct GNUNET_NAT_Test *
375 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
376                        int is_tcp, uint16_t bnd_port, uint16_t adv_port,
377                        GNUNET_NAT_TestCallback report, void *report_cls)
378 {
379   struct GNUNET_NAT_Test *ret;
380   struct sockaddr_in sa;
381   const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
382   const socklen_t addrlens[] = { sizeof (sa) };
383
384   memset (&sa, 0, sizeof (sa));
385   sa.sin_family = AF_INET;
386   sa.sin_port = htons (bnd_port);
387 #if HAVE_SOCKADDR_IN_SIN_LEN
388   sa.sin_len = sizeof (sa);
389 #endif
390
391   ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
392   ret->cfg = cfg;
393   ret->is_tcp = is_tcp;
394   ret->data = bnd_port;
395   ret->adv_port = adv_port;
396   ret->report = report;
397   ret->report_cls = report_cls;
398   if (bnd_port == 0)
399   {
400     ret->nat =
401         GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
402                              &reversal_cb, ret);
403   }
404   else
405   {
406     ret->lsock =
407         GNUNET_NETWORK_socket_create (AF_INET,
408                                       (is_tcp ==
409                                        GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
410                                       0);
411     if ((ret->lsock == NULL) ||
412         (GNUNET_OK !=
413          GNUNET_NETWORK_socket_bind (ret->lsock, (const struct sockaddr *) &sa,
414                                      sizeof (sa))))
415     {
416       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417                   _
418                   ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
419                   GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
420                   STRERROR (errno));
421       if (NULL != ret->lsock)
422         GNUNET_NETWORK_socket_close (ret->lsock);
423       GNUNET_free (ret);
424       return NULL;
425     }
426     if (GNUNET_YES == is_tcp)
427     {
428       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
429       ret->ltask =
430           GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
431                                          ret->lsock, &do_accept, ret);
432     }
433     else
434     {
435       ret->ltask =
436           GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
437                                          ret->lsock, &do_udp_read, ret);
438     }
439     ret->nat =
440         GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
441                              &addr_cb, NULL, ret);
442   }
443   return ret;
444 }
445
446
447 /**
448  * Stop an active NAT test.
449  *
450  * @param tst test to stop.
451  */
452 void
453 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
454 {
455   struct NatActivity *pos;
456   struct ClientActivity *cpos;
457
458   while (NULL != (cpos = tst->ca_head))
459   {
460     GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
461     GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
462     GNUNET_free (cpos);
463   }
464   while (NULL != (pos = tst->na_head))
465   {
466     GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
467     GNUNET_SCHEDULER_cancel (pos->rtask);
468     GNUNET_NETWORK_socket_close (pos->sock);
469     GNUNET_free (pos);
470   }
471   if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
472     GNUNET_SCHEDULER_cancel (tst->ltask);
473   if (NULL != tst->lsock)
474     GNUNET_NETWORK_socket_close (tst->lsock);
475   GNUNET_NAT_unregister (tst->nat);
476   GNUNET_free (tst);
477 }
478
479 /* end of nat_test.c */