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"
33 * Entry we keep for each incoming connection.
38 * This is a doubly-linked list.
40 struct NatActivity *next;
43 * This is a doubly-linked list.
45 struct NatActivity *prev;
48 * Socket of the incoming connection.
50 struct GNUNET_NETWORK_Handle *sock;
53 * Handle of the master context.
55 struct GNUNET_NAT_Test *h;
58 * Task reading from the incoming connection.
60 GNUNET_SCHEDULER_TaskIdentifier rtask;
65 * Entry we keep for each connection to the gnunet-nat-service.
70 * This is a doubly-linked list.
72 struct ClientActivity *next;
75 * This is a doubly-linked list.
77 struct ClientActivity *prev;
80 * Socket of the incoming connection.
82 struct GNUNET_CLIENT_Connection *client;
88 * Handle to a NAT test.
90 struct GNUNET_NAT_Test
96 const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Function to call with success report
101 GNUNET_NAT_TestCallback report;
104 * Closure for 'report'.
109 * Handle to NAT traversal in use
111 struct GNUNET_NAT_Handle *nat;
114 * Handle to listen socket, or NULL
116 struct GNUNET_NETWORK_Handle *lsock;
119 * Head of list of nat activities.
121 struct NatActivity *na_head;
124 * Tail of list of nat activities.
126 struct NatActivity *na_tail;
129 * Head of list of client activities.
131 struct ClientActivity *ca_head;
134 * Tail of list of client activities.
136 struct ClientActivity *ca_tail;
139 * Identity of task for the listen socket (if any)
141 GNUNET_SCHEDULER_TaskIdentifier ltask;
144 * GNUNET_YES if we're testing TCP
149 * Data that should be transmitted or source-port.
154 * Advertised port to the other peer.
162 * Function called from GNUNET_NAT_register whenever someone asks us
163 * to do connection reversal.
165 * @param cls closure, our 'struct GNUNET_NAT_Handle'
166 * @param addr public IP address of the other peer
167 * @param addrlen actual lenght of the address
170 reversal_cb (void *cls,
171 const struct sockaddr *addr,
174 struct GNUNET_NAT_Test *h = cls;
175 const struct sockaddr_in *sa;
177 if (addrlen != sizeof (struct sockaddr_in))
179 sa = (const struct sockaddr_in *) addr;
180 if (h->data != sa->sin_port)
183 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
185 "Received connection reversal request for wrong port\n");
187 return; /* wrong port */
190 h->report (h->report_cls, GNUNET_OK);
195 * Activity on our incoming socket. Read data from the
196 * incoming connection.
198 * @param cls the 'struct NatActivity'
199 * @param tc scheduler context
202 do_udp_read (void *cls,
203 const struct GNUNET_SCHEDULER_TaskContext *tc)
205 struct GNUNET_NAT_Test *tst = cls;
208 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
212 if ( (NULL != tc->write_ready) &&
213 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
216 GNUNET_NETWORK_socket_recv (tst->lsock,
220 if (data == tst->data)
221 tst->report (tst->report_cls, GNUNET_OK);
224 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
226 "Received data mismatches expected value\n");
231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
233 "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,
258 if ( (NULL != tc->write_ready) &&
259 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
262 GNUNET_NETWORK_socket_recv (na->sock,
266 if (data == tst->data)
267 tst->report (tst->report_cls, GNUNET_OK);
270 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
272 "Received data mismatches expected value\n");
277 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
279 "Failed to receive data from inbound connection\n");
281 GNUNET_NETWORK_socket_close (na->sock);
287 * Activity on our listen socket. Accept the
288 * incoming connection.
290 * @param cls the 'struct GNUNET_NAT_Test'
291 * @param tc scheduler context
294 do_accept (void *cls,
295 const struct GNUNET_SCHEDULER_TaskContext *tc)
297 struct GNUNET_NAT_Test *tst = cls;
298 struct GNUNET_NETWORK_Handle *s;
299 struct NatActivity *wl;
301 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
302 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
304 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
308 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
311 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
312 return; /* odd error */
315 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
317 "Got an inbound connection, waiting for data\n");
319 wl = GNUNET_malloc (sizeof (struct NatActivity));
322 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
326 GNUNET_CONTAINER_DLL_insert (tst->na_head,
333 * Address-callback, used to send message to gnunet-nat-server.
336 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
337 * the previous (now invalid) one
338 * @param addr either the previous or the new public IP address
339 * @param addrlen actual lenght of the address
344 const struct sockaddr *addr,
347 struct GNUNET_NAT_Test *h = cls;
348 struct ClientActivity *ca;
349 struct GNUNET_CLIENT_Connection *client;
350 struct GNUNET_NAT_TestMessage msg;
351 const struct sockaddr_in *sa;
353 if (GNUNET_YES != add_remove)
355 if (addrlen != sizeof (struct sockaddr_in))
356 return; /* ignore IPv6 here */
358 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
360 "Asking gnunet-nat-server to connect to `%s'\n",
361 GNUNET_a2s (addr, addrlen));
363 sa = (const struct sockaddr_in*) addr;
364 msg.header.size = htons (sizeof(struct GNUNET_NAT_TestMessage));
365 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
366 msg.dst_ipv4 = sa->sin_addr.s_addr;
367 msg.dport = sa->sin_port;
369 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
371 client = GNUNET_CLIENT_connect ("gnunet-nat-server",
375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
376 _("Failed to connect to `gnunet-nat-server'\n"));
379 ca = GNUNET_malloc (sizeof (struct ClientActivity));
381 GNUNET_CONTAINER_DLL_insert (h->ca_head,
384 GNUNET_break (GNUNET_OK ==
385 GNUNET_CLIENT_transmit_and_get_response (client,
387 GNUNET_TIME_UNIT_SECONDS,
394 * Start testing if NAT traversal works using the
395 * given configuration (IPv4-only).
397 * @param cfg configuration for the NAT traversal
398 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
399 * @param bnd_port port to bind to, 0 for connection reversal
400 * @param adv_port externally advertised port to use
401 * @param report function to call with the result of the test
402 * @param report_cls closure for report
403 * @return handle to cancel NAT test
405 struct GNUNET_NAT_Test *
406 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
410 GNUNET_NAT_TestCallback report,
413 struct GNUNET_NAT_Test *ret;
414 struct sockaddr_in sa;
415 const struct sockaddr *addrs[] = { (const struct sockaddr*) &sa };
416 const socklen_t addrlens[] = { sizeof (sa) };
418 memset (&sa, 0, sizeof (sa));
419 sa.sin_family = AF_INET;
420 sa.sin_port = htons (bnd_port);
421 #if HAVE_SOCKADDR_IN_SIN_LEN
422 sa.sin_len = sizeof (sa);
425 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
427 ret->is_tcp = is_tcp;
428 ret->data = bnd_port;
429 ret->adv_port = adv_port;
430 ret->report = report;
431 ret->report_cls = report_cls;
434 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
437 &addr_cb, &reversal_cb, ret);
441 ret->lsock = GNUNET_NETWORK_socket_create (AF_INET,
445 if ( (ret->lsock == NULL) ||
446 (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
447 (const struct sockaddr*) &sa,
450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
452 GNUNET_a2s ((const struct sockaddr*)&sa,
455 if (NULL != ret->lsock)
456 GNUNET_NETWORK_socket_close (ret->lsock);
460 if (GNUNET_YES == is_tcp)
462 GNUNET_break (GNUNET_OK ==
463 GNUNET_NETWORK_socket_listen (ret->lsock, 5));
464 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
471 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
476 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
479 &addr_cb, NULL, ret);
486 * Stop an active NAT test.
488 * @param tst test to stop.
491 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
493 struct NatActivity *pos;
494 struct ClientActivity *cpos;
496 while (NULL != (cpos = tst->ca_head))
498 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
501 GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
504 while (NULL != (pos = tst->na_head))
506 GNUNET_CONTAINER_DLL_remove (tst->na_head,
509 GNUNET_SCHEDULER_cancel (pos->rtask);
510 GNUNET_NETWORK_socket_close (pos->sock);
513 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
514 GNUNET_SCHEDULER_cancel (tst->ltask);
515 if (NULL != tst->lsock)
516 GNUNET_NETWORK_socket_close (tst->lsock);
517 GNUNET_NAT_unregister (tst->nat);
521 /* end of nat_test.c */