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 * Handle to a NAT test.
67 struct GNUNET_NAT_Test
73 const struct GNUNET_CONFIGURATION_Handle *cfg;
76 * Function to call with success report
78 GNUNET_NAT_TestCallback report;
81 * Closure for 'report'.
86 * Handle to NAT traversal in use
88 struct GNUNET_NAT_Handle *nat;
91 * Handle to listen socket, or NULL
93 struct GNUNET_NETWORK_Handle *lsock;
96 * Head of list of nat activities.
98 struct NatActivity *head;
101 * Tail of list of nat activities.
103 struct NatActivity *tail;
106 * Identity of task for the listen socket (if any)
108 GNUNET_SCHEDULER_TaskIdentifier ltask;
111 * GNUNET_YES if we're testing TCP
116 * Data that should be transmitted or source-port.
121 * Advertised port to the other peer.
129 * Function called from GNUNET_NAT_register
130 * whenever someone asks us to do connection
133 * @param cls closure, our 'struct GNUNET_NAT_Handle'
134 * @param addr public IP address of the other peer
135 * @param addrlen actual lenght of the address
138 reversal_cb (void *cls,
139 const struct sockaddr *addr,
142 struct GNUNET_NAT_Test *h = cls;
143 const struct sockaddr_in *sa;
145 if (addrlen != sizeof (struct sockaddr_in))
147 sa = (const struct sockaddr_in *) addr;
148 if (h->data != sa->sin_port)
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Received connection reversal request for wrong port\n");
152 return; /* wrong port */
155 h->report (h->report_cls, GNUNET_OK);
160 * Activity on our incoming socket. Read data from the
161 * incoming connection.
163 * @param cls the 'struct NatActivity'
164 * @param tc scheduler context
168 const struct GNUNET_SCHEDULER_TaskContext *tc)
170 struct NatActivity *na = cls;
171 struct GNUNET_NAT_Test *tst;
173 na->rtask = GNUNET_SCHEDULER_NO_TASK;
175 GNUNET_CONTAINER_DLL_remove (tst->head,
180 // fimxe: read from socket...
182 GNUNET_NETWORK_socket_close (na->sock);
188 * Activity on our listen socket. Accept the
189 * incoming connection.
191 * @param cls the 'struct GNUNET_NAT_Test'
192 * @param tc scheduler context
195 do_accept (void *cls,
196 const struct GNUNET_SCHEDULER_TaskContext *tc)
198 struct GNUNET_NAT_Test *tst = cls;
199 struct GNUNET_NETWORK_Handle *s;
200 struct NatActivity *wl;
202 tst->ltask = GNUNET_SCHEDULER_NO_TASK;
203 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
205 tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
209 s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
211 return; /* odd error */
212 wl = GNUNET_malloc (sizeof (struct NatActivity));
215 wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
219 GNUNET_CONTAINER_DLL_insert (tst->head,
226 * Address-callback, used to send message to gnunet-nat-server.
229 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
230 * the previous (now invalid) one
231 * @param addr either the previous or the new public IP address
232 * @param addrlen actual lenght of the address
237 const struct sockaddr *addr,
240 struct GNUNET_NAT_Test *h = cls;
241 struct GNUNET_CLIENT_Connection *client;
242 struct GNUNET_NAT_TestMessage msg;
243 const struct sockaddr_in *sa;
245 if (GNUNET_YES != add_remove)
247 if (addrlen != sizeof (struct sockaddr_in))
248 return; /* ignore IPv6 here */
249 sa = (const struct sockaddr_in*) addr;
250 msg.header.size = htons (sizeof(struct GNUNET_NAT_TestMessage));
251 msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
252 msg.dst_ipv4 = sa->sin_addr.s_addr;
253 msg.dport = sa->sin_port;
255 msg.is_tcp = htonl ((uint32_t) h->is_tcp);
257 client = GNUNET_CLIENT_connect ("gnunet-nat-server",
259 GNUNET_break (GNUNET_OK ==
260 GNUNET_CLIENT_transmit_and_get_response (client,
262 GNUNET_TIME_UNIT_SECONDS,
265 GNUNET_CLIENT_disconnect (client, GNUNET_YES);
270 * Start testing if NAT traversal works using the
271 * given configuration (IPv4-only).
273 * @param cfg configuration for the NAT traversal
274 * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
275 * @param bnd_port port to bind to, 0 for connection reversal
276 * @param adv_port externally advertised port to use
277 * @param report function to call with the result of the test
278 * @param report_cls closure for report
279 * @return handle to cancel NAT test
281 struct GNUNET_NAT_Test *
282 GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
286 GNUNET_NAT_TestCallback report,
289 struct GNUNET_NAT_Test *ret;
290 struct sockaddr_in sa;
291 const struct sockaddr *addrs[] = { (const struct sockaddr*) &sa };
292 const socklen_t addrlens[] = { sizeof (sa) };
294 memset (&sa, 0, sizeof (sa));
295 sa.sin_port = htons (bnd_port);
296 #if HAVE_SOCKADDR_IN_SIN_LEN
297 sa.sin_len = sizeof (sa);
300 ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
302 ret->is_tcp = is_tcp;
303 ret->data = bnd_port;
304 ret->adv_port = adv_port;
305 ret->report = report;
306 ret->report_cls = report_cls;
309 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
312 &addr_cb, &reversal_cb, ret);
316 ret->lsock = GNUNET_NETWORK_socket_create (AF_INET,
317 (is_tcp==GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM, 0);
318 if ( (ret->lsock == NULL) ||
319 (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
320 (const struct sockaddr*) &sa,
323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
324 _("Failed to create listen socket for NAT test\n"));
325 if (NULL != ret->lsock)
326 GNUNET_NETWORK_socket_close (ret->lsock);
330 GNUNET_break (GNUNET_OK ==
331 GNUNET_NETWORK_socket_listen (ret->lsock, 5));
332 ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
336 ret->nat = GNUNET_NAT_register (cfg, is_tcp,
339 &addr_cb, NULL, ret);
346 * Stop an active NAT test.
348 * @param tst test to stop.
351 GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
353 struct NatActivity *pos;
355 while (NULL != (pos = tst->head))
357 GNUNET_CONTAINER_DLL_remove (tst->head,
360 GNUNET_SCHEDULER_cancel (pos->rtask);
361 GNUNET_NETWORK_socket_close (pos->sock);
364 if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
365 GNUNET_SCHEDULER_cancel (tst->ltask);
366 if (NULL != tst->lsock)
367 GNUNET_NETWORK_socket_close (tst->lsock);
368 GNUNET_NAT_unregister (tst->nat);
372 /* end of nat_test.c */