2 This file is part of GNUnet.
3 Copyright (C) 2016, 2017 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file nat-auto/gnunet-service-nat-auto.c
23 * @brief NAT autoconfiguration service
24 * @author Christian Grothoff
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?)
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"
46 * How long do we wait until we forcefully terminate autoconfiguration?
48 #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply ( \
49 GNUNET_TIME_UNIT_SECONDS, 5)
53 * Internal data structure we track for each of our clients.
60 struct ClientHandle *next;
65 struct ClientHandle *prev;
68 * Underlying handle for this client with the service.
70 struct GNUNET_SERVICE_Client *client;
73 * Message queue for communicating with the client.
75 struct GNUNET_MQ_Handle *mq;
80 * Context for autoconfiguration operations.
82 struct AutoconfigContext
87 struct AutoconfigContext *prev;
92 struct AutoconfigContext *next;
95 * Which client asked the question.
97 struct ClientHandle *ch;
100 * Configuration we are creating.
102 struct GNUNET_CONFIGURATION_Handle *c;
105 * Original configuration (for diffing).
107 struct GNUNET_CONFIGURATION_Handle *orig;
110 * Timeout task to force termination.
112 struct GNUNET_SCHEDULER_Task *timeout_task;
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.
122 * Status code to return to the client.
124 enum GNUNET_NAT_StatusCode status_code;
127 * NAT type to return to the client.
129 enum GNUNET_NAT_Type type;
134 * Head of client DLL.
136 static struct ClientHandle *ch_head;
139 * Tail of client DLL.
141 static struct ClientHandle *ch_tail;
144 * DLL of our autoconfiguration operations.
146 static struct AutoconfigContext *ac_head;
149 * DLL of our autoconfiguration operations.
151 static struct AutoconfigContext *ac_tail;
154 * Handle to our current configuration.
156 static const struct GNUNET_CONFIGURATION_Handle *cfg;
159 * Handle to the statistics service.
161 static struct GNUNET_STATISTICS_Handle *stats;
165 * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
168 * @param cls client who sent the message
169 * @param message the message received
170 * @return #GNUNET_OK if message is well-formed
173 check_autoconfig_request (void *cls,
175 GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
177 return GNUNET_OK; /* checked later */
182 * Stop all pending activities with respect to the @a ac
184 * @param ac autoconfiguration to terminate activities for
187 terminate_ac_activities (struct AutoconfigContext *ac)
189 if (NULL != ac->timeout_task)
191 GNUNET_SCHEDULER_cancel (ac->timeout_task);
192 ac->timeout_task = NULL;
198 * Finish handling the autoconfiguration request and send
199 * the response to the client.
201 * @param cls the `struct AutoconfigContext` to conclude
204 conclude_autoconfig_request (void *cls)
206 struct AutoconfigContext *ac = cls;
207 struct ClientHandle *ch = ac->ch;
208 struct GNUNET_NAT_AUTO_AutoconfigResultMessage *arm;
209 struct GNUNET_MQ_Envelope *env;
212 struct GNUNET_CONFIGURATION_Handle *diff;
214 ac->timeout_task = NULL;
215 terminate_ac_activities (ac);
217 /* Send back response */
218 diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
220 buf = GNUNET_CONFIGURATION_serialize (diff,
222 GNUNET_CONFIGURATION_destroy (diff);
223 env = GNUNET_MQ_msg_extra (arm,
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],
232 GNUNET_MQ_send (ch->mq,
236 GNUNET_CONFIGURATION_destroy (ac->orig);
237 GNUNET_CONFIGURATION_destroy (ac->c);
238 GNUNET_CONTAINER_DLL_remove (ac_head,
242 GNUNET_SERVICE_client_continue (ch->client);
247 * Check if all autoconfiguration operations have concluded,
248 * and if they have, send the result back to the client.
250 * @param ac autoconfiguation context to check
253 check_autoconfig_finished (struct AutoconfigContext *ac)
255 GNUNET_SCHEDULER_cancel (ac->timeout_task);
257 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
263 * Update ENABLE_UPNPC configuration option.
265 * @param ac autoconfiguration to update
268 update_enable_upnpc_option (struct AutoconfigContext *ac)
270 switch (ac->enable_upnpc)
273 GNUNET_CONFIGURATION_set_value_string (ac->c,
280 GNUNET_CONFIGURATION_set_value_string (ac->c,
287 /* We are unsure, do not change option */
294 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
297 * @param cls client who sent the message
298 * @param message the message received
301 handle_autoconfig_request (void *cls,
303 GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
305 struct ClientHandle *ch = cls;
306 size_t left = ntohs (message->header.size) - sizeof(*message);
307 struct AutoconfigContext *ac;
309 ac = GNUNET_new (struct AutoconfigContext);
310 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
312 ac->c = GNUNET_CONFIGURATION_create ();
314 GNUNET_CONFIGURATION_deserialize (ac->c,
315 (const char *) &message[1],
320 GNUNET_SERVICE_client_drop (ch->client);
321 GNUNET_CONFIGURATION_destroy (ac->c);
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Received REQUEST_AUTO_CONFIG message from client\n");
328 GNUNET_CONTAINER_DLL_insert (ac_head,
332 = GNUNET_CONFIGURATION_dup (ac->c);
334 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
335 &conclude_autoconfig_request,
337 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
339 /* Probe for upnpc */
341 GNUNET_OS_check_helper_binary ("upnpc",
345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
346 _ ("UPnP client `upnpc` command not found, disabling UPnP\n"));
347 ac->enable_upnpc = GNUNET_NO;
351 /* We might at some point be behind NAT, try upnpc */
352 ac->enable_upnpc = GNUNET_YES;
354 update_enable_upnpc_option (ac);
356 /* Finally, check if we are already done */
357 check_autoconfig_finished (ac);
362 * Task run during shutdown.
367 shutdown_task (void *cls)
369 struct AutoconfigContext *ac;
371 while (NULL != (ac = ac_head))
373 GNUNET_CONTAINER_DLL_remove (ac_head,
376 terminate_ac_activities (ac);
381 GNUNET_STATISTICS_destroy (stats,
392 * @param c configuration to use
393 * @param service the initialized service
397 const struct GNUNET_CONFIGURATION_Handle *c,
398 struct GNUNET_SERVICE_Handle *service)
401 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
403 stats = GNUNET_STATISTICS_create ("nat-auto",
409 * Callback called when a client connects to the service.
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`
417 client_connect_cb (void *cls,
418 struct GNUNET_SERVICE_Client *c,
419 struct GNUNET_MQ_Handle *mq)
421 struct ClientHandle *ch;
423 ch = GNUNET_new (struct ClientHandle);
426 GNUNET_CONTAINER_DLL_insert (ch_head,
434 * Callback called when a client disconnected from the service
436 * @param cls closure for the service
437 * @param c the client that disconnected
438 * @param internal_cls a `struct ClientHandle *`
441 client_disconnect_cb (void *cls,
442 struct GNUNET_SERVICE_Client *c,
445 struct ClientHandle *ch = internal_cls;
447 GNUNET_CONTAINER_DLL_remove (ch_head,
455 * Define "main" method using service macro.
459 GNUNET_SERVICE_OPTION_NONE,
462 &client_disconnect_cb,
464 GNUNET_MQ_hd_var_size (autoconfig_request,
465 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
466 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
468 GNUNET_MQ_handler_end ());
471 #if defined(__linux__) && defined(__GLIBC__)
475 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
477 void __attribute__ ((constructor))
478 GNUNET_ARM_memory_init ()
480 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
481 mallopt (M_TOP_PAD, 1 * 1024);
488 /* end of gnunet-service-nat.c */