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
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.
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.
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.
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 (GNUNET_TIME_UNIT_SECONDS, 5)
52 * 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,
174 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
176 return GNUNET_OK; /* checked later */
181 * Stop all pending activities with respect to the @a ac
183 * @param ac autoconfiguration to terminate activities for
186 terminate_ac_activities (struct AutoconfigContext *ac)
188 if (NULL != ac->timeout_task)
190 GNUNET_SCHEDULER_cancel (ac->timeout_task);
191 ac->timeout_task = NULL;
197 * Finish handling the autoconfiguration request and send
198 * the response to the client.
200 * @param cls the `struct AutoconfigContext` to conclude
203 conclude_autoconfig_request (void *cls)
205 struct AutoconfigContext *ac = cls;
206 struct ClientHandle *ch = ac->ch;
207 struct GNUNET_NAT_AUTO_AutoconfigResultMessage *arm;
208 struct GNUNET_MQ_Envelope *env;
211 struct GNUNET_CONFIGURATION_Handle *diff;
213 ac->timeout_task = NULL;
214 terminate_ac_activities (ac);
216 /* Send back response */
217 diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
219 buf = GNUNET_CONFIGURATION_serialize (diff,
221 GNUNET_CONFIGURATION_destroy (diff);
222 env = GNUNET_MQ_msg_extra (arm,
224 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
225 arm->status_code = htonl ((uint32_t) ac->status_code);
226 arm->type = htonl ((uint32_t) ac->type);
227 GNUNET_memcpy (&arm[1],
231 GNUNET_MQ_send (ch->mq,
235 GNUNET_CONFIGURATION_destroy (ac->orig);
236 GNUNET_CONFIGURATION_destroy (ac->c);
237 GNUNET_CONTAINER_DLL_remove (ac_head,
241 GNUNET_SERVICE_client_continue (ch->client);
246 * Check if all autoconfiguration operations have concluded,
247 * and if they have, send the result back to the client.
249 * @param ac autoconfiguation context to check
252 check_autoconfig_finished (struct AutoconfigContext *ac)
254 GNUNET_SCHEDULER_cancel (ac->timeout_task);
256 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
262 * Update ENABLE_UPNPC configuration option.
264 * @param ac autoconfiguration to update
267 update_enable_upnpc_option (struct AutoconfigContext *ac)
269 switch (ac->enable_upnpc)
272 GNUNET_CONFIGURATION_set_value_string (ac->c,
278 GNUNET_CONFIGURATION_set_value_string (ac->c,
284 /* We are unsure, do not change option */
291 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
294 * @param cls client who sent the message
295 * @param message the message received
298 handle_autoconfig_request (void *cls,
299 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
301 struct ClientHandle *ch = cls;
302 size_t left = ntohs (message->header.size) - sizeof (*message);
303 struct AutoconfigContext *ac;
305 ac = GNUNET_new (struct AutoconfigContext);
306 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
308 ac->c = GNUNET_CONFIGURATION_create ();
310 GNUNET_CONFIGURATION_deserialize (ac->c,
311 (const char *) &message[1],
316 GNUNET_SERVICE_client_drop (ch->client);
317 GNUNET_CONFIGURATION_destroy (ac->c);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Received REQUEST_AUTO_CONFIG message from client\n");
324 GNUNET_CONTAINER_DLL_insert (ac_head,
328 = GNUNET_CONFIGURATION_dup (ac->c);
330 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
331 &conclude_autoconfig_request,
333 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
335 /* Probe for upnpc */
337 GNUNET_OS_check_helper_binary ("upnpc",
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
342 _("UPnP client `upnpc` command not found, disabling UPnP\n"));
343 ac->enable_upnpc = GNUNET_NO;
347 /* We might at some point be behind NAT, try upnpc */
348 ac->enable_upnpc = GNUNET_YES;
350 update_enable_upnpc_option (ac);
352 /* Finally, check if we are already done */
353 check_autoconfig_finished (ac);
358 * Task run during shutdown.
363 shutdown_task (void *cls)
365 struct AutoconfigContext *ac;
367 while (NULL != (ac = ac_head))
369 GNUNET_CONTAINER_DLL_remove (ac_head,
372 terminate_ac_activities (ac);
377 GNUNET_STATISTICS_destroy (stats,
388 * @param c configuration to use
389 * @param service the initialized service
393 const struct GNUNET_CONFIGURATION_Handle *c,
394 struct GNUNET_SERVICE_Handle *service)
397 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
399 stats = GNUNET_STATISTICS_create ("nat-auto",
405 * Callback called when a client connects to the service.
407 * @param cls closure for the service
408 * @param c the new client that connected to the service
409 * @param mq the message queue used to send messages to the client
410 * @return a `struct ClientHandle`
413 client_connect_cb (void *cls,
414 struct GNUNET_SERVICE_Client *c,
415 struct GNUNET_MQ_Handle *mq)
417 struct ClientHandle *ch;
419 ch = GNUNET_new (struct ClientHandle);
422 GNUNET_CONTAINER_DLL_insert (ch_head,
430 * Callback called when a client disconnected from the service
432 * @param cls closure for the service
433 * @param c the client that disconnected
434 * @param internal_cls a `struct ClientHandle *`
437 client_disconnect_cb (void *cls,
438 struct GNUNET_SERVICE_Client *c,
441 struct ClientHandle *ch = internal_cls;
443 GNUNET_CONTAINER_DLL_remove (ch_head,
451 * Define "main" method using service macro.
455 GNUNET_SERVICE_OPTION_NONE,
458 &client_disconnect_cb,
460 GNUNET_MQ_hd_var_size (autoconfig_request,
461 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
462 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
464 GNUNET_MQ_handler_end ());
467 #if defined(LINUX) && defined(__GLIBC__)
471 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
473 void __attribute__ ((constructor))
474 GNUNET_ARM_memory_init ()
476 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
477 mallopt (M_TOP_PAD, 1 * 1024);
482 /* end of gnunet-service-nat.c */