e91f63beb3f39e6bd15980ced7872af26d018a80
[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   h->server_stdout
225     = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
226                         GNUNET_NO, GNUNET_YES);
227   if (NULL == h->server_stdout)
228   {
229     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
230                          "pipe");
231     try_again (h);
232     return;
233   }
234   GNUNET_assert (NULL !=
235                  inet_ntop (AF_INET,
236                             &h->internal_address,
237                             ia,
238                             sizeof (ia)));
239   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240               "Starting `%s' at `%s'\n",
241               "gnunet-helper-nat-server",
242               ia);
243   /* Start the server process */
244   binary
245     = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
246   h->server_proc
247     = GNUNET_OS_start_process (GNUNET_NO,
248                                0,
249                                NULL,
250                                h->server_stdout,
251                                NULL,
252                                binary,
253                                "gnunet-helper-nat-server",
254                                ia,
255                                NULL);
256   GNUNET_free (binary);
257   if (NULL == h->server_proc)
258   {
259     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
260                 _("Failed to start %s\n"),
261                 "gnunet-helper-nat-server");
262     GNUNET_DISK_pipe_close (h->server_stdout);
263     h->server_stdout = NULL;
264     try_again (h);
265     return;
266   }
267   /* Close the write end of the read pipe */
268   GNUNET_DISK_pipe_close_end (h->server_stdout,
269                               GNUNET_DISK_PIPE_END_WRITE);
270   h->server_stdout_handle
271     = GNUNET_DISK_pipe_handle (h->server_stdout,
272                                GNUNET_DISK_PIPE_END_READ);
273   h->server_read_task
274     = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
275                                       h->server_stdout_handle,
276                                       &nat_server_read,
277                                       h);
278 }
279
280
281 /**
282  * Start the gnunet-helper-nat-server and process incoming
283  * requests.
284  *
285  * @param internal_address
286  * @param cb function to call if we receive a request
287  * @param cb_cls closure for @a cb
288  * @return NULL on error
289  */
290 struct HelperContext *
291 GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
292                              GN_ReversalCallback cb,
293                              void *cb_cls)
294 {
295   struct HelperContext *h;
296
297   h = GNUNET_new (struct HelperContext);
298   h->cb = cb;
299   h->cb_cls = cb_cls;
300   h->internal_address = *internal_address;
301   restart_nat_server (h);
302   if (NULL == h->server_stdout)
303   {
304     GN_stop_gnunet_nat_server_ (h);
305     return NULL;
306   }
307   return h;
308 }
309
310
311 /**
312  * Start the gnunet-helper-nat-server and process incoming
313  * requests.
314  *
315  * @param h helper context to stop
316  */
317 void
318 GN_stop_gnunet_nat_server_ (struct HelperContext *h)
319 {
320   if (NULL != h->server_read_task)
321   {
322     GNUNET_SCHEDULER_cancel (h->server_read_task);
323     h->server_read_task = NULL;
324   }
325   if (NULL != h->server_proc)
326   {
327     if (0 != GNUNET_OS_process_kill (h->server_proc,
328                                      GNUNET_TERM_SIG))
329       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
330                            "kill");
331     GNUNET_OS_process_wait (h->server_proc);
332     GNUNET_OS_process_destroy (h->server_proc);
333     h->server_proc = NULL;
334     GNUNET_DISK_pipe_close (h->server_stdout);
335     h->server_stdout = NULL;
336     h->server_stdout_handle = NULL;
337   }
338   if (NULL != h->server_stdout)
339   {
340     GNUNET_DISK_pipe_close (h->server_stdout);
341     h->server_stdout = NULL;
342     h->server_stdout_handle = NULL;
343   }
344   GNUNET_free (h);
345 }
346
347
348 /**
349  * We want to connect to a peer that is behind NAT.  Run the
350  * gnunet-helper-nat-client to send dummy ICMP responses to cause
351  * that peer to connect to us (connection reversal).
352  *
353  * @param internal_address out internal address to use
354  * @param internal_port port to use
355  * @param remote_v4 the address of the peer (IPv4-only)
356  * @return #GNUNET_SYSERR on error,
357  *         #GNUNET_OK otherwise
358  */
359 int
360 GN_request_connection_reversal (const struct in_addr *internal_address,
361                                 uint16_t internal_port,
362                                 const struct in_addr *remote_v4)
363 {
364   char intv4[INET_ADDRSTRLEN];
365   char remv4[INET_ADDRSTRLEN];
366   char port_as_string[6];
367   struct GNUNET_OS_Process *proc;
368   char *binary;
369
370   if (NULL == inet_ntop (AF_INET,
371                          internal_address,
372                          intv4,
373                          INET_ADDRSTRLEN))
374   {
375     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
376                          "inet_ntop");
377     return GNUNET_SYSERR;
378   }
379   if (NULL == inet_ntop (AF_INET,
380                          remote_v4,
381                          remv4,
382                          INET_ADDRSTRLEN))
383   {
384     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
385                          "inet_ntop");
386     return GNUNET_SYSERR;
387   }
388   GNUNET_snprintf (port_as_string,
389                    sizeof (port_as_string),
390                    "%d",
391                    internal_port);
392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393               "Running gnunet-helper-nat-client %s %s %u\n",
394               intv4,
395               remv4,
396               internal_port);
397   binary
398     = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
399   proc
400     = GNUNET_OS_start_process (GNUNET_NO,
401                                0,
402                                NULL,
403                                NULL,
404                                NULL,
405                                binary,
406                                "gnunet-helper-nat-client",
407                                intv4,
408                                remv4,
409                                port_as_string,
410                                NULL);
411   GNUNET_free (binary);
412   if (NULL == proc)
413     return GNUNET_SYSERR;
414   /* we know that the gnunet-helper-nat-client will terminate virtually
415    * instantly */
416   GNUNET_OS_process_wait (proc);
417   GNUNET_OS_process_destroy (proc);
418   return GNUNET_OK;
419 }
420
421
422 /* end of gnunet-service-nat_helper.c */