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__)
34 * Entry we keep for each incoming connection.
39 * This is a doubly-linked list.
41 struct NatActivity *next;
44 * This is a doubly-linked list.
46 struct NatActivity *prev;
49 * Socket of the incoming connection.
51 struct GNUNET_NETWORK_Handle *sock;
54 * Handle of the master context.
56 struct GNUNET_NAT_Test *h;
59 * Task reading from the incoming connection.
61 GNUNET_SCHEDULER_TaskIdentifier rtask;
66 * Entry we keep for each connection to the gnunet-nat-service.
71 * This is a doubly-linked list.
73 struct ClientActivity *next;
76 * This is a doubly-linked list.
78 struct ClientActivity *prev;
81 * Socket of the incoming connection.
83 struct GNUNET_CLIENT_Connection *client;
89 * Handle to a NAT test.
91 struct GNUNET_NAT_Test
97 const struct GNUNET_CONFIGURATION_Handle *cfg;
100 * Function to call with success report
102 GNUNET_NAT_TestCallback report;
105 * Closure for 'report'.
110 * Handle to NAT traversal in use
112 struct GNUNET_NAT_Handle *nat;
115 * Handle to listen socket, or NULL
117 struct GNUNET_NETWORK_Handle *lsock;
120 * Head of list of nat activities.
122 struct NatActivity *na_head;
125 * Tail of list of nat activities.
127 struct NatActivity *na_tail;
130 * Head of list of client activities.
132 struct ClientActivity *ca_head;
135 * Tail of list of client activities.
137 struct ClientActivity *ca_tail;
140 * Identity of task for the listen socket (if any)
142 GNUNET_SCHEDULER_TaskIdentifier ltask;
145 * GNUNET_YES if we're testing TCP
150 * Data that should be transmitted or source-port.
155 * Advertised port to the other peer.
163 * Function called from GNUNET_NAT_register whenever someone asks us
164 * to do connection reversal.
166 * @param cls closure, our 'struct GNUNET_NAT_Handle'
167 * @param addr public IP address of the other peer
168 * @param addrlen actual lenght of the address
171 reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
173 struct GNUNET_NAT_Test *h = cls;
174 const struct sockaddr_in *sa;
176 if (addrlen != sizeof (struct sockaddr_in))
178 sa = (const struct sockaddr_in *) addr;
179 if (h->data != sa->sin_port)
182 LOG (GNUNET_ERROR_TYPE_DEBUG,
183 "Received connection reversal request for wrong port\n");
185 return; /* wrong port */
188 h->report (h->report_cls, GNUNET_OK);
193 * Activity on our incoming socket. Read data from the
194 * incoming connection.
196 * @param cls the 'struct NatActivity'
197 * @param tc scheduler context
200 do_udp_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 struct GNUNET_NAT_Test *tst = cls;
206 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
208 if ((NULL != tc->write_ready) &&
209 (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
211 GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
213 if (data == tst->data)
214 tst->report (tst->report_cls, GNUNET_OK);
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 "Received data mismatches expected value\n");
223 LOG (GNUNET_ERROR_TYPE_DEBUG,
224 "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
237 do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 struct NatActivity *na = cls;
240 struct GNUNET_NAT_Test *tst;
243 na->rtask = GNUNET_SCHEDULER_NO_TASK;
245 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
246 if ((NULL != tc->write_ready) &&
247 (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
249 GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
251 if (data == tst->data)
252 tst->report (tst->report_cls, GNUNET_OK);
255 LOG (GNUNET_ERROR_TYPE_DEBUG,
256 "Received data mismatches expected value\n");
261 LOG (GNUNET_ERROR_TYPE_DEBUG,
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 LOG (GNUNET_ERROR_TYPE_DEBUG,
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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asking gnunet-nat-server to connect to `%s'\n",
334 GNUNET_a2s (addr, addrlen));
336 sa = (const struct sockaddr_in *) addr;
337 msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
338 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
339 msg.dst_ipv4 = sa->sin_addr.s_addr;
340 msg.dport = sa->sin_port;
342 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
344 client = GNUNET_CLIENT_connect ("gnunet-nat-server", h->cfg);
347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348 _("Failed to connect to `gnunet-nat-server'\n"));
351 ca = GNUNET_malloc (sizeof (struct ClientActivity));
353 GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
354 GNUNET_break (GNUNET_OK ==
355 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
356 GNUNET_TIME_UNIT_SECONDS,
363 * Start testing if NAT traversal works using the
364 * given configuration (IPv4-only).
366 * @param cfg configuration for the NAT traversal
367 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
368 * @param bnd_port port to bind to, 0 for connection reversal
369 * @param adv_port externally advertised port to use
370 * @param report function to call with the result of the test
371 * @param report_cls closure for report
372 * @return handle to cancel NAT test
374 struct GNUNET_NAT_Test *
375 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
376 int is_tcp, uint16_t bnd_port, uint16_t adv_port,
377 GNUNET_NAT_TestCallback report, void *report_cls)
379 struct GNUNET_NAT_Test *ret;
380 struct sockaddr_in sa;
381 const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
382 const socklen_t addrlens[] = { sizeof (sa) };
384 memset (&sa, 0, sizeof (sa));
385 sa.sin_family = AF_INET;
386 sa.sin_port = htons (bnd_port);
387 #if HAVE_SOCKADDR_IN_SIN_LEN
388 sa.sin_len = sizeof (sa);
391 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
393 ret->is_tcp = is_tcp;
394 ret->data = bnd_port;
395 ret->adv_port = adv_port;
396 ret->report = report;
397 ret->report_cls = report_cls;
401 GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
407 GNUNET_NETWORK_socket_create (AF_INET,
409 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
411 if ((ret->lsock == NULL) ||
413 GNUNET_NETWORK_socket_bind (ret->lsock, (const struct sockaddr *) &sa,
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
419 GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
421 if (NULL != ret->lsock)
422 GNUNET_NETWORK_socket_close (ret->lsock);
426 if (GNUNET_YES == is_tcp)
428 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
430 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
431 ret->lsock, &do_accept, ret);
436 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
437 ret->lsock, &do_udp_read, ret);
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 while (NULL != (cpos = tst->ca_head))
460 GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
461 GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
464 while (NULL != (pos = tst->na_head))
466 GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, pos);
467 GNUNET_SCHEDULER_cancel (pos->rtask);
468 GNUNET_NETWORK_socket_close (pos->sock);
471 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
472 GNUNET_SCHEDULER_cancel (tst->ltask);
473 if (NULL != tst->lsock)
474 GNUNET_NETWORK_socket_close (tst->lsock);
475 GNUNET_NAT_unregister (tst->nat);
479 /* end of nat_test.c */