2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 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 nat/gnunet-service-nat_helper.c
23 * @brief runs the gnunet-helper-nat-server
24 * @author Milan Bouchet-Valat
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-nat_helper.h"
33 * Information we keep per NAT helper process.
39 * IP address we pass to the NAT helper.
41 struct in_addr internal_address;
44 * Function to call if we receive a reversal request.
46 GN_ReversalCallback cb;
54 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
56 struct GNUNET_TIME_Relative server_retry_delay;
59 * ID of select gnunet-helper-nat-server stdout read task
61 struct GNUNET_SCHEDULER_Task *server_read_task;
64 * The process id of the server process (if behind NAT)
66 struct GNUNET_OS_Process *server_proc;
69 * stdout pipe handle for the gnunet-helper-nat-server process
71 struct GNUNET_DISK_PipeHandle *server_stdout;
74 * stdout file handle (for reading) for the gnunet-helper-nat-server process
76 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
81 * Task that restarts the gnunet-helper-nat-server process after a crash
82 * after a certain delay.
84 * @param cls a `struct HelperContext`
87 restart_nat_server (void *cls);
91 * Try again starting the helper later
93 * @param h context of the helper
96 try_again (struct HelperContext *h)
98 GNUNET_assert (NULL == h->server_read_task);
100 = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
102 = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
109 * We have been notified that gnunet-helper-nat-server has written
110 * something to stdout. Handle the output, then reschedule this
111 * function to be called again once more is available.
113 * @param cls the `struct HelperContext`
116 nat_server_read (void *cls)
118 struct HelperContext *h = cls;
122 const char *port_start;
123 struct sockaddr_in sin_addr;
125 h->server_read_task = NULL;
130 = GNUNET_DISK_file_read (h->server_stdout_handle,
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "Finished reading from server stdout with code: %d\n",
138 if (0 != GNUNET_OS_process_kill (h->server_proc,
140 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
143 GNUNET_OS_process_wait (h->server_proc);
144 GNUNET_OS_process_destroy (h->server_proc);
145 h->server_proc = NULL;
146 GNUNET_DISK_pipe_close (h->server_stdout);
147 h->server_stdout = NULL;
148 h->server_stdout_handle = NULL;
154 for (size_t i = 0; i < sizeof (mybuf); i++)
156 if (mybuf[i] == '\n')
161 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
164 port_start = &mybuf[i + 1];
168 /* construct socket address of sender */
172 sin_addr.sin_family = AF_INET;
173 #if HAVE_SOCKADDR_IN_SIN_LEN
174 sin_addr.sin_len = sizeof (sin_addr);
176 if ( (NULL == port_start) ||
177 (1 != SSCANF (port_start,
180 (-1 == inet_pton (AF_INET,
182 &sin_addr.sin_addr)))
184 /* should we restart gnunet-helper-nat-server? */
185 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
186 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
189 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
190 h->server_stdout_handle,
195 sin_addr.sin_port = htons ((uint16_t) port);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "gnunet-helper-nat-server read: %s:%d\n",
203 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
204 h->server_stdout_handle,
211 * Task that restarts the gnunet-helper-nat-server process after a crash
212 * after a certain delay.
214 * @param cls a `struct HelperContext`
217 restart_nat_server (void *cls)
219 struct HelperContext *h = cls;
221 char ia[INET_ADDRSTRLEN];
223 h->server_read_task = NULL;
224 GNUNET_assert (NULL !=
226 &h->internal_address,
229 /* Start the server process */
231 = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
233 GNUNET_OS_check_helper_binary (binary,
237 /* move instantly to max delay, as this is unlikely to be fixed */
238 h->server_retry_delay
239 = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
240 GNUNET_free (binary);
245 = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
246 GNUNET_NO, GNUNET_YES);
247 if (NULL == h->server_stdout)
249 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
251 GNUNET_free (binary);
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Starting `%s' at `%s'\n",
257 "gnunet-helper-nat-server",
260 = GNUNET_OS_start_process (GNUNET_NO,
266 "gnunet-helper-nat-server",
269 GNUNET_free (binary);
270 if (NULL == h->server_proc)
272 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
273 _("Failed to start %s\n"),
274 "gnunet-helper-nat-server");
275 GNUNET_DISK_pipe_close (h->server_stdout);
276 h->server_stdout = NULL;
280 /* Close the write end of the read pipe */
281 GNUNET_DISK_pipe_close_end (h->server_stdout,
282 GNUNET_DISK_PIPE_END_WRITE);
283 h->server_stdout_handle
284 = GNUNET_DISK_pipe_handle (h->server_stdout,
285 GNUNET_DISK_PIPE_END_READ);
287 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
288 h->server_stdout_handle,
295 * Start the gnunet-helper-nat-server and process incoming
298 * @param internal_address
299 * @param cb function to call if we receive a request
300 * @param cb_cls closure for @a cb
301 * @return NULL on error
303 struct HelperContext *
304 GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
305 GN_ReversalCallback cb,
308 struct HelperContext *h;
310 h = GNUNET_new (struct HelperContext);
313 h->internal_address = *internal_address;
314 restart_nat_server (h);
315 if (NULL == h->server_stdout)
317 GN_stop_gnunet_nat_server_ (h);
325 * Start the gnunet-helper-nat-server and process incoming
328 * @param h helper context to stop
331 GN_stop_gnunet_nat_server_ (struct HelperContext *h)
333 if (NULL != h->server_read_task)
335 GNUNET_SCHEDULER_cancel (h->server_read_task);
336 h->server_read_task = NULL;
338 if (NULL != h->server_proc)
340 if (0 != GNUNET_OS_process_kill (h->server_proc,
342 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
344 GNUNET_OS_process_wait (h->server_proc);
345 GNUNET_OS_process_destroy (h->server_proc);
346 h->server_proc = NULL;
347 GNUNET_DISK_pipe_close (h->server_stdout);
348 h->server_stdout = NULL;
349 h->server_stdout_handle = NULL;
351 if (NULL != h->server_stdout)
353 GNUNET_DISK_pipe_close (h->server_stdout);
354 h->server_stdout = NULL;
355 h->server_stdout_handle = NULL;
362 * We want to connect to a peer that is behind NAT. Run the
363 * gnunet-helper-nat-client to send dummy ICMP responses to cause
364 * that peer to connect to us (connection reversal).
366 * @param internal_address out internal address to use
367 * @param internal_port port to use
368 * @param remote_v4 the address of the peer (IPv4-only)
369 * @return #GNUNET_SYSERR on error,
370 * #GNUNET_OK otherwise
373 GN_request_connection_reversal (const struct in_addr *internal_address,
374 uint16_t internal_port,
375 const struct in_addr *remote_v4)
377 char intv4[INET_ADDRSTRLEN];
378 char remv4[INET_ADDRSTRLEN];
379 char port_as_string[6];
380 struct GNUNET_OS_Process *proc;
383 if (NULL == inet_ntop (AF_INET,
388 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
390 return GNUNET_SYSERR;
392 if (NULL == inet_ntop (AF_INET,
397 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
399 return GNUNET_SYSERR;
401 GNUNET_snprintf (port_as_string,
402 sizeof (port_as_string),
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Running gnunet-helper-nat-client %s %s %u\n",
411 = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
413 = GNUNET_OS_start_process (GNUNET_NO,
419 "gnunet-helper-nat-client",
424 GNUNET_free (binary);
426 return GNUNET_SYSERR;
427 /* we know that the gnunet-helper-nat-client will terminate virtually
429 GNUNET_OS_process_wait (proc);
430 GNUNET_OS_process_destroy (proc);
435 /* end of gnunet-service-nat_helper.c */