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, const struct sockaddr *addr, socklen_t addrlen)
172 struct GNUNET_NAT_Test *h = cls;
173 const struct sockaddr_in *sa;
175 if (addrlen != sizeof (struct sockaddr_in))
177 sa = (const struct sockaddr_in *) addr;
178 if (h->data != sa->sin_port)
181 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
182 "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, const struct GNUNET_SCHEDULER_TaskContext *tc)
201 struct GNUNET_NAT_Test *tst = cls;
205 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
207 if ((NULL != tc->write_ready) &&
208 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
210 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
212 if (data == tst->data)
213 tst->report (tst->report_cls, GNUNET_OK);
216 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
217 "Received data mismatches expected value\n");
222 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
223 "Failed to receive data from inbound connection\n");
229 * Activity on our incoming socket. Read data from the
230 * incoming connection.
232 * @param cls the 'struct NatActivity'
233 * @param tc scheduler context
236 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
238 struct NatActivity *na = cls;
239 struct GNUNET_NAT_Test *tst;
242 na->rtask = GNUNET_SCHEDULER_NO_TASK;
244 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
245 if ((NULL != tc->write_ready) &&
246 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
248 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
250 if (data == tst->data)
251 tst->report (tst->report_cls, GNUNET_OK);
254 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
256 "Received data mismatches expected value\n");
261 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
262 "Failed to receive data from inbound connection\n");
264 GNUNET_NETWORK_socket_close (na->sock);
270 * Activity on our listen socket. Accept the
271 * incoming connection.
273 * @param cls the 'struct GNUNET_NAT_Test'
274 * @param tc scheduler context
277 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct GNUNET_NAT_Test *tst = cls;
280 struct GNUNET_NETWORK_Handle *s;
281 struct NatActivity *wl;
283 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
284 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
287 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
289 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
292 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
293 return; /* odd error */
296 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
297 "Got an inbound connection, waiting for data\n");
299 wl = GNUNET_malloc (sizeof (struct NatActivity));
303 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, wl->sock,
305 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
310 * Address-callback, used to send message to gnunet-nat-server.
313 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
314 * the previous (now invalid) one
315 * @param addr either the previous or the new public IP address
316 * @param addrlen actual lenght of the address
319 addr_cb (void *cls, int add_remove, const struct sockaddr *addr,
322 struct GNUNET_NAT_Test *h = cls;
323 struct ClientActivity *ca;
324 struct GNUNET_CLIENT_Connection *client;
325 struct GNUNET_NAT_TestMessage msg;
326 const struct sockaddr_in *sa;
328 if (GNUNET_YES != add_remove)
330 if (addrlen != sizeof (struct sockaddr_in))
331 return; /* ignore IPv6 here */
333 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat",
334 "Asking gnunet-nat-server to connect to `%s'\n",
335 GNUNET_a2s (addr, addrlen));
337 sa = (const struct sockaddr_in *) addr;
338 msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
339 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
340 msg.dst_ipv4 = sa->sin_addr.s_addr;
341 msg.dport = sa->sin_port;
343 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
345 client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg);
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 _("Failed to connect to `gnunet-nat-server'\n"));
352 ca = GNUNET_malloc (sizeof (struct ClientActivity));
354 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
355 GNUNET_break (GNUNET_OK ==
356 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
357 GNUNET_TIME_UNIT_SECONDS,
364 * Start testing if NAT traversal works using the
365 * given configuration (IPv4-only).
367 * @param cfg configuration for the NAT traversal
368 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
369 * @param bnd_port port to bind to, 0 for connection reversal
370 * @param adv_port externally advertised port to use
371 * @param report function to call with the result of the test
372 * @param report_cls closure for report
373 * @return handle to cancel NAT test
375 struct GNUNET_NAT_Test *
376 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
377 int is_tcp, uint16_t bnd_port, uint16_t adv_port,
378 GNUNET_NAT_TestCallback report, void *report_cls)
380 struct GNUNET_NAT_Test *ret;
381 struct sockaddr_in sa;
382 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
383 const socklen_t addrlens[] = { sizeof (sa) };
385 memset (&sa, 0, sizeof (sa));
386 sa.sin_family = AF_INET;
387 sa.sin_port = htons (bnd_port);
388 #if HAVE_SOCKADDR_IN_SIN_LEN
389 sa.sin_len = sizeof (sa);
392 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
394 ret->is_tcp = is_tcp;
395 ret->data = bnd_port;
396 ret->adv_port = adv_port;
397 ret->report = report;
398 ret->report_cls = report_cls;
402 GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
408 GNUNET_NETWORK_socket_create (AF_INET,
410 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
412 if ((ret->lsock == NULL) ||
414 GNUNET_NETWORK_socket_bind (ret->lsock, (const struct sockaddr *) &sa,
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
420 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
422 if (NULL != ret->lsock)
423 GNUNET_NETWORK_socket_close (ret->lsock);
427 if (GNUNET_YES == is_tcp)
429 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
431 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
432 ret->lsock, &do_accept, ret);
437 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
438 ret->lsock, &do_udp_read, ret);
441 GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
442 &addr_cb, NULL, ret);
449 * Stop an active NAT test.
451 * @param tst test to stop.
454 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
456 struct NatActivity *pos;
457 struct ClientActivity *cpos;
459 while (NULL != (cpos = tst->ca_head))
461 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
462 GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
465 while (NULL != (pos = tst->na_head))
467 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
468 GNUNET_SCHEDULER_cancel (pos->rtask);
469 GNUNET_NETWORK_socket_close (pos->sock);
472 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
473 GNUNET_SCHEDULER_cancel (tst->ltask);
474 if (NULL != tst->lsock)
475 GNUNET_NETWORK_socket_close (tst->lsock);
476 GNUNET_NAT_unregister (tst->nat);
480 /* end of nat_test.c */