aboutsummaryrefslogtreecommitdiff
path: root/src/nat-auto/nat_auto_api_test.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-01-10 18:50:16 +0100
committerChristian Grothoff <christian@grothoff.org>2017-01-10 18:50:16 +0100
commit07c8655e1ee8aa603def55b018222f3244235d1e (patch)
tree705267efc5a1628532ada0786955ab32dfeff0dc /src/nat-auto/nat_auto_api_test.c
parentea5a38ca2537d0892049dc3ce64576d2a9bafa97 (diff)
downloadgnunet-07c8655e1ee8aa603def55b018222f3244235d1e.tar.gz
gnunet-07c8655e1ee8aa603def55b018222f3244235d1e.zip
fix misc compilation issues in nat-auto
Diffstat (limited to 'src/nat-auto/nat_auto_api_test.c')
-rw-r--r--src/nat-auto/nat_auto_api_test.c120
1 files changed, 60 insertions, 60 deletions
diff --git a/src/nat-auto/nat_auto_api_test.c b/src/nat-auto/nat_auto_api_test.c
index 8359e3cb5..fb2bcd679 100644
--- a/src/nat-auto/nat_auto_api_test.c
+++ b/src/nat-auto/nat_auto_api_test.c
@@ -24,7 +24,8 @@
24 */ 24 */
25#include "platform.h" 25#include "platform.h"
26#include "gnunet_util_lib.h" 26#include "gnunet_util_lib.h"
27#include "gnunet_nat_lib.h" 27#include "gnunet_nat_service.h"
28#include "gnunet_nat_auto_service.h"
28#include "nat-auto.h" 29#include "nat-auto.h"
29 30
30#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__) 31#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
@@ -153,19 +154,19 @@ struct GNUNET_NAT_AUTO_Test
153 struct GNUNET_SCHEDULER_Task *ttask; 154 struct GNUNET_SCHEDULER_Task *ttask;
154 155
155 /** 156 /**
156 * #GNUNET_YES if we're testing TCP 157 * Section name of plugin to test.
157 */ 158 */
158 int is_tcp; 159 char *section_name;
159 160
160 /** 161 /**
161 * Data that should be transmitted or source-port. 162 * IPPROTO_TCP or IPPROTO_UDP.
162 */ 163 */
163 uint16_t data; 164 int proto;
164 165
165 /** 166 /**
166 * Advertised port to the other peer. 167 * Data that should be transmitted or source-port.
167 */ 168 */
168 uint16_t adv_port; 169 uint16_t data;
169 170
170 /** 171 /**
171 * Status code to be reported to the timeout/status call 172 * Status code to be reported to the timeout/status call
@@ -358,12 +359,14 @@ mq_error_handler (void *cls,
358 * @param cls closure 359 * @param cls closure
359 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean 360 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
360 * the previous (now invalid) one 361 * the previous (now invalid) one
362 * @param ac address class the address belongs to
361 * @param addr either the previous or the new public IP address 363 * @param addr either the previous or the new public IP address
362 * @param addrlen actual length of the @a addr 364 * @param addrlen actual length of the @a addr
363 */ 365 */
364static void 366static void
365addr_cb (void *cls, 367addr_cb (void *cls,
366 int add_remove, 368 int add_remove,
369 enum GNUNET_NAT_AddressClass ac,
367 const struct sockaddr *addr, 370 const struct sockaddr *addr,
368 socklen_t addrlen) 371 socklen_t addrlen)
369{ 372{
@@ -411,58 +414,50 @@ addr_cb (void *cls,
411 msg->dst_ipv4 = sa->sin_addr.s_addr; 414 msg->dst_ipv4 = sa->sin_addr.s_addr;
412 msg->dport = sa->sin_port; 415 msg->dport = sa->sin_port;
413 msg->data = h->data; 416 msg->data = h->data;
414 msg->is_tcp = htonl ((uint32_t) h->is_tcp); 417 msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP));
415 GNUNET_MQ_send (ca->mq, 418 GNUNET_MQ_send (ca->mq,
416 env); 419 env);
417} 420}
418 421
419 422
420/** 423/**
421 * Timeout task for a nat test. 424 * Calls the report-callback reporting failure.
422 * Calls the report-callback with a timeout return value
423 * 425 *
424 * Destroys the nat handle after the callback has been processed. 426 * Destroys the nat handle after the callback has been processed.
425 * 427 *
426 * @param cls handle to the timed out NAT test 428 * @param cls handle to the timed out NAT test
427 */ 429 */
428static void 430static void
429do_timeout (void *cls) 431do_fail (void *cls)
430{ 432{
431 struct GNUNET_NAT_AUTO_Test *nh = cls; 433 struct GNUNET_NAT_AUTO_Test *nh = cls;
432 434
433 nh->ttask = NULL; 435 nh->ttask = NULL;
434 nh->report (nh->report_cls, 436 nh->report (nh->report_cls,
435 (GNUNET_NAT_ERROR_SUCCESS == nh->status) 437 nh->status);
436 ? GNUNET_NAT_ERROR_TIMEOUT
437 : nh->status);
438} 438}
439 439
440 440
441/** 441/**
442 * Start testing if NAT traversal works using the 442 * Start testing if NAT traversal works using the given configuration.
443 * given configuration (IPv4-only). 443 * The transport adapters should be down while using this function.
444 *
445 * ALL failures are reported directly to the report callback
446 * 444 *
447 * @param cfg configuration for the NAT traversal 445 * @param cfg configuration for the NAT traversal
448 * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP 446 * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
449 * @param bnd_port port to bind to, 0 for connection reversal 447 * @param section_name configuration section to use for configuration
450 * @param adv_port externally advertised port to use
451 * @param timeout delay after which the test should be aborted
452 * @param report function to call with the result of the test 448 * @param report function to call with the result of the test
453 * @param report_cls closure for @a report 449 * @param report_cls closure for @a report
454 * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback 450 * @return handle to cancel NAT test
455 */ 451 */
456struct GNUNET_NAT_AUTO_Test * 452struct GNUNET_NAT_AUTO_Test *
457GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 453GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
458 int is_tcp, 454 uint8_t proto,
459 uint16_t bnd_port, 455 const char *section_name,
460 uint16_t adv_port, 456 GNUNET_NAT_TestCallback report,
461 struct GNUNET_TIME_Relative timeout, 457 void *report_cls)
462 GNUNET_NAT_TestCallback report,
463 void *report_cls)
464{ 458{
465 struct GNUNET_NAT_AUTO_Test *nh; 459 struct GNUNET_NAT_AUTO_Test *nh;
460 unsigned long long bnd_port;
466 struct sockaddr_in sa; 461 struct sockaddr_in sa;
467 const struct sockaddr *addrs[] = { 462 const struct sockaddr *addrs[] = {
468 (const struct sockaddr *) &sa 463 (const struct sockaddr *) &sa
@@ -471,18 +466,30 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
471 sizeof (sa) 466 sizeof (sa)
472 }; 467 };
473 468
469 if ( (GNUNET_OK !=
470 GNUNET_CONFIGURATION_get_value_number (cfg,
471 section_name,
472 "PORT",
473 &bnd_port)) ||
474 (bnd_port > 65535) )
475 {
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
477 _("Failed to find valid PORT in section `%s'\n"),
478 section_name);
479 return NULL;
480 }
481
474 memset (&sa, 0, sizeof (sa)); 482 memset (&sa, 0, sizeof (sa));
475 sa.sin_family = AF_INET; 483 sa.sin_family = AF_INET;
476 sa.sin_port = htons (bnd_port); 484 sa.sin_port = htons ((uint16_t) bnd_port);
477#if HAVE_SOCKADDR_IN_SIN_LEN 485#if HAVE_SOCKADDR_IN_SIN_LEN
478 sa.sin_len = sizeof (sa); 486 sa.sin_len = sizeof (sa);
479#endif 487#endif
480 488
481 nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test); 489 nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test);
482 nh->cfg = cfg; 490 nh->cfg = cfg;
483 nh->is_tcp = is_tcp; 491 nh->proto = proto;
484 nh->data = bnd_port; 492 nh->section_name = GNUNET_strdup (section_name);
485 nh->adv_port = adv_port;
486 nh->report = report; 493 nh->report = report;
487 nh->report_cls = report_cls; 494 nh->report_cls = report_cls;
488 nh->status = GNUNET_NAT_ERROR_SUCCESS; 495 nh->status = GNUNET_NAT_ERROR_SUCCESS;
@@ -490,28 +497,24 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
490 { 497 {
491 nh->nat 498 nh->nat
492 = GNUNET_NAT_register (cfg, 499 = GNUNET_NAT_register (cfg,
493 is_tcp, 500 section_name,
494 0, 501 proto,
495 0, 502 0, NULL, NULL,
496 NULL,
497 NULL,
498 &addr_cb, 503 &addr_cb,
499 &reversal_cb, 504 &reversal_cb,
500 nh, 505 nh);
501 NULL);
502 } 506 }
503 else 507 else
504 { 508 {
505 nh->lsock = 509 nh->lsock
506 GNUNET_NETWORK_socket_create (AF_INET, 510 = GNUNET_NETWORK_socket_create (AF_INET,
507 (is_tcp == 511 proto,
508 GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
509 0); 512 0);
510 if ((nh->lsock == NULL) || 513 if ( (NULL == nh->lsock) ||
511 (GNUNET_OK != 514 (GNUNET_OK !=
512 GNUNET_NETWORK_socket_bind (nh->lsock, 515 GNUNET_NETWORK_socket_bind (nh->lsock,
513 (const struct sockaddr *) &sa, 516 (const struct sockaddr *) &sa,
514 sizeof (sa)))) 517 sizeof (sa))))
515 { 518 {
516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
517 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"), 520 _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
@@ -524,11 +527,11 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
524 nh->lsock = NULL; 527 nh->lsock = NULL;
525 } 528 }
526 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR; 529 nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
527 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, 530 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
528 nh); 531 nh);
529 return nh; 532 return nh;
530 } 533 }
531 if (GNUNET_YES == is_tcp) 534 if (IPPROTO_TCP == proto)
532 { 535 {
533 GNUNET_break (GNUNET_OK == 536 GNUNET_break (GNUNET_OK ==
534 GNUNET_NETWORK_socket_listen (nh->lsock, 537 GNUNET_NETWORK_socket_listen (nh->lsock,
@@ -550,17 +553,16 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
550 LOG (GNUNET_ERROR_TYPE_INFO, 553 LOG (GNUNET_ERROR_TYPE_INFO,
551 "NAT test listens on port %u (%s)\n", 554 "NAT test listens on port %u (%s)\n",
552 bnd_port, 555 bnd_port,
553 (GNUNET_YES == is_tcp) ? "tcp" : "udp"); 556 (IPPROTO_TCP == proto) ? "tcp" : "udp");
554 nh->nat = GNUNET_NAT_register (cfg, 557 nh->nat = GNUNET_NAT_register (cfg,
555 is_tcp, 558 section_name,
556 adv_port, 559 proto,
557 1, 560 1,
558 addrs, 561 addrs,
559 addrlens, 562 addrlens,
560 &addr_cb, 563 &addr_cb,
561 NULL, 564 NULL,
562 nh, 565 nh);
563 NULL);
564 if (NULL == nh->nat) 566 if (NULL == nh->nat)
565 { 567 {
566 LOG (GNUNET_ERROR_TYPE_INFO, 568 LOG (GNUNET_ERROR_TYPE_INFO,
@@ -576,14 +578,11 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
576 nh->lsock = NULL; 578 nh->lsock = NULL;
577 } 579 }
578 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED; 580 nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
579 nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout, 581 nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
580 nh); 582 nh);
581 return nh; 583 return nh;
582 } 584 }
583 } 585 }
584 nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout,
585 &do_timeout,
586 nh);
587 return nh; 586 return nh;
588} 587}
589 588
@@ -638,6 +637,7 @@ GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst)
638 GNUNET_NAT_unregister (tst->nat); 637 GNUNET_NAT_unregister (tst->nat);
639 tst->nat = NULL; 638 tst->nat = NULL;
640 } 639 }
640 GNUNET_free (tst->section_name);
641 GNUNET_free (tst); 641 GNUNET_free (tst);
642} 642}
643 643