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_auto_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_service.h"
28 #include "gnunet_nat_auto_service.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "nat-auto", __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_AUTO_Test *h;
61 * Task reading from the incoming connection.
63 struct GNUNET_SCHEDULER_Task *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_MQ_Handle *mq;
88 * Handle to overall NAT test.
90 struct GNUNET_NAT_AUTO_Test *h;
96 * Handle to a NAT test.
98 struct GNUNET_NAT_AUTO_Test
104 const struct GNUNET_CONFIGURATION_Handle *cfg;
107 * Function to call with success report
109 GNUNET_NAT_TestCallback report;
112 * Closure for @e report.
117 * Handle to NAT traversal in use
119 struct GNUNET_NAT_Handle *nat;
122 * Handle to listen socket, or NULL
124 struct GNUNET_NETWORK_Handle *lsock;
127 * Head of list of nat activities.
129 struct NatActivity *na_head;
132 * Tail of list of nat activities.
134 struct NatActivity *na_tail;
137 * Head of list of client activities.
139 struct ClientActivity *ca_head;
142 * Tail of list of client activities.
144 struct ClientActivity *ca_tail;
147 * Identity of task for the listen socket (if any)
149 struct GNUNET_SCHEDULER_Task *ltask;
152 * Task identifier for the timeout (if any)
154 struct GNUNET_SCHEDULER_Task *ttask;
157 * Section name of plugin to test.
162 * IPPROTO_TCP or IPPROTO_UDP.
167 * Data that should be transmitted or source-port.
172 * Status code to be reported to the timeout/status call
174 enum GNUNET_NAT_StatusCode status;
179 * Function called from #GNUNET_NAT_register whenever someone asks us
180 * to do connection reversal.
182 * @param cls closure, our `struct GNUNET_NAT_Handle`
183 * @param addr public IP address of the other peer
184 * @param addrlen actual lenght of the @a addr
187 reversal_cb (void *cls,
188 const struct sockaddr *addr,
191 struct GNUNET_NAT_AUTO_Test *h = cls;
192 const struct sockaddr_in *sa;
194 if (sizeof (struct sockaddr_in) != addrlen)
196 sa = (const struct sockaddr_in *) addr;
197 if (h->data != sa->sin_port)
199 LOG (GNUNET_ERROR_TYPE_DEBUG,
200 "Received connection reversal request for wrong port\n");
201 return; /* wrong port */
204 h->report (h->report_cls,
205 GNUNET_NAT_ERROR_SUCCESS);
210 * Activity on our incoming socket. Read data from the
211 * incoming connection.
213 * @param cls the `struct GNUNET_NAT_AUTO_Test`
216 do_udp_read (void *cls)
218 struct GNUNET_NAT_AUTO_Test *tst = cls;
220 const struct GNUNET_SCHEDULER_TaskContext *tc;
222 tc = GNUNET_SCHEDULER_get_task_context ();
224 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
228 if ((NULL != tc->write_ready) &&
229 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
232 GNUNET_NETWORK_socket_recv (tst->lsock,
236 if (data == tst->data)
237 tst->report (tst->report_cls,
238 GNUNET_NAT_ERROR_SUCCESS);
240 LOG (GNUNET_ERROR_TYPE_DEBUG,
241 "Received data mismatches expected value\n");
244 LOG (GNUNET_ERROR_TYPE_DEBUG,
245 "Failed to receive data from inbound connection\n");
250 * Activity on our incoming socket. Read data from the
251 * incoming connection.
253 * @param cls the `struct NatActivity`
258 struct NatActivity *na = cls;
259 struct GNUNET_NAT_AUTO_Test *tst;
261 const struct GNUNET_SCHEDULER_TaskContext *tc;
263 tc = GNUNET_SCHEDULER_get_task_context ();
266 GNUNET_CONTAINER_DLL_remove (tst->na_head,
269 if ((NULL != tc->write_ready) &&
270 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
273 GNUNET_NETWORK_socket_recv (na->sock,
277 if (data == tst->data)
278 tst->report (tst->report_cls,
279 GNUNET_NAT_ERROR_SUCCESS);
281 LOG (GNUNET_ERROR_TYPE_DEBUG,
282 "Received data does not match expected value\n");
285 LOG (GNUNET_ERROR_TYPE_DEBUG,
286 "Failed to receive data from inbound connection\n");
287 GNUNET_NETWORK_socket_close (na->sock);
293 * Activity on our listen socket. Accept the
294 * incoming connection.
296 * @param cls the `struct GNUNET_NAT_AUTO_Test`
299 do_accept (void *cls)
301 struct GNUNET_NAT_AUTO_Test *tst = cls;
302 struct GNUNET_NETWORK_Handle *s;
303 struct NatActivity *wl;
305 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
309 s = GNUNET_NETWORK_socket_accept (tst->lsock,
314 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
316 return; /* odd error */
318 LOG (GNUNET_ERROR_TYPE_DEBUG,
319 "Got an inbound connection, waiting for data\n");
320 wl = GNUNET_new (struct NatActivity);
324 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
328 GNUNET_CONTAINER_DLL_insert (tst->na_head,
335 * We got disconnected from the NAT server. Stop
336 * waiting for a reply.
338 * @param cls the `struct ClientActivity`
339 * @param error error code
342 mq_error_handler (void *cls,
343 enum GNUNET_MQ_Error error)
345 struct ClientActivity *ca = cls;
346 struct GNUNET_NAT_AUTO_Test *tst = ca->h;
348 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
351 GNUNET_MQ_destroy (ca->mq);
357 * Address-callback, used to send message to gnunet-nat-server.
360 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
361 * the previous (now invalid) one
362 * @param ac address class the address belongs to
363 * @param addr either the previous or the new public IP address
364 * @param addrlen actual length of the @a addr
369 enum GNUNET_NAT_AddressClass ac,
370 const struct sockaddr *addr,
373 struct GNUNET_NAT_AUTO_Test *h = cls;
374 struct ClientActivity *ca;
375 struct GNUNET_MQ_Envelope *env;
376 struct GNUNET_NAT_AUTO_TestMessage *msg;
377 const struct sockaddr_in *sa;
379 if (GNUNET_YES != add_remove)
381 if (addrlen != sizeof (struct sockaddr_in))
383 LOG (GNUNET_ERROR_TYPE_DEBUG,
384 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
387 return; /* ignore IPv6 here */
389 LOG (GNUNET_ERROR_TYPE_INFO,
390 "Asking gnunet-nat-server to connect to `%s'\n",
394 ca = GNUNET_new (struct ClientActivity);
396 ca->mq = GNUNET_CLIENT_connect (h->cfg,
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 _("Failed to connect to `gnunet-nat-server'\n"));
408 GNUNET_CONTAINER_DLL_insert (h->ca_head,
411 sa = (const struct sockaddr_in *) addr;
412 env = GNUNET_MQ_msg (msg,
413 GNUNET_MESSAGE_TYPE_NAT_TEST);
414 msg->dst_ipv4 = sa->sin_addr.s_addr;
415 msg->dport = sa->sin_port;
417 msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP));
418 GNUNET_MQ_send (ca->mq,
424 * Calls the report-callback reporting failure.
426 * Destroys the nat handle after the callback has been processed.
428 * @param cls handle to the timed out NAT test
433 struct GNUNET_NAT_AUTO_Test *nh = cls;
436 nh->report (nh->report_cls,
442 * Start testing if NAT traversal works using the given configuration.
443 * The transport adapters should be down while using this function.
445 * @param cfg configuration for the NAT traversal
446 * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
447 * @param section_name configuration section to use for configuration
448 * @param report function to call with the result of the test
449 * @param report_cls closure for @a report
450 * @return handle to cancel NAT test
452 struct GNUNET_NAT_AUTO_Test *
453 GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
455 const char *section_name,
456 GNUNET_NAT_TestCallback report,
459 struct GNUNET_NAT_AUTO_Test *nh;
460 unsigned long long bnd_port;
461 struct sockaddr_in sa;
462 const struct sockaddr *addrs[] = {
463 (const struct sockaddr *) &sa
465 const socklen_t addrlens[] = {
470 GNUNET_CONFIGURATION_get_value_number (cfg,
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
477 _("Failed to find valid PORT in section `%s'\n"),
482 memset (&sa, 0, sizeof (sa));
483 sa.sin_family = AF_INET;
484 sa.sin_port = htons ((uint16_t) bnd_port);
485 #if HAVE_SOCKADDR_IN_SIN_LEN
486 sa.sin_len = sizeof (sa);
489 nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test);
492 nh->section_name = GNUNET_strdup (section_name);
494 nh->report_cls = report_cls;
495 nh->status = GNUNET_NAT_ERROR_SUCCESS;
499 = GNUNET_NAT_register (cfg,
510 = GNUNET_NETWORK_socket_create (AF_INET,
511 (IPPROTO_UDP == proto)
515 if ( (NULL == nh->lsock) ||
517 GNUNET_NETWORK_socket_bind (nh->lsock,
518 (const struct sockaddr *) &sa,
521 LOG (GNUNET_ERROR_TYPE_ERROR,
522 _("Failed to create socket bound to `%s' for NAT test: %s\n"),
523 GNUNET_a2s ((const struct sockaddr *) &sa,
526 if (NULL != nh->lsock)
528 GNUNET_NETWORK_socket_close (nh->lsock);
531 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
532 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
536 if (IPPROTO_TCP == proto)
538 GNUNET_break (GNUNET_OK ==
539 GNUNET_NETWORK_socket_listen (nh->lsock,
542 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
550 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
555 LOG (GNUNET_ERROR_TYPE_INFO,
556 "NAT test listens on port %llu (%s)\n",
558 (IPPROTO_TCP == proto) ? "tcp" : "udp");
559 nh->nat = GNUNET_NAT_register (cfg,
570 LOG (GNUNET_ERROR_TYPE_INFO,
571 _("NAT test failed to start NAT library\n"));
572 if (NULL != nh->ltask)
574 GNUNET_SCHEDULER_cancel (nh->ltask);
577 if (NULL != nh->lsock)
579 GNUNET_NETWORK_socket_close (nh->lsock);
582 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
583 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
593 * Stop an active NAT test.
595 * @param tst test to stop.
598 GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst)
600 struct NatActivity *pos;
601 struct ClientActivity *cpos;
603 LOG (GNUNET_ERROR_TYPE_DEBUG,
604 "Stopping NAT test\n");
605 while (NULL != (cpos = tst->ca_head))
607 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
610 GNUNET_MQ_destroy (cpos->mq);
613 while (NULL != (pos = tst->na_head))
615 GNUNET_CONTAINER_DLL_remove (tst->na_head,
618 GNUNET_SCHEDULER_cancel (pos->rtask);
619 GNUNET_NETWORK_socket_close (pos->sock);
622 if (NULL != tst->ttask)
624 GNUNET_SCHEDULER_cancel (tst->ttask);
627 if (NULL != tst->ltask)
629 GNUNET_SCHEDULER_cancel (tst->ltask);
632 if (NULL != tst->lsock)
634 GNUNET_NETWORK_socket_close (tst->lsock);
637 if (NULL != tst->nat)
639 GNUNET_NAT_unregister (tst->nat);
642 GNUNET_free (tst->section_name);
646 /* end of nat_auto_api_test.c */