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
200 const struct GNUNET_SCHEDULER_TaskContext *tc)
202 struct NatActivity *na = cls;
203 struct GNUNET_NAT_Test *tst;
206 na->rtask = GNUNET_SCHEDULER_NO_TASK;
208 GNUNET_CONTAINER_DLL_remove (tst->na_head,
211 if ( (NULL != tc->write_ready) &&
212 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
215 GNUNET_NETWORK_socket_recv (na->sock,
219 if (data == tst->data)
220 tst->report (tst->report_cls, GNUNET_OK);
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Received data mismatches expected value\n");
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Failed to receive data from inbound connection\n");
228 GNUNET_NETWORK_socket_close (na->sock);
234 * Activity on our listen socket. Accept the
235 * incoming connection.
237 * @param cls the 'struct GNUNET_NAT_Test'
238 * @param tc scheduler context
241 do_accept (void *cls,
242 const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct GNUNET_NAT_Test *tst = cls;
245 struct GNUNET_NETWORK_Handle *s;
246 struct NatActivity *wl;
248 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
249 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
251 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
255 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
258 GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
259 return; /* odd error */
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Got an inbound connection, waiting for data\n");
263 wl = GNUNET_malloc (sizeof (struct NatActivity));
266 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
270 GNUNET_CONTAINER_DLL_insert (tst->na_head,
277 * Address-callback, used to send message to gnunet-nat-server.
280 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
281 * the previous (now invalid) one
282 * @param addr either the previous or the new public IP address
283 * @param addrlen actual lenght of the address
288 const struct sockaddr *addr,
291 struct GNUNET_NAT_Test *h = cls;
292 struct ClientActivity *ca;
293 struct GNUNET_CLIENT_Connection *client;
294 struct GNUNET_NAT_TestMessage msg;
295 const struct sockaddr_in *sa;
297 if (GNUNET_YES != add_remove)
299 if (addrlen != sizeof (struct sockaddr_in))
300 return; /* ignore IPv6 here */
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Asking gnunet-nat-server to connect to `%s'\n",
303 GNUNET_a2s (addr, addrlen));
304 sa = (const struct sockaddr_in*) addr;
305 msg.header.size = htons (sizeof(struct GNUNET_NAT_TestMessage));
306 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
307 msg.dst_ipv4 = sa->sin_addr.s_addr;
308 msg.dport = sa->sin_port;
310 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
312 client = GNUNET_CLIENT_connect ("gnunet-nat-server",
316 ca = GNUNET_malloc (sizeof (struct ClientActivity));
318 GNUNET_CONTAINER_DLL_insert (h->ca_head,
321 GNUNET_break (GNUNET_OK ==
322 GNUNET_CLIENT_transmit_and_get_response (client,
324 GNUNET_TIME_UNIT_SECONDS,
331 * Start testing if NAT traversal works using the
332 * given configuration (IPv4-only).
334 * @param cfg configuration for the NAT traversal
335 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
336 * @param bnd_port port to bind to, 0 for connection reversal
337 * @param adv_port externally advertised port to use
338 * @param report function to call with the result of the test
339 * @param report_cls closure for report
340 * @return handle to cancel NAT test
342 struct GNUNET_NAT_Test *
343 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
347 GNUNET_NAT_TestCallback report,
350 struct GNUNET_NAT_Test *ret;
351 struct sockaddr_in sa;
352 const struct sockaddr *addrs[] = { (const struct sockaddr*) &sa };
353 const socklen_t addrlens[] = { sizeof (sa) };
355 memset (&sa, 0, sizeof (sa));
356 sa.sin_port = htons (bnd_port);
357 #if HAVE_SOCKADDR_IN_SIN_LEN
358 sa.sin_len = sizeof (sa);
361 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
363 ret->is_tcp = is_tcp;
364 ret->data = bnd_port;
365 ret->adv_port = adv_port;
366 ret->report = report;
367 ret->report_cls = report_cls;
370 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
373 &addr_cb, &reversal_cb, ret);
377 ret->lsock = GNUNET_NETWORK_socket_create (AF_INET,
378 (is_tcp==GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM, 0);
379 if ( (ret->lsock == NULL) ||
380 (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
381 (const struct sockaddr*) &sa,
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 _("Failed to create listen socket for NAT test\n"));
386 if (NULL != ret->lsock)
387 GNUNET_NETWORK_socket_close (ret->lsock);
391 GNUNET_break (GNUNET_OK ==
392 GNUNET_NETWORK_socket_listen (ret->lsock, 5));
393 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
397 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
400 &addr_cb, NULL, ret);
407 * Stop an active NAT test.
409 * @param tst test to stop.
412 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
414 struct NatActivity *pos;
415 struct ClientActivity *cpos;
417 while (NULL != (cpos = tst->ca_head))
419 GNUNET_CONTAINER_DLL_remove (tst->ca_head,
422 GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
425 while (NULL != (pos = tst->na_head))
427 GNUNET_CONTAINER_DLL_remove (tst->na_head,
430 GNUNET_SCHEDULER_cancel (pos->rtask);
431 GNUNET_NETWORK_socket_close (pos->sock);
434 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
435 GNUNET_SCHEDULER_cancel (tst->ltask);
436 if (NULL != tst->lsock)
437 GNUNET_NETWORK_socket_close (tst->lsock);
438 GNUNET_NAT_unregister (tst->nat);
442 /* end of nat_test.c */