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"
31 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
33 #define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
36 * 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_Test *h;
61 * Task reading from the incoming connection.
63 GNUNET_SCHEDULER_TaskIdentifier rtask;
68 * Entry we keep for each connection to the gnunet-nat-service.
73 * This is a doubly-linked list.
75 struct ClientActivity *next;
78 * This is a doubly-linked list.
80 struct ClientActivity *prev;
83 * Socket of the incoming connection.
85 struct GNUNET_CLIENT_Connection *client;
91 * Handle to a NAT test.
93 struct GNUNET_NAT_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 GNUNET_SCHEDULER_TaskIdentifier ltask;
147 * GNUNET_YES if we're testing TCP
152 * Data that should be transmitted or source-port.
157 * Advertised port to the other peer.
165 * Function called from #GNUNET_NAT_register whenever someone asks us
166 * to do connection reversal.
168 * @param cls closure, our `struct GNUNET_NAT_Handle`
169 * @param addr public IP address of the other peer
170 * @param addrlen actual lenght of the address
173 reversal_cb (void *cls,
174 const struct sockaddr *addr,
177 struct GNUNET_NAT_Test *h = cls;
178 const struct sockaddr_in *sa;
180 if (addrlen != sizeof (struct sockaddr_in))
182 sa = (const struct sockaddr_in *) addr;
183 if (h->data != sa->sin_port)
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "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, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 struct GNUNET_NAT_Test *tst = cls;
208 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
210 if ((NULL != tc->write_ready) &&
211 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
213 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
215 if (data == tst->data)
216 tst->report (tst->report_cls, GNUNET_OK);
218 LOG (GNUNET_ERROR_TYPE_DEBUG,
219 "Received data mismatches expected value\n");
222 LOG (GNUNET_ERROR_TYPE_DEBUG,
223 "Failed to receive data from inbound connection\n");
228 * Activity on our incoming socket. Read data from the
229 * incoming connection.
231 * @param cls the 'struct NatActivity'
232 * @param tc scheduler context
235 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 struct NatActivity *na = cls;
238 struct GNUNET_NAT_Test *tst;
241 na->rtask = GNUNET_SCHEDULER_NO_TASK;
243 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
244 if ((NULL != tc->write_ready) &&
245 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
247 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
249 if (data == tst->data)
250 tst->report (tst->report_cls, GNUNET_OK);
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "Received data mismatches expected value\n");
256 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Failed to receive data from inbound connection\n");
258 GNUNET_NETWORK_socket_close (na->sock);
264 * Activity on our listen socket. Accept the
265 * incoming connection.
267 * @param cls the 'struct GNUNET_NAT_Test'
268 * @param tc scheduler context
271 do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
273 struct GNUNET_NAT_Test *tst = cls;
274 struct GNUNET_NETWORK_Handle *s;
275 struct NatActivity *wl;
277 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
278 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
281 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
283 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
286 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
287 return; /* odd error */
289 LOG (GNUNET_ERROR_TYPE_DEBUG,
290 "Got an inbound connection, waiting for data\n");
291 wl = GNUNET_malloc (sizeof (struct NatActivity));
295 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, wl->sock,
297 GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
302 * Address-callback, used to send message to gnunet-nat-server.
305 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
306 * the previous (now invalid) one
307 * @param addr either the previous or the new public IP address
308 * @param addrlen actual lenght of the address
313 const struct sockaddr *addr,
316 struct GNUNET_NAT_Test *h = cls;
317 struct ClientActivity *ca;
318 struct GNUNET_CLIENT_Connection *client;
319 struct GNUNET_NAT_TestMessage msg;
320 const struct sockaddr_in *sa;
322 if (GNUNET_YES != add_remove)
324 if (addrlen != sizeof (struct sockaddr_in))
326 LOG (GNUNET_ERROR_TYPE_DEBUG,
327 "NAT test ignores IPv6 address `%s' returned from NAT library\n",
328 GNUNET_a2s (addr, addrlen));
329 return; /* ignore IPv6 here */
331 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asking gnunet-nat-server to connect to `%s'\n",
332 GNUNET_a2s (addr, addrlen));
333 sa = (const struct sockaddr_in *) addr;
334 msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
335 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
336 msg.dst_ipv4 = sa->sin_addr.s_addr;
337 msg.dport = sa->sin_port;
339 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
341 client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg);
344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
345 _("Failed to connect to `gnunet-nat-server'\n"));
348 ca = GNUNET_malloc (sizeof (struct ClientActivity));
350 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
351 GNUNET_break (GNUNET_OK ==
352 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
360 * Start testing if NAT traversal works using the
361 * given configuration (IPv4-only).
363 * @param cfg configuration for the NAT traversal
364 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
365 * @param bnd_port port to bind to, 0 for connection reversal
366 * @param adv_port externally advertised port to use
367 * @param report function to call with the result of the test
368 * @param report_cls closure for report
369 * @return handle to cancel NAT test
371 struct GNUNET_NAT_Test *
372 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
373 int is_tcp, uint16_t bnd_port, uint16_t adv_port,
374 GNUNET_NAT_TestCallback report, void *report_cls)
376 struct GNUNET_NAT_Test *ret;
377 struct sockaddr_in sa;
378 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
379 const socklen_t addrlens[] = { sizeof (sa) };
381 memset (&sa, 0, sizeof (sa));
382 sa.sin_family = AF_INET;
383 sa.sin_port = htons (bnd_port);
384 #if HAVE_SOCKADDR_IN_SIN_LEN
385 sa.sin_len = sizeof (sa);
388 ret = GNUNET_new (struct GNUNET_NAT_Test);
390 ret->is_tcp = is_tcp;
391 ret->data = bnd_port;
392 ret->adv_port = adv_port;
393 ret->report = report;
394 ret->report_cls = report_cls;
398 GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
404 GNUNET_NETWORK_socket_create (AF_INET,
406 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
408 if ((ret->lsock == NULL) ||
410 GNUNET_NETWORK_socket_bind (ret->lsock, (const struct sockaddr *) &sa,
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
415 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
417 if (NULL != ret->lsock)
418 GNUNET_NETWORK_socket_close (ret->lsock);
422 if (GNUNET_YES == is_tcp)
424 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
426 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
427 ret->lsock, &do_accept, ret);
432 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
433 ret->lsock, &do_udp_read, ret);
435 LOG (GNUNET_ERROR_TYPE_DEBUG,
436 "NAT test listens on port %u (%s)\n",
438 (GNUNET_YES == is_tcp) ? "tcp" : "udp");
440 GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
441 &addr_cb, NULL, ret);
448 * Stop an active NAT test.
450 * @param tst test to stop.
453 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
455 struct NatActivity *pos;
456 struct ClientActivity *cpos;
458 LOG (GNUNET_ERROR_TYPE_DEBUG,
459 "Stopping NAT test\n");
460 while (NULL != (cpos = tst->ca_head))
462 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
463 GNUNET_CLIENT_disconnect (cpos->client);
466 while (NULL != (pos = tst->na_head))
468 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
469 GNUNET_SCHEDULER_cancel (pos->rtask);
470 GNUNET_NETWORK_socket_close (pos->sock);
473 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
474 GNUNET_SCHEDULER_cancel (tst->ltask);
475 if (NULL != tst->lsock)
476 GNUNET_NETWORK_socket_close (tst->lsock);
477 GNUNET_NAT_unregister (tst->nat);
481 /* end of nat_test.c */