2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
33 #define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
36 * Entry we keep for each incoming connection.
41 * This is a doubly-linked list.
43 struct NatActivity *next;
46 * This is a doubly-linked list.
48 struct NatActivity *prev;
51 * Socket of the incoming connection.
53 struct GNUNET_NETWORK_Handle *sock;
56 * Handle of the master context.
58 struct GNUNET_NAT_Test *h;
61 * Task reading from the incoming connection.
63 GNUNET_SCHEDULER_TaskIdentifier rtask;
68 * Entry we keep for each connection to the gnunet-nat-service.
73 * This is a doubly-linked list.
75 struct ClientActivity *next;
78 * This is a doubly-linked list.
80 struct ClientActivity *prev;
83 * Socket of the incoming connection.
85 struct GNUNET_CLIENT_Connection *client;
91 * Handle to a NAT test.
93 struct GNUNET_NAT_Test
99 const struct GNUNET_CONFIGURATION_Handle *cfg;
102 * Function to call with success report
104 GNUNET_NAT_TestCallback report;
107 * Closure for @e report.
112 * Handle to NAT traversal in use
114 struct GNUNET_NAT_Handle *nat;
117 * Handle to listen socket, or NULL
119 struct GNUNET_NETWORK_Handle *lsock;
122 * Head of list of nat activities.
124 struct NatActivity *na_head;
127 * Tail of list of nat activities.
129 struct NatActivity *na_tail;
132 * Head of list of client activities.
134 struct ClientActivity *ca_head;
137 * Tail of list of client activities.
139 struct ClientActivity *ca_tail;
142 * Identity of task for the listen socket (if any)
144 GNUNET_SCHEDULER_TaskIdentifier ltask;
147 * Task identifier for the timeout (if any)
149 GNUNET_SCHEDULER_TaskIdentifier ttask;
152 * GNUNET_YES if we're testing TCP
157 * Data that should be transmitted or source-port.
162 * Advertised port to the other peer.
167 * Status code to be reported to the timeout/status call
169 enum GNUNET_NAT_StatusCode status;
174 * Function called from #GNUNET_NAT_register whenever someone asks us
175 * to do connection reversal.
177 * @param cls closure, our `struct GNUNET_NAT_Handle`
178 * @param addr public IP address of the other peer
179 * @param addrlen actual lenght of the address
182 reversal_cb (void *cls,
183 const struct sockaddr *addr,
186 struct GNUNET_NAT_Test *h = cls;
187 const struct sockaddr_in *sa;
189 if (sizeof (struct sockaddr_in) != addrlen)
191 sa = (const struct sockaddr_in *) addr;
192 if (h->data != sa->sin_port)
194 LOG (GNUNET_ERROR_TYPE_DEBUG,
195 "Received connection reversal request for wrong port\n");
196 return; /* wrong port */
199 h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
204 * Activity on our incoming socket. Read data from the
205 * incoming connection.
207 * @param cls the `struct GNUNET_NAT_Test`
208 * @param tc scheduler context
211 do_udp_read (void *cls,
212 const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct GNUNET_NAT_Test *tst = cls;
218 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
221 if ((NULL != tc->write_ready) &&
222 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
224 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
226 if (data == tst->data)
227 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
229 LOG (GNUNET_ERROR_TYPE_DEBUG,
230 "Received data mismatches expected value\n");
233 LOG (GNUNET_ERROR_TYPE_DEBUG,
234 "Failed to receive data from inbound connection\n");
239 * Activity on our incoming socket. Read data from the
240 * incoming connection.
242 * @param cls the `struct NatActivity`
243 * @param tc scheduler context
247 const struct GNUNET_SCHEDULER_TaskContext *tc)
249 struct NatActivity *na = cls;
250 struct GNUNET_NAT_Test *tst;
253 na->rtask = GNUNET_SCHEDULER_NO_TASK;
255 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
256 if ((NULL != tc->write_ready) &&
257 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
259 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
261 if (data == tst->data)
262 tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
264 LOG (GNUNET_ERROR_TYPE_DEBUG,
265 "Received data does not match expected value\n");
268 LOG (GNUNET_ERROR_TYPE_DEBUG,
269 "Failed to receive data from inbound connection\n");
270 GNUNET_NETWORK_socket_close (na->sock);
276 * Activity on our listen socket. Accept the
277 * incoming connection.
279 * @param cls the `struct GNUNET_NAT_Test`
280 * @param tc scheduler context
283 do_accept (void *cls,
284 const struct GNUNET_SCHEDULER_TaskContext *tc)
286 struct GNUNET_NAT_Test *tst = cls;
287 struct GNUNET_NETWORK_Handle *s;
288 struct NatActivity *wl;
290 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
291 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
294 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
296 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
299 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
300 return; /* odd error */
302 LOG (GNUNET_ERROR_TYPE_DEBUG,
303 "Got an inbound connection, waiting for data\n");
304 wl = GNUNET_new (struct NatActivity);
308 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
311 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
316 * Address-callback, used to send message to gnunet-nat-server.
319 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
320 * the previous (now invalid) one
321 * @param addr either the previous or the new public IP address
322 * @param addrlen actual length of the @a addr
327 const struct sockaddr *addr,
330 struct GNUNET_NAT_Test *h = cls;
331 struct ClientActivity *ca;
332 struct GNUNET_CLIENT_Connection *client;
333 struct GNUNET_NAT_TestMessage msg;
334 const struct sockaddr_in *sa;
336 if (GNUNET_YES != add_remove)
338 if (addrlen != sizeof (struct sockaddr_in))
340 LOG (GNUNET_ERROR_TYPE_DEBUG,
341 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
342 GNUNET_a2s (addr, addrlen));
343 return; /* ignore IPv6 here */
345 LOG (GNUNET_ERROR_TYPE_INFO,
346 "Asking gnunet-nat-server to connect to `%s'\n",
347 GNUNET_a2s (addr, addrlen));
348 sa = (const struct sockaddr_in *) addr;
349 msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
350 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
351 msg.dst_ipv4 = sa->sin_addr.s_addr;
352 msg.dport = sa->sin_port;
354 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
356 client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg);
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Failed to connect to `gnunet-nat-server'\n"));
363 ca = GNUNET_new (struct ClientActivity);
365 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
366 GNUNET_break (GNUNET_OK ==
367 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
375 * Timeout task for a nat test.
376 * Calls the report-callback with a timeout return value
378 * Destroys the nat handle after the callback has been processed.
380 * @param cls handle to the timed out NAT test
384 do_timeout (void *cls,
385 const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct GNUNET_NAT_Test *nh = cls;
389 nh->ttask = GNUNET_SCHEDULER_NO_TASK;
390 nh->report (nh->report_cls,
391 (GNUNET_NAT_ERROR_SUCCESS == nh->status)
392 ? GNUNET_NAT_ERROR_TIMEOUT
398 * Start testing if NAT traversal works using the
399 * given configuration (IPv4-only).
401 * ALL failures are reported directly to the report callback
403 * @param cfg configuration for the NAT traversal
404 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
405 * @param bnd_port port to bind to, 0 for connection reversal
406 * @param adv_port externally advertised port to use
407 * @param timeout delay after which the test should be aborted
408 * @param report function to call with the result of the test
409 * @param report_cls closure for @a report
410 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
412 struct GNUNET_NAT_Test *
413 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
417 struct GNUNET_TIME_Relative timeout,
418 GNUNET_NAT_TestCallback report,
421 struct GNUNET_NAT_Test *nh;
422 struct sockaddr_in sa;
423 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
424 const socklen_t addrlens[] = { sizeof (sa) };
426 memset (&sa, 0, sizeof (sa));
427 sa.sin_family = AF_INET;
428 sa.sin_port = htons (bnd_port);
429 #if HAVE_SOCKADDR_IN_SIN_LEN
430 sa.sin_len = sizeof (sa);
433 nh = GNUNET_new (struct GNUNET_NAT_Test);
437 nh->adv_port = adv_port;
439 nh->report_cls = report_cls;
440 nh->status = GNUNET_NAT_ERROR_SUCCESS;
444 = GNUNET_NAT_register (cfg, is_tcp, 0, 0,
452 GNUNET_NETWORK_socket_create (AF_INET,
454 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
456 if ((nh->lsock == NULL) ||
458 GNUNET_NETWORK_socket_bind (nh->lsock, (const struct sockaddr *) &sa,
461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
462 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
463 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
465 if (NULL != nh->lsock)
467 GNUNET_NETWORK_socket_close (nh->lsock);
470 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
471 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
474 if (GNUNET_YES == is_tcp)
476 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
478 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
479 nh->lsock, &do_accept, nh);
484 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
485 nh->lsock, &do_udp_read, nh);
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "NAT test listens on port %u (%s)\n",
490 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
491 nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1,
496 LOG (GNUNET_ERROR_TYPE_ERROR,
497 _("NAT test failed to start NAT library\n"));
498 if (GNUNET_SCHEDULER_NO_TASK != nh->ltask)
500 GNUNET_SCHEDULER_cancel (nh->ltask);
501 nh->ltask = GNUNET_SCHEDULER_NO_TASK;
503 if (NULL != nh->lsock)
505 GNUNET_NETWORK_socket_close (nh->lsock);
508 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
509 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
513 nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
519 * Stop an active NAT test.
521 * @param tst test to stop.
524 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
526 struct NatActivity *pos;
527 struct ClientActivity *cpos;
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Stopping NAT test\n");
531 while (NULL != (cpos = tst->ca_head))
533 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
534 GNUNET_CLIENT_disconnect (cpos->client);
537 while (NULL != (pos = tst->na_head))
539 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
540 GNUNET_SCHEDULER_cancel (pos->rtask);
541 GNUNET_NETWORK_socket_close (pos->sock);
544 if (GNUNET_SCHEDULER_NO_TASK != tst->ttask)
545 GNUNET_SCHEDULER_cancel (tst->ttask);
546 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
547 GNUNET_SCHEDULER_cancel (tst->ltask);
548 if (NULL != tst->lsock)
549 GNUNET_NETWORK_socket_close (tst->lsock);
550 if (NULL != tst->nat)
551 GNUNET_NAT_unregister (tst->nat);
555 /* end of nat_test.c */