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 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.
17 * @file nat/gnunet-service-nat_helper.c
18 * @brief runs the gnunet-helper-nat-server
19 * @author Milan Bouchet-Valat
20 * @author Christian Grothoff
23 #include "gnunet_util_lib.h"
24 #include "gnunet-service-nat_helper.h"
28 * Information we keep per NAT helper process.
34 * IP address we pass to the NAT helper.
36 struct in_addr internal_address;
39 * Function to call if we receive a reversal request.
41 GN_ReversalCallback cb;
49 * How long do we wait for restarting a crashed gnunet-helper-nat-server?
51 struct GNUNET_TIME_Relative server_retry_delay;
54 * ID of select gnunet-helper-nat-server stdout read task
56 struct GNUNET_SCHEDULER_Task *server_read_task;
59 * The process id of the server process (if behind NAT)
61 struct GNUNET_OS_Process *server_proc;
64 * stdout pipe handle for the gnunet-helper-nat-server process
66 struct GNUNET_DISK_PipeHandle *server_stdout;
69 * stdout file handle (for reading) for the gnunet-helper-nat-server process
71 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
76 * Task that restarts the gnunet-helper-nat-server process after a crash
77 * after a certain delay.
79 * @param cls a `struct HelperContext`
82 restart_nat_server (void *cls);
86 * Try again starting the helper later
88 * @param h context of the helper
91 try_again (struct HelperContext *h)
93 GNUNET_assert (NULL == h->server_read_task);
95 = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
97 = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
104 * We have been notified that gnunet-helper-nat-server has written
105 * something to stdout. Handle the output, then reschedule this
106 * function to be called again once more is available.
108 * @param cls the `struct HelperContext`
111 nat_server_read (void *cls)
113 struct HelperContext *h = cls;
117 const char *port_start;
118 struct sockaddr_in sin_addr;
120 h->server_read_task = NULL;
125 = GNUNET_DISK_file_read (h->server_stdout_handle,
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Finished reading from server stdout with code: %d\n",
133 if (0 != GNUNET_OS_process_kill (h->server_proc,
135 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
138 GNUNET_OS_process_wait (h->server_proc);
139 GNUNET_OS_process_destroy (h->server_proc);
140 h->server_proc = NULL;
141 GNUNET_DISK_pipe_close (h->server_stdout);
142 h->server_stdout = NULL;
143 h->server_stdout_handle = NULL;
149 for (size_t i = 0; i < sizeof (mybuf); i++)
151 if (mybuf[i] == '\n')
156 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
159 port_start = &mybuf[i + 1];
163 /* construct socket address of sender */
167 sin_addr.sin_family = AF_INET;
168 #if HAVE_SOCKADDR_IN_SIN_LEN
169 sin_addr.sin_len = sizeof (sin_addr);
171 if ( (NULL == port_start) ||
172 (1 != SSCANF (port_start,
175 (-1 == inet_pton (AF_INET,
177 &sin_addr.sin_addr)))
179 /* should we restart gnunet-helper-nat-server? */
180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
181 _("gnunet-helper-nat-server generated malformed address `%s'\n"),
184 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
185 h->server_stdout_handle,
190 sin_addr.sin_port = htons ((uint16_t) port);
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "gnunet-helper-nat-server read: %s:%d\n",
198 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
199 h->server_stdout_handle,
206 * Task that restarts the gnunet-helper-nat-server process after a crash
207 * after a certain delay.
209 * @param cls a `struct HelperContext`
212 restart_nat_server (void *cls)
214 struct HelperContext *h = cls;
216 char ia[INET_ADDRSTRLEN];
218 h->server_read_task = NULL;
219 GNUNET_assert (NULL !=
221 &h->internal_address,
224 /* Start the server process */
226 = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
228 GNUNET_OS_check_helper_binary (binary,
232 /* move instantly to max delay, as this is unlikely to be fixed */
233 h->server_retry_delay
234 = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
235 GNUNET_free (binary);
240 = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
241 GNUNET_NO, GNUNET_YES);
242 if (NULL == h->server_stdout)
244 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
246 GNUNET_free (binary);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Starting `%s' at `%s'\n",
252 "gnunet-helper-nat-server",
255 = GNUNET_OS_start_process (GNUNET_NO,
261 "gnunet-helper-nat-server",
264 GNUNET_free (binary);
265 if (NULL == h->server_proc)
267 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
268 _("Failed to start %s\n"),
269 "gnunet-helper-nat-server");
270 GNUNET_DISK_pipe_close (h->server_stdout);
271 h->server_stdout = NULL;
275 /* Close the write end of the read pipe */
276 GNUNET_DISK_pipe_close_end (h->server_stdout,
277 GNUNET_DISK_PIPE_END_WRITE);
278 h->server_stdout_handle
279 = GNUNET_DISK_pipe_handle (h->server_stdout,
280 GNUNET_DISK_PIPE_END_READ);
282 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
283 h->server_stdout_handle,
290 * Start the gnunet-helper-nat-server and process incoming
293 * @param internal_address
294 * @param cb function to call if we receive a request
295 * @param cb_cls closure for @a cb
296 * @return NULL on error
298 struct HelperContext *
299 GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
300 GN_ReversalCallback cb,
303 struct HelperContext *h;
305 h = GNUNET_new (struct HelperContext);
308 h->internal_address = *internal_address;
309 restart_nat_server (h);
310 if (NULL == h->server_stdout)
312 GN_stop_gnunet_nat_server_ (h);
320 * Start the gnunet-helper-nat-server and process incoming
323 * @param h helper context to stop
326 GN_stop_gnunet_nat_server_ (struct HelperContext *h)
328 if (NULL != h->server_read_task)
330 GNUNET_SCHEDULER_cancel (h->server_read_task);
331 h->server_read_task = NULL;
333 if (NULL != h->server_proc)
335 if (0 != GNUNET_OS_process_kill (h->server_proc,
337 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
339 GNUNET_OS_process_wait (h->server_proc);
340 GNUNET_OS_process_destroy (h->server_proc);
341 h->server_proc = NULL;
342 GNUNET_DISK_pipe_close (h->server_stdout);
343 h->server_stdout = NULL;
344 h->server_stdout_handle = NULL;
346 if (NULL != h->server_stdout)
348 GNUNET_DISK_pipe_close (h->server_stdout);
349 h->server_stdout = NULL;
350 h->server_stdout_handle = NULL;
357 * We want to connect to a peer that is behind NAT. Run the
358 * gnunet-helper-nat-client to send dummy ICMP responses to cause
359 * that peer to connect to us (connection reversal).
361 * @param internal_address out internal address to use
362 * @param internal_port port to use
363 * @param remote_v4 the address of the peer (IPv4-only)
364 * @return #GNUNET_SYSERR on error,
365 * #GNUNET_OK otherwise
368 GN_request_connection_reversal (const struct in_addr *internal_address,
369 uint16_t internal_port,
370 const struct in_addr *remote_v4)
372 char intv4[INET_ADDRSTRLEN];
373 char remv4[INET_ADDRSTRLEN];
374 char port_as_string[6];
375 struct GNUNET_OS_Process *proc;
378 if (NULL == inet_ntop (AF_INET,
383 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
385 return GNUNET_SYSERR;
387 if (NULL == inet_ntop (AF_INET,
392 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
394 return GNUNET_SYSERR;
396 GNUNET_snprintf (port_as_string,
397 sizeof (port_as_string),
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Running gnunet-helper-nat-client %s %s %u\n",
406 = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
408 = GNUNET_OS_start_process (GNUNET_NO,
414 "gnunet-helper-nat-client",
419 GNUNET_free (binary);
421 return GNUNET_SYSERR;
422 /* we know that the gnunet-helper-nat-client will terminate virtually
424 GNUNET_OS_process_wait (proc);
425 GNUNET_OS_process_destroy (proc);
430 /* end of gnunet-service-nat_helper.c */