-remove debug message
[oweals/gnunet.git] / src / nat-auto / gnunet-service-nat-auto.c
1 /*
2    This file is part of GNUnet.
3    Copyright (C) 2016, 2017 GNUnet e.V.
4
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.
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    Affero General Public License for more details.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file nat-auto/gnunet-service-nat-auto.c
23  * @brief NAT autoconfiguration service
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - merge client handle and autoconfig context
28  * - implement "more" autoconfig:
29  *   + re-work gnunet-nat-server & integrate!
30  *   + integrate "legacy" code
31  *   + test manually punched NAT (how?)
32  */
33 #include "platform.h"
34 #include <math.h>
35 #include "gnunet_util_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_nat_service.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet_resolver_service.h"
41 #include "nat-auto.h"
42 #include <gcrypt.h>
43
44
45 /**
46  * How long do we wait until we forcefully terminate autoconfiguration?
47  */
48 #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply ( \
49     GNUNET_TIME_UNIT_SECONDS, 5)
50
51
52 /**
53  * Internal data structure we track for each of our clients.
54  */
55 struct ClientHandle
56 {
57   /**
58    * Kept in a DLL.
59    */
60   struct ClientHandle *next;
61
62   /**
63    * Kept in a DLL.
64    */
65   struct ClientHandle *prev;
66
67   /**
68    * Underlying handle for this client with the service.
69    */
70   struct GNUNET_SERVICE_Client *client;
71
72   /**
73    * Message queue for communicating with the client.
74    */
75   struct GNUNET_MQ_Handle *mq;
76 };
77
78
79 /**
80  * Context for autoconfiguration operations.
81  */
82 struct AutoconfigContext
83 {
84   /**
85    * Kept in a DLL.
86    */
87   struct AutoconfigContext *prev;
88
89   /**
90    * Kept in a DLL.
91    */
92   struct AutoconfigContext *next;
93
94   /**
95    * Which client asked the question.
96    */
97   struct ClientHandle *ch;
98
99   /**
100    * Configuration we are creating.
101    */
102   struct GNUNET_CONFIGURATION_Handle *c;
103
104   /**
105    * Original configuration (for diffing).
106    */
107   struct GNUNET_CONFIGURATION_Handle *orig;
108
109   /**
110    * Timeout task to force termination.
111    */
112   struct GNUNET_SCHEDULER_Task *timeout_task;
113
114   /**
115    * #GNUNET_YES if upnpc should be used,
116    * #GNUNET_NO if upnpc should not be used,
117    * #GNUNET_SYSERR if we should simply not change the option.
118    */
119   int enable_upnpc;
120
121   /**
122    * Status code to return to the client.
123    */
124   enum GNUNET_NAT_StatusCode status_code;
125
126   /**
127    * NAT type to return to the client.
128    */
129   enum GNUNET_NAT_Type type;
130 };
131
132
133 /**
134  * Head of client DLL.
135  */
136 static struct ClientHandle *ch_head;
137
138 /**
139  * Tail of client DLL.
140  */
141 static struct ClientHandle *ch_tail;
142
143 /**
144  * DLL of our autoconfiguration operations.
145  */
146 static struct AutoconfigContext *ac_head;
147
148 /**
149  * DLL of our autoconfiguration operations.
150  */
151 static struct AutoconfigContext *ac_tail;
152
153 /**
154  * Handle to our current configuration.
155  */
156 static const struct GNUNET_CONFIGURATION_Handle *cfg;
157
158 /**
159  * Handle to the statistics service.
160  */
161 static struct GNUNET_STATISTICS_Handle *stats;
162
163
164 /**
165  * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
166  * from client.
167  *
168  * @param cls client who sent the message
169  * @param message the message received
170  * @return #GNUNET_OK if message is well-formed
171  */
172 static int
173 check_autoconfig_request (void *cls,
174                           const struct
175                           GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
176 {
177   return GNUNET_OK;  /* checked later */
178 }
179
180
181 /**
182  * Stop all pending activities with respect to the @a ac
183  *
184  * @param ac autoconfiguration to terminate activities for
185  */
186 static void
187 terminate_ac_activities (struct AutoconfigContext *ac)
188 {
189   if (NULL != ac->timeout_task)
190   {
191     GNUNET_SCHEDULER_cancel (ac->timeout_task);
192     ac->timeout_task = NULL;
193   }
194 }
195
196
197 /**
198  * Finish handling the autoconfiguration request and send
199  * the response to the client.
200  *
201  * @param cls the `struct AutoconfigContext` to conclude
202  */
203 static void
204 conclude_autoconfig_request (void *cls)
205 {
206   struct AutoconfigContext *ac = cls;
207   struct ClientHandle *ch = ac->ch;
208   struct GNUNET_NAT_AUTO_AutoconfigResultMessage *arm;
209   struct GNUNET_MQ_Envelope *env;
210   size_t c_size;
211   char *buf;
212   struct GNUNET_CONFIGURATION_Handle *diff;
213
214   ac->timeout_task = NULL;
215   terminate_ac_activities (ac);
216
217   /* Send back response */
218   diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
219                                         ac->c);
220   buf = GNUNET_CONFIGURATION_serialize (diff,
221                                         &c_size);
222   GNUNET_CONFIGURATION_destroy (diff);
223   env = GNUNET_MQ_msg_extra (arm,
224                              c_size,
225                              GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
226   arm->status_code = htonl ((uint32_t) ac->status_code);
227   arm->type = htonl ((uint32_t) ac->type);
228   GNUNET_memcpy (&arm[1],
229                  buf,
230                  c_size);
231   GNUNET_free (buf);
232   GNUNET_MQ_send (ch->mq,
233                   env);
234
235   /* clean up */
236   GNUNET_CONFIGURATION_destroy (ac->orig);
237   GNUNET_CONFIGURATION_destroy (ac->c);
238   GNUNET_CONTAINER_DLL_remove (ac_head,
239                                ac_tail,
240                                ac);
241   GNUNET_free (ac);
242   GNUNET_SERVICE_client_continue (ch->client);
243 }
244
245
246 /**
247  * Check if all autoconfiguration operations have concluded,
248  * and if they have, send the result back to the client.
249  *
250  * @param ac autoconfiguation context to check
251  */
252 static void
253 check_autoconfig_finished (struct AutoconfigContext *ac)
254 {
255   GNUNET_SCHEDULER_cancel (ac->timeout_task);
256   ac->timeout_task
257     = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
258                                 ac);
259 }
260
261
262 /**
263  * Update ENABLE_UPNPC configuration option.
264  *
265  * @param ac autoconfiguration to update
266  */
267 static void
268 update_enable_upnpc_option (struct AutoconfigContext *ac)
269 {
270   switch (ac->enable_upnpc)
271   {
272   case GNUNET_YES:
273     GNUNET_CONFIGURATION_set_value_string (ac->c,
274                                            "NAT",
275                                            "ENABLE_UPNP",
276                                            "YES");
277     break;
278
279   case GNUNET_NO:
280     GNUNET_CONFIGURATION_set_value_string (ac->c,
281                                            "NAT",
282                                            "ENABLE_UPNP",
283                                            "NO");
284     break;
285
286   case GNUNET_SYSERR:
287     /* We are unsure, do not change option */
288     break;
289   }
290 }
291
292
293 /**
294  * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
295  * client.
296  *
297  * @param cls client who sent the message
298  * @param message the message received
299  */
300 static void
301 handle_autoconfig_request (void *cls,
302                            const struct
303                            GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
304 {
305   struct ClientHandle *ch = cls;
306   size_t left = ntohs (message->header.size) - sizeof(*message);
307   struct AutoconfigContext *ac;
308
309   ac = GNUNET_new (struct AutoconfigContext);
310   ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
311   ac->ch = ch;
312   ac->c = GNUNET_CONFIGURATION_create ();
313   if (GNUNET_OK !=
314       GNUNET_CONFIGURATION_deserialize (ac->c,
315                                         (const char *) &message[1],
316                                         left,
317                                         NULL))
318   {
319     GNUNET_break (0);
320     GNUNET_SERVICE_client_drop (ch->client);
321     GNUNET_CONFIGURATION_destroy (ac->c);
322     GNUNET_free (ac);
323     return;
324   }
325   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326               "Received REQUEST_AUTO_CONFIG message from client\n");
327
328   GNUNET_CONTAINER_DLL_insert (ac_head,
329                                ac_tail,
330                                ac);
331   ac->orig
332     = GNUNET_CONFIGURATION_dup (ac->c);
333   ac->timeout_task
334     = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
335                                     &conclude_autoconfig_request,
336                                     ac);
337   ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
338
339   /* Probe for upnpc */
340   if (GNUNET_SYSERR ==
341       GNUNET_OS_check_helper_binary ("upnpc",
342                                      GNUNET_NO,
343                                      NULL))
344   {
345     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
346                 _ ("UPnP client `upnpc` command not found, disabling UPnP\n"));
347     ac->enable_upnpc = GNUNET_NO;
348   }
349   else
350   {
351     /* We might at some point be behind NAT, try upnpc */
352     ac->enable_upnpc = GNUNET_YES;
353   }
354   update_enable_upnpc_option (ac);
355
356   /* Finally, check if we are already done */
357   check_autoconfig_finished (ac);
358 }
359
360
361 /**
362  * Task run during shutdown.
363  *
364  * @param cls unused
365  */
366 static void
367 shutdown_task (void *cls)
368 {
369   struct AutoconfigContext *ac;
370
371   while (NULL != (ac = ac_head))
372   {
373     GNUNET_CONTAINER_DLL_remove (ac_head,
374                                  ac_tail,
375                                  ac);
376     terminate_ac_activities (ac);
377     GNUNET_free (ac);
378   }
379   if (NULL != stats)
380   {
381     GNUNET_STATISTICS_destroy (stats,
382                                GNUNET_NO);
383     stats = NULL;
384   }
385 }
386
387
388 /**
389  * Setup NAT service.
390  *
391  * @param cls closure
392  * @param c configuration to use
393  * @param service the initialized service
394  */
395 static void
396 run (void *cls,
397      const struct GNUNET_CONFIGURATION_Handle *c,
398      struct GNUNET_SERVICE_Handle *service)
399 {
400   cfg = c;
401   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
402                                  NULL);
403   stats = GNUNET_STATISTICS_create ("nat-auto",
404                                     cfg);
405 }
406
407
408 /**
409  * Callback called when a client connects to the service.
410  *
411  * @param cls closure for the service
412  * @param c the new client that connected to the service
413  * @param mq the message queue used to send messages to the client
414  * @return a `struct ClientHandle`
415  */
416 static void *
417 client_connect_cb (void *cls,
418                    struct GNUNET_SERVICE_Client *c,
419                    struct GNUNET_MQ_Handle *mq)
420 {
421   struct ClientHandle *ch;
422
423   ch = GNUNET_new (struct ClientHandle);
424   ch->mq = mq;
425   ch->client = c;
426   GNUNET_CONTAINER_DLL_insert (ch_head,
427                                ch_tail,
428                                ch);
429   return ch;
430 }
431
432
433 /**
434  * Callback called when a client disconnected from the service
435  *
436  * @param cls closure for the service
437  * @param c the client that disconnected
438  * @param internal_cls a `struct ClientHandle *`
439  */
440 static void
441 client_disconnect_cb (void *cls,
442                       struct GNUNET_SERVICE_Client *c,
443                       void *internal_cls)
444 {
445   struct ClientHandle *ch = internal_cls;
446
447   GNUNET_CONTAINER_DLL_remove (ch_head,
448                                ch_tail,
449                                ch);
450   GNUNET_free (ch);
451 }
452
453
454 /**
455  * Define "main" method using service macro.
456  */
457 GNUNET_SERVICE_MAIN
458   ("nat-auto",
459   GNUNET_SERVICE_OPTION_NONE,
460   &run,
461   &client_connect_cb,
462   &client_disconnect_cb,
463   NULL,
464   GNUNET_MQ_hd_var_size (autoconfig_request,
465                          GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
466                          struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
467                          NULL),
468   GNUNET_MQ_handler_end ());
469
470
471 #if defined(__linux__) && defined(__GLIBC__)
472 #include <malloc.h>
473
474 /**
475  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
476  */
477 void __attribute__ ((constructor))
478 GNUNET_ARM_memory_init ()
479 {
480   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
481   mallopt (M_TOP_PAD, 1 * 1024);
482   malloc_trim (0);
483 }
484
485
486 #endif
487
488 /* end of gnunet-service-nat.c */