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 * + test manually punched NAT (how?)
34 #include "gnunet_util_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_nat_service.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_resolver_service.h"
45 * How long do we wait until we forcefully terminate autoconfiguration?
47 #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
51 * Internal data structure we track for each of our clients.
59 struct ClientHandle *next;
64 struct ClientHandle *prev;
67 * Underlying handle for this client with the service.
69 struct GNUNET_SERVICE_Client *client;
72 * Message queue for communicating with the client.
74 struct GNUNET_MQ_Handle *mq;
79 * Context for autoconfiguration operations.
81 struct AutoconfigContext
86 struct AutoconfigContext *prev;
91 struct AutoconfigContext *next;
94 * Which client asked the question.
96 struct ClientHandle *ch;
99 * Configuration we are creating.
101 struct GNUNET_CONFIGURATION_Handle *c;
104 * Original configuration (for diffing).
106 struct GNUNET_CONFIGURATION_Handle *orig;
109 * Timeout task to force termination.
111 struct GNUNET_SCHEDULER_Task *timeout_task;
114 * #GNUNET_YES if upnpc should be used,
115 * #GNUNET_NO if upnpc should not be used,
116 * #GNUNET_SYSERR if we should simply not change the option.
121 * Status code to return to the client.
123 enum GNUNET_NAT_StatusCode status_code;
126 * NAT type to return to the client.
128 enum GNUNET_NAT_Type type;
133 * Head of client DLL.
135 static struct ClientHandle *ch_head;
138 * Tail of client DLL.
140 static struct ClientHandle *ch_tail;
143 * DLL of our autoconfiguration operations.
145 static struct AutoconfigContext *ac_head;
148 * DLL of our autoconfiguration operations.
150 static struct AutoconfigContext *ac_tail;
153 * Handle to our current configuration.
155 static const struct GNUNET_CONFIGURATION_Handle *cfg;
158 * Handle to the statistics service.
160 static struct GNUNET_STATISTICS_Handle *stats;
164 * Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message
167 * @param cls client who sent the message
168 * @param message the message received
169 * @return #GNUNET_OK if message is well-formed
172 check_autoconfig_request (void *cls,
173 const struct GNUNET_NAT_AutoconfigRequestMessage *message)
175 return GNUNET_OK; /* checked later */
180 * Stop all pending activities with respect to the @a ac
182 * @param ac autoconfiguration to terminate activities for
185 terminate_ac_activities (struct AutoconfigContext *ac)
187 if (NULL != ac->timeout_task)
189 GNUNET_SCHEDULER_cancel (ac->timeout_task);
190 ac->timeout_task = NULL;
196 * Finish handling the autoconfiguration request and send
197 * the response to the client.
199 * @param cls the `struct AutoconfigContext` to conclude
202 conclude_autoconfig_request (void *cls)
204 struct AutoconfigContext *ac = cls;
205 struct ClientHandle *ch = ac->ch;
206 struct GNUNET_NAT_AutoconfigResultMessage *arm;
207 struct GNUNET_MQ_Envelope *env;
210 struct GNUNET_CONFIGURATION_Handle *diff;
212 ac->timeout_task = NULL;
213 terminate_ac_activities (ac);
215 /* Send back response */
216 diff = GNUNET_CONFIGURATION_get_diff (ac->orig,
218 buf = GNUNET_CONFIGURATION_serialize (diff,
220 GNUNET_CONFIGURATION_destroy (diff);
221 env = GNUNET_MQ_msg_extra (arm,
223 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
224 arm->status_code = htonl ((uint32_t) ac->status_code);
225 arm->type = htonl ((uint32_t) ac->type);
226 GNUNET_memcpy (&arm[1],
230 GNUNET_MQ_send (ch->mq,
234 GNUNET_CONFIGURATION_destroy (ac->orig);
235 GNUNET_CONFIGURATION_destroy (ac->c);
236 GNUNET_CONTAINER_DLL_remove (ac_head,
240 GNUNET_SERVICE_client_continue (ch->client);
245 * Check if all autoconfiguration operations have concluded,
246 * and if they have, send the result back to the client.
248 * @param ac autoconfiguation context to check
251 check_autoconfig_finished (struct AutoconfigContext *ac)
253 GNUNET_SCHEDULER_cancel (ac->timeout_task);
255 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request,
261 * Update ENABLE_UPNPC configuration option.
263 * @param ac autoconfiguration to update
266 update_enable_upnpc_option (struct AutoconfigContext *ac)
268 switch (ac->enable_upnpc)
271 GNUNET_CONFIGURATION_set_value_string (ac->c,
277 GNUNET_CONFIGURATION_set_value_string (ac->c,
283 /* We are unsure, do not change option */
290 * Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from
293 * @param cls client who sent the message
294 * @param message the message received
297 handle_autoconfig_request (void *cls,
298 const struct GNUNET_NAT_AutoconfigRequestMessage *message)
300 struct ClientHandle *ch = cls;
301 size_t left = ntohs (message->header.size) - sizeof (*message);
302 struct AutoconfigContext *ac;
304 ac = GNUNET_new (struct AutoconfigContext);
305 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
307 ac->c = GNUNET_CONFIGURATION_create ();
309 GNUNET_CONFIGURATION_deserialize (ac->c,
310 (const char *) &message[1],
315 GNUNET_SERVICE_client_drop (ch->client);
316 GNUNET_CONFIGURATION_destroy (ac->c);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Received REQUEST_AUTO_CONFIG message from client\n");
323 GNUNET_CONTAINER_DLL_insert (ac_head,
327 = GNUNET_CONFIGURATION_dup (ac->c);
329 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT,
330 &conclude_autoconfig_request,
332 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
334 /* Probe for upnpc */
336 GNUNET_OS_check_helper_binary ("upnpc",
340 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
341 _("UPnP client `upnpc` command not found, disabling UPnP\n"));
342 ac->enable_upnpc = GNUNET_NO;
346 /* We might at some point be behind NAT, try upnpc */
347 ac->enable_upnpc = GNUNET_YES;
349 update_enable_upnpc_option (ac);
351 /* Finally, check if we are already done */
352 check_autoconfig_finished (ac);
357 * Task run during shutdown.
362 shutdown_task (void *cls)
364 struct AutoconfigContext *ac;
366 while (NULL != (ac = ac_head))
368 GNUNET_CONTAINER_DLL_remove (ac_head,
371 terminate_ac_activities (ac);
376 GNUNET_STATISTICS_destroy (stats,
387 * @param c configuration to use
388 * @param service the initialized service
392 const struct GNUNET_CONFIGURATION_Handle *c,
393 struct GNUNET_SERVICE_Handle *service)
396 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
398 stats = GNUNET_STATISTICS_create ("nat-auto",
404 * Callback called when a client connects to the service.
406 * @param cls closure for the service
407 * @param c the new client that connected to the service
408 * @param mq the message queue used to send messages to the client
409 * @return a `struct ClientHandle`
412 client_connect_cb (void *cls,
413 struct GNUNET_SERVICE_Client *c,
414 struct GNUNET_MQ_Handle *mq)
416 struct ClientHandle *ch;
418 ch = GNUNET_new (struct ClientHandle);
421 GNUNET_CONTAINER_DLL_insert (ch_head,
429 * Callback called when a client disconnected from the service
431 * @param cls closure for the service
432 * @param c the client that disconnected
433 * @param internal_cls a `struct ClientHandle *`
436 client_disconnect_cb (void *cls,
437 struct GNUNET_SERVICE_Client *c,
440 struct ClientHandle *ch = internal_cls;
442 GNUNET_CONTAINER_DLL_remove (ch_head,
450 * Define "main" method using service macro.
454 GNUNET_SERVICE_OPTION_NONE,
457 &client_disconnect_cb,
459 GNUNET_MQ_hd_var_size (autoconfig_request,
460 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
461 struct GNUNET_NAT_AutoconfigRequestMessage,
463 GNUNET_MQ_handler_end ());
466 #if defined(LINUX) && defined(__GLIBC__)
470 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
472 void __attribute__ ((constructor))
473 GNUNET_ARM_memory_init ()
475 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
476 mallopt (M_TOP_PAD, 1 * 1024);
481 /* end of gnunet-service-nat.c */