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/>.
20 * @file nat/gnunet-service-nat_helper.c
21 * @brief runs the gnunet-helper-nat-server
22 * @author Milan Bouchet-Valat
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet-service-nat_helper.h"
31 * Information we keep per NAT helper process.
37 * IP address we pass to the NAT helper.
39 struct in_addr internal_address;
42 * Function to call if we receive a reversal request.
44 GN_ReversalCallback cb;
52 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
54 struct GNUNET_TIME_Relative server_retry_delay;
57 * ID of select gnunet-helper-nat-server stdout read task
59 struct GNUNET_SCHEDULER_Task *server_read_task;
62 * The process id of the server process (if behind NAT)
64 struct GNUNET_OS_Process *server_proc;
67 * stdout pipe handle for the gnunet-helper-nat-server process
69 struct GNUNET_DISK_PipeHandle *server_stdout;
72 * stdout file handle (for reading) for the gnunet-helper-nat-server process
74 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
79 * Task that restarts the gnunet-helper-nat-server process after a crash
80 * after a certain delay.
82 * @param cls a `struct HelperContext`
85 restart_nat_server (void *cls);
89 * Try again starting the helper later
91 * @param h context of the helper
94 try_again (struct HelperContext *h)
96 GNUNET_assert (NULL == h->server_read_task);
98 = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
100 = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
107 * We have been notified that gnunet-helper-nat-server has written
108 * something to stdout. Handle the output, then reschedule this
109 * function to be called again once more is available.
111 * @param cls the `struct HelperContext`
114 nat_server_read (void *cls)
116 struct HelperContext *h = cls;
120 const char *port_start;
121 struct sockaddr_in sin_addr;
123 h->server_read_task = NULL;
128 = GNUNET_DISK_file_read (h->server_stdout_handle,
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134 "Finished reading from server stdout with code: %d\n",
136 if (0 != GNUNET_OS_process_kill (h->server_proc,
138 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
141 GNUNET_OS_process_wait (h->server_proc);
142 GNUNET_OS_process_destroy (h->server_proc);
143 h->server_proc = NULL;
144 GNUNET_DISK_pipe_close (h->server_stdout);
145 h->server_stdout = NULL;
146 h->server_stdout_handle = NULL;
152 for (size_t i = 0; i < sizeof (mybuf); i++)
154 if (mybuf[i] == '\n')
159 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
162 port_start = &mybuf[i + 1];
166 /* construct socket address of sender */
170 sin_addr.sin_family = AF_INET;
171 #if HAVE_SOCKADDR_IN_SIN_LEN
172 sin_addr.sin_len = sizeof (sin_addr);
174 if ( (NULL == port_start) ||
175 (1 != SSCANF (port_start,
178 (-1 == inet_pton (AF_INET,
180 &sin_addr.sin_addr)))
182 /* should we restart gnunet-helper-nat-server? */
183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
184 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
187 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
188 h->server_stdout_handle,
193 sin_addr.sin_port = htons ((uint16_t) port);
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "gnunet-helper-nat-server read: %s:%d\n",
201 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
202 h->server_stdout_handle,
209 * Task that restarts the gnunet-helper-nat-server process after a crash
210 * after a certain delay.
212 * @param cls a `struct HelperContext`
215 restart_nat_server (void *cls)
217 struct HelperContext *h = cls;
219 char ia[INET_ADDRSTRLEN];
221 h->server_read_task = NULL;
222 GNUNET_assert (NULL !=
224 &h->internal_address,
227 /* Start the server process */
229 = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
231 GNUNET_OS_check_helper_binary (binary,
235 /* move instantly to max delay, as this is unlikely to be fixed */
236 h->server_retry_delay
237 = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
238 GNUNET_free (binary);
243 = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
244 GNUNET_NO, GNUNET_YES);
245 if (NULL == h->server_stdout)
247 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
249 GNUNET_free (binary);
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Starting `%s' at `%s'\n",
255 "gnunet-helper-nat-server",
258 = GNUNET_OS_start_process (GNUNET_NO,
264 "gnunet-helper-nat-server",
267 GNUNET_free (binary);
268 if (NULL == h->server_proc)
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
271 _("Failed to start %s\n"),
272 "gnunet-helper-nat-server");
273 GNUNET_DISK_pipe_close (h->server_stdout);
274 h->server_stdout = NULL;
278 /* Close the write end of the read pipe */
279 GNUNET_DISK_pipe_close_end (h->server_stdout,
280 GNUNET_DISK_PIPE_END_WRITE);
281 h->server_stdout_handle
282 = GNUNET_DISK_pipe_handle (h->server_stdout,
283 GNUNET_DISK_PIPE_END_READ);
285 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
286 h->server_stdout_handle,
293 * Start the gnunet-helper-nat-server and process incoming
296 * @param internal_address
297 * @param cb function to call if we receive a request
298 * @param cb_cls closure for @a cb
299 * @return NULL on error
301 struct HelperContext *
302 GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
303 GN_ReversalCallback cb,
306 struct HelperContext *h;
308 h = GNUNET_new (struct HelperContext);
311 h->internal_address = *internal_address;
312 restart_nat_server (h);
313 if (NULL == h->server_stdout)
315 GN_stop_gnunet_nat_server_ (h);
323 * Start the gnunet-helper-nat-server and process incoming
326 * @param h helper context to stop
329 GN_stop_gnunet_nat_server_ (struct HelperContext *h)
331 if (NULL != h->server_read_task)
333 GNUNET_SCHEDULER_cancel (h->server_read_task);
334 h->server_read_task = NULL;
336 if (NULL != h->server_proc)
338 if (0 != GNUNET_OS_process_kill (h->server_proc,
340 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
342 GNUNET_OS_process_wait (h->server_proc);
343 GNUNET_OS_process_destroy (h->server_proc);
344 h->server_proc = NULL;
345 GNUNET_DISK_pipe_close (h->server_stdout);
346 h->server_stdout = NULL;
347 h->server_stdout_handle = NULL;
349 if (NULL != h->server_stdout)
351 GNUNET_DISK_pipe_close (h->server_stdout);
352 h->server_stdout = NULL;
353 h->server_stdout_handle = NULL;
360 * We want to connect to a peer that is behind NAT. Run the
361 * gnunet-helper-nat-client to send dummy ICMP responses to cause
362 * that peer to connect to us (connection reversal).
364 * @param internal_address out internal address to use
365 * @param internal_port port to use
366 * @param remote_v4 the address of the peer (IPv4-only)
367 * @return #GNUNET_SYSERR on error,
368 * #GNUNET_OK otherwise
371 GN_request_connection_reversal (const struct in_addr *internal_address,
372 uint16_t internal_port,
373 const struct in_addr *remote_v4)
375 char intv4[INET_ADDRSTRLEN];
376 char remv4[INET_ADDRSTRLEN];
377 char port_as_string[6];
378 struct GNUNET_OS_Process *proc;
381 if (NULL == inet_ntop (AF_INET,
386 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
388 return GNUNET_SYSERR;
390 if (NULL == inet_ntop (AF_INET,
395 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
397 return GNUNET_SYSERR;
399 GNUNET_snprintf (port_as_string,
400 sizeof (port_as_string),
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Running gnunet-helper-nat-client %s %s %u\n",
409 = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
411 = GNUNET_OS_start_process (GNUNET_NO,
417 "gnunet-helper-nat-client",
422 GNUNET_free (binary);
424 return GNUNET_SYSERR;
425 /* we know that the gnunet-helper-nat-client will terminate virtually
427 GNUNET_OS_process_wait (proc);
428 GNUNET_OS_process_destroy (proc);
433 /* end of gnunet-service-nat_helper.c */