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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file nat/nat_auto_api_test.c
17 * @brief functions to test if the NAT configuration is successful at achieving NAT traversal (with the help of a gnunet-nat-server)
18 * @author Christian Grothoff
21 #include "gnunet_util_lib.h"
22 #include "gnunet_nat_service.h"
23 #include "gnunet_nat_auto_service.h"
26 #define LOG(kind,...) GNUNET_log_from (kind, "nat-auto", __VA_ARGS__)
28 #define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
31 * Entry we keep for each incoming connection.
36 * This is a doubly-linked list.
38 struct NatActivity *next;
41 * This is a doubly-linked list.
43 struct NatActivity *prev;
46 * Socket of the incoming connection.
48 struct GNUNET_NETWORK_Handle *sock;
51 * Handle of the master context.
53 struct GNUNET_NAT_AUTO_Test *h;
56 * Task reading from the incoming connection.
58 struct GNUNET_SCHEDULER_Task *rtask;
63 * Entry we keep for each connection to the gnunet-nat-service.
68 * This is a doubly-linked list.
70 struct ClientActivity *next;
73 * This is a doubly-linked list.
75 struct ClientActivity *prev;
78 * Socket of the incoming connection.
80 struct GNUNET_MQ_Handle *mq;
83 * Handle to overall NAT test.
85 struct GNUNET_NAT_AUTO_Test *h;
91 * Handle to a NAT test.
93 struct GNUNET_NAT_AUTO_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 struct GNUNET_SCHEDULER_Task *ltask;
147 * Task identifier for the timeout (if any)
149 struct GNUNET_SCHEDULER_Task *ttask;
152 * Section name of plugin to test.
157 * IPPROTO_TCP or IPPROTO_UDP.
162 * Data that should be transmitted or source-port.
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 @a addr
182 reversal_cb (void *cls,
183 const struct sockaddr *addr,
186 struct GNUNET_NAT_AUTO_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,
200 GNUNET_NAT_ERROR_SUCCESS);
205 * Activity on our incoming socket. Read data from the
206 * incoming connection.
208 * @param cls the `struct GNUNET_NAT_AUTO_Test`
211 do_udp_read (void *cls)
213 struct GNUNET_NAT_AUTO_Test *tst = cls;
215 const struct GNUNET_SCHEDULER_TaskContext *tc;
217 tc = GNUNET_SCHEDULER_get_task_context ();
219 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
223 if ((NULL != tc->write_ready) &&
224 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
227 GNUNET_NETWORK_socket_recv (tst->lsock,
231 if (data == tst->data)
232 tst->report (tst->report_cls,
233 GNUNET_NAT_ERROR_SUCCESS);
235 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 "Received data mismatches expected value\n");
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Failed to receive data from inbound connection\n");
245 * Activity on our incoming socket. Read data from the
246 * incoming connection.
248 * @param cls the `struct NatActivity`
253 struct NatActivity *na = cls;
254 struct GNUNET_NAT_AUTO_Test *tst;
256 const struct GNUNET_SCHEDULER_TaskContext *tc;
258 tc = GNUNET_SCHEDULER_get_task_context ();
261 GNUNET_CONTAINER_DLL_remove (tst->na_head,
264 if ((NULL != tc->write_ready) &&
265 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
268 GNUNET_NETWORK_socket_recv (na->sock,
272 if (data == tst->data)
273 tst->report (tst->report_cls,
274 GNUNET_NAT_ERROR_SUCCESS);
276 LOG (GNUNET_ERROR_TYPE_DEBUG,
277 "Received data does not match expected value\n");
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Failed to receive data from inbound connection\n");
282 GNUNET_NETWORK_socket_close (na->sock);
288 * Activity on our listen socket. Accept the
289 * incoming connection.
291 * @param cls the `struct GNUNET_NAT_AUTO_Test`
294 do_accept (void *cls)
296 struct GNUNET_NAT_AUTO_Test *tst = cls;
297 struct GNUNET_NETWORK_Handle *s;
298 struct NatActivity *wl;
300 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
304 s = GNUNET_NETWORK_socket_accept (tst->lsock,
309 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO,
311 return; /* odd error */
313 LOG (GNUNET_ERROR_TYPE_DEBUG,
314 "Got an inbound connection, waiting for data\n");
315 wl = GNUNET_new (struct NatActivity);
319 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
323 GNUNET_CONTAINER_DLL_insert (tst->na_head,
330 * We got disconnected from the NAT server. Stop
331 * waiting for a reply.
333 * @param cls the `struct ClientActivity`
334 * @param error error code
337 mq_error_handler (void *cls,
338 enum GNUNET_MQ_Error error)
340 struct ClientActivity *ca = cls;
341 struct GNUNET_NAT_AUTO_Test *tst = ca->h;
343 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
346 GNUNET_MQ_destroy (ca->mq);
352 * Address-callback, used to send message to gnunet-nat-server.
355 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
356 * the previous (now invalid) one
357 * @param ac address class the address belongs to
358 * @param addr either the previous or the new public IP address
359 * @param addrlen actual length of the @a addr
364 enum GNUNET_NAT_AddressClass ac,
365 const struct sockaddr *addr,
368 struct GNUNET_NAT_AUTO_Test *h = cls;
369 struct ClientActivity *ca;
370 struct GNUNET_MQ_Envelope *env;
371 struct GNUNET_NAT_AUTO_TestMessage *msg;
372 const struct sockaddr_in *sa;
374 if (GNUNET_YES != add_remove)
376 if (addrlen != sizeof (struct sockaddr_in))
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
382 return; /* ignore IPv6 here */
384 LOG (GNUNET_ERROR_TYPE_INFO,
385 "Asking gnunet-nat-server to connect to `%s'\n",
389 ca = GNUNET_new (struct ClientActivity);
391 ca->mq = GNUNET_CLIENT_connect (h->cfg,
399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400 _("Failed to connect to `gnunet-nat-server'\n"));
403 GNUNET_CONTAINER_DLL_insert (h->ca_head,
406 sa = (const struct sockaddr_in *) addr;
407 env = GNUNET_MQ_msg (msg,
408 GNUNET_MESSAGE_TYPE_NAT_TEST);
409 msg->dst_ipv4 = sa->sin_addr.s_addr;
410 msg->dport = sa->sin_port;
412 msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP));
413 GNUNET_MQ_send (ca->mq,
419 * Calls the report-callback reporting failure.
421 * Destroys the nat handle after the callback has been processed.
423 * @param cls handle to the timed out NAT test
428 struct GNUNET_NAT_AUTO_Test *nh = cls;
431 nh->report (nh->report_cls,
437 * Start testing if NAT traversal works using the given configuration.
438 * The transport adapters should be down while using this function.
440 * @param cfg configuration for the NAT traversal
441 * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
442 * @param section_name configuration section to use for configuration
443 * @param report function to call with the result of the test
444 * @param report_cls closure for @a report
445 * @return handle to cancel NAT test
447 struct GNUNET_NAT_AUTO_Test *
448 GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
450 const char *section_name,
451 GNUNET_NAT_TestCallback report,
454 struct GNUNET_NAT_AUTO_Test *nh;
455 unsigned long long bnd_port;
456 struct sockaddr_in sa;
457 const struct sockaddr *addrs[] = {
458 (const struct sockaddr *) &sa
460 const socklen_t addrlens[] = {
465 GNUNET_CONFIGURATION_get_value_number (cfg,
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472 _("Failed to find valid PORT in section `%s'\n"),
477 memset (&sa, 0, sizeof (sa));
478 sa.sin_family = AF_INET;
479 sa.sin_port = htons ((uint16_t) bnd_port);
480 #if HAVE_SOCKADDR_IN_SIN_LEN
481 sa.sin_len = sizeof (sa);
484 nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test);
487 nh->section_name = GNUNET_strdup (section_name);
489 nh->report_cls = report_cls;
490 nh->status = GNUNET_NAT_ERROR_SUCCESS;
494 = GNUNET_NAT_register (cfg,
505 = GNUNET_NETWORK_socket_create (AF_INET,
506 (IPPROTO_UDP == proto)
510 if ( (NULL == nh->lsock) ||
512 GNUNET_NETWORK_socket_bind (nh->lsock,
513 (const struct sockaddr *) &sa,
516 LOG (GNUNET_ERROR_TYPE_ERROR,
517 _("Failed to create 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_fail,
531 if (IPPROTO_TCP == proto)
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 %llu (%s)\n",
553 (IPPROTO_TCP == proto) ? "tcp" : "udp");
554 nh->nat = GNUNET_NAT_register (cfg,
565 LOG (GNUNET_ERROR_TYPE_INFO,
566 _("NAT test failed to start NAT library\n"));
567 if (NULL != nh->ltask)
569 GNUNET_SCHEDULER_cancel (nh->ltask);
572 if (NULL != nh->lsock)
574 GNUNET_NETWORK_socket_close (nh->lsock);
577 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
578 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
588 * Stop an active NAT test.
590 * @param tst test to stop.
593 GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst)
595 struct NatActivity *pos;
596 struct ClientActivity *cpos;
598 LOG (GNUNET_ERROR_TYPE_DEBUG,
599 "Stopping NAT test\n");
600 while (NULL != (cpos = tst->ca_head))
602 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
605 GNUNET_MQ_destroy (cpos->mq);
608 while (NULL != (pos = tst->na_head))
610 GNUNET_CONTAINER_DLL_remove (tst->na_head,
613 GNUNET_SCHEDULER_cancel (pos->rtask);
614 GNUNET_NETWORK_socket_close (pos->sock);
617 if (NULL != tst->ttask)
619 GNUNET_SCHEDULER_cancel (tst->ttask);
622 if (NULL != tst->ltask)
624 GNUNET_SCHEDULER_cancel (tst->ltask);
627 if (NULL != tst->lsock)
629 GNUNET_NETWORK_socket_close (tst->lsock);
632 if (NULL != tst->nat)
634 GNUNET_NAT_unregister (tst->nat);
637 GNUNET_free (tst->section_name);
641 /* end of nat_auto_api_test.c */