aboutsummaryrefslogtreecommitdiff
path: root/src/nat-auto/gnunet-service-nat-auto.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat-auto/gnunet-service-nat-auto.c')
-rw-r--r--src/nat-auto/gnunet-service-nat-auto.c327
1 files changed, 163 insertions, 164 deletions
diff --git a/src/nat-auto/gnunet-service-nat-auto.c b/src/nat-auto/gnunet-service-nat-auto.c
index 0b2995441..199a759ea 100644
--- a/src/nat-auto/gnunet-service-nat-auto.c
+++ b/src/nat-auto/gnunet-service-nat-auto.c
@@ -1,19 +1,19 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2016, 2017 GNUnet e.V. 3 Copyright (C) 2016, 2017 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 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 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, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
@@ -45,15 +45,13 @@
45/** 45/**
46 * How long do we wait until we forcefully terminate autoconfiguration? 46 * How long do we wait until we forcefully terminate autoconfiguration?
47 */ 47 */
48#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) 48#define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
49 49
50 50
51/** 51/**
52 * Internal data structure we track for each of our clients. 52 * Internal data structure we track for each of our clients.
53 */ 53 */
54struct ClientHandle 54struct ClientHandle {
55{
56
57 /** 55 /**
58 * Kept in a DLL. 56 * Kept in a DLL.
59 */ 57 */
@@ -79,8 +77,7 @@ struct ClientHandle
79/** 77/**
80 * Context for autoconfiguration operations. 78 * Context for autoconfiguration operations.
81 */ 79 */
82struct AutoconfigContext 80struct AutoconfigContext {
83{
84 /** 81 /**
85 * Kept in a DLL. 82 * Kept in a DLL.
86 */ 83 */
@@ -170,8 +167,8 @@ static struct GNUNET_STATISTICS_Handle *stats;
170 * @return #GNUNET_OK if message is well-formed 167 * @return #GNUNET_OK if message is well-formed
171 */ 168 */
172static int 169static int
173check_autoconfig_request (void *cls, 170check_autoconfig_request(void *cls,
174 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) 171 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
175{ 172{
176 return GNUNET_OK; /* checked later */ 173 return GNUNET_OK; /* checked later */
177} 174}
@@ -183,13 +180,13 @@ check_autoconfig_request (void *cls,
183 * @param ac autoconfiguration to terminate activities for 180 * @param ac autoconfiguration to terminate activities for
184 */ 181 */
185static void 182static void
186terminate_ac_activities (struct AutoconfigContext *ac) 183terminate_ac_activities(struct AutoconfigContext *ac)
187{ 184{
188 if (NULL != ac->timeout_task) 185 if (NULL != ac->timeout_task)
189 { 186 {
190 GNUNET_SCHEDULER_cancel (ac->timeout_task); 187 GNUNET_SCHEDULER_cancel(ac->timeout_task);
191 ac->timeout_task = NULL; 188 ac->timeout_task = NULL;
192 } 189 }
193} 190}
194 191
195 192
@@ -200,7 +197,7 @@ terminate_ac_activities (struct AutoconfigContext *ac)
200 * @param cls the `struct AutoconfigContext` to conclude 197 * @param cls the `struct AutoconfigContext` to conclude
201 */ 198 */
202static void 199static void
203conclude_autoconfig_request (void *cls) 200conclude_autoconfig_request(void *cls)
204{ 201{
205 struct AutoconfigContext *ac = cls; 202 struct AutoconfigContext *ac = cls;
206 struct ClientHandle *ch = ac->ch; 203 struct ClientHandle *ch = ac->ch;
@@ -211,34 +208,34 @@ conclude_autoconfig_request (void *cls)
211 struct GNUNET_CONFIGURATION_Handle *diff; 208 struct GNUNET_CONFIGURATION_Handle *diff;
212 209
213 ac->timeout_task = NULL; 210 ac->timeout_task = NULL;
214 terminate_ac_activities (ac); 211 terminate_ac_activities(ac);
215 212
216 /* Send back response */ 213 /* Send back response */
217 diff = GNUNET_CONFIGURATION_get_diff (ac->orig, 214 diff = GNUNET_CONFIGURATION_get_diff(ac->orig,
218 ac->c); 215 ac->c);
219 buf = GNUNET_CONFIGURATION_serialize (diff, 216 buf = GNUNET_CONFIGURATION_serialize(diff,
220 &c_size); 217 &c_size);
221 GNUNET_CONFIGURATION_destroy (diff); 218 GNUNET_CONFIGURATION_destroy(diff);
222 env = GNUNET_MQ_msg_extra (arm, 219 env = GNUNET_MQ_msg_extra(arm,
223 c_size, 220 c_size,
224 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT); 221 GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT);
225 arm->status_code = htonl ((uint32_t) ac->status_code); 222 arm->status_code = htonl((uint32_t)ac->status_code);
226 arm->type = htonl ((uint32_t) ac->type); 223 arm->type = htonl((uint32_t)ac->type);
227 GNUNET_memcpy (&arm[1], 224 GNUNET_memcpy(&arm[1],
228 buf, 225 buf,
229 c_size); 226 c_size);
230 GNUNET_free (buf); 227 GNUNET_free(buf);
231 GNUNET_MQ_send (ch->mq, 228 GNUNET_MQ_send(ch->mq,
232 env); 229 env);
233 230
234 /* clean up */ 231 /* clean up */
235 GNUNET_CONFIGURATION_destroy (ac->orig); 232 GNUNET_CONFIGURATION_destroy(ac->orig);
236 GNUNET_CONFIGURATION_destroy (ac->c); 233 GNUNET_CONFIGURATION_destroy(ac->c);
237 GNUNET_CONTAINER_DLL_remove (ac_head, 234 GNUNET_CONTAINER_DLL_remove(ac_head,
238 ac_tail, 235 ac_tail,
239 ac); 236 ac);
240 GNUNET_free (ac); 237 GNUNET_free(ac);
241 GNUNET_SERVICE_client_continue (ch->client); 238 GNUNET_SERVICE_client_continue(ch->client);
242} 239}
243 240
244 241
@@ -249,12 +246,12 @@ conclude_autoconfig_request (void *cls)
249 * @param ac autoconfiguation context to check 246 * @param ac autoconfiguation context to check
250 */ 247 */
251static void 248static void
252check_autoconfig_finished (struct AutoconfigContext *ac) 249check_autoconfig_finished(struct AutoconfigContext *ac)
253{ 250{
254 GNUNET_SCHEDULER_cancel (ac->timeout_task); 251 GNUNET_SCHEDULER_cancel(ac->timeout_task);
255 ac->timeout_task 252 ac->timeout_task
256 = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request, 253 = GNUNET_SCHEDULER_add_now(&conclude_autoconfig_request,
257 ac); 254 ac);
258} 255}
259 256
260 257
@@ -264,26 +261,28 @@ check_autoconfig_finished (struct AutoconfigContext *ac)
264 * @param ac autoconfiguration to update 261 * @param ac autoconfiguration to update
265 */ 262 */
266static void 263static void
267update_enable_upnpc_option (struct AutoconfigContext *ac) 264update_enable_upnpc_option(struct AutoconfigContext *ac)
268{ 265{
269 switch (ac->enable_upnpc) 266 switch (ac->enable_upnpc)
270 { 267 {
271 case GNUNET_YES: 268 case GNUNET_YES:
272 GNUNET_CONFIGURATION_set_value_string (ac->c, 269 GNUNET_CONFIGURATION_set_value_string(ac->c,
273 "NAT", 270 "NAT",
274 "ENABLE_UPNP", 271 "ENABLE_UPNP",
275 "YES"); 272 "YES");
276 break; 273 break;
277 case GNUNET_NO: 274
278 GNUNET_CONFIGURATION_set_value_string (ac->c, 275 case GNUNET_NO:
279 "NAT", 276 GNUNET_CONFIGURATION_set_value_string(ac->c,
280 "ENABLE_UPNP", 277 "NAT",
281 "NO"); 278 "ENABLE_UPNP",
282 break; 279 "NO");
283 case GNUNET_SYSERR: 280 break;
284 /* We are unsure, do not change option */ 281
285 break; 282 case GNUNET_SYSERR:
286 } 283 /* We are unsure, do not change option */
284 break;
285 }
287} 286}
288 287
289 288
@@ -295,62 +294,62 @@ update_enable_upnpc_option (struct AutoconfigContext *ac)
295 * @param message the message received 294 * @param message the message received
296 */ 295 */
297static void 296static void
298handle_autoconfig_request (void *cls, 297handle_autoconfig_request(void *cls,
299 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) 298 const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message)
300{ 299{
301 struct ClientHandle *ch = cls; 300 struct ClientHandle *ch = cls;
302 size_t left = ntohs (message->header.size) - sizeof (*message); 301 size_t left = ntohs(message->header.size) - sizeof(*message);
303 struct AutoconfigContext *ac; 302 struct AutoconfigContext *ac;
304 303
305 ac = GNUNET_new (struct AutoconfigContext); 304 ac = GNUNET_new(struct AutoconfigContext);
306 ac->status_code = GNUNET_NAT_ERROR_SUCCESS; 305 ac->status_code = GNUNET_NAT_ERROR_SUCCESS;
307 ac->ch = ch; 306 ac->ch = ch;
308 ac->c = GNUNET_CONFIGURATION_create (); 307 ac->c = GNUNET_CONFIGURATION_create();
309 if (GNUNET_OK != 308 if (GNUNET_OK !=
310 GNUNET_CONFIGURATION_deserialize (ac->c, 309 GNUNET_CONFIGURATION_deserialize(ac->c,
311 (const char *) &message[1], 310 (const char *)&message[1],
312 left, 311 left,
313 NULL)) 312 NULL))
314 { 313 {
315 GNUNET_break (0); 314 GNUNET_break(0);
316 GNUNET_SERVICE_client_drop (ch->client); 315 GNUNET_SERVICE_client_drop(ch->client);
317 GNUNET_CONFIGURATION_destroy (ac->c); 316 GNUNET_CONFIGURATION_destroy(ac->c);
318 GNUNET_free (ac); 317 GNUNET_free(ac);
319 return; 318 return;
320 } 319 }
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 320 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
322 "Received REQUEST_AUTO_CONFIG message from client\n"); 321 "Received REQUEST_AUTO_CONFIG message from client\n");
323 322
324 GNUNET_CONTAINER_DLL_insert (ac_head, 323 GNUNET_CONTAINER_DLL_insert(ac_head,
325 ac_tail, 324 ac_tail,
326 ac); 325 ac);
327 ac->orig 326 ac->orig
328 = GNUNET_CONFIGURATION_dup (ac->c); 327 = GNUNET_CONFIGURATION_dup(ac->c);
329 ac->timeout_task 328 ac->timeout_task
330 = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT, 329 = GNUNET_SCHEDULER_add_delayed(AUTOCONFIG_TIMEOUT,
331 &conclude_autoconfig_request, 330 &conclude_autoconfig_request,
332 ac); 331 ac);
333 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */ 332 ac->enable_upnpc = GNUNET_SYSERR; /* undecided */
334 333
335 /* Probe for upnpc */ 334 /* Probe for upnpc */
336 if (GNUNET_SYSERR == 335 if (GNUNET_SYSERR ==
337 GNUNET_OS_check_helper_binary ("upnpc", 336 GNUNET_OS_check_helper_binary("upnpc",
338 GNUNET_NO, 337 GNUNET_NO,
339 NULL)) 338 NULL))
340 { 339 {
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 340 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
342 _("UPnP client `upnpc` command not found, disabling UPnP\n")); 341 _("UPnP client `upnpc` command not found, disabling UPnP\n"));
343 ac->enable_upnpc = GNUNET_NO; 342 ac->enable_upnpc = GNUNET_NO;
344 } 343 }
345 else 344 else
346 { 345 {
347 /* We might at some point be behind NAT, try upnpc */ 346 /* We might at some point be behind NAT, try upnpc */
348 ac->enable_upnpc = GNUNET_YES; 347 ac->enable_upnpc = GNUNET_YES;
349 } 348 }
350 update_enable_upnpc_option (ac); 349 update_enable_upnpc_option(ac);
351 350
352 /* Finally, check if we are already done */ 351 /* Finally, check if we are already done */
353 check_autoconfig_finished (ac); 352 check_autoconfig_finished(ac);
354} 353}
355 354
356 355
@@ -360,24 +359,24 @@ handle_autoconfig_request (void *cls,
360 * @param cls unused 359 * @param cls unused
361 */ 360 */
362static void 361static void
363shutdown_task (void *cls) 362shutdown_task(void *cls)
364{ 363{
365 struct AutoconfigContext *ac; 364 struct AutoconfigContext *ac;
366 365
367 while (NULL != (ac = ac_head)) 366 while (NULL != (ac = ac_head))
368 { 367 {
369 GNUNET_CONTAINER_DLL_remove (ac_head, 368 GNUNET_CONTAINER_DLL_remove(ac_head,
370 ac_tail, 369 ac_tail,
371 ac); 370 ac);
372 terminate_ac_activities (ac); 371 terminate_ac_activities(ac);
373 GNUNET_free (ac); 372 GNUNET_free(ac);
374 } 373 }
375 if (NULL != stats) 374 if (NULL != stats)
376 { 375 {
377 GNUNET_STATISTICS_destroy (stats, 376 GNUNET_STATISTICS_destroy(stats,
378 GNUNET_NO); 377 GNUNET_NO);
379 stats = NULL; 378 stats = NULL;
380 } 379 }
381} 380}
382 381
383 382
@@ -389,15 +388,15 @@ shutdown_task (void *cls)
389 * @param service the initialized service 388 * @param service the initialized service
390 */ 389 */
391static void 390static void
392run (void *cls, 391run(void *cls,
393 const struct GNUNET_CONFIGURATION_Handle *c, 392 const struct GNUNET_CONFIGURATION_Handle *c,
394 struct GNUNET_SERVICE_Handle *service) 393 struct GNUNET_SERVICE_Handle *service)
395{ 394{
396 cfg = c; 395 cfg = c;
397 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 396 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
398 NULL); 397 NULL);
399 stats = GNUNET_STATISTICS_create ("nat-auto", 398 stats = GNUNET_STATISTICS_create("nat-auto",
400 cfg); 399 cfg);
401} 400}
402 401
403 402
@@ -410,18 +409,18 @@ run (void *cls,
410 * @return a `struct ClientHandle` 409 * @return a `struct ClientHandle`
411 */ 410 */
412static void * 411static void *
413client_connect_cb (void *cls, 412client_connect_cb(void *cls,
414 struct GNUNET_SERVICE_Client *c, 413 struct GNUNET_SERVICE_Client *c,
415 struct GNUNET_MQ_Handle *mq) 414 struct GNUNET_MQ_Handle *mq)
416{ 415{
417 struct ClientHandle *ch; 416 struct ClientHandle *ch;
418 417
419 ch = GNUNET_new (struct ClientHandle); 418 ch = GNUNET_new(struct ClientHandle);
420 ch->mq = mq; 419 ch->mq = mq;
421 ch->client = c; 420 ch->client = c;
422 GNUNET_CONTAINER_DLL_insert (ch_head, 421 GNUNET_CONTAINER_DLL_insert(ch_head,
423 ch_tail, 422 ch_tail,
424 ch); 423 ch);
425 return ch; 424 return ch;
426} 425}
427 426
@@ -434,16 +433,16 @@ client_connect_cb (void *cls,
434 * @param internal_cls a `struct ClientHandle *` 433 * @param internal_cls a `struct ClientHandle *`
435 */ 434 */
436static void 435static void
437client_disconnect_cb (void *cls, 436client_disconnect_cb(void *cls,
438 struct GNUNET_SERVICE_Client *c, 437 struct GNUNET_SERVICE_Client *c,
439 void *internal_cls) 438 void *internal_cls)
440{ 439{
441 struct ClientHandle *ch = internal_cls; 440 struct ClientHandle *ch = internal_cls;
442 441
443 GNUNET_CONTAINER_DLL_remove (ch_head, 442 GNUNET_CONTAINER_DLL_remove(ch_head,
444 ch_tail, 443 ch_tail,
445 ch); 444 ch);
446 GNUNET_free (ch); 445 GNUNET_free(ch);
447} 446}
448 447
449 448
@@ -451,17 +450,17 @@ client_disconnect_cb (void *cls,
451 * Define "main" method using service macro. 450 * Define "main" method using service macro.
452 */ 451 */
453GNUNET_SERVICE_MAIN 452GNUNET_SERVICE_MAIN
454("nat-auto", 453 ("nat-auto",
455 GNUNET_SERVICE_OPTION_NONE, 454 GNUNET_SERVICE_OPTION_NONE,
456 &run, 455 &run,
457 &client_connect_cb, 456 &client_connect_cb,
458 &client_disconnect_cb, 457 &client_disconnect_cb,
459 NULL, 458 NULL,
460 GNUNET_MQ_hd_var_size (autoconfig_request, 459 GNUNET_MQ_hd_var_size(autoconfig_request,
461 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, 460 GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG,
462 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage, 461 struct GNUNET_NAT_AUTO_AutoconfigRequestMessage,
463 NULL), 462 NULL),
464 GNUNET_MQ_handler_end ()); 463 GNUNET_MQ_handler_end());
465 464
466 465
467#if defined(LINUX) && defined(__GLIBC__) 466#if defined(LINUX) && defined(__GLIBC__)
@@ -471,11 +470,11 @@ GNUNET_SERVICE_MAIN
471 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 470 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
472 */ 471 */
473void __attribute__ ((constructor)) 472void __attribute__ ((constructor))
474GNUNET_ARM_memory_init () 473GNUNET_ARM_memory_init()
475{ 474{
476 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 475 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
477 mallopt (M_TOP_PAD, 1 * 1024); 476 mallopt(M_TOP_PAD, 1 * 1024);
478 malloc_trim (0); 477 malloc_trim(0);
479} 478}
480#endif 479#endif
481 480