2 This file is part of GNUnet.
3 Copyright (C) 2011, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file nat/nat_api_test.c
22 * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server)
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_nat_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
32 #define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
35 * Entry we keep for each incoming connection.
40 * This is a doubly-linked list.
42 struct NatActivity *next;
45 * This is a doubly-linked list.
47 struct NatActivity *prev;
50 * Socket of the incoming connection.
52 struct GNUNET_NETWORK_Handle *sock;
55 * Handle of the master context.
57 struct GNUNET_NAT_Test *h;
60 * Task reading from the incoming connection.
62 struct GNUNET_SCHEDULER_Task *rtask;
67 * Entry we keep for each connection to the gnunet-nat-service.
72 * This is a doubly-linked list.
74 struct ClientActivity *next;
77 * This is a doubly-linked list.
79 struct ClientActivity *prev;
82 * Socket of the incoming connection.
84 struct GNUNET_MQ_Handle *mq;
87 * Handle to overall NAT test.
89 struct GNUNET_NAT_Test *h;
95 * Handle to a NAT test.
97 struct GNUNET_NAT_Test
103 const struct GNUNET_CONFIGURATION_Handle *cfg;
106 * Function to call with success report
108 GNUNET_NAT_TestCallback report;
111 * Closure for @e report.
116 * Handle to NAT traversal in use
118 struct GNUNET_NAT_Handle *nat;
121 * Handle to listen socket, or NULL
123 struct GNUNET_NETWORK_Handle *lsock;
126 * Head of list of nat activities.
128 struct NatActivity *na_head;
131 * Tail of list of nat activities.
133 struct NatActivity *na_tail;
136 * Head of list of client activities.
138 struct ClientActivity *ca_head;
141 * Tail of list of client activities.
143 struct ClientActivity *ca_tail;
146 * Identity of task for the listen socket (if any)
148 struct GNUNET_SCHEDULER_Task *ltask;
151 * Task identifier for the timeout (if any)
153 struct GNUNET_SCHEDULER_Task *ttask;
156 * #GNUNET_YES if we're testing TCP
161 * Data that should be transmitted or source-port.
166 * Advertised port to the other peer.
171 * Status code to be reported to the timeout/status call
173 enum GNUNET_NAT_StatusCode status;
178 * Function called from #GNUNET_NAT_register whenever someone asks us
179 * to do connection reversal.
181 * @param cls closure, our `struct GNUNET_NAT_Handle`
182 * @param addr public IP address of the other peer
183 * @param addrlen actual lenght of the @a addr
186 reversal_cb (void *cls,
187 const struct sockaddr *addr,
190 struct GNUNET_NAT_Test *h = cls;
191 const struct sockaddr_in *sa;
193 if (sizeof (struct sockaddr_in) != addrlen)
195 sa = (const struct sockaddr_in *) addr;
196 if (h->data != sa->sin_port)
198 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "Received connection reversal request for wrong port\n");
200 return; /* wrong port */
203 h->report (h->report_cls,
204 GNUNET_NAT_ERROR_SUCCESS);
209 * Activity on our incoming socket. Read data from the
210 * incoming connection.
212 * @param cls the `struct GNUNET_NAT_Test`
215 do_udp_read (void *cls)
217 struct GNUNET_NAT_Test *tst = cls;
219 const struct GNUNET_SCHEDULER_TaskContext *tc;
221 tc = GNUNET_SCHEDULER_get_task_context ();
223 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
227 if ((NULL != tc->write_ready) &&
228 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
231 GNUNET_NETWORK_socket_recv (tst->lsock,
235 if (data == tst->data)
236 tst->report (tst->report_cls,
237 GNUNET_NAT_ERROR_SUCCESS);
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Received data mismatches expected value\n");
243 LOG (GNUNET_ERROR_TYPE_DEBUG,
244 "Failed to receive data from inbound connection\n");
249 * Activity on our incoming socket. Read data from the
250 * incoming connection.
252 * @param cls the `struct NatActivity`
257 struct NatActivity *na = cls;
258 struct GNUNET_NAT_Test *tst;
260 const struct GNUNET_SCHEDULER_TaskContext *tc;
262 tc = GNUNET_SCHEDULER_get_task_context ();
265 GNUNET_CONTAINER_DLL_remove (tst->na_head,
268 if ((NULL != tc->write_ready) &&
269 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
272 GNUNET_NETWORK_socket_recv (na->sock,
276 if (data == tst->data)
277 tst->report (tst->report_cls,
278 GNUNET_NAT_ERROR_SUCCESS);
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Received data does not match expected value\n");
284 LOG (GNUNET_ERROR_TYPE_DEBUG,
285 "Failed to receive data from inbound connection\n");
286 GNUNET_NETWORK_socket_close (na->sock);
292 * Activity on our listen socket. Accept the
293 * incoming connection.
295 * @param cls the `struct GNUNET_NAT_Test`
298 do_accept (void *cls)
300 struct GNUNET_NAT_Test *tst = cls;
301 struct GNUNET_NETWORK_Handle *s;
302 struct NatActivity *wl;
304 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
308 s = GNUNET_NETWORK_socket_accept (tst->lsock,
313 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
315 return; /* odd error */
317 LOG (GNUNET_ERROR_TYPE_DEBUG,
318 "Got an inbound connection, waiting for data\n");
319 wl = GNUNET_new (struct NatActivity);
323 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
327 GNUNET_CONTAINER_DLL_insert (tst->na_head,
334 * We got disconnected from the NAT server. Stop
335 * waiting for a reply.
337 * @param cls the `struct ClientActivity`
338 * @param error error code
341 mq_error_handler (void *cls,
342 enum GNUNET_MQ_Error error)
344 struct ClientActivity *ca = cls;
345 struct GNUNET_NAT_Test *tst = ca->h;
347 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
350 GNUNET_MQ_destroy (ca->mq);
356 * Address-callback, used to send message to gnunet-nat-server.
359 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
360 * the previous (now invalid) one
361 * @param addr either the previous or the new public IP address
362 * @param addrlen actual length of the @a addr
367 const struct sockaddr *addr,
370 struct GNUNET_NAT_Test *h = cls;
371 struct ClientActivity *ca;
372 struct GNUNET_MQ_Envelope *env;
373 struct GNUNET_NAT_TestMessage *msg;
374 const struct sockaddr_in *sa;
376 if (GNUNET_YES != add_remove)
378 if (addrlen != sizeof (struct sockaddr_in))
380 LOG (GNUNET_ERROR_TYPE_DEBUG,
381 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
384 return; /* ignore IPv6 here */
386 LOG (GNUNET_ERROR_TYPE_INFO,
387 "Asking gnunet-nat-server to connect to `%s'\n",
391 ca = GNUNET_new (struct ClientActivity);
393 ca->mq = GNUNET_CLIENT_connecT (h->cfg,
401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
402 _("Failed to connect to `gnunet-nat-server'\n"));
405 GNUNET_CONTAINER_DLL_insert (h->ca_head,
408 sa = (const struct sockaddr_in *) addr;
409 env = GNUNET_MQ_msg (msg,
410 GNUNET_MESSAGE_TYPE_NAT_TEST);
411 msg->dst_ipv4 = sa->sin_addr.s_addr;
412 msg->dport = sa->sin_port;
414 msg->is_tcp = htonl ((uint32_t) h->is_tcp);
415 GNUNET_MQ_send (ca->mq,
421 * Timeout task for a nat test.
422 * Calls the report-callback with a timeout return value
424 * Destroys the nat handle after the callback has been processed.
426 * @param cls handle to the timed out NAT test
429 do_timeout (void *cls)
431 struct GNUNET_NAT_Test *nh = cls;
434 nh->report (nh->report_cls,
435 (GNUNET_NAT_ERROR_SUCCESS == nh->status)
436 ? GNUNET_NAT_ERROR_TIMEOUT
442 * Start testing if NAT traversal works using the
443 * given configuration (IPv4-only).
445 * ALL failures are reported directly to the report callback
447 * @param cfg configuration for the NAT traversal
448 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
449 * @param bnd_port port to bind to, 0 for connection reversal
450 * @param adv_port externally advertised port to use
451 * @param timeout delay after which the test should be aborted
452 * @param report function to call with the result of the test
453 * @param report_cls closure for @a report
454 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
456 struct GNUNET_NAT_Test *
457 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
461 struct GNUNET_TIME_Relative timeout,
462 GNUNET_NAT_TestCallback report,
465 struct GNUNET_NAT_Test *nh;
466 struct sockaddr_in sa;
467 const struct sockaddr *addrs[] = {
468 (const struct sockaddr *) &sa
470 const socklen_t addrlens[] = {
474 memset (&sa, 0, sizeof (sa));
475 sa.sin_family = AF_INET;
476 sa.sin_port = htons (bnd_port);
477 #if HAVE_SOCKADDR_IN_SIN_LEN
478 sa.sin_len = sizeof (sa);
481 nh = GNUNET_new (struct GNUNET_NAT_Test);
485 nh->adv_port = adv_port;
487 nh->report_cls = report_cls;
488 nh->status = GNUNET_NAT_ERROR_SUCCESS;
492 = GNUNET_NAT_register (cfg,
506 GNUNET_NETWORK_socket_create (AF_INET,
508 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
510 if ((nh->lsock == NULL) ||
512 GNUNET_NETWORK_socket_bind (nh->lsock,
513 (const struct sockaddr *) &sa,
516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
517 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
518 GNUNET_a2s ((const struct sockaddr *) &sa,
521 if (NULL != nh->lsock)
523 GNUNET_NETWORK_socket_close (nh->lsock);
526 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
527 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout,
531 if (GNUNET_YES == is_tcp)
533 GNUNET_break (GNUNET_OK ==
534 GNUNET_NETWORK_socket_listen (nh->lsock,
537 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
545 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
550 LOG (GNUNET_ERROR_TYPE_INFO,
551 "NAT test listens on port %u (%s)\n",
553 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
554 nh->nat = GNUNET_NAT_register (cfg,
566 LOG (GNUNET_ERROR_TYPE_INFO,
567 _("NAT test failed to start NAT library\n"));
568 if (NULL != nh->ltask)
570 GNUNET_SCHEDULER_cancel (nh->ltask);
573 if (NULL != nh->lsock)
575 GNUNET_NETWORK_socket_close (nh->lsock);
578 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
579 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout,
584 nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout,
592 * Stop an active NAT test.
594 * @param tst test to stop.
597 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
599 struct NatActivity *pos;
600 struct ClientActivity *cpos;
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Stopping NAT test\n");
604 while (NULL != (cpos = tst->ca_head))
606 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
609 GNUNET_MQ_destroy (cpos->mq);
612 while (NULL != (pos = tst->na_head))
614 GNUNET_CONTAINER_DLL_remove (tst->na_head,
617 GNUNET_SCHEDULER_cancel (pos->rtask);
618 GNUNET_NETWORK_socket_close (pos->sock);
621 if (NULL != tst->ttask)
623 GNUNET_SCHEDULER_cancel (tst->ttask);
626 if (NULL != tst->ltask)
628 GNUNET_SCHEDULER_cancel (tst->ltask);
631 if (NULL != tst->lsock)
633 GNUNET_NETWORK_socket_close (tst->lsock);
636 if (NULL != tst->nat)
638 GNUNET_NAT_unregister (tst->nat);
644 /* end of nat_test.c */