GNUnet  0.10.x
gnunet-service-nat-auto.c
Go to the documentation of this file.
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 
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"
41 #include "nat-auto.h"
42 #include <gcrypt.h>
43 
44 
48 #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
49 
50 
54 struct ClientHandle {
58  struct ClientHandle *next;
59 
63  struct ClientHandle *prev;
64 
69 
73  struct GNUNET_MQ_Handle *mq;
74 };
75 
76 
85 
90 
94  struct ClientHandle *ch;
95 
100 
105 
110 
117 
121  enum GNUNET_NAT_StatusCode status_code;
122 
127 };
128 
129 
133 static struct ClientHandle *ch_head;
134 
138 static struct ClientHandle *ch_tail;
139 
143 static struct AutoconfigContext *ac_head;
144 
148 static struct AutoconfigContext *ac_tail;
149 
153 static const struct GNUNET_CONFIGURATION_Handle *cfg;
154 
159 
160 
169 static int
171  const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
172 {
173  return GNUNET_OK; /* checked later */
174 }
175 
176 
182 static void
184 {
185  if (NULL != ac->timeout_task)
186  {
188  ac->timeout_task = NULL;
189  }
190 }
191 
192 
199 static void
201 {
202  struct AutoconfigContext *ac = cls;
203  struct ClientHandle *ch = ac->ch;
205  struct GNUNET_MQ_Envelope *env;
206  size_t c_size;
207  char *buf;
208  struct GNUNET_CONFIGURATION_Handle *diff;
209 
210  ac->timeout_task = NULL;
212 
213  /* Send back response */
215  ac->c);
217  &c_size);
219  env = GNUNET_MQ_msg_extra(arm,
220  c_size,
222  arm->status_code = htonl((uint32_t)ac->status_code);
223  arm->type = htonl((uint32_t)ac->type);
224  GNUNET_memcpy(&arm[1],
225  buf,
226  c_size);
227  GNUNET_free(buf);
228  GNUNET_MQ_send(ch->mq,
229  env);
230 
231  /* clean up */
235  ac_tail,
236  ac);
237  GNUNET_free(ac);
239 }
240 
241 
248 static void
250 {
252  ac->timeout_task
254  ac);
255 }
256 
257 
263 static void
265 {
266  switch (ac->enable_upnpc)
267  {
268  case GNUNET_YES:
270  "NAT",
271  "ENABLE_UPNP",
272  "YES");
273  break;
274 
275  case GNUNET_NO:
277  "NAT",
278  "ENABLE_UPNP",
279  "NO");
280  break;
281 
282  case GNUNET_SYSERR:
283  /* We are unsure, do not change option */
284  break;
285  }
286 }
287 
288 
296 static void
298  const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
299 {
300  struct ClientHandle *ch = cls;
301  size_t left = ntohs(message->header.size) - sizeof(*message);
302  struct AutoconfigContext *ac;
303 
304  ac = GNUNET_new(struct AutoconfigContext);
306  ac->ch = ch;
308  if (GNUNET_OK !=
310  (const char *)&message[1],
311  left,
312  NULL))
313  {
314  GNUNET_break(0);
317  GNUNET_free(ac);
318  return;
319  }
321  "Received REQUEST_AUTO_CONFIG message from client\n");
322 
324  ac_tail,
325  ac);
326  ac->orig
328  ac->timeout_task
331  ac);
332  ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
333 
334  /* Probe for upnpc */
335  if (GNUNET_SYSERR ==
337  GNUNET_NO,
338  NULL))
339  {
341  _("UPnP client `upnpc` command not found, disabling UPnP\n"));
342  ac->enable_upnpc = GNUNET_NO;
343  }
344  else
345  {
346  /* We might at some point be behind NAT, try upnpc */
347  ac->enable_upnpc = GNUNET_YES;
348  }
350 
351  /* Finally, check if we are already done */
353 }
354 
355 
361 static void
362 shutdown_task(void *cls)
363 {
364  struct AutoconfigContext *ac;
365 
366  while (NULL != (ac = ac_head))
367  {
369  ac_tail,
370  ac);
372  GNUNET_free(ac);
373  }
374  if (NULL != stats)
375  {
377  GNUNET_NO);
378  stats = NULL;
379  }
380 }
381 
382 
390 static void
391 run(void *cls,
392  const struct GNUNET_CONFIGURATION_Handle *c,
394 {
395  cfg = c;
397  NULL);
398  stats = GNUNET_STATISTICS_create("nat-auto",
399  cfg);
400 }
401 
402 
411 static void *
413  struct GNUNET_SERVICE_Client *c,
414  struct GNUNET_MQ_Handle *mq)
415 {
416  struct ClientHandle *ch;
417 
418  ch = GNUNET_new(struct ClientHandle);
419  ch->mq = mq;
420  ch->client = c;
422  ch_tail,
423  ch);
424  return ch;
425 }
426 
427 
435 static void
437  struct GNUNET_SERVICE_Client *c,
438  void *internal_cls)
439 {
440  struct ClientHandle *ch = internal_cls;
441 
443  ch_tail,
444  ch);
445  GNUNET_free(ch);
446 }
447 
448 
453  ("nat-auto",
455  &run,
458  NULL,
459  GNUNET_MQ_hd_var_size(autoconfig_request,
462  NULL),
464 
465 
466 #if defined(LINUX) && defined(__GLIBC__)
467 #include <malloc.h>
468 
472 void __attribute__ ((constructor))
473 GNUNET_ARM_memory_init()
474 {
475  mallopt(M_TRIM_THRESHOLD, 4 * 1024);
476  mallopt(M_TOP_PAD, 1 * 1024);
477  malloc_trim(0);
478 }
479 #endif
480 
481 /* end of gnunet-service-nat.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
static struct GNUNET_CADET_Channel * ch
Channel handle.
Definition: gnunet-cadet.c:117
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
Message from NAT service with the autoconfiguration result.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
struct GNUNET_CONFIGURATION_Handle * c
Configuration we are creating.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
struct ClientHandle * prev
Kept in a DLL.
Handle to a service.
Definition: service.c:114
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
Client requesting automatic configuration.
Definition: nat-auto.h:70
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Setup NAT service.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
Service responding with proposed configuration.
Definition: nat-auto.h:83
static void conclude_autoconfig_request(void *cls)
Finish handling the autoconfiguration request and send the response to the client.
static void handle_autoconfig_request(void *cls, const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
Handler for #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from client.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
int enable_upnpc
GNUNET_YES if upnpc should be used, GNUNET_NO if upnpc should not be used, GNUNET_SYSERR if we should...
#define GNUNET_NO
Definition: gnunet_common.h:78
struct GNUNET_SCHEDULER_Task * timeout_task
Timeout task to force termination.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
int GNUNET_OS_check_helper_binary(const char *binary, int check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
static const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to our current configuration.
Handle for the service.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int32_t status_code
An enum GNUNET_NAT_StatusCode in NBO.
Definition: nat-auto.h:92
struct AutoconfigContext * prev
Kept in a DLL.
static void shutdown_task(void *cls)
Task run during shutdown.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static void check_autoconfig_finished(struct AutoconfigContext *ac)
Check if all autoconfiguration operations have concluded, and if they have, send the result back to t...
Handle to a client that is connected to a service.
Definition: service.c:246
static struct ClientHandle * ch_tail
Tail of client DLL.
static struct AutoconfigContext * ac_head
DLL of our autoconfiguration operations.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1237
enum GNUNET_NAT_Type type
NAT type to return to the client.
int32_t type
An enum GNUNET_NAT_Type in NBO.
Definition: nat-auto.h:97
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_get_diff(const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new)
Compute configuration with only entries that have been changed.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
Context for autoconfiguration operations.
GNUNET_NAT_StatusCode
Error Types for the NAT subsystem (which can then later be converted/resolved to a string) ...
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
static char buf[2048]
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
GNUNET_SERVICE_MAIN("nat-auto", GNUNET_SERVICE_OPTION_NONE, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_var_size(autoconfig_request, GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, struct GNUNET_NAT_AUTO_AutoconfigRequestMessage, NULL), GNUNET_MQ_handler_end())
Define "main" method using service macro.
struct GNUNET_SERVICE_Client * client
The handle to this client.
Struct containing information about a client, handle to connect to it, and any pending messages that ...
static void terminate_ac_activities(struct AutoconfigContext *ac)
Stop all pending activities with respect to the ac.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_MQ_Handle * mq
The message queue to this client.
struct AutoconfigContext * next
Kept in a DLL.
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG
Message to ask NAT service to request autoconfiguration.
static int check_autoconfig_request(void *cls, const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
Check validity of #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG message from client.
static void update_enable_upnpc_option(struct AutoconfigContext *ac)
Update ENABLE_UPNPC configuration option.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
char * GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
Serializes the given configuration.
Handle to a message queue.
Definition: mq.c:84
enum RadiotapType __attribute__
Messages for interaction with gnunet-nat-auto-service.
configuration data
Definition: configuration.c:83
struct ClientHandle * ch
Which client asked the question.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
struct GNUNET_CONFIGURATION_Handle * orig
Original configuration (for diffing).
struct GNUNET_MessageHeader header
Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG.
Definition: nat-auto.h:74
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
#define GNUNET_YES
Definition: gnunet_common.h:77
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
GNUNET_NAT_Type
What the situation of the NAT connectivity.
static struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
int GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, const char *basedir)
De-serializes configuration.
enum GNUNET_NAT_StatusCode status_code
Status code to return to the client.
struct ClientHandle * next
Kept in a DLL.
#define AUTOCONFIG_TIMEOUT
How long do we wait until we forcefully terminate autoconfiguration?
static struct AutoconfigContext * ac_tail
DLL of our autoconfiguration operations.
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
static struct ClientHandle * ch_head
Head of client DLL.
#define GNUNET_free(ptr)
Wrapper around free.
static void * client_connect_cb(void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
Callback called when a client connects to the service.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956