GNUnet 0.21.2
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 ( \
49 GNUNET_TIME_UNIT_SECONDS, 5)
50
51
55struct ClientHandle
56{
61
66
71
75 struct GNUNET_MQ_Handle *mq;
76};
77
78
83{
88
93
98
103
108
113
120
125
130};
131
132
136static struct ClientHandle *ch_head;
137
141static struct ClientHandle *ch_tail;
142
147
152
156static const struct GNUNET_CONFIGURATION_Handle *cfg;
157
162
163
172static int
174 const struct
176{
177 return GNUNET_OK; /* checked later */
178}
179
180
186static void
188{
189 if (NULL != ac->timeout_task)
190 {
192 ac->timeout_task = NULL;
193 }
194}
195
196
203static void
205{
206 struct AutoconfigContext *ac = cls;
207 struct ClientHandle *ch = ac->ch;
209 struct GNUNET_MQ_Envelope *env;
210 size_t c_size;
211 char *buf;
212 struct GNUNET_CONFIGURATION_Handle *diff;
213
214 ac->timeout_task = NULL;
216
217 /* Send back response */
219 ac->c);
221 &c_size);
224 c_size,
226 arm->status_code = htonl ((uint32_t) ac->status_code);
227 arm->type = htonl ((uint32_t) ac->type);
228 GNUNET_memcpy (&arm[1],
229 buf,
230 c_size);
231 GNUNET_free (buf);
233 env);
234
235 /* clean up */
239 ac_tail,
240 ac);
241 GNUNET_free (ac);
243}
244
245
252static void
254{
256 ac->timeout_task
258 ac);
259}
260
261
267static void
269{
270 switch (ac->enable_upnpc)
271 {
272 case GNUNET_YES:
274 "NAT",
275 "ENABLE_UPNP",
276 "YES");
277 break;
278
279 case GNUNET_NO:
281 "NAT",
282 "ENABLE_UPNP",
283 "NO");
284 break;
285
286 case GNUNET_SYSERR:
287 /* We are unsure, do not change option */
288 break;
289 }
290}
291
292
300static void
302 const struct
304{
305 struct ClientHandle *ch = cls;
306 size_t left = ntohs (message->header.size) - sizeof(*message);
307 struct AutoconfigContext *ac;
308
309 ac = GNUNET_new (struct AutoconfigContext);
311 ac->ch = ch;
313 if (GNUNET_OK !=
315 (const char *) &message[1],
316 left,
317 NULL))
318 {
319 GNUNET_break (0);
322 GNUNET_free (ac);
323 return;
324 }
326 "Received REQUEST_AUTO_CONFIG message from client\n");
327
329 ac_tail,
330 ac);
331 ac->orig
333 ac->timeout_task
336 ac);
337 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
338
339 /* Probe for upnpc */
340 if (GNUNET_SYSERR ==
342 GNUNET_NO,
343 NULL))
344 {
346 _ ("UPnP client `upnpc` command not found, disabling UPnP\n"));
348 }
349 else
350 {
351 /* We might at some point be behind NAT, try upnpc */
353 }
355
356 /* Finally, check if we are already done */
358}
359
360
366static void
367shutdown_task (void *cls)
368{
369 struct AutoconfigContext *ac;
370
371 while (NULL != (ac = ac_head))
372 {
374 ac_tail,
375 ac);
377 GNUNET_free (ac);
378 }
379 if (NULL != stats)
380 {
382 GNUNET_NO);
383 stats = NULL;
384 }
385}
386
387
395static void
396run (void *cls,
397 const struct GNUNET_CONFIGURATION_Handle *c,
399{
400 cfg = c;
402 NULL);
403 stats = GNUNET_STATISTICS_create ("nat-auto",
404 cfg);
405}
406
407
416static void *
418 struct GNUNET_SERVICE_Client *c,
419 struct GNUNET_MQ_Handle *mq)
420{
421 struct ClientHandle *ch;
422
423 ch = GNUNET_new (struct ClientHandle);
424 ch->mq = mq;
425 ch->client = c;
427 ch_tail,
428 ch);
429 return ch;
430}
431
432
440static void
442 struct GNUNET_SERVICE_Client *c,
443 void *internal_cls)
444{
445 struct ClientHandle *ch = internal_cls;
446
448 ch_tail,
449 ch);
450 GNUNET_free (ch);
451}
452
453
458 ("nat-auto",
460 &run,
463 NULL,
464 GNUNET_MQ_hd_var_size (autoconfig_request,
467 NULL),
469
470
471#if defined(__linux__) && defined(__GLIBC__)
472#include <malloc.h>
473
477void __attribute__ ((constructor))
478GNUNET_ARM_memory_init ()
479{
480 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
481 mallopt (M_TOP_PAD, 1 * 1024);
482 malloc_trim (0);
483}
484
485
486#endif
487
488/* end of gnunet-service-nat.c */
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct GNUNET_CADET_Channel * ch
Channel handle.
Definition: gnunet-cadet.c:117
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
static struct AutoconfigContext * ac_tail
DLL of our autoconfiguration operations.
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.
static void conclude_autoconfig_request(void *cls)
Finish handling the autoconfiguration request and send the response to the client.
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 struct ClientHandle * ch_head
Head of client DLL.
static struct AutoconfigContext * ac_head
DLL of our autoconfiguration operations.
static void update_enable_upnpc_option(struct AutoconfigContext *ac)
Update ENABLE_UPNPC configuration option.
#define AUTOCONFIG_TIMEOUT
How long do we wait until we forcefully terminate autoconfiguration?
static struct GNUNET_STATISTICS_Handle * stats
Handle to the statistics service.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to our current configuration.
static struct ClientHandle * ch_tail
Tail of client DLL.
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.
static void shutdown_task(void *cls)
Task run during shutdown.
static void terminate_ac_activities(struct AutoconfigContext *ac)
Stop all pending activities with respect to the ac.
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *c, struct GNUNET_SERVICE_Handle *service)
Setup NAT service.
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...
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.
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
struct GNUNET_PQ_ResultSpec __attribute__
Constants for network protocols.
Functions related to doing DNS lookups.
API to create, modify and access statistics.
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.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, const char *source_filename)
De-serializes configuration.
char * GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
Serializes the given configuration.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
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_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#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:63
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
GNUNET_NAT_StatusCode
Error Types for the NAT subsystem (which can then later be converted/resolved to a string)
GNUNET_NAT_Type
What the situation of the NAT connectivity.
@ GNUNET_NAT_ERROR_SUCCESS
Just the default.
enum GNUNET_GenericReturnValue GNUNET_OS_check_helper_binary(const char *binary, bool check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT
Message from NAT service with the autoconfiguration result.
#define GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG
Message to ask NAT service to request autoconfiguration.
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,...
Definition: scheduler.c:1340
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:981
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:1305
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:1278
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2484
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2455
@ GNUNET_SERVICE_OPTION_NONE
Use defaults.
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Context for autoconfiguration operations.
struct AutoconfigContext * prev
Kept in a DLL.
struct AutoconfigContext * next
Kept in a DLL.
int enable_upnpc
GNUNET_YES if upnpc should be used, GNUNET_NO if upnpc should not be used, GNUNET_SYSERR if we should...
struct GNUNET_CONFIGURATION_Handle * c
Configuration we are creating.
enum GNUNET_NAT_Type type
NAT type to return to the client.
struct GNUNET_CONFIGURATION_Handle * orig
Original configuration (for diffing).
enum GNUNET_NAT_StatusCode status_code
Status code to return to the client.
struct ClientHandle * ch
Which client asked the question.
struct GNUNET_SCHEDULER_Task * timeout_task
Timeout task to force termination.
Struct containing information about a client, handle to connect to it, and any pending messages that ...
struct ClientHandle * prev
Kept in a DLL.
struct ClientHandle * next
Kept in a DLL.
struct GNUNET_SERVICE_Client * client
The handle to this client.
struct GNUNET_MQ_Handle * mq
The message queue to this client.
struct GNUNET_MQ_Handle * mq
Message Queue for the channel (which we are implementing).
Definition: cadet.h:142
Handle to a message queue.
Definition: mq.c:87
Client requesting automatic configuration.
Definition: nat-auto.h:71
struct GNUNET_MessageHeader header
Header with type #GNUNET_MESSAGE_TYPE_NAT_REQUEST_AUTO_CFG.
Definition: nat-auto.h:75
Service responding with proposed configuration.
Definition: nat-auto.h:85
int32_t type
An enum GNUNET_NAT_Type in NBO.
Definition: nat-auto.h:99
int32_t status_code
An enum GNUNET_NAT_StatusCode in NBO.
Definition: nat-auto.h:94
Entry in list of pending tasks.
Definition: scheduler.c:136
Handle to a client that is connected to a service.
Definition: service.c:246
Handle to a service.
Definition: service.c:117
Handle for the service.