doc: gnunet-c-tutorial: Add nodes.
[oweals/gnunet.git] / src / nat / gnunet-service-nat_helper.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
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
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-nat_helper.h"
30
31
32 /**
33  * Information we keep per NAT helper process.
34  */
35 struct HelperContext
36 {
37
38   /**
39    * IP address we pass to the NAT helper.
40    */
41   struct in_addr internal_address;
42
43   /**
44    * Function to call if we receive a reversal request.
45    */
46   GN_ReversalCallback cb;
47
48   /**
49    * Closure for @e cb.
50    */
51   void *cb_cls;
52
53   /**
54    * How long do we wait for restarting a crashed gnunet-helper-nat-server?
55    */
56   struct GNUNET_TIME_Relative server_retry_delay;
57
58   /**
59    * ID of select gnunet-helper-nat-server stdout read task
60    */
61   struct GNUNET_SCHEDULER_Task *server_read_task;
62
63   /**
64    * The process id of the server process (if behind NAT)
65    */
66   struct GNUNET_OS_Process *server_proc;
67
68   /**
69    * stdout pipe handle for the gnunet-helper-nat-server process
70    */
71   struct GNUNET_DISK_PipeHandle *server_stdout;
72
73   /**
74    * stdout file handle (for reading) for the gnunet-helper-nat-server process
75    */
76   const struct GNUNET_DISK_FileHandle *server_stdout_handle;
77 };
78
79
80 /**
81  * Task that restarts the gnunet-helper-nat-server process after a crash
82  * after a certain delay.
83  *
84  * @param cls a `struct HelperContext`
85  */
86 static void
87 restart_nat_server (void *cls);
88
89
90 /**
91  * Try again starting the helper later
92  *
93  * @param h context of the helper
94  */
95 static void
96 try_again (struct HelperContext *h)
97 {
98   GNUNET_assert (NULL == h->server_read_task);
99   h->server_retry_delay
100     = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
101   h->server_read_task
102     = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
103                                     &restart_nat_server,
104                                     h);
105 }
106
107
108 /**
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.
112  *
113  * @param cls the `struct HelperContext`
114  */
115 static void
116 nat_server_read (void *cls)
117 {
118   struct HelperContext *h = cls;
119   char mybuf[40];
120   ssize_t bytes;
121   int port;
122   const char *port_start;
123   struct sockaddr_in sin_addr;
124
125   h->server_read_task = NULL;
126   memset (mybuf,
127           0,
128           sizeof (mybuf));
129   bytes
130     = GNUNET_DISK_file_read (h->server_stdout_handle,
131                              mybuf,
132                              sizeof (mybuf));
133   if (bytes < 1)
134   {
135     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136                 "Finished reading from server stdout with code: %d\n",
137                 (int) bytes);
138     if (0 != GNUNET_OS_process_kill (h->server_proc,
139                                      GNUNET_TERM_SIG))
140       GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
141                                 "nat",
142                                 "kill");
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;
149     try_again (h);
150     return;
151   }
152
153   port_start = NULL;
154   for (size_t i = 0; i < sizeof (mybuf); i++)
155   {
156     if (mybuf[i] == '\n')
157     {
158       mybuf[i] = '\0';
159       break;
160     }
161     if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf)))
162     {
163       mybuf[i] = '\0';
164       port_start = &mybuf[i + 1];
165     }
166   }
167
168   /* construct socket address of sender */
169   memset (&sin_addr,
170           0,
171           sizeof (sin_addr));
172   sin_addr.sin_family = AF_INET;
173 #if HAVE_SOCKADDR_IN_SIN_LEN
174   sin_addr.sin_len = sizeof (sin_addr);
175 #endif
176   if ( (NULL == port_start) ||
177        (1 != SSCANF (port_start,
178                      "%d",
179                      &port)) ||
180        (-1 == inet_pton (AF_INET,
181                          mybuf,
182                          &sin_addr.sin_addr)))
183   {
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"),
187                 mybuf);
188     h->server_read_task
189       = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
190                                         h->server_stdout_handle,
191                                         &nat_server_read,
192                                         h);
193     return;
194   }
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",
198               mybuf,
199               port);
200   h->cb (h->cb_cls,
201          &sin_addr);
202   h->server_read_task
203     = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
204                                       h->server_stdout_handle,
205                                       &nat_server_read,
206                                       h);
207 }
208
209
210 /**
211  * Task that restarts the gnunet-helper-nat-server process after a crash
212  * after a certain delay.
213  *
214  * @param cls a `struct HelperContext`
215  */
216 static void
217 restart_nat_server (void *cls)
218 {
219   struct HelperContext *h = cls;
220   char *binary;
221   char ia[INET_ADDRSTRLEN];
222
223   h->server_read_task = NULL;
224   GNUNET_assert (NULL !=
225                  inet_ntop (AF_INET,
226                             &h->internal_address,
227                             ia,
228                             sizeof (ia)));
229   /* Start the server process */
230   binary
231     = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
232   if (GNUNET_YES !=
233       GNUNET_OS_check_helper_binary (binary,
234                                      GNUNET_YES,
235                                      ia))
236   {
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);
241     try_again (h);
242     return;
243   }
244   h->server_stdout
245     = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
246                         GNUNET_NO, GNUNET_YES);
247   if (NULL == h->server_stdout)
248   {
249     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
250                          "pipe");
251     GNUNET_free (binary);
252     try_again (h);
253     return;
254   }
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256               "Starting `%s' at `%s'\n",
257               "gnunet-helper-nat-server",
258               ia);
259   h->server_proc
260     = GNUNET_OS_start_process (GNUNET_NO,
261                                0,
262                                NULL,
263                                h->server_stdout,
264                                NULL,
265                                binary,
266                                "gnunet-helper-nat-server",
267                                ia,
268                                NULL);
269   GNUNET_free (binary);
270   if (NULL == h->server_proc)
271   {
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;
277     try_again (h);
278     return;
279   }
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);
286   h->server_read_task
287     = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
288                                       h->server_stdout_handle,
289                                       &nat_server_read,
290                                       h);
291 }
292
293
294 /**
295  * Start the gnunet-helper-nat-server and process incoming
296  * requests.
297  *
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
302  */
303 struct HelperContext *
304 GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
305                              GN_ReversalCallback cb,
306                              void *cb_cls)
307 {
308   struct HelperContext *h;
309
310   h = GNUNET_new (struct HelperContext);
311   h->cb = cb;
312   h->cb_cls = cb_cls;
313   h->internal_address = *internal_address;
314   restart_nat_server (h);
315   if (NULL == h->server_stdout)
316   {
317     GN_stop_gnunet_nat_server_ (h);
318     return NULL;
319   }
320   return h;
321 }
322
323
324 /**
325  * Start the gnunet-helper-nat-server and process incoming
326  * requests.
327  *
328  * @param h helper context to stop
329  */
330 void
331 GN_stop_gnunet_nat_server_ (struct HelperContext *h)
332 {
333   if (NULL != h->server_read_task)
334   {
335     GNUNET_SCHEDULER_cancel (h->server_read_task);
336     h->server_read_task = NULL;
337   }
338   if (NULL != h->server_proc)
339   {
340     if (0 != GNUNET_OS_process_kill (h->server_proc,
341                                      GNUNET_TERM_SIG))
342       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
343                            "kill");
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;
350   }
351   if (NULL != h->server_stdout)
352   {
353     GNUNET_DISK_pipe_close (h->server_stdout);
354     h->server_stdout = NULL;
355     h->server_stdout_handle = NULL;
356   }
357   GNUNET_free (h);
358 }
359
360
361 /**
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).
365  *
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
371  */
372 int
373 GN_request_connection_reversal (const struct in_addr *internal_address,
374                                 uint16_t internal_port,
375                                 const struct in_addr *remote_v4)
376 {
377   char intv4[INET_ADDRSTRLEN];
378   char remv4[INET_ADDRSTRLEN];
379   char port_as_string[6];
380   struct GNUNET_OS_Process *proc;
381   char *binary;
382
383   if (NULL == inet_ntop (AF_INET,
384                          internal_address,
385                          intv4,
386                          INET_ADDRSTRLEN))
387   {
388     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
389                          "inet_ntop");
390     return GNUNET_SYSERR;
391   }
392   if (NULL == inet_ntop (AF_INET,
393                          remote_v4,
394                          remv4,
395                          INET_ADDRSTRLEN))
396   {
397     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
398                          "inet_ntop");
399     return GNUNET_SYSERR;
400   }
401   GNUNET_snprintf (port_as_string,
402                    sizeof (port_as_string),
403                    "%d",
404                    internal_port);
405   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406               "Running gnunet-helper-nat-client %s %s %u\n",
407               intv4,
408               remv4,
409               internal_port);
410   binary
411     = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
412   proc
413     = GNUNET_OS_start_process (GNUNET_NO,
414                                0,
415                                NULL,
416                                NULL,
417                                NULL,
418                                binary,
419                                "gnunet-helper-nat-client",
420                                intv4,
421                                remv4,
422                                port_as_string,
423                                NULL);
424   GNUNET_free (binary);
425   if (NULL == proc)
426     return GNUNET_SYSERR;
427   /* we know that the gnunet-helper-nat-client will terminate virtually
428    * instantly */
429   GNUNET_OS_process_wait (proc);
430   GNUNET_OS_process_destroy (proc);
431   return GNUNET_OK;
432 }
433
434
435 /* end of gnunet-service-nat_helper.c */