diff options
Diffstat (limited to 'src/nat/nat.c')
-rw-r--r-- | src/nat/nat.c | 282 |
1 files changed, 116 insertions, 166 deletions
diff --git a/src/nat/nat.c b/src/nat/nat.c index ed72be9f8..6069d40ad 100644 --- a/src/nat/nat.c +++ b/src/nat/nat.c | |||
@@ -386,8 +386,7 @@ remove_from_address_list_by_source (struct GNUNET_NAT_Handle *h, | |||
386 | continue; | 386 | continue; |
387 | GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos); | 387 | GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos); |
388 | if (NULL != h->address_callback) | 388 | if (NULL != h->address_callback) |
389 | h->address_callback (h->callback_cls, | 389 | h->address_callback (h->callback_cls, GNUNET_NO, |
390 | GNUNET_NO, | ||
391 | (const struct sockaddr *) &pos[1], pos->addrlen); | 390 | (const struct sockaddr *) &pos[1], pos->addrlen); |
392 | GNUNET_free (pos); | 391 | GNUNET_free (pos); |
393 | } | 392 | } |
@@ -416,10 +415,10 @@ add_to_address_list_as_is (struct GNUNET_NAT_Handle *h, | |||
416 | lal->source = src; | 415 | lal->source = src; |
417 | GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal); | 416 | GNUNET_CONTAINER_DLL_insert (h->lal_head, h->lal_tail, lal); |
418 | #if DEBUG_NAT | 417 | #if DEBUG_NAT |
419 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 418 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", |
420 | "nat", | 419 | "Adding address `%s' from source %d\n", GNUNET_a2s (arg, |
421 | "Adding address `%s' from source %d\n", | 420 | arg_size), |
422 | GNUNET_a2s (arg, arg_size), src); | 421 | src); |
423 | #endif | 422 | #endif |
424 | if (NULL != h->address_callback) | 423 | if (NULL != h->address_callback) |
425 | h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size); | 424 | h->address_callback (h->callback_cls, GNUNET_YES, arg, arg_size); |
@@ -438,8 +437,7 @@ add_to_address_list_as_is (struct GNUNET_NAT_Handle *h, | |||
438 | * @param arg_size number of bytes in arg | 437 | * @param arg_size number of bytes in arg |
439 | */ | 438 | */ |
440 | static void | 439 | static void |
441 | add_to_address_list (struct GNUNET_NAT_Handle *h, | 440 | add_to_address_list (struct GNUNET_NAT_Handle *h, enum LocalAddressSource src, |
442 | enum LocalAddressSource src, | ||
443 | const struct sockaddr *arg, socklen_t arg_size) | 441 | const struct sockaddr *arg, socklen_t arg_size) |
444 | { | 442 | { |
445 | struct sockaddr_in s4; | 443 | struct sockaddr_in s4; |
@@ -452,17 +450,13 @@ add_to_address_list (struct GNUNET_NAT_Handle *h, | |||
452 | in4 = (const struct sockaddr_in *) arg; | 450 | in4 = (const struct sockaddr_in *) arg; |
453 | s4 = *in4; | 451 | s4 = *in4; |
454 | s4.sin_port = htons (h->adv_port); | 452 | s4.sin_port = htons (h->adv_port); |
455 | add_to_address_list_as_is (h, | 453 | add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4, |
456 | src, | ||
457 | (const struct sockaddr *) &s4, | ||
458 | sizeof (struct sockaddr_in)); | 454 | sizeof (struct sockaddr_in)); |
459 | if (GNUNET_YES == h->enable_nat_server) | 455 | if (GNUNET_YES == h->enable_nat_server) |
460 | { | 456 | { |
461 | /* also add with PORT = 0 to indicate NAT server is enabled */ | 457 | /* also add with PORT = 0 to indicate NAT server is enabled */ |
462 | s4.sin_port = htons (0); | 458 | s4.sin_port = htons (0); |
463 | add_to_address_list_as_is (h, | 459 | add_to_address_list_as_is (h, src, (const struct sockaddr *) &s4, |
464 | src, | ||
465 | (const struct sockaddr *) &s4, | ||
466 | sizeof (struct sockaddr_in)); | 460 | sizeof (struct sockaddr_in)); |
467 | } | 461 | } |
468 | } | 462 | } |
@@ -473,9 +467,7 @@ add_to_address_list (struct GNUNET_NAT_Handle *h, | |||
473 | in6 = (const struct sockaddr_in6 *) arg; | 467 | in6 = (const struct sockaddr_in6 *) arg; |
474 | s6 = *in6; | 468 | s6 = *in6; |
475 | s6.sin6_port = htons (h->adv_port); | 469 | s6.sin6_port = htons (h->adv_port); |
476 | add_to_address_list_as_is (h, | 470 | add_to_address_list_as_is (h, src, (const struct sockaddr *) &s6, |
477 | src, | ||
478 | (const struct sockaddr *) &s6, | ||
479 | sizeof (struct sockaddr_in6)); | 471 | sizeof (struct sockaddr_in6)); |
480 | } | 472 | } |
481 | } | 473 | } |
@@ -497,8 +489,8 @@ add_to_address_list (struct GNUNET_NAT_Handle *h, | |||
497 | */ | 489 | */ |
498 | static void | 490 | static void |
499 | add_ip_to_address_list (struct GNUNET_NAT_Handle *h, | 491 | add_ip_to_address_list (struct GNUNET_NAT_Handle *h, |
500 | enum LocalAddressSource src, | 492 | enum LocalAddressSource src, const void *addr, |
501 | const void *addr, socklen_t addrlen) | 493 | socklen_t addrlen) |
502 | { | 494 | { |
503 | struct sockaddr_in s4; | 495 | struct sockaddr_in s4; |
504 | const struct in_addr *in4; | 496 | const struct in_addr *in4; |
@@ -515,17 +507,13 @@ add_ip_to_address_list (struct GNUNET_NAT_Handle *h, | |||
515 | s4.sin_len = (u_char) sizeof (struct sockaddr_in); | 507 | s4.sin_len = (u_char) sizeof (struct sockaddr_in); |
516 | #endif | 508 | #endif |
517 | s4.sin_addr = *in4; | 509 | s4.sin_addr = *in4; |
518 | add_to_address_list (h, | 510 | add_to_address_list (h, src, (const struct sockaddr *) &s4, |
519 | src, | ||
520 | (const struct sockaddr *) &s4, | ||
521 | sizeof (struct sockaddr_in)); | 511 | sizeof (struct sockaddr_in)); |
522 | if (GNUNET_YES == h->enable_nat_server) | 512 | if (GNUNET_YES == h->enable_nat_server) |
523 | { | 513 | { |
524 | /* also add with PORT = 0 to indicate NAT server is enabled */ | 514 | /* also add with PORT = 0 to indicate NAT server is enabled */ |
525 | s4.sin_port = htons (0); | 515 | s4.sin_port = htons (0); |
526 | add_to_address_list (h, | 516 | add_to_address_list (h, src, (const struct sockaddr *) &s4, |
527 | src, | ||
528 | (const struct sockaddr *) &s4, | ||
529 | sizeof (struct sockaddr_in)); | 517 | sizeof (struct sockaddr_in)); |
530 | 518 | ||
531 | } | 519 | } |
@@ -542,9 +530,7 @@ add_ip_to_address_list (struct GNUNET_NAT_Handle *h, | |||
542 | s6.sin6_len = (u_char) sizeof (struct sockaddr_in6); | 530 | s6.sin6_len = (u_char) sizeof (struct sockaddr_in6); |
543 | #endif | 531 | #endif |
544 | s6.sin6_addr = *in6; | 532 | s6.sin6_addr = *in6; |
545 | add_to_address_list (h, | 533 | add_to_address_list (h, src, (const struct sockaddr *) &s6, |
546 | src, | ||
547 | (const struct sockaddr *) &s6, | ||
548 | sizeof (struct sockaddr_in6)); | 534 | sizeof (struct sockaddr_in6)); |
549 | } | 535 | } |
550 | } | 536 | } |
@@ -562,8 +548,8 @@ add_ip_to_address_list (struct GNUNET_NAT_Handle *h, | |||
562 | * @param cls the NAT handle | 548 | * @param cls the NAT handle |
563 | * @param tc scheduler context | 549 | * @param tc scheduler context |
564 | */ | 550 | */ |
565 | static void | 551 | static void resolve_dns (void *cls, |
566 | resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 552 | const struct GNUNET_SCHEDULER_TaskContext *tc); |
567 | 553 | ||
568 | 554 | ||
569 | /** | 555 | /** |
@@ -585,8 +571,8 @@ process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen) | |||
585 | h->ext_dns = NULL; | 571 | h->ext_dns = NULL; |
586 | if (1 == inet_pton (AF_INET, h->external_address, &dummy)) | 572 | if (1 == inet_pton (AF_INET, h->external_address, &dummy)) |
587 | return; /* repated lookup pointless: was numeric! */ | 573 | return; /* repated lookup pointless: was numeric! */ |
588 | h->dns_task = GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency, | 574 | h->dns_task = |
589 | &resolve_dns, h); | 575 | GNUNET_SCHEDULER_add_delayed (h->dyndns_frequency, &resolve_dns, h); |
590 | return; | 576 | return; |
591 | } | 577 | } |
592 | add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen); | 578 | add_to_address_list (h, LAL_EXTERNAL_IP, addr, addrlen); |
@@ -599,8 +585,8 @@ process_external_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen) | |||
599 | * @param cls the NAT handle | 585 | * @param cls the NAT handle |
600 | * @param tc scheduler context | 586 | * @param tc scheduler context |
601 | */ | 587 | */ |
602 | static void | 588 | static void resolve_hostname (void *cls, |
603 | resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | 589 | const struct GNUNET_SCHEDULER_TaskContext *tc); |
604 | 590 | ||
605 | 591 | ||
606 | /** | 592 | /** |
@@ -620,8 +606,9 @@ process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen) | |||
620 | if (addr == NULL) | 606 | if (addr == NULL) |
621 | { | 607 | { |
622 | h->hostname_dns = NULL; | 608 | h->hostname_dns = NULL; |
623 | h->hostname_task = GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency, | 609 | h->hostname_task = |
624 | &resolve_hostname, h); | 610 | GNUNET_SCHEDULER_add_delayed (h->hostname_dns_frequency, |
611 | &resolve_hostname, h); | ||
625 | return; | 612 | return; |
626 | } | 613 | } |
627 | add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen); | 614 | add_to_address_list (h, LAL_HOSTNAME_DNS, addr, addrlen); |
@@ -640,9 +627,7 @@ process_hostname_ip (void *cls, const struct sockaddr *addr, socklen_t addrlen) | |||
640 | * @return GNUNET_OK to continue iterating | 627 | * @return GNUNET_OK to continue iterating |
641 | */ | 628 | */ |
642 | static int | 629 | static int |
643 | process_interfaces (void *cls, | 630 | process_interfaces (void *cls, const char *name, int isDefault, |
644 | const char *name, | ||
645 | int isDefault, | ||
646 | const struct sockaddr *addr, socklen_t addrlen) | 631 | const struct sockaddr *addr, socklen_t addrlen) |
647 | { | 632 | { |
648 | struct GNUNET_NAT_Handle *h = cls; | 633 | struct GNUNET_NAT_Handle *h = cls; |
@@ -657,9 +642,8 @@ process_interfaces (void *cls, | |||
657 | s4 = (struct sockaddr_in *) addr; | 642 | s4 = (struct sockaddr_in *) addr; |
658 | ip = &s4->sin_addr; | 643 | ip = &s4->sin_addr; |
659 | if (GNUNET_YES == h->use_localaddresses) | 644 | if (GNUNET_YES == h->use_localaddresses) |
660 | add_ip_to_address_list (h, | 645 | add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s4->sin_addr, |
661 | LAL_INTERFACE_ADDRESS, | 646 | sizeof (struct in_addr)); |
662 | &s4->sin_addr, sizeof (struct in_addr)); | ||
663 | break; | 647 | break; |
664 | case AF_INET6: | 648 | case AF_INET6: |
665 | s6 = (struct sockaddr_in6 *) addr; | 649 | s6 = (struct sockaddr_in6 *) addr; |
@@ -670,24 +654,22 @@ process_interfaces (void *cls, | |||
670 | } | 654 | } |
671 | ip = &s6->sin6_addr; | 655 | ip = &s6->sin6_addr; |
672 | if (GNUNET_YES == h->use_localaddresses) | 656 | if (GNUNET_YES == h->use_localaddresses) |
673 | add_ip_to_address_list (h, | 657 | add_ip_to_address_list (h, LAL_INTERFACE_ADDRESS, &s6->sin6_addr, |
674 | LAL_INTERFACE_ADDRESS, | 658 | sizeof (struct in6_addr)); |
675 | &s6->sin6_addr, sizeof (struct in6_addr)); | ||
676 | break; | 659 | break; |
677 | default: | 660 | default: |
678 | GNUNET_break (0); | 661 | GNUNET_break (0); |
679 | return GNUNET_OK; | 662 | return GNUNET_OK; |
680 | } | 663 | } |
681 | if ((h->internal_address == NULL) && | 664 | if ((h->internal_address == NULL) && (h->server_proc == NULL) && |
682 | (h->server_proc == NULL) && | ||
683 | (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) && | 665 | (h->server_read_task == GNUNET_SCHEDULER_NO_TASK) && |
684 | (GNUNET_YES == isDefault) && | 666 | (GNUNET_YES == isDefault) && ((addr->sa_family == AF_INET) || |
685 | ((addr->sa_family == AF_INET) || (addr->sa_family == AF_INET6))) | 667 | (addr->sa_family == AF_INET6))) |
686 | { | 668 | { |
687 | /* no internal address configured, but we found a "default" | 669 | /* no internal address configured, but we found a "default" |
688 | * interface, try using that as our 'internal' address */ | 670 | * interface, try using that as our 'internal' address */ |
689 | h->internal_address = GNUNET_strdup (inet_ntop (addr->sa_family, | 671 | h->internal_address = |
690 | ip, buf, sizeof (buf))); | 672 | GNUNET_strdup (inet_ntop (addr->sa_family, ip, buf, sizeof (buf))); |
691 | start_gnunet_nat_server (h); | 673 | start_gnunet_nat_server (h); |
692 | } | 674 | } |
693 | return GNUNET_OK; | 675 | return GNUNET_OK; |
@@ -736,13 +718,12 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
736 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | 718 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) |
737 | return; | 719 | return; |
738 | memset (mybuf, 0, sizeof (mybuf)); | 720 | memset (mybuf, 0, sizeof (mybuf)); |
739 | bytes = GNUNET_DISK_file_read (h->server_stdout_handle, | 721 | bytes = |
740 | mybuf, sizeof (mybuf)); | 722 | GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf)); |
741 | if (bytes < 1) | 723 | if (bytes < 1) |
742 | { | 724 | { |
743 | #if DEBUG_NAT | 725 | #if DEBUG_NAT |
744 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 726 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", |
745 | "nat", | ||
746 | "Finished reading from server stdout with code: %d\n", | 727 | "Finished reading from server stdout with code: %d\n", |
747 | bytes); | 728 | bytes); |
748 | #endif | 729 | #endif |
@@ -787,13 +768,11 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
787 | #if HAVE_SOCKADDR_IN_SIN_LEN | 768 | #if HAVE_SOCKADDR_IN_SIN_LEN |
788 | sin_addr.sin_len = sizeof (sin_addr); | 769 | sin_addr.sin_len = sizeof (sin_addr); |
789 | #endif | 770 | #endif |
790 | if ((NULL == port_start) || | 771 | if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) || |
791 | (1 != sscanf (port_start, "%d", &port)) || | ||
792 | (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) | 772 | (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) |
793 | { | 773 | { |
794 | /* should we restart gnunet-helper-nat-server? */ | 774 | /* should we restart gnunet-helper-nat-server? */ |
795 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | 775 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat", |
796 | "nat", | ||
797 | _ | 776 | _ |
798 | ("gnunet-helper-nat-server generated malformed address `%s'\n"), | 777 | ("gnunet-helper-nat-server generated malformed address `%s'\n"), |
799 | mybuf); | 778 | mybuf); |
@@ -805,16 +784,15 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
805 | } | 784 | } |
806 | sin_addr.sin_port = htons ((uint16_t) port); | 785 | sin_addr.sin_port = htons ((uint16_t) port); |
807 | #if DEBUG_NAT | 786 | #if DEBUG_NAT |
808 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 787 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", |
809 | "nat", | ||
810 | "gnunet-helper-nat-server read: %s:%d\n", mybuf, port); | 788 | "gnunet-helper-nat-server read: %s:%d\n", mybuf, port); |
811 | #endif | 789 | #endif |
812 | h->reversal_callback (h->callback_cls, | 790 | h->reversal_callback (h->callback_cls, (const struct sockaddr *) &sin_addr, |
813 | (const struct sockaddr *) &sin_addr, sizeof (sin_addr)); | 791 | sizeof (sin_addr)); |
814 | h->server_read_task = | 792 | h->server_read_task = |
815 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 793 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
816 | h->server_stdout_handle, | 794 | h->server_stdout_handle, &nat_server_read, |
817 | &nat_server_read, h); | 795 | h); |
818 | } | 796 | } |
819 | 797 | ||
820 | 798 | ||
@@ -827,28 +805,25 @@ nat_server_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
827 | static void | 805 | static void |
828 | start_gnunet_nat_server (struct GNUNET_NAT_Handle *h) | 806 | start_gnunet_nat_server (struct GNUNET_NAT_Handle *h) |
829 | { | 807 | { |
830 | if ((h->behind_nat == GNUNET_YES) && | 808 | if ((h->behind_nat == GNUNET_YES) && (h->enable_nat_server == GNUNET_YES) && |
831 | (h->enable_nat_server == GNUNET_YES) && | ||
832 | (h->internal_address != NULL) && | 809 | (h->internal_address != NULL) && |
833 | (NULL != (h->server_stdout = GNUNET_DISK_pipe (GNUNET_YES, | 810 | (NULL != |
834 | GNUNET_NO, GNUNET_YES)))) | 811 | (h->server_stdout = |
812 | GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES)))) | ||
835 | { | 813 | { |
836 | #if DEBUG_NAT | 814 | #if DEBUG_NAT |
837 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 815 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat" "Starting %s at `%s'\n", |
838 | "nat" | ||
839 | "Starting %s at `%s'\n", | ||
840 | "gnunet-helper-nat-server", h->internal_address); | 816 | "gnunet-helper-nat-server", h->internal_address); |
841 | #endif | 817 | #endif |
842 | /* Start the server process */ | 818 | /* Start the server process */ |
843 | h->server_proc = GNUNET_OS_start_process (NULL, | 819 | h->server_proc = |
844 | h->server_stdout, | 820 | GNUNET_OS_start_process (NULL, h->server_stdout, |
845 | "gnunet-helper-nat-server", | 821 | "gnunet-helper-nat-server", |
846 | "gnunet-helper-nat-server", | 822 | "gnunet-helper-nat-server", |
847 | h->internal_address, NULL); | 823 | h->internal_address, NULL); |
848 | if (h->server_proc == NULL) | 824 | if (h->server_proc == NULL) |
849 | { | 825 | { |
850 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | 826 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat", |
851 | "nat", | ||
852 | _("Failed to start %s\n"), "gnunet-helper-nat-server"); | 827 | _("Failed to start %s\n"), "gnunet-helper-nat-server"); |
853 | GNUNET_DISK_pipe_close (h->server_stdout); | 828 | GNUNET_DISK_pipe_close (h->server_stdout); |
854 | h->server_stdout = NULL; | 829 | h->server_stdout = NULL; |
@@ -857,13 +832,12 @@ start_gnunet_nat_server (struct GNUNET_NAT_Handle *h) | |||
857 | { | 832 | { |
858 | /* Close the write end of the read pipe */ | 833 | /* Close the write end of the read pipe */ |
859 | GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); | 834 | GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); |
860 | h->server_stdout_handle | 835 | h->server_stdout_handle = |
861 | = GNUNET_DISK_pipe_handle (h->server_stdout, | 836 | GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ); |
862 | GNUNET_DISK_PIPE_END_READ); | 837 | h->server_read_task = |
863 | h->server_read_task | 838 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
864 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 839 | h->server_stdout_handle, |
865 | h->server_stdout_handle, | 840 | &nat_server_read, h); |
866 | &nat_server_read, h); | ||
867 | } | 841 | } |
868 | } | 842 | } |
869 | } | 843 | } |
@@ -883,8 +857,8 @@ list_interfaces (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
883 | h->ifc_task = GNUNET_SCHEDULER_NO_TASK; | 857 | h->ifc_task = GNUNET_SCHEDULER_NO_TASK; |
884 | remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS); | 858 | remove_from_address_list_by_source (h, LAL_INTERFACE_ADDRESS); |
885 | GNUNET_OS_network_interfaces_list (&process_interfaces, h); | 859 | GNUNET_OS_network_interfaces_list (&process_interfaces, h); |
886 | h->ifc_task = GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, | 860 | h->ifc_task = |
887 | &list_interfaces, h); | 861 | GNUNET_SCHEDULER_add_delayed (h->ifc_scan_frequency, &list_interfaces, h); |
888 | } | 862 | } |
889 | 863 | ||
890 | 864 | ||
@@ -901,9 +875,9 @@ resolve_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
901 | 875 | ||
902 | h->hostname_task = GNUNET_SCHEDULER_NO_TASK; | 876 | h->hostname_task = GNUNET_SCHEDULER_NO_TASK; |
903 | remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS); | 877 | remove_from_address_list_by_source (h, LAL_HOSTNAME_DNS); |
904 | h->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, | 878 | h->hostname_dns = |
905 | HOSTNAME_RESOLVE_TIMEOUT, | 879 | GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT, |
906 | &process_hostname_ip, h); | 880 | &process_hostname_ip, h); |
907 | } | 881 | } |
908 | 882 | ||
909 | 883 | ||
@@ -921,10 +895,10 @@ resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
921 | 895 | ||
922 | h->dns_task = GNUNET_SCHEDULER_NO_TASK; | 896 | h->dns_task = GNUNET_SCHEDULER_NO_TASK; |
923 | remove_from_address_list_by_source (h, LAL_EXTERNAL_IP); | 897 | remove_from_address_list_by_source (h, LAL_EXTERNAL_IP); |
924 | h->ext_dns = GNUNET_RESOLVER_ip_get (h->external_address, | 898 | h->ext_dns = |
925 | AF_INET, | 899 | GNUNET_RESOLVER_ip_get (h->external_address, AF_INET, |
926 | GNUNET_TIME_UNIT_MINUTES, | 900 | GNUNET_TIME_UNIT_MINUTES, &process_external_ip, |
927 | &process_external_ip, h); | 901 | h); |
928 | } | 902 | } |
929 | 903 | ||
930 | 904 | ||
@@ -938,8 +912,8 @@ resolve_dns (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
938 | * @param addrlen actual lenght of the address | 912 | * @param addrlen actual lenght of the address |
939 | */ | 913 | */ |
940 | static void | 914 | static void |
941 | upnp_add (void *cls, | 915 | upnp_add (void *cls, int add_remove, const struct sockaddr *addr, |
942 | int add_remove, const struct sockaddr *addr, socklen_t addrlen) | 916 | socklen_t addrlen) |
943 | { | 917 | { |
944 | struct GNUNET_NAT_Handle *h = cls; | 918 | struct GNUNET_NAT_Handle *h = cls; |
945 | struct LocalAddressList *pos; | 919 | struct LocalAddressList *pos; |
@@ -955,13 +929,12 @@ upnp_add (void *cls, | |||
955 | while (NULL != (pos = next)) | 929 | while (NULL != (pos = next)) |
956 | { | 930 | { |
957 | next = pos->next; | 931 | next = pos->next; |
958 | if ((pos->source != LAL_UPNP) || | 932 | if ((pos->source != LAL_UPNP) || (pos->addrlen != addrlen) || |
959 | (pos->addrlen != addrlen) || (0 != memcmp (&pos[1], addr, addrlen))) | 933 | (0 != memcmp (&pos[1], addr, addrlen))) |
960 | continue; | 934 | continue; |
961 | GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos); | 935 | GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, pos); |
962 | if (NULL != h->address_callback) | 936 | if (NULL != h->address_callback) |
963 | h->address_callback (h->callback_cls, | 937 | h->address_callback (h->callback_cls, GNUNET_NO, |
964 | GNUNET_NO, | ||
965 | (const struct sockaddr *) &pos[1], pos->addrlen); | 938 | (const struct sockaddr *) &pos[1], pos->addrlen); |
966 | GNUNET_free (pos); | 939 | GNUNET_free (pos); |
967 | return; /* only remove once */ | 940 | return; /* only remove once */ |
@@ -1036,8 +1009,9 @@ add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
1036 | GNUNET_break (0); | 1009 | GNUNET_break (0); |
1037 | break; | 1010 | break; |
1038 | } | 1011 | } |
1039 | if (0 != memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, | 1012 | if (0 != |
1040 | &any, sizeof (struct in6_addr))) | 1013 | memcmp (&((const struct sockaddr_in6 *) sa)->sin6_addr, &any, |
1014 | sizeof (struct in6_addr))) | ||
1041 | add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, | 1015 | add_to_address_list (h, LAL_BINDTO_ADDRESS, sa, |
1042 | sizeof (struct sockaddr_in6)); | 1016 | sizeof (struct sockaddr_in6)); |
1043 | break; | 1017 | break; |
@@ -1068,12 +1042,9 @@ add_from_bind (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
1068 | * @return NULL on error, otherwise handle that can be used to unregister | 1042 | * @return NULL on error, otherwise handle that can be used to unregister |
1069 | */ | 1043 | */ |
1070 | struct GNUNET_NAT_Handle * | 1044 | struct GNUNET_NAT_Handle * |
1071 | GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | 1045 | GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, int is_tcp, |
1072 | int is_tcp, | 1046 | uint16_t adv_port, unsigned int num_addrs, |
1073 | uint16_t adv_port, | 1047 | const struct sockaddr **addrs, const socklen_t * addrlens, |
1074 | unsigned int num_addrs, | ||
1075 | const struct sockaddr **addrs, | ||
1076 | const socklen_t * addrlens, | ||
1077 | GNUNET_NAT_AddressCallback address_callback, | 1048 | GNUNET_NAT_AddressCallback address_callback, |
1078 | GNUNET_NAT_ReversalCallback reversal_callback, | 1049 | GNUNET_NAT_ReversalCallback reversal_callback, |
1079 | void *callback_cls) | 1050 | void *callback_cls) |
@@ -1083,8 +1054,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1083 | unsigned int i; | 1054 | unsigned int i; |
1084 | 1055 | ||
1085 | #if DEBUG_NAT | 1056 | #if DEBUG_NAT |
1086 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 1057 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", |
1087 | "nat", | ||
1088 | "Registered with NAT service at port %u with %u IP bound local addresses\n", | 1058 | "Registered with NAT service at port %u with %u IP bound local addresses\n", |
1089 | (unsigned int) adv_port, num_addrs); | 1059 | (unsigned int) adv_port, num_addrs); |
1090 | #endif | 1060 | #endif |
@@ -1114,16 +1084,14 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1114 | if (GNUNET_OK == | 1084 | if (GNUNET_OK == |
1115 | GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS")) | 1085 | GNUNET_CONFIGURATION_have_value (cfg, "nat", "INTERNAL_ADDRESS")) |
1116 | { | 1086 | { |
1117 | (void) GNUNET_CONFIGURATION_get_value_string (cfg, | 1087 | (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat", |
1118 | "nat", | ||
1119 | "INTERNAL_ADDRESS", | 1088 | "INTERNAL_ADDRESS", |
1120 | &h->internal_address); | 1089 | &h->internal_address); |
1121 | } | 1090 | } |
1122 | if ((h->internal_address != NULL) && | 1091 | if ((h->internal_address != NULL) && |
1123 | (inet_pton (AF_INET, h->internal_address, &in_addr) != 1)) | 1092 | (inet_pton (AF_INET, h->internal_address, &in_addr) != 1)) |
1124 | { | 1093 | { |
1125 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | 1094 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat", |
1126 | "nat", | ||
1127 | _("Malformed %s `%s' given in configuration!\n"), | 1095 | _("Malformed %s `%s' given in configuration!\n"), |
1128 | "INTERNAL_ADDRESS", h->internal_address); | 1096 | "INTERNAL_ADDRESS", h->internal_address); |
1129 | GNUNET_free (h->internal_address); | 1097 | GNUNET_free (h->internal_address); |
@@ -1133,57 +1101,45 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1133 | if (GNUNET_OK == | 1101 | if (GNUNET_OK == |
1134 | GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS")) | 1102 | GNUNET_CONFIGURATION_have_value (cfg, "nat", "EXTERNAL_ADDRESS")) |
1135 | { | 1103 | { |
1136 | (void) GNUNET_CONFIGURATION_get_value_string (cfg, | 1104 | (void) GNUNET_CONFIGURATION_get_value_string (cfg, "nat", |
1137 | "nat", | ||
1138 | "EXTERNAL_ADDRESS", | 1105 | "EXTERNAL_ADDRESS", |
1139 | &h->external_address); | 1106 | &h->external_address); |
1140 | } | 1107 | } |
1141 | if ((h->external_address != NULL) && | 1108 | if ((h->external_address != NULL) && |
1142 | (inet_pton (AF_INET, h->external_address, &in_addr) != 1)) | 1109 | (inet_pton (AF_INET, h->external_address, &in_addr) != 1)) |
1143 | { | 1110 | { |
1144 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | 1111 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat", |
1145 | "nat", | ||
1146 | _("Malformed %s `%s' given in configuration!\n"), | 1112 | _("Malformed %s `%s' given in configuration!\n"), |
1147 | "EXTERNAL_ADDRESS", h->external_address); | 1113 | "EXTERNAL_ADDRESS", h->external_address); |
1148 | GNUNET_free (h->external_address); | 1114 | GNUNET_free (h->external_address); |
1149 | h->external_address = NULL; | 1115 | h->external_address = NULL; |
1150 | } | 1116 | } |
1151 | h->behind_nat = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1117 | h->behind_nat = |
1152 | "nat", "BEHIND_NAT"); | 1118 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "BEHIND_NAT"); |
1153 | h->nat_punched = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1119 | h->nat_punched = |
1154 | "nat", "PUNCHED_NAT"); | 1120 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "PUNCHED_NAT"); |
1155 | h->enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1121 | h->enable_nat_client = |
1156 | "nat", | 1122 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_CLIENT"); |
1157 | "ENABLE_NAT_CLIENT"); | 1123 | h->enable_nat_server = |
1158 | h->enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1124 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_NAT_SERVER"); |
1159 | "nat", | 1125 | h->enable_upnp = |
1160 | "ENABLE_NAT_SERVER"); | 1126 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "ENABLE_UPNP"); |
1161 | h->enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1127 | h->use_localaddresses = |
1162 | "nat", "ENABLE_UPNP"); | 1128 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_LOCALADDR"); |
1163 | h->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1129 | h->use_hostname = |
1164 | "nat", | 1130 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "USE_HOSTNAME"); |
1165 | "USE_LOCALADDR"); | 1131 | h->disable_ipv6 = |
1166 | h->use_hostname = GNUNET_CONFIGURATION_get_value_yesno (cfg, | 1132 | GNUNET_CONFIGURATION_get_value_yesno (cfg, "nat", "DISABLEV6"); |
1167 | "nat", | ||
1168 | "USE_HOSTNAME"); | ||
1169 | h->disable_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
1170 | "nat", "DISABLEV6"); | ||
1171 | if (GNUNET_OK != | 1133 | if (GNUNET_OK != |
1172 | GNUNET_CONFIGURATION_get_value_time (cfg, | 1134 | GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "DYNDNS_FREQUENCY", |
1173 | "nat", | ||
1174 | "DYNDNS_FREQUENCY", | ||
1175 | &h->dyndns_frequency)) | 1135 | &h->dyndns_frequency)) |
1176 | h->dyndns_frequency = DYNDNS_FREQUENCY; | 1136 | h->dyndns_frequency = DYNDNS_FREQUENCY; |
1177 | if (GNUNET_OK != | 1137 | if (GNUNET_OK != |
1178 | GNUNET_CONFIGURATION_get_value_time (cfg, | 1138 | GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "IFC_SCAN_FREQUENCY", |
1179 | "nat", | ||
1180 | "IFC_SCAN_FREQUENCY", | ||
1181 | &h->ifc_scan_frequency)) | 1139 | &h->ifc_scan_frequency)) |
1182 | h->ifc_scan_frequency = IFC_SCAN_FREQUENCY; | 1140 | h->ifc_scan_frequency = IFC_SCAN_FREQUENCY; |
1183 | if (GNUNET_OK != | 1141 | if (GNUNET_OK != |
1184 | GNUNET_CONFIGURATION_get_value_time (cfg, | 1142 | GNUNET_CONFIGURATION_get_value_time (cfg, "nat", "HOSTNAME_DNS_FREQUENCY", |
1185 | "nat", | ||
1186 | "HOSTNAME_DNS_FREQUENCY", | ||
1187 | &h->hostname_dns_frequency)) | 1143 | &h->hostname_dns_frequency)) |
1188 | h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY; | 1144 | h->hostname_dns_frequency = HOSTNAME_DNS_FREQUENCY; |
1189 | 1145 | ||
@@ -1191,8 +1147,8 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1191 | h->enable_nat_server = GNUNET_NO; | 1147 | h->enable_nat_server = GNUNET_NO; |
1192 | 1148 | ||
1193 | /* Check if NAT was hole-punched */ | 1149 | /* Check if NAT was hole-punched */ |
1194 | if ((NULL != h->address_callback) && | 1150 | if ((NULL != h->address_callback) && (h->external_address != NULL) && |
1195 | (h->external_address != NULL) && (h->nat_punched == GNUNET_YES)) | 1151 | (h->nat_punched == GNUNET_YES)) |
1196 | { | 1152 | { |
1197 | h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h); | 1153 | h->dns_task = GNUNET_SCHEDULER_add_now (&resolve_dns, h); |
1198 | h->enable_nat_server = GNUNET_NO; | 1154 | h->enable_nat_server = GNUNET_NO; |
@@ -1200,8 +1156,7 @@ GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
1200 | } | 1156 | } |
1201 | 1157 | ||
1202 | /* Test for SUID binaries */ | 1158 | /* Test for SUID binaries */ |
1203 | if ((h->behind_nat == GNUNET_YES) && | 1159 | if ((h->behind_nat == GNUNET_YES) && (GNUNET_YES == h->enable_nat_server) && |
1204 | (GNUNET_YES == h->enable_nat_server) && | ||
1205 | (GNUNET_YES != | 1160 | (GNUNET_YES != |
1206 | GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server"))) | 1161 | GNUNET_OS_check_helper_binary ("gnunet-helper-nat-server"))) |
1207 | { | 1162 | { |
@@ -1313,8 +1268,7 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h) | |||
1313 | { | 1268 | { |
1314 | GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal); | 1269 | GNUNET_CONTAINER_DLL_remove (h->lal_head, h->lal_tail, lal); |
1315 | if (NULL != h->address_callback) | 1270 | if (NULL != h->address_callback) |
1316 | h->address_callback (h->callback_cls, | 1271 | h->address_callback (h->callback_cls, GNUNET_NO, |
1317 | GNUNET_NO, | ||
1318 | (const struct sockaddr *) &lal[1], lal->addrlen); | 1272 | (const struct sockaddr *) &lal[1], lal->addrlen); |
1319 | GNUNET_free (lal); | 1273 | GNUNET_free (lal); |
1320 | } | 1274 | } |
@@ -1349,8 +1303,7 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, | |||
1349 | 1303 | ||
1350 | if (h->internal_address == NULL) | 1304 | if (h->internal_address == NULL) |
1351 | { | 1305 | { |
1352 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | 1306 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "nat", |
1353 | "nat", | ||
1354 | _ | 1307 | _ |
1355 | ("Internal IP address not known, cannot use ICMP NAT traversal method\n")); | 1308 | ("Internal IP address not known, cannot use ICMP NAT traversal method\n")); |
1356 | return; | 1309 | return; |
@@ -1363,17 +1316,14 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, | |||
1363 | } | 1316 | } |
1364 | GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port); | 1317 | GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port); |
1365 | #if DEBUG_NAT | 1318 | #if DEBUG_NAT |
1366 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 1319 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", |
1367 | "nat", | ||
1368 | _("Running gnunet-helper-nat-client %s %s %u\n"), | 1320 | _("Running gnunet-helper-nat-client %s %s %u\n"), |
1369 | h->internal_address, inet4, (unsigned int) h->adv_port); | 1321 | h->internal_address, inet4, (unsigned int) h->adv_port); |
1370 | #endif | 1322 | #endif |
1371 | proc = GNUNET_OS_start_process (NULL, | 1323 | proc = |
1372 | NULL, | 1324 | GNUNET_OS_start_process (NULL, NULL, "gnunet-helper-nat-client", |
1373 | "gnunet-helper-nat-client", | 1325 | "gnunet-helper-nat-client", h->internal_address, |
1374 | "gnunet-helper-nat-client", | 1326 | inet4, port_as_string, NULL); |
1375 | h->internal_address, | ||
1376 | inet4, port_as_string, NULL); | ||
1377 | if (NULL == proc) | 1327 | if (NULL == proc) |
1378 | return; | 1328 | return; |
1379 | /* we know that the gnunet-helper-nat-client will terminate virtually | 1329 | /* we know that the gnunet-helper-nat-client will terminate virtually |
@@ -1394,8 +1344,8 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, | |||
1394 | * GNUNET_SYSERR if the address is malformed | 1344 | * GNUNET_SYSERR if the address is malformed |
1395 | */ | 1345 | */ |
1396 | int | 1346 | int |
1397 | GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, | 1347 | GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr, |
1398 | const void *addr, socklen_t addrlen) | 1348 | socklen_t addrlen) |
1399 | { | 1349 | { |
1400 | struct LocalAddressList *pos; | 1350 | struct LocalAddressList *pos; |
1401 | const struct sockaddr_in *in4; | 1351 | const struct sockaddr_in *in4; |