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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 \
34 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
37 * 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.
70 struct ClientActivity {
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_AUTO_Test *h;
94 * Handle to a NAT test.
96 struct GNUNET_NAT_AUTO_Test {
100 const struct GNUNET_CONFIGURATION_Handle *cfg;
103 * Function to call with success report
105 GNUNET_NAT_TestCallback report;
108 * Closure for @e report.
113 * Handle to NAT traversal in use
115 struct GNUNET_NAT_Handle *nat;
118 * Handle to listen socket, or NULL
120 struct GNUNET_NETWORK_Handle *lsock;
123 * Head of list of nat activities.
125 struct NatActivity *na_head;
128 * Tail of list of nat activities.
130 struct NatActivity *na_tail;
133 * Head of list of client activities.
135 struct ClientActivity *ca_head;
138 * Tail of list of client activities.
140 struct ClientActivity *ca_tail;
143 * Identity of task for the listen socket (if any)
145 struct GNUNET_SCHEDULER_Task *ltask;
148 * Task identifier for the timeout (if any)
150 struct GNUNET_SCHEDULER_Task *ttask;
153 * Section name of plugin to test.
158 * IPPROTO_TCP or IPPROTO_UDP.
163 * Data that should be transmitted or source-port.
168 * Status code to be reported to the timeout/status call
170 enum GNUNET_NAT_StatusCode status;
175 * Function called from #GNUNET_NAT_register whenever someone asks us
176 * to do connection reversal.
178 * @param cls closure, our `struct GNUNET_NAT_Handle`
179 * @param addr public IP address of the other peer
180 * @param addrlen actual lenght of the @a addr
183 reversal_cb(void *cls, const struct sockaddr *addr, socklen_t addrlen)
185 struct GNUNET_NAT_AUTO_Test *h = cls;
186 const struct sockaddr_in *sa;
188 if (sizeof(struct sockaddr_in) != addrlen)
190 sa = (const struct sockaddr_in *)addr;
191 if (h->data != sa->sin_port)
193 LOG(GNUNET_ERROR_TYPE_DEBUG,
194 "Received connection reversal request for wrong port\n");
195 return; /* wrong port */
198 h->report(h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
203 * Activity on our incoming socket. Read data from the
204 * incoming connection.
206 * @param cls the `struct GNUNET_NAT_AUTO_Test`
209 do_udp_read(void *cls)
211 struct GNUNET_NAT_AUTO_Test *tst = cls;
213 const struct GNUNET_SCHEDULER_TaskContext *tc;
215 tc = GNUNET_SCHEDULER_get_task_context();
216 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
220 if ((NULL != tc->write_ready) &&
221 (GNUNET_NETWORK_fdset_isset(tc->read_ready, tst->lsock)) &&
223 GNUNET_NETWORK_socket_recv(tst->lsock, &data, sizeof(data))))
225 if (data == tst->data)
226 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
228 LOG(GNUNET_ERROR_TYPE_DEBUG,
229 "Received data mismatches expected value\n");
232 LOG(GNUNET_ERROR_TYPE_DEBUG,
233 "Failed to receive data from inbound connection\n");
238 * Activity on our incoming socket. Read data from the
239 * incoming connection.
241 * @param cls the `struct NatActivity`
246 struct NatActivity *na = cls;
247 struct GNUNET_NAT_AUTO_Test *tst;
249 const struct GNUNET_SCHEDULER_TaskContext *tc;
251 tc = GNUNET_SCHEDULER_get_task_context();
254 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, na);
255 if ((NULL != tc->write_ready) &&
256 (GNUNET_NETWORK_fdset_isset(tc->read_ready, na->sock)) &&
258 GNUNET_NETWORK_socket_recv(na->sock, &data, sizeof(data))))
260 if (data == tst->data)
261 tst->report(tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
263 LOG(GNUNET_ERROR_TYPE_DEBUG,
264 "Received data does not match expected value\n");
267 LOG(GNUNET_ERROR_TYPE_DEBUG,
268 "Failed to receive data from inbound connection\n");
269 GNUNET_NETWORK_socket_close(na->sock);
275 * Activity on our listen socket. Accept the
276 * incoming connection.
278 * @param cls the `struct GNUNET_NAT_AUTO_Test`
283 struct GNUNET_NAT_AUTO_Test *tst = cls;
284 struct GNUNET_NETWORK_Handle *s;
285 struct NatActivity *wl;
287 tst->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
291 s = GNUNET_NETWORK_socket_accept(tst->lsock, NULL, NULL);
294 GNUNET_log_strerror(GNUNET_ERROR_TYPE_INFO, "accept");
295 return; /* odd error */
297 LOG(GNUNET_ERROR_TYPE_DEBUG,
298 "Got an inbound connection, waiting for data\n");
299 wl = GNUNET_new(struct NatActivity);
302 wl->rtask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
306 GNUNET_CONTAINER_DLL_insert(tst->na_head, tst->na_tail, wl);
311 * We got disconnected from the NAT server. Stop
312 * waiting for a reply.
314 * @param cls the `struct ClientActivity`
315 * @param error error code
318 mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
320 struct ClientActivity *ca = cls;
321 struct GNUNET_NAT_AUTO_Test *tst = ca->h;
323 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, ca);
324 GNUNET_MQ_destroy(ca->mq);
330 * Address-callback, used to send message to gnunet-nat-server.
333 * @param app_ctx[in,out] location where the app can store stuff
334 * on add and retrieve it on remove
335 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
336 * the previous (now invalid) one
337 * @param ac address class the address belongs to
338 * @param addr either the previous or the new public IP address
339 * @param addrlen actual length of the @a addr
345 enum GNUNET_NAT_AddressClass ac,
346 const struct sockaddr *addr,
349 struct GNUNET_NAT_AUTO_Test *h = cls;
350 struct ClientActivity *ca;
351 struct GNUNET_MQ_Envelope *env;
352 struct GNUNET_NAT_AUTO_TestMessage *msg;
353 const struct sockaddr_in *sa;
356 if (GNUNET_YES != add_remove)
358 if (addrlen != sizeof(struct sockaddr_in))
360 LOG(GNUNET_ERROR_TYPE_DEBUG,
361 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
362 GNUNET_a2s(addr, addrlen));
363 return; /* ignore IPv6 here */
365 LOG(GNUNET_ERROR_TYPE_INFO,
366 "Asking gnunet-nat-server to connect to `%s'\n",
367 GNUNET_a2s(addr, addrlen));
369 ca = GNUNET_new(struct ClientActivity);
371 ca->mq = GNUNET_CLIENT_connect(h->cfg,
379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
380 _("Failed to connect to `gnunet-nat-server'\n"));
383 GNUNET_CONTAINER_DLL_insert(h->ca_head, h->ca_tail, ca);
384 sa = (const struct sockaddr_in *)addr;
385 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_NAT_TEST);
386 msg->dst_ipv4 = sa->sin_addr.s_addr;
387 msg->dport = sa->sin_port;
389 msg->is_tcp = htonl((uint32_t)(h->proto == IPPROTO_TCP));
390 GNUNET_MQ_send(ca->mq, env);
395 * Calls the report-callback reporting failure.
397 * Destroys the nat handle after the callback has been processed.
399 * @param cls handle to the timed out NAT test
404 struct GNUNET_NAT_AUTO_Test *nh = cls;
407 nh->report(nh->report_cls, nh->status);
412 * Start testing if NAT traversal works using the given configuration.
413 * The transport adapters should be down while using this function.
415 * @param cfg configuration for the NAT traversal
416 * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
417 * @param section_name configuration section to use for configuration
418 * @param report function to call with the result of the test
419 * @param report_cls closure for @a report
420 * @return handle to cancel NAT test
422 struct GNUNET_NAT_AUTO_Test *
423 GNUNET_NAT_AUTO_test_start(const struct GNUNET_CONFIGURATION_Handle *cfg,
425 const char *section_name,
426 GNUNET_NAT_TestCallback report,
429 struct GNUNET_NAT_AUTO_Test *nh;
430 unsigned long long bnd_port;
431 struct sockaddr_in sa;
432 const struct sockaddr *addrs[] = { (const struct sockaddr *)&sa };
433 const socklen_t addrlens[] = { sizeof(sa) };
435 if ((GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
441 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
442 _("Failed to find valid PORT in section `%s'\n"),
447 memset(&sa, 0, sizeof(sa));
448 sa.sin_family = AF_INET;
449 sa.sin_port = htons((uint16_t)bnd_port);
450 #if HAVE_SOCKADDR_IN_SIN_LEN
451 sa.sin_len = sizeof(sa);
454 nh = GNUNET_new(struct GNUNET_NAT_AUTO_Test);
457 nh->section_name = GNUNET_strdup(section_name);
459 nh->report_cls = report_cls;
460 nh->status = GNUNET_NAT_ERROR_SUCCESS;
463 nh->nat = GNUNET_NAT_register(cfg,
476 GNUNET_NETWORK_socket_create(AF_INET,
477 (IPPROTO_UDP == proto) ? SOCK_DGRAM
480 if ((NULL == nh->lsock) ||
481 (GNUNET_OK != GNUNET_NETWORK_socket_bind(nh->lsock,
482 (const struct sockaddr *)&sa,
485 LOG(GNUNET_ERROR_TYPE_ERROR,
486 _("Failed to create socket bound to `%s' for NAT test: %s\n"),
487 GNUNET_a2s((const struct sockaddr *)&sa, sizeof(sa)),
489 if (NULL != nh->lsock)
491 GNUNET_NETWORK_socket_close(nh->lsock);
494 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
495 nh->ttask = GNUNET_SCHEDULER_add_now(&do_fail, nh);
498 if (IPPROTO_TCP == proto)
500 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_listen(nh->lsock, 5));
501 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
508 nh->ltask = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
513 LOG(GNUNET_ERROR_TYPE_INFO,
514 "NAT test listens on port %llu (%s)\n",
516 (IPPROTO_TCP == proto) ? "tcp" : "udp");
517 nh->nat = GNUNET_NAT_register(cfg,
528 LOG(GNUNET_ERROR_TYPE_INFO,
529 _("NAT test failed to start NAT library\n"));
530 if (NULL != nh->ltask)
532 GNUNET_SCHEDULER_cancel(nh->ltask);
535 if (NULL != nh->lsock)
537 GNUNET_NETWORK_socket_close(nh->lsock);
540 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
541 nh->ttask = GNUNET_SCHEDULER_add_now(&do_fail, nh);
550 * Stop an active NAT test.
552 * @param tst test to stop.
555 GNUNET_NAT_AUTO_test_stop(struct GNUNET_NAT_AUTO_Test *tst)
557 struct NatActivity *pos;
558 struct ClientActivity *cpos;
560 LOG(GNUNET_ERROR_TYPE_DEBUG, "Stopping NAT test\n");
561 while (NULL != (cpos = tst->ca_head))
563 GNUNET_CONTAINER_DLL_remove(tst->ca_head, tst->ca_tail, cpos);
564 GNUNET_MQ_destroy(cpos->mq);
567 while (NULL != (pos = tst->na_head))
569 GNUNET_CONTAINER_DLL_remove(tst->na_head, tst->na_tail, pos);
570 GNUNET_SCHEDULER_cancel(pos->rtask);
571 GNUNET_NETWORK_socket_close(pos->sock);
574 if (NULL != tst->ttask)
576 GNUNET_SCHEDULER_cancel(tst->ttask);
579 if (NULL != tst->ltask)
581 GNUNET_SCHEDULER_cancel(tst->ltask);
584 if (NULL != tst->lsock)
586 GNUNET_NETWORK_socket_close(tst->lsock);
589 if (NULL != tst->nat)
591 GNUNET_NAT_unregister(tst->nat);
594 GNUNET_free(tst->section_name);
598 /* end of nat_auto_api_test.c */