dbg support:
[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",
256                        "Received data mismatches expected value\n");
257 #endif
258   }
259 #if DEBUG_NAT
260   else
261     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
262                      "Failed to receive data from inbound connection\n");
263 #endif
264   GNUNET_NETWORK_socket_close (na->sock);
265   GNUNET_free (na);
266 }
267
268
269 /**
270  * Activity on our listen socket. Accept the
271  * incoming connection.
272  *
273  * @param cls the 'struct GNUNET_NAT_Test'
274  * @param tc scheduler context
275  */
276 static void
277 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 {
279   struct GNUNET_NAT_Test *tst = cls;
280   struct GNUNET_NETWORK_Handle *s;
281   struct NatActivity *wl;
282
283   tst->ltask = GNUNET_SCHEDULER_NO_TASK;
284   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
285     return;
286   tst->ltask =
287       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
288                                      &do_accept, tst);
289   s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
290   if (NULL == s)
291   {
292     GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
293     return;                     /* odd error */
294   }
295 #if DEBUG_NAT
296   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
297                    "Got an inbound connection, waiting for data\n");
298 #endif
299   wl = GNUNET_malloc (sizeof (struct NatActivity));
300   wl->sock = s;
301   wl->h = tst;
302   wl->rtask =
303       GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, wl->sock,
304                                      &do_read, wl);
305   GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
306 }
307
308
309 /**
310  * Address-callback, used to send message to gnunet-nat-server.
311  *
312  * @param cls closure
313  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
314  *     the previous (now invalid) one
315  * @param addr either the previous or the new public IP address
316  * @param addrlen actual lenght of the address
317  */
318 static void
319 addr_cb (void *cls, int add_remove, const struct sockaddr *addr,
320          socklen_t addrlen)
321 {
322   struct GNUNET_NAT_Test *h = cls;
323   struct ClientActivity *ca;
324   struct GNUNET_CLIENT_Connection *client;
325   struct GNUNET_NAT_TestMessage msg;
326   const struct sockaddr_in *sa;
327
328   if (GNUNET_YES != add_remove)
329     return;
330   if (addrlen != sizeof (struct sockaddr_in))
331     return;                     /* ignore IPv6 here */
332 #if DEBUG_NAT
333   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
334                    "Asking gnunet-nat-server to connect to `%s'\n",
335                    GNUNET_a2s (addr, addrlen));
336 #endif
337   sa = (const struct sockaddr_in *) addr;
338   msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
339   msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
340   msg.dst_ipv4 = sa->sin_addr.s_addr;
341   msg.dport = sa->sin_port;
342   msg.data = h->data;
343   msg.is_tcp = htonl ((uint32_t) h->is_tcp);
344
345   client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg);
346   if (NULL == client)
347   {
348     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349                 _("Failed to connect to `gnunet-nat-server'\n"));
350     return;
351   }
352   ca = GNUNET_malloc (sizeof (struct ClientActivity));
353   ca->client = client;
354   GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
355   GNUNET_break (GNUNET_OK ==
356                 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
357                                                          GNUNET_TIME_UNIT_SECONDS,
358                                                          GNUNET_YES, NULL,
359                                                          NULL));
360 }
361
362
363 /**
364  * Start testing if NAT traversal works using the
365  * given configuration (IPv4-only).
366  *
367  * @param cfg configuration for the NAT traversal
368  * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
369  * @param bnd_port port to bind to, 0 for connection reversal
370  * @param adv_port externally advertised port to use
371  * @param report function to call with the result of the test
372  * @param report_cls closure for report
373  * @return handle to cancel NAT test
374  */
375 struct GNUNET_NAT_Test *
376 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
377                        int is_tcp, uint16_t bnd_port, uint16_t adv_port,
378                        GNUNET_NAT_TestCallback report, void *report_cls)
379 {
380   struct GNUNET_NAT_Test *ret;
381   struct sockaddr_in sa;
382   const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
383   const socklen_t addrlens[] = { sizeof (sa) };
384
385   memset (&sa, 0, sizeof (sa));
386   sa.sin_family = AF_INET;
387   sa.sin_port = htons (bnd_port);
388 #if HAVE_SOCKADDR_IN_SIN_LEN
389   sa.sin_len = sizeof (sa);
390 #endif
391
392   ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
393   ret->cfg = cfg;
394   ret->is_tcp = is_tcp;
395   ret->data = bnd_port;
396   ret->adv_port = adv_port;
397   ret->report = report;
398   ret->report_cls = report_cls;
399   if (bnd_port == 0)
400   {
401     ret->nat =
402         GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
403                              &reversal_cb, ret);
404   }
405   else
406   {
407     ret->lsock =
408         GNUNET_NETWORK_socket_create (AF_INET,
409                                       (is_tcp ==
410                                        GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
411                                       0);
412     if ((ret->lsock == NULL) ||
413         (GNUNET_OK !=
414          GNUNET_NETWORK_socket_bind (ret->lsock, (const struct sockaddr *) &sa,
415                                      sizeof (sa))))
416     {
417       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418                   _
419                   ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
420                   GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
421                   STRERROR (errno));
422       if (NULL != ret->lsock)
423         GNUNET_NETWORK_socket_close (ret->lsock);
424       GNUNET_free (ret);
425       return NULL;
426     }
427     if (GNUNET_YES == is_tcp)
428     {
429       GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
430       ret->ltask =
431           GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
432                                          ret->lsock, &do_accept, ret);
433     }
434     else
435     {
436       ret->ltask =
437           GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
438                                          ret->lsock, &do_udp_read, ret);
439     }
440     ret->nat =
441         GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
442                              &addr_cb, NULL, ret);
443   }
444   return ret;
445 }
446
447
448 /**
449  * Stop an active NAT test.
450  *
451  * @param tst test to stop.
452  */
453 void
454 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
455 {
456   struct NatActivity *pos;
457   struct ClientActivity *cpos;
458
459   while (NULL != (cpos = tst->ca_head))
460   {
461     GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
462     GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
463     GNUNET_free (cpos);
464   }
465   while (NULL != (pos = tst->na_head))
466   {
467     GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
468     GNUNET_SCHEDULER_cancel (pos->rtask);
469     GNUNET_NETWORK_socket_close (pos->sock);
470     GNUNET_free (pos);
471   }
472   if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
473     GNUNET_SCHEDULER_cancel (tst->ltask);
474   if (NULL != tst->lsock)
475     GNUNET_NETWORK_socket_close (tst->lsock);
476   GNUNET_NAT_unregister (tst->nat);
477   GNUNET_free (tst);
478 }
479
480 /* end of nat_test.c */