From 8ab1946b4136cb0b20a47661bb54dbe0820ba04f Mon Sep 17 00:00:00 2001 From: "Nathan S. Evans" Date: Fri, 11 Jun 2010 11:39:20 +0000 Subject: [PATCH] explicitly add user provided external address to known addresses when used with NAT --- src/transport/plugin_transport_tcp.c | 28 +++++++++++++++++++++++++--- src/transport/plugin_transport_udp.c | 27 +++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index 7ad3a8e4b..459057219 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c @@ -2154,6 +2154,8 @@ libgnunet_plugin_transport_tcp_init (void *cls) int only_nat_addresses; char *internal_address; char *external_address; + struct sockaddr_in in_addr; + struct IPv4TcpAddress t4; service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg); if (service == NULL) @@ -2223,6 +2225,11 @@ libgnunet_plugin_transport_tcp_init (void *cls) return NULL; } + if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1)) + { + GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address); + } + internal_address = NULL; if ((GNUNET_YES == behind_nat) && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (env->cfg, @@ -2240,6 +2247,10 @@ libgnunet_plugin_transport_tcp_init (void *cls) return NULL; } + if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1)) + { + GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address); + } aport = 0; if ((GNUNET_OK != @@ -2328,15 +2339,26 @@ libgnunet_plugin_transport_tcp_init (void *cls) GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin); - /* FIXME: do the two calls below periodically again and - not just once (since the info we get might change...) */ - GNUNET_OS_network_interfaces_list (&process_interfaces, plugin); + if (plugin->behind_nat == GNUNET_NO) + { + GNUNET_OS_network_interfaces_list (&process_interfaces, plugin); + } + plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched, env->cfg, AF_UNSPEC, HOSTNAME_RESOLVE_TIMEOUT, &process_hostname_ips, plugin); + + if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &t4.ipv4_addr) == 1)) + { + t4.t_port = htons(0); + plugin->env->notify_address (plugin->env->cls, + "tcp", + &t4, sizeof(t4), GNUNET_TIME_UNIT_FOREVER_REL); + } + return api; } diff --git a/src/transport/plugin_transport_udp.c b/src/transport/plugin_transport_udp.c index 14b80d9e9..8caabd0d5 100644 --- a/src/transport/plugin_transport_udp.c +++ b/src/transport/plugin_transport_udp.c @@ -1671,6 +1671,7 @@ libgnunet_plugin_transport_udp_init (void *cls) int only_nat_addresses; char *internal_address; char *external_address; + struct sockaddr_in in_addr; service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg); if (service == NULL) @@ -1736,6 +1737,11 @@ libgnunet_plugin_transport_udp_init (void *cls) return NULL; } + if ((external_address != NULL) && (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1)) + { + GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed EXTERNAL_ADDRESS %s given in configuration!\n", external_address); + } + internal_address = NULL; if ((GNUNET_YES == behind_nat) && (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (env->cfg, @@ -1753,6 +1759,11 @@ libgnunet_plugin_transport_udp_init (void *cls) return NULL; } + if ((internal_address != NULL) && (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1)) + { + GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", "Malformed INTERNAL_ADDRESS %s given in configuration!\n", internal_address); + } + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", @@ -1799,7 +1810,11 @@ libgnunet_plugin_transport_udp_init (void *cls) plugin->service = service; - GNUNET_OS_network_interfaces_list (&process_interfaces, plugin); + if (plugin->behind_nat == GNUNET_NO) + { + GNUNET_OS_network_interfaces_list (&process_interfaces, plugin); + } + plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched, env->cfg, AF_UNSPEC, @@ -1807,6 +1822,15 @@ libgnunet_plugin_transport_udp_init (void *cls) &process_hostname_ips, plugin); + if ((plugin->behind_nat == GNUNET_YES) && (inet_pton(AF_INET, plugin->external_address, &in_addr.sin_addr) == 1)) + { + in_addr.sin_port = htons(0); + in_addr.sin_family = AF_INET; + plugin->env->notify_address (plugin->env->cls, + "udp", + &in_addr, sizeof(in_addr), GNUNET_TIME_UNIT_FOREVER_REL); + } + sockets_created = udp_transport_server_start (plugin); GNUNET_assert (sockets_created == 1); @@ -1814,7 +1838,6 @@ libgnunet_plugin_transport_udp_init (void *cls) return api; } - void * libgnunet_plugin_transport_udp_done (void *cls) { -- 2.25.1