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)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Received connection reversal request for wrong port\n");
184 return; /* wrong port */
187 h->report (h->report_cls, GNUNET_OK);
192 * Activity on our incoming socket. Read data from the
193 * incoming connection.
195 * @param cls the 'struct NatActivity'
196 * @param tc scheduler context
199 do_udp_read (void *cls,
200 const struct GNUNET_SCHEDULER_TaskContext *tc)
202 struct GNUNET_NAT_Test *tst = cls;
205 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
209 if ( (NULL != tc->write_ready) &&
210 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
213 GNUNET_NETWORK_socket_recv (tst->lsock,
217 if (data == tst->data)
218 tst->report (tst->report_cls, GNUNET_OK);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Received data mismatches expected value\n");
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Failed to receive data from inbound connection\n");
230 * Activity on our incoming socket. Read data from the
231 * incoming connection.
233 * @param cls the 'struct NatActivity'
234 * @param tc scheduler context
238 const struct GNUNET_SCHEDULER_TaskContext *tc)
240 struct NatActivity *na = cls;
241 struct GNUNET_NAT_Test *tst;
244 na->rtask = GNUNET_SCHEDULER_NO_TASK;
246 GNUNET_CONTAINER_DLL_remove (tst->na_head,
249 if ( (NULL != tc->write_ready) &&
250 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
253 GNUNET_NETWORK_socket_recv (na->sock,
257 if (data == tst->data)
258 tst->report (tst->report_cls, GNUNET_OK);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Received data mismatches expected value\n");
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Failed to receive data from inbound connection\n");
266 GNUNET_NETWORK_socket_close (na->sock);
272 * Activity on our listen socket. Accept the
273 * incoming connection.
275 * @param cls the 'struct GNUNET_NAT_Test'
276 * @param tc scheduler context
279 do_accept (void *cls,
280 const struct GNUNET_SCHEDULER_TaskContext *tc)
282 struct GNUNET_NAT_Test *tst = cls;
283 struct GNUNET_NETWORK_Handle *s;
284 struct NatActivity *wl;
286 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
287 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
289 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
293 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
296 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
297 return; /* odd error */
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Got an inbound connection, waiting for data\n");
301 wl = GNUNET_malloc (sizeof (struct NatActivity));
304 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
308 GNUNET_CONTAINER_DLL_insert (tst->na_head,
315 * Address-callback, used to send message to gnunet-nat-server.
318 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
319 * the previous (now invalid) one
320 * @param addr either the previous or the new public IP address
321 * @param addrlen actual lenght of the address
326 const struct sockaddr *addr,
329 struct GNUNET_NAT_Test *h = cls;
330 struct ClientActivity *ca;
331 struct GNUNET_CLIENT_Connection *client;
332 struct GNUNET_NAT_TestMessage msg;
333 const struct sockaddr_in *sa;
335 if (GNUNET_YES != add_remove)
337 if (addrlen != sizeof (struct sockaddr_in))
338 return; /* ignore IPv6 here */
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Asking gnunet-nat-server to connect to `%s'\n",
341 GNUNET_a2s (addr, addrlen));
342 sa = (const struct sockaddr_in*) addr;
343 msg.header.size = htons (sizeof(struct GNUNET_NAT_TestMessage));
344 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
345 msg.dst_ipv4 = sa->sin_addr.s_addr;
346 msg.dport = sa->sin_port;
348 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
350 client = GNUNET_CLIENT_connect ("gnunet-nat-server",
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355 _("Failed to connect to `gnunet-nat-server'\n"));
358 ca = GNUNET_malloc (sizeof (struct ClientActivity));
360 GNUNET_CONTAINER_DLL_insert (h->ca_head,
363 GNUNET_break (GNUNET_OK ==
364 GNUNET_CLIENT_transmit_and_get_response (client,
366 GNUNET_TIME_UNIT_SECONDS,
373 * Start testing if NAT traversal works using the
374 * given configuration (IPv4-only).
376 * @param cfg configuration for the NAT traversal
377 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
378 * @param bnd_port port to bind to, 0 for connection reversal
379 * @param adv_port externally advertised port to use
380 * @param report function to call with the result of the test
381 * @param report_cls closure for report
382 * @return handle to cancel NAT test
384 struct GNUNET_NAT_Test *
385 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
389 GNUNET_NAT_TestCallback report,
392 struct GNUNET_NAT_Test *ret;
393 struct sockaddr_in sa;
394 const struct sockaddr *addrs[] = { (const struct sockaddr*) &sa };
395 const socklen_t addrlens[] = { sizeof (sa) };
397 memset (&sa, 0, sizeof (sa));
398 sa.sin_family = AF_INET;
399 sa.sin_port = htons (bnd_port);
400 #if HAVE_SOCKADDR_IN_SIN_LEN
401 sa.sin_len = sizeof (sa);
404 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
406 ret->is_tcp = is_tcp;
407 ret->data = bnd_port;
408 ret->adv_port = adv_port;
409 ret->report = report;
410 ret->report_cls = report_cls;
413 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
416 &addr_cb, &reversal_cb, ret);
420 ret->lsock = GNUNET_NETWORK_socket_create (AF_INET,
424 if ( (ret->lsock == NULL) ||
425 (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
426 (const struct sockaddr*) &sa,
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
431 GNUNET_a2s ((const struct sockaddr*)&sa,
434 if (NULL != ret->lsock)
435 GNUNET_NETWORK_socket_close (ret->lsock);
439 if (GNUNET_YES == is_tcp)
441 GNUNET_break (GNUNET_OK ==
442 GNUNET_NETWORK_socket_listen (ret->lsock, 5));
443 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
450 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
455 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
458 &addr_cb, NULL, ret);
465 * Stop an active NAT test.
467 * @param tst test to stop.
470 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
472 struct NatActivity *pos;
473 struct ClientActivity *cpos;
475 while (NULL != (cpos = tst->ca_head))
477 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
480 GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
483 while (NULL != (pos = tst->na_head))
485 GNUNET_CONTAINER_DLL_remove (tst->na_head,
488 GNUNET_SCHEDULER_cancel (pos->rtask);
489 GNUNET_NETWORK_socket_close (pos->sock);
492 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
493 GNUNET_SCHEDULER_cancel (tst->ltask);
494 if (NULL != tst->lsock)
495 GNUNET_NETWORK_socket_close (tst->lsock);
496 GNUNET_NAT_unregister (tst->nat);
500 /* end of nat_test.c */