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;
79 * Handle to the GNUnet configuration
81 const struct GNUNET_CONFIGURATION_Handle *cfg;
86 * Task that restarts the gnunet-helper-nat-server process after a crash
87 * after a certain delay.
89 * @param cls a `struct HelperContext`
92 restart_nat_server (void *cls);
96 * Try again starting the helper later
98 * @param h context of the helper
101 try_again (struct HelperContext *h)
103 GNUNET_assert (NULL == h->server_read_task);
104 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
105 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
112 * We have been notified that gnunet-helper-nat-server has written
113 * something to stdout. Handle the output, then reschedule this
114 * function to be called again once more is available.
116 * @param cls the `struct HelperContext`
119 nat_server_read (void *cls)
121 struct HelperContext *h = cls;
125 const char *port_start;
126 struct sockaddr_in sin_addr;
128 h->server_read_task = NULL;
129 memset (mybuf, 0, sizeof (mybuf));
131 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Finished reading from server stdout with code: %d\n",
137 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
138 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
139 GNUNET_OS_process_wait (h->server_proc);
140 GNUNET_OS_process_destroy (h->server_proc);
141 h->server_proc = NULL;
142 GNUNET_DISK_pipe_close (h->server_stdout);
143 h->server_stdout = NULL;
144 h->server_stdout_handle = NULL;
150 for (size_t i = 0; i < sizeof (mybuf); i++)
152 if (mybuf[i] == '\n')
157 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
160 port_start = &mybuf[i + 1];
164 /* construct socket address of sender */
165 memset (&sin_addr, 0, sizeof (sin_addr));
166 sin_addr.sin_family = AF_INET;
167 #if HAVE_SOCKADDR_IN_SIN_LEN
168 sin_addr.sin_len = sizeof (sin_addr);
170 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
171 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
173 /* should we restart gnunet-helper-nat-server? */
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
176 "gnunet-helper-nat-server generated malformed address `%s'\n"),
178 h->server_read_task =
179 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
180 h->server_stdout_handle,
185 sin_addr.sin_port = htons ((uint16_t) port);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "gnunet-helper-nat-server read: %s:%d\n",
190 h->cb (h->cb_cls, &sin_addr);
191 h->server_read_task =
192 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
193 h->server_stdout_handle,
200 * Task that restarts the gnunet-helper-nat-server process after a crash
201 * after a certain delay.
203 * @param cls a `struct HelperContext`
206 restart_nat_server (void *cls)
208 struct HelperContext *h = cls;
210 char ia[INET_ADDRSTRLEN];
212 h->server_read_task = NULL;
213 GNUNET_assert (NULL !=
214 inet_ntop (AF_INET, &h->internal_address, ia, sizeof (ia)));
215 /* Start the server process */
216 binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server");
217 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia))
219 /* move instantly to max delay, as this is unlikely to be fixed */
220 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
221 GNUNET_free (binary);
226 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
227 if (NULL == h->server_stdout)
229 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
230 GNUNET_free (binary);
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Starting `%s' at `%s'\n",
236 "gnunet-helper-nat-server",
238 h->server_proc = GNUNET_OS_start_process (GNUNET_NO,
244 "gnunet-helper-nat-server",
247 GNUNET_free (binary);
248 if (NULL == h->server_proc)
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
251 _ ("Failed to start %s\n"),
252 "gnunet-helper-nat-server");
253 GNUNET_DISK_pipe_close (h->server_stdout);
254 h->server_stdout = NULL;
258 /* Close the write end of the read pipe */
259 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
260 h->server_stdout_handle =
261 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
262 h->server_read_task =
263 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
264 h->server_stdout_handle,
271 * Start the gnunet-helper-nat-server and process incoming
274 * @param internal_address
275 * @param cb function to call if we receive a request
276 * @param cb_cls closure for @a cb
277 * @param cfg Handle to the GNUnet configuration
278 * @return NULL on error
280 struct HelperContext *
281 GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
282 GN_ReversalCallback cb,
284 const struct GNUNET_CONFIGURATION_Handle *cfg)
286 struct HelperContext *h;
288 h = GNUNET_new (struct HelperContext);
291 h->internal_address = *internal_address;
293 restart_nat_server (h);
294 if (NULL == h->server_stdout)
296 GN_stop_gnunet_nat_server_ (h);
304 * Start the gnunet-helper-nat-server and process incoming
307 * @param h helper context to stop
310 GN_stop_gnunet_nat_server_ (struct HelperContext *h)
312 if (NULL != h->server_read_task)
314 GNUNET_SCHEDULER_cancel (h->server_read_task);
315 h->server_read_task = NULL;
317 if (NULL != h->server_proc)
319 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
321 GNUNET_OS_process_wait (h->server_proc);
322 GNUNET_OS_process_destroy (h->server_proc);
323 h->server_proc = NULL;
324 GNUNET_DISK_pipe_close (h->server_stdout);
325 h->server_stdout = NULL;
326 h->server_stdout_handle = NULL;
328 if (NULL != h->server_stdout)
330 GNUNET_DISK_pipe_close (h->server_stdout);
331 h->server_stdout = NULL;
332 h->server_stdout_handle = NULL;
339 * We want to connect to a peer that is behind NAT. Run the
340 * gnunet-helper-nat-client to send dummy ICMP responses to cause
341 * that peer to connect to us (connection reversal).
343 * @param internal_address out internal address to use
344 * @param internal_port port to use
345 * @param remote_v4 the address of the peer (IPv4-only)
346 * @param cfg handle to the GNUnet configuration
347 * @return #GNUNET_SYSERR on error,
348 * #GNUNET_OK otherwise
351 GN_request_connection_reversal (const struct in_addr *internal_address,
352 uint16_t internal_port,
353 const struct in_addr *remote_v4,
354 const struct GNUNET_CONFIGURATION_Handle *cfg)
356 char intv4[INET_ADDRSTRLEN];
357 char remv4[INET_ADDRSTRLEN];
358 char port_as_string[6];
359 struct GNUNET_OS_Process *proc;
362 if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
365 return GNUNET_SYSERR;
367 if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
370 return GNUNET_SYSERR;
372 GNUNET_snprintf (port_as_string,
373 sizeof (port_as_string),
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Running gnunet-helper-nat-client %s %s %u\n",
381 binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client");
382 proc = GNUNET_OS_start_process (GNUNET_NO,
388 "gnunet-helper-nat-client",
393 GNUNET_free (binary);
395 return GNUNET_SYSERR;
396 /* we know that the gnunet-helper-nat-client will terminate virtually
398 GNUNET_OS_process_wait (proc);
399 GNUNET_OS_process_destroy (proc);
404 /* end of gnunet-service-nat_helper.c */