diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-02-23 13:49:06 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-02-23 13:49:06 +0000 |
commit | 989554955ea8a1ea0932b52912eb916d91181582 (patch) | |
tree | 1893a1ea2c06c47edcfe95e1d9e791646906f44a /src | |
parent | 9c33336202e5a0e0ab610ef224cb4437f50c244d (diff) | |
download | gnunet-989554955ea8a1ea0932b52912eb916d91181582.tar.gz gnunet-989554955ea8a1ea0932b52912eb916d91181582.zip |
mangle hellos so that external address gets sent out
Diffstat (limited to 'src')
-rw-r--r-- | src/transport/plugin_transport_udp_nat.c | 83 |
1 files changed, 68 insertions, 15 deletions
diff --git a/src/transport/plugin_transport_udp_nat.c b/src/transport/plugin_transport_udp_nat.c index e2976fdab..c2f30887b 100644 --- a/src/transport/plugin_transport_udp_nat.c +++ b/src/transport/plugin_transport_udp_nat.c | |||
@@ -324,11 +324,16 @@ struct Plugin | |||
324 | 324 | ||
325 | /** | 325 | /** |
326 | * Starting port, will listen on num_ports (if available) starting | 326 | * Starting port, will listen on num_ports (if available) starting |
327 | * at this number | 327 | * at this number (or will listen only on this port if non-nat'd) |
328 | */ | 328 | */ |
329 | uint16_t starting_port; | 329 | uint16_t starting_port; |
330 | 330 | ||
331 | /** | 331 | /** |
332 | * Main listen port, only needed for testing... | ||
333 | */ | ||
334 | uint16_t listen_port; | ||
335 | |||
336 | /** | ||
332 | * Starting port for sending out crazy messages | 337 | * Starting port for sending out crazy messages |
333 | */ | 338 | */ |
334 | uint32_t random_starting_port; | 339 | uint32_t random_starting_port; |
@@ -338,12 +343,17 @@ struct Plugin | |||
338 | */ | 343 | */ |
339 | uint16_t num_ports; | 344 | uint16_t num_ports; |
340 | 345 | ||
341 | /* | 346 | /** |
342 | * The external address given to us by the user. Must be actual | 347 | * The external address given to us by the user. Must be actual |
343 | * outside visible address for NAT punching to work. | 348 | * outside visible address for NAT punching to work. |
344 | */ | 349 | */ |
345 | char *external_address; | 350 | char *external_address; |
346 | 351 | ||
352 | /** | ||
353 | * The internal address given to us by the user (or discovered). | ||
354 | */ | ||
355 | char *internal_address; | ||
356 | |||
347 | /* | 357 | /* |
348 | * FD Read set | 358 | * FD Read set |
349 | */ | 359 | */ |
@@ -634,6 +644,14 @@ udp_nat_plugin_send (void *cls, | |||
634 | ssize_t sent; | 644 | ssize_t sent; |
635 | struct MessageQueue *temp_message; | 645 | struct MessageQueue *temp_message; |
636 | struct PeerSession *peer_session; | 646 | struct PeerSession *peer_session; |
647 | struct sockaddr_in *sockaddr = (struct sockaddr_in *)addr; | ||
648 | int other_peer_natd; | ||
649 | |||
650 | other_peer_natd = GNUNET_NO; | ||
651 | if ((sockaddr->sin_family == AF_INET) && (ntohs(sockaddr->sin_port) == 0)) | ||
652 | { | ||
653 | other_peer_natd = GNUNET_YES; | ||
654 | } | ||
637 | 655 | ||
638 | /** | 656 | /** |
639 | * FIXME: find which sock in udp_nat_socks to actually send the message on, | 657 | * FIXME: find which sock in udp_nat_socks to actually send the message on, |
@@ -641,10 +659,9 @@ udp_nat_plugin_send (void *cls, | |||
641 | * udp sock (or address, or index of udp sock array...) | 659 | * udp sock (or address, or index of udp sock array...) |
642 | */ | 660 | */ |
643 | sent = 0; | 661 | sent = 0; |
644 | #if DISTINGUISH | 662 | |
645 | if (plugin->behind_nat == GNUNET_NO) | 663 | if (other_peer_natd == GNUNET_YES) |
646 | { | 664 | { |
647 | #endif | ||
648 | peer_session = find_session(plugin, target); | 665 | peer_session = find_session(plugin, target); |
649 | if (peer_session == NULL) /* We have a new peer to add */ | 666 | if (peer_session == NULL) /* We have a new peer to add */ |
650 | { | 667 | { |
@@ -675,7 +692,7 @@ udp_nat_plugin_send (void *cls, | |||
675 | peer_session->messages->cont_cls = cont_cls; | 692 | peer_session->messages->cont_cls = cont_cls; |
676 | #if DEBUG_UDP_NAT | 693 | #if DEBUG_UDP_NAT |
677 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat", | 694 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp-nat", |
678 | _("Set up peer session for peer %s\n"), GNUNET_i2s(target)); | 695 | _("Other peer is NAT'd, set up peer session for peer %s\n"), GNUNET_i2s(target)); |
679 | #endif | 696 | #endif |
680 | run_gnunet_nat_client(plugin, addr, addrlen); | 697 | run_gnunet_nat_client(plugin, addr, addrlen); |
681 | 698 | ||
@@ -699,13 +716,11 @@ udp_nat_plugin_send (void *cls, | |||
699 | peer_session->messages = temp_message; | 716 | peer_session->messages = temp_message; |
700 | } | 717 | } |
701 | } | 718 | } |
702 | #if DISTINGUISH | ||
703 | } | 719 | } |
704 | else /* We are behind a NAT, so we can just send the message as is */ | 720 | else /* Other peer not behind a NAT, so we can just send the message as is */ |
705 | { | 721 | { |
706 | sent = udp_nat_real_send(cls, udp_nat_socks[0], target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls); | 722 | sent = udp_nat_real_send(cls, udp_nat_socks[0]->desc, target, msgbuf, msgbuf_size, priority, timeout, addr, addrlen, cont, cont_cls); |
707 | } | 723 | } |
708 | #endif | ||
709 | 724 | ||
710 | return sent; | 725 | return sent; |
711 | } | 726 | } |
@@ -737,13 +752,22 @@ process_interfaces (void *cls, | |||
737 | if (af == AF_INET) | 752 | if (af == AF_INET) |
738 | { | 753 | { |
739 | v4 = (struct sockaddr_in *) addr; | 754 | v4 = (struct sockaddr_in *) addr; |
740 | v4->sin_port = htons (plugin->starting_port); | 755 | if (plugin->behind_nat == GNUNET_YES) |
756 | { | ||
757 | inet_pton(AF_INET, plugin->external_address, &v4->sin_addr); | ||
758 | v4->sin_port = htons (0); /* Indicates to receiver we are behind NAT */ | ||
759 | } | ||
760 | else | ||
761 | v4->sin_port = htons (plugin->listen_port); | ||
741 | } | 762 | } |
742 | else | 763 | else |
743 | { | 764 | { |
744 | GNUNET_assert (af == AF_INET6); | 765 | GNUNET_assert (af == AF_INET6); |
745 | v6 = (struct sockaddr_in6 *) addr; | 766 | v6 = (struct sockaddr_in6 *) addr; |
746 | v6->sin6_port = htons (plugin->starting_port); | 767 | if (plugin->behind_nat == GNUNET_YES) |
768 | v6->sin6_port = htons (0); | ||
769 | else | ||
770 | v6->sin6_port = htons (plugin->listen_port); | ||
747 | } | 771 | } |
748 | 772 | ||
749 | #if !IPV6 | 773 | #if !IPV6 |
@@ -1276,7 +1300,7 @@ udp_nat_transport_server_start (void *cls) | |||
1276 | "Starting gnunet-nat-server process\n"); | 1300 | "Starting gnunet-nat-server process\n"); |
1277 | #endif | 1301 | #endif |
1278 | /* Start the server process */ | 1302 | /* Start the server process */ |
1279 | plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->external_address, NULL); | 1303 | plugin->server_pid = GNUNET_OS_start_process(NULL, plugin->server_stdout, "gnunet-nat-server", "gnunet-nat-server", plugin->internal_address, NULL); |
1280 | if (plugin->server_pid == GNUNET_SYSERR) | 1304 | if (plugin->server_pid == GNUNET_SYSERR) |
1281 | return GNUNET_SYSERR; | 1305 | return GNUNET_SYSERR; |
1282 | /* Close the write end of the read pipe */ | 1306 | /* Close the write end of the read pipe */ |
@@ -1331,7 +1355,7 @@ udp_nat_transport_server_start (void *cls) | |||
1331 | serverAddrv4.sin_family = AF_INET; | 1355 | serverAddrv4.sin_family = AF_INET; |
1332 | serverAddrv4.sin_addr.s_addr = INADDR_ANY; | 1356 | serverAddrv4.sin_addr.s_addr = INADDR_ANY; |
1333 | if (i == 0) | 1357 | if (i == 0) |
1334 | serverAddrv4.sin_port = htons (plugin->starting_port); | 1358 | serverAddrv4.sin_port = htons (plugin->listen_port); |
1335 | else | 1359 | else |
1336 | serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a non-root port */ | 1360 | serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a non-root port */ |
1337 | addrlen = sizeof (serverAddrv4); | 1361 | addrlen = sizeof (serverAddrv4); |
@@ -1575,8 +1599,10 @@ libgnunet_plugin_transport_udp_nat_init (void *cls) | |||
1575 | int sockets_created; | 1599 | int sockets_created; |
1576 | int behind_nat; | 1600 | int behind_nat; |
1577 | 1601 | ||
1602 | char *internal_address; | ||
1578 | char *external_address; | 1603 | char *external_address; |
1579 | char *starting_port; | 1604 | char *starting_port; |
1605 | char *listen_port; | ||
1580 | 1606 | ||
1581 | service = GNUNET_SERVICE_start ("transport-udp-nat", env->sched, env->cfg); | 1607 | service = GNUNET_SERVICE_start ("transport-udp-nat", env->sched, env->cfg); |
1582 | if (service == NULL) | 1608 | if (service == NULL) |
@@ -1601,6 +1627,20 @@ libgnunet_plugin_transport_udp_nat_init (void *cls) | |||
1601 | GNUNET_SERVICE_stop (service); | 1627 | GNUNET_SERVICE_stop (service); |
1602 | } | 1628 | } |
1603 | 1629 | ||
1630 | if (GNUNET_OK != | ||
1631 | GNUNET_CONFIGURATION_get_value_string (env->cfg, | ||
1632 | "transport-udp-nat", | ||
1633 | "INTERNAL_ADDRESS", | ||
1634 | &internal_address)) | ||
1635 | { | ||
1636 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
1637 | "udp_nat", | ||
1638 | _ | ||
1639 | ("Require INTERNAL_ADDRESS for service `%s' in configuration!\n"), | ||
1640 | "transport-udp"); | ||
1641 | GNUNET_SERVICE_stop (service); | ||
1642 | } | ||
1643 | |||
1604 | if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (env->cfg, | 1644 | if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (env->cfg, |
1605 | "transport-udp-nat", | 1645 | "transport-udp-nat", |
1606 | "BEHIND_NAT")) | 1646 | "BEHIND_NAT")) |
@@ -1611,9 +1651,18 @@ libgnunet_plugin_transport_udp_nat_init (void *cls) | |||
1611 | GNUNET_CONFIGURATION_get_value_string (env->cfg, | 1651 | GNUNET_CONFIGURATION_get_value_string (env->cfg, |
1612 | "transport-udp-nat", | 1652 | "transport-udp-nat", |
1613 | "PORT", | 1653 | "PORT", |
1654 | &listen_port); | ||
1655 | |||
1656 | /* Yes we'll use PORT as the default configuration value, but | ||
1657 | * for testing we need two different ports for the peers so | ||
1658 | * STARTING_PORT will have to do! | ||
1659 | */ | ||
1660 | GNUNET_CONFIGURATION_get_value_string (env->cfg, | ||
1661 | "transport-udp-nat", | ||
1662 | "STARTING_PORT", | ||
1614 | &starting_port); | 1663 | &starting_port); |
1615 | 1664 | ||
1616 | mtu = 1240; | 1665 | mtu = 1240; |
1617 | if (mtu < 1200) | 1666 | if (mtu < 1200) |
1618 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | 1667 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, |
1619 | "udp_nat", | 1668 | "udp_nat", |
@@ -1622,6 +1671,10 @@ libgnunet_plugin_transport_udp_nat_init (void *cls) | |||
1622 | 1671 | ||
1623 | plugin = GNUNET_malloc (sizeof (struct Plugin)); | 1672 | plugin = GNUNET_malloc (sizeof (struct Plugin)); |
1624 | plugin->external_address = external_address; | 1673 | plugin->external_address = external_address; |
1674 | plugin->internal_address = internal_address; | ||
1675 | if (listen_port != NULL) | ||
1676 | plugin->listen_port = atoi(listen_port); | ||
1677 | |||
1625 | if (starting_port != NULL) | 1678 | if (starting_port != NULL) |
1626 | plugin->starting_port = atoi(starting_port); | 1679 | plugin->starting_port = atoi(starting_port); |
1627 | else | 1680 | else |