2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
34 * Information we track per client.
41 struct GNUNET_SCHEDULER_Task *tt;
46 struct GNUNET_SERVICE_Client *client;
53 static const struct GNUNET_CONFIGURATION_Handle *cfg;
57 * Try contacting the peer using autonomous NAT traveral method.
59 * @param dst_ipv4 IPv4 address to send the fake ICMP message
60 * @param dport destination port to include in ICMP message
61 * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO)
64 try_anat (uint32_t dst_ipv4,
68 struct GNUNET_NAT_Handle *h;
69 struct sockaddr_in lsa;
70 struct sockaddr_in rsa;
71 const struct sockaddr *sa;
74 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
75 "Asking for connection reversal with %x and code %u\n",
76 (unsigned int) dst_ipv4,
77 (unsigned int) dport);
78 memset (&lsa, 0, sizeof (lsa));
79 lsa.sin_family = AF_INET;
80 #if HAVE_SOCKADDR_IN_SIN_LEN
81 lsa.sin_len = sizeof (sa);
83 lsa.sin_addr.s_addr = 0;
84 lsa.sin_port = htons (dport);
85 memset (&rsa, 0, sizeof (rsa));
86 rsa.sin_family = AF_INET;
87 #if HAVE_SOCKADDR_IN_SIN_LEN
88 rsa.sin_len = sizeof (sa);
90 rsa.sin_addr.s_addr = dst_ipv4;
91 rsa.sin_port = htons (dport);
92 sa_len = sizeof (lsa);
93 sa = (const struct sockaddr *) &lsa;
94 h = GNUNET_NAT_register (cfg,
96 is_tcp ? IPPROTO_TCP : IPPROTO_UDP,
101 GNUNET_NAT_request_reversal (h,
104 GNUNET_NAT_unregister (h);
109 * Closure for #tcp_send.
116 struct GNUNET_NETWORK_Handle *s;
126 * Task called by the scheduler once we can do the TCP send
127 * (or once we failed to connect...).
129 * @param cls the `struct TcpContext`
134 struct TcpContext *ctx = cls;
135 const struct GNUNET_SCHEDULER_TaskContext *tc;
137 tc = GNUNET_SCHEDULER_get_task_context ();
138 if ((NULL != tc->write_ready) &&
139 (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s)))
142 GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof (ctx->data)))
144 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send");
146 GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR);
148 GNUNET_NETWORK_socket_close (ctx->s);
154 * Try to send @a data to the
155 * IP @a dst_ipv4' at port @a dport via TCP.
157 * @param dst_ipv4 target IP
158 * @param dport target port
159 * @param data data to send
162 try_send_tcp (uint32_t dst_ipv4,
166 struct GNUNET_NETWORK_Handle *s;
167 struct sockaddr_in sa;
168 struct TcpContext *ctx;
170 s = GNUNET_NETWORK_socket_create (AF_INET,
175 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
179 memset (&sa, 0, sizeof (sa));
180 sa.sin_family = AF_INET;
181 #if HAVE_SOCKADDR_IN_SIN_LEN
182 sa.sin_len = sizeof (sa);
184 sa.sin_addr.s_addr = dst_ipv4;
185 sa.sin_port = htons (dport);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Sending TCP message to `%s'\n",
188 GNUNET_a2s ((struct sockaddr *) &sa,
191 GNUNET_NETWORK_socket_connect (s,
192 (const struct sockaddr *) &sa,
194 (errno != EINPROGRESS) )
196 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
198 GNUNET_NETWORK_socket_close (s);
201 ctx = GNUNET_new (struct TcpContext);
204 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS,
212 * Try to send @a data to the
213 * IP @a dst_ipv4 at port @a dport via UDP.
215 * @param dst_ipv4 target IP
216 * @param dport target port
217 * @param data data to send
220 try_send_udp (uint32_t dst_ipv4,
224 struct GNUNET_NETWORK_Handle *s;
225 struct sockaddr_in sa;
227 s = GNUNET_NETWORK_socket_create (AF_INET,
232 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
236 memset (&sa, 0, sizeof (sa));
237 sa.sin_family = AF_INET;
238 #if HAVE_SOCKADDR_IN_SIN_LEN
239 sa.sin_len = sizeof (sa);
241 sa.sin_addr.s_addr = dst_ipv4;
242 sa.sin_port = htons (dport);
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Sending UDP packet to `%s'\n",
245 GNUNET_a2s ((struct sockaddr *) &sa,
248 GNUNET_NETWORK_socket_sendto (s,
251 (const struct sockaddr *) &sa,
253 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
255 GNUNET_NETWORK_socket_close (s);
260 * We've received a request to probe a NAT
263 * @param cls handle to client (we always close)
264 * @param msg message with details about what to test
267 handle_test (void *cls,
268 const struct GNUNET_NAT_AUTO_TestMessage *tm)
270 struct ClientData *cd = cls;
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Received test request\n");
275 dport = ntohs (tm->dport);
277 try_anat (tm->dst_ipv4,
279 (int) ntohl (tm->is_tcp));
280 else if (GNUNET_YES == ntohl (tm->is_tcp))
281 try_send_tcp (tm->dst_ipv4,
285 try_send_udp (tm->dst_ipv4,
288 GNUNET_SERVICE_client_drop (cd->client);
293 * Main function that will be run.
296 * @param c configuration
297 * @param srv service handle
301 const struct GNUNET_CONFIGURATION_Handle *c,
302 struct GNUNET_SERVICE_Handle *srv)
309 * Forcefully drops client after 1s.
311 * @param cls our `struct ClientData` of a client to drop
314 force_timeout (void *cls)
316 struct ClientData *cd = cls;
319 GNUNET_SERVICE_client_drop (cd->client);
325 * Callback called when a client connects to the service.
327 * @param cls closure for the service
328 * @param c the new client that connected to the service
329 * @param mq the message queue used to send messages to the client
330 * @return our `struct ClientData`
333 client_connect_cb (void *cls,
334 struct GNUNET_SERVICE_Client *c,
335 struct GNUNET_MQ_Handle *mq)
337 struct ClientData *cd;
339 cd = GNUNET_new (struct ClientData);
341 cd->tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
349 * Callback called when a client disconnected from the service
351 * @param cls closure for the service
352 * @param c the client that disconnected
353 * @param internal_cls our `struct ClientData`
356 client_disconnect_cb (void *cls,
357 struct GNUNET_SERVICE_Client *c,
360 struct ClientData *cd = internal_cls;
363 GNUNET_SCHEDULER_cancel (cd->tt);
369 * Define "main" method using service macro.
373 GNUNET_SERVICE_OPTION_NONE,
376 &client_disconnect_cb,
378 GNUNET_MQ_hd_fixed_size (test,
379 GNUNET_MESSAGE_TYPE_NAT_TEST,
380 struct GNUNET_NAT_AUTO_TestMessage,
382 GNUNET_MQ_handler_end ());
385 #if defined(LINUX) && defined(__GLIBC__)
389 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
391 void __attribute__ ((constructor))
392 GNUNET_ARM_memory_init ()
394 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
395 mallopt (M_TOP_PAD, 1 * 1024);
403 /* end of gnunet-nat-server.c */