2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file src/nat/gnunet-nat-server.c
23 * @brief Daemon to run on 'gnunet.org' to help test NAT traversal code
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nat_service.h"
29 #include "gnunet_protocols.h"
36 static struct GNUNET_SERVER_Handle *server;
41 static const struct GNUNET_CONFIGURATION_Handle *cfg;
45 * Try contacting the peer using autonomous NAT traveral method.
47 * @param dst_ipv4 IPv4 address to send the fake ICMP message
48 * @param dport destination port to include in ICMP message
49 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
52 try_anat (uint32_t dst_ipv4,
56 struct GNUNET_NAT_Handle *h;
57 struct sockaddr_in lsa;
58 struct sockaddr_in rsa;
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
62 "Asking for connection reversal with %x and code %u\n",
63 (unsigned int) dst_ipv4,
64 (unsigned int) dport);
65 memset (&lsa, 0, sizeof (lsa));
66 lsa.sin_family = AF_INET;
67 #if HAVE_SOCKADDR_IN_SIN_LEN
68 lsa.sin_len = sizeof (sa);
70 lsa.sin_addr.s_addr = 0;
71 lsa.sin_port = htons (dport);
72 memset (&rsa, 0, sizeof (rsa));
73 rsa.sin_family = AF_INET;
74 #if HAVE_SOCKADDR_IN_SIN_LEN
75 rsa.sin_len = sizeof (sa);
77 rsa.sin_addr.s_addr = dst_ipv4;
78 rsa.sin_port = htons (dport);
79 sa_len = sizeof (lsa);
80 h = GNUNET_NAT_register (cfg,
82 is_tcp ? IPPROTO_TCP : IPPROTO_UDP,
84 (const struct sockaddr **) &lsa,
87 GNUNET_NAT_request_reversal (h,
90 GNUNET_NAT_unregister (h);
95 * Closure for #tcp_send.
102 struct GNUNET_NETWORK_Handle *s;
112 * Task called by the scheduler once we can do the TCP send
113 * (or once we failed to connect...).
115 * @param cls the `struct TcpContext`
120 struct TcpContext *ctx = cls;
121 const struct GNUNET_SCHEDULER_TaskContext *tc;
123 tc = GNUNET_SCHEDULER_get_task_context ();
124 if ((NULL != tc->write_ready) &&
125 (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s)))
128 GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof (ctx->data)))
130 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
132 GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR);
134 GNUNET_NETWORK_socket_close (ctx->s);
140 * Try to send @a data to the
141 * IP @a dst_ipv4' at port @a dport via TCP.
143 * @param dst_ipv4 target IP
144 * @param dport target port
145 * @param data data to send
148 try_send_tcp (uint32_t dst_ipv4,
152 struct GNUNET_NETWORK_Handle *s;
153 struct sockaddr_in sa;
154 struct TcpContext *ctx;
156 s = GNUNET_NETWORK_socket_create (AF_INET,
161 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
165 memset (&sa, 0, sizeof (sa));
166 sa.sin_family = AF_INET;
167 #if HAVE_SOCKADDR_IN_SIN_LEN
168 sa.sin_len = sizeof (sa);
170 sa.sin_addr.s_addr = dst_ipv4;
171 sa.sin_port = htons (dport);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Sending TCP message to `%s'\n",
174 GNUNET_a2s ((struct sockaddr *) &sa,
177 GNUNET_NETWORK_socket_connect (s,
178 (const struct sockaddr *) &sa,
180 (errno != EINPROGRESS) )
182 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
184 GNUNET_NETWORK_socket_close (s);
187 ctx = GNUNET_new (struct TcpContext);
190 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
198 * Try to send @a data to the
199 * IP @a dst_ipv4 at port @a dport via UDP.
201 * @param dst_ipv4 target IP
202 * @param dport target port
203 * @param data data to send
206 try_send_udp (uint32_t dst_ipv4,
210 struct GNUNET_NETWORK_Handle *s;
211 struct sockaddr_in sa;
213 s = GNUNET_NETWORK_socket_create (AF_INET,
218 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
222 memset (&sa, 0, sizeof (sa));
223 sa.sin_family = AF_INET;
224 #if HAVE_SOCKADDR_IN_SIN_LEN
225 sa.sin_len = sizeof (sa);
227 sa.sin_addr.s_addr = dst_ipv4;
228 sa.sin_port = htons (dport);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Sending UDP packet to `%s'\n",
231 GNUNET_a2s ((struct sockaddr *) &sa,
234 GNUNET_NETWORK_socket_sendto (s,
237 (const struct sockaddr *) &sa,
239 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
241 GNUNET_NETWORK_socket_close (s);
246 * We've received a request to probe a NAT
250 * @param client handle to client (we always close)
251 * @param msg message with details about what to test
255 struct GNUNET_SERVER_Client *client,
256 const struct GNUNET_MessageHeader *msg)
258 const struct GNUNET_NAT_AUTO_TestMessage *tm;
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Received test request\n");
263 tm = (const struct GNUNET_NAT_AUTO_TestMessage *) msg;
264 dport = ntohs (tm->dport);
266 try_anat (tm->dst_ipv4,
268 (int) ntohl (tm->is_tcp));
269 else if (GNUNET_YES == ntohl (tm->is_tcp))
270 try_send_tcp (tm->dst_ipv4,
274 try_send_udp (tm->dst_ipv4,
277 GNUNET_SERVER_receive_done (client,
283 * Task run during shutdown.
288 shutdown_task (void *cls)
290 GNUNET_SERVER_destroy (server);
296 * Main function that will be run.
299 * @param args remaining command-line arguments
300 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
301 * @param c configuration
307 const struct GNUNET_CONFIGURATION_Handle *c)
309 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
310 {&test, NULL, GNUNET_MESSAGE_TYPE_NAT_TEST,
311 sizeof (struct GNUNET_NAT_AUTO_TestMessage)},
315 struct sockaddr_in in4;
316 struct sockaddr_in6 in6;
323 struct sockaddr *sa[] = {
324 (struct sockaddr *) &in4,
325 (struct sockaddr *) &in6,
330 if ( (NULL == args[0]) ||
331 (1 != SSCANF (args[0], "%u", &port)) ||
336 _("Please pass valid port number as the first argument! (got `%s')\n"),
340 memset (&in4, 0, sizeof (in4));
341 memset (&in6, 0, sizeof (in6));
342 in4.sin_family = AF_INET;
343 in4.sin_port = htons ((uint16_t) port);
344 in6.sin6_family = AF_INET6;
345 in6.sin6_port = htons ((uint16_t) port);
346 #if HAVE_SOCKADDR_IN_SIN_LEN
347 in4.sin_len = sizeof (in4);
348 in6.sin6_len = sizeof (in6);
350 server = GNUNET_SERVER_create (NULL,
352 (struct sockaddr * const *) sa,
354 GNUNET_TIME_UNIT_SECONDS,
356 GNUNET_SERVER_add_handlers (server,
358 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
364 * Main function of gnunet-nat-server.
366 * @param argc number of command-line arguments
367 * @param argv command line
368 * @return 0 on success, -1 on error
371 main (int argc, char *const argv[])
373 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
374 GNUNET_GETOPT_OPTION_END
378 GNUNET_STRINGS_get_utf8_args (argc, argv,
383 GNUNET_PROGRAM_run (argc,
385 "gnunet-nat-server [options] PORT",
386 _("GNUnet NAT traversal test helper daemon"),
391 GNUNET_free ((void*) argv);
394 GNUNET_free ((void*) argv);
399 /* end of gnunet-nat-server.c */