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
394 GNUNET_NAT_test_stop (nh);
399 * Start testing if NAT traversal works using the
400 * given configuration (IPv4-only).
402 * ALL failures are reported directly to the report callback
404 * @param cfg configuration for the NAT traversal
405 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
406 * @param bnd_port port to bind to, 0 for connection reversal
407 * @param adv_port externally advertised port to use
408 * @param timeout delay after which the test should be aborted
409 * @param report function to call with the result of the test
410 * @param report_cls closure for @a report
411 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
413 struct GNUNET_NAT_Test *
414 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
418 struct GNUNET_TIME_Relative timeout,
419 GNUNET_NAT_TestCallback report,
422 struct GNUNET_NAT_Test *nh;
423 struct sockaddr_in sa;
424 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
425 const socklen_t addrlens[] = { sizeof (sa) };
427 memset (&sa, 0, sizeof (sa));
428 sa.sin_family = AF_INET;
429 sa.sin_port = htons (bnd_port);
430 #if HAVE_SOCKADDR_IN_SIN_LEN
431 sa.sin_len = sizeof (sa);
434 nh = GNUNET_new (struct GNUNET_NAT_Test);
438 nh->adv_port = adv_port;
440 nh->report_cls = report_cls;
441 nh->ttask = GNUNET_SCHEDULER_NO_TASK;
442 nh->status = GNUNET_NAT_ERROR_SUCCESS;
446 GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
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)
466 GNUNET_NETWORK_socket_close (nh->lsock);
467 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
468 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
471 if (GNUNET_YES == is_tcp)
473 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
475 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
476 nh->lsock, &do_accept, nh);
481 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
482 nh->lsock, &do_udp_read, nh);
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "NAT test listens on port %u (%s)\n",
487 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
488 nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1,
493 LOG (GNUNET_ERROR_TYPE_ERROR,
494 _("NAT test failed to start NAT library\n"));
495 if (GNUNET_SCHEDULER_NO_TASK != nh->ltask)
497 GNUNET_SCHEDULER_cancel (nh->ltask);
498 nh->ltask = GNUNET_SCHEDULER_NO_TASK;
500 if (NULL != nh->lsock)
502 GNUNET_NETWORK_socket_close (nh->lsock);
505 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
506 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, nh);
510 nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
516 * Stop an active NAT test.
518 * @param tst test to stop.
521 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
523 struct NatActivity *pos;
524 struct ClientActivity *cpos;
526 LOG (GNUNET_ERROR_TYPE_DEBUG,
527 "Stopping NAT test\n");
528 while (NULL != (cpos = tst->ca_head))
530 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
531 GNUNET_CLIENT_disconnect (cpos->client);
534 while (NULL != (pos = tst->na_head))
536 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
537 GNUNET_SCHEDULER_cancel (pos->rtask);
538 GNUNET_NETWORK_socket_close (pos->sock);
541 if (GNUNET_SCHEDULER_NO_TASK != tst->ttask)
542 GNUNET_SCHEDULER_cancel (tst->ttask);
543 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
544 GNUNET_SCHEDULER_cancel (tst->ltask);
545 if (NULL != tst->lsock)
546 GNUNET_NETWORK_socket_close (tst->lsock);
547 if (NULL != tst->nat)
548 GNUNET_NAT_unregister (tst->nat);
552 /* end of nat_test.c */