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/>.
20 * @file nat-auto/gnunet-service-nat-auto.c
21 * @brief NAT autoconfiguration service
22 * @author Christian Grothoff
25 * - merge client handle and autoconfig context
26 * - implement "more" autoconfig:
27 * + re-work gnunet-nat-server & integrate!
28 * + integrate "legacy" code
29 * + test manually punched NAT (how?)
33 #include "gnunet_util_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_nat_service.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_resolver_service.h"
44 * How long do we wait until we forcefully terminate autoconfiguration?
46 #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
50 * Internal data structure we track for each of our clients.
58 struct ClientHandle *next;
63 struct ClientHandle *prev;
66 * Underlying handle for this client with the service.
68 struct GNUNET_SERVICE_Client *client;
71 * Message queue for communicating with the client.
73 struct GNUNET_MQ_Handle *mq;
78 * Context for autoconfiguration operations.
80 struct AutoconfigContext
85 struct AutoconfigContext *prev;
90 struct AutoconfigContext *next;
93 * Which client asked the question.
95 struct ClientHandle *ch;
98 * Configuration we are creating.
100 struct GNUNET_CONFIGURATION_Handle *c;
103 * Original configuration (for diffing).
105 struct GNUNET_CONFIGURATION_Handle *orig;
108 * Timeout task to force termination.
110 struct GNUNET_SCHEDULER_Task *timeout_task;
113 * #GNUNET_YES if upnpc should be used,
114 * #GNUNET_NO if upnpc should not be used,
115 * #GNUNET_SYSERR if we should simply not change the option.
120 * Status code to return to the client.
122 enum GNUNET_NAT_StatusCode status_code;
125 * NAT type to return to the client.
127 enum GNUNET_NAT_Type type;
132 * Head of client DLL.
134 static struct ClientHandle *ch_head;
137 * Tail of client DLL.
139 static struct ClientHandle *ch_tail;
142 * DLL of our autoconfiguration operations.
144 static struct AutoconfigContext *ac_head;
147 * DLL of our autoconfiguration operations.
149 static struct AutoconfigContext *ac_tail;
152 * Handle to our current configuration.
154 static const struct GNUNET_CONFIGURATION_Handle *cfg;
157 * Handle to the statistics service.
159 static struct GNUNET_STATISTICS_Handle *stats;
163 * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
166 * @param cls client who sent the message
167 * @param message the message received
168 * @return #GNUNET_OK if message is well-formed
171 check_autoconfig_request (void *cls,
172 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
174 return GNUNET_OK; /* checked later */
179 * Stop all pending activities with respect to the @a ac
181 * @param ac autoconfiguration to terminate activities for
184 terminate_ac_activities (struct AutoconfigContext *ac)
186 if (NULL != ac->timeout_task)
188 GNUNET_SCHEDULER_cancel (ac->timeout_task);
189 ac->timeout_task = NULL;
195 * Finish handling the autoconfiguration request and send
196 * the response to the client.
198 * @param cls the `struct AutoconfigContext` to conclude
201 conclude_autoconfig_request (void *cls)
203 struct AutoconfigContext *ac = cls;
204 struct ClientHandle *ch = ac->ch;
205 struct GNUNET_NAT_AUTO_AutoconfigResultMessage *arm;
206 struct GNUNET_MQ_Envelope *env;
209 struct GNUNET_CONFIGURATION_Handle *diff;
211 ac->timeout_task = NULL;
212 terminate_ac_activities (ac);
214 /* Send back response */
215 diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
217 buf = GNUNET_CONFIGURATION_serialize (diff,
219 GNUNET_CONFIGURATION_destroy (diff);
220 env = GNUNET_MQ_msg_extra (arm,
222 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
223 arm->status_code = htonl ((uint32_t) ac->status_code);
224 arm->type = htonl ((uint32_t) ac->type);
225 GNUNET_memcpy (&arm[1],
229 GNUNET_MQ_send (ch->mq,
233 GNUNET_CONFIGURATION_destroy (ac->orig);
234 GNUNET_CONFIGURATION_destroy (ac->c);
235 GNUNET_CONTAINER_DLL_remove (ac_head,
239 GNUNET_SERVICE_client_continue (ch->client);
244 * Check if all autoconfiguration operations have concluded,
245 * and if they have, send the result back to the client.
247 * @param ac autoconfiguation context to check
250 check_autoconfig_finished (struct AutoconfigContext *ac)
252 GNUNET_SCHEDULER_cancel (ac->timeout_task);
254 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
260 * Update ENABLE_UPNPC configuration option.
262 * @param ac autoconfiguration to update
265 update_enable_upnpc_option (struct AutoconfigContext *ac)
267 switch (ac->enable_upnpc)
270 GNUNET_CONFIGURATION_set_value_string (ac->c,
276 GNUNET_CONFIGURATION_set_value_string (ac->c,
282 /* We are unsure, do not change option */
289 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
292 * @param cls client who sent the message
293 * @param message the message received
296 handle_autoconfig_request (void *cls,
297 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
299 struct ClientHandle *ch = cls;
300 size_t left = ntohs (message->header.size) - sizeof (*message);
301 struct AutoconfigContext *ac;
303 ac = GNUNET_new (struct AutoconfigContext);
304 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
306 ac->c = GNUNET_CONFIGURATION_create ();
308 GNUNET_CONFIGURATION_deserialize (ac->c,
309 (const char *) &message[1],
314 GNUNET_SERVICE_client_drop (ch->client);
315 GNUNET_CONFIGURATION_destroy (ac->c);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Received REQUEST_AUTO_CONFIG message from client\n");
322 GNUNET_CONTAINER_DLL_insert (ac_head,
326 = GNUNET_CONFIGURATION_dup (ac->c);
328 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
329 &conclude_autoconfig_request,
331 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
333 /* Probe for upnpc */
335 GNUNET_OS_check_helper_binary ("upnpc",
339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
340 _("UPnP client `upnpc` command not found, disabling UPnP\n"));
341 ac->enable_upnpc = GNUNET_NO;
345 /* We might at some point be behind NAT, try upnpc */
346 ac->enable_upnpc = GNUNET_YES;
348 update_enable_upnpc_option (ac);
350 /* Finally, check if we are already done */
351 check_autoconfig_finished (ac);
356 * Task run during shutdown.
361 shutdown_task (void *cls)
363 struct AutoconfigContext *ac;
365 while (NULL != (ac = ac_head))
367 GNUNET_CONTAINER_DLL_remove (ac_head,
370 terminate_ac_activities (ac);
375 GNUNET_STATISTICS_destroy (stats,
386 * @param c configuration to use
387 * @param service the initialized service
391 const struct GNUNET_CONFIGURATION_Handle *c,
392 struct GNUNET_SERVICE_Handle *service)
395 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
397 stats = GNUNET_STATISTICS_create ("nat-auto",
403 * Callback called when a client connects to the service.
405 * @param cls closure for the service
406 * @param c the new client that connected to the service
407 * @param mq the message queue used to send messages to the client
408 * @return a `struct ClientHandle`
411 client_connect_cb (void *cls,
412 struct GNUNET_SERVICE_Client *c,
413 struct GNUNET_MQ_Handle *mq)
415 struct ClientHandle *ch;
417 ch = GNUNET_new (struct ClientHandle);
420 GNUNET_CONTAINER_DLL_insert (ch_head,
428 * Callback called when a client disconnected from the service
430 * @param cls closure for the service
431 * @param c the client that disconnected
432 * @param internal_cls a `struct ClientHandle *`
435 client_disconnect_cb (void *cls,
436 struct GNUNET_SERVICE_Client *c,
439 struct ClientHandle *ch = internal_cls;
441 GNUNET_CONTAINER_DLL_remove (ch_head,
449 * Define "main" method using service macro.
453 GNUNET_SERVICE_OPTION_NONE,
456 &client_disconnect_cb,
458 GNUNET_MQ_hd_var_size (autoconfig_request,
459 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
460 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
462 GNUNET_MQ_handler_end ());
465 #if defined(LINUX) && defined(__GLIBC__)
469 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
471 void __attribute__ ((constructor))
472 GNUNET_ARM_memory_init ()
474 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
475 mallopt (M_TOP_PAD, 1 * 1024);
480 /* end of gnunet-service-nat.c */