From b0a0aeac28295e7de9b427d621803ad6a3a2d76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1=D1?= =?UTF-8?q?=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= Date: Wed, 29 Feb 2012 18:39:09 +0400 Subject: [PATCH] Try to not to create dead-end NAT sessions --- src/include/gnunet_nat_lib.h | 4 +++- src/nat/nat.c | 13 ++++++++----- src/transport/plugin_transport_tcp.c | 19 ++++++++++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/include/gnunet_nat_lib.h b/src/include/gnunet_nat_lib.h index 6c1db80..a46b453 100644 --- a/src/include/gnunet_nat_lib.h +++ b/src/include/gnunet_nat_lib.h @@ -117,8 +117,10 @@ GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *h, const void *addr, * * @param h handle (used for configuration) * @param sa the address of the peer (IPv4-only) + * @return GNUNET_SYSERR on error, GNUNET_NO if nat client is disabled, + * GNUNET_OK otherwise */ -void +int GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, const struct sockaddr_in *sa); diff --git a/src/nat/nat.c b/src/nat/nat.c index 5117f5d..3e5c4c1 100644 --- a/src/nat/nat.c +++ b/src/nat/nat.c @@ -1310,8 +1310,10 @@ GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *h) * * @param h NAT handle for us (largely used for configuration) * @param sa the address of the peer (IPv4-only) + * @return GNUNET_SYSERR on error, GNUNET_NO if nat client is disabled, + * GNUNET_OK otherwise */ -void +int GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, const struct sockaddr_in *sa) { @@ -1320,20 +1322,20 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, struct GNUNET_OS_Process *proc; if (GNUNET_YES != h->enable_nat_client) - return; /* not permitted / possible */ + return GNUNET_NO; /* not permitted / possible */ if (h->internal_address == NULL) { LOG (GNUNET_ERROR_TYPE_WARNING, "nat", _ ("Internal IP address not known, cannot use ICMP NAT traversal method\n")); - return; + return GNUNET_SYSERR; } GNUNET_assert (sa->sin_family == AF_INET); if (NULL == inet_ntop (AF_INET, &sa->sin_addr, inet4, INET_ADDRSTRLEN)) { GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "inet_ntop"); - return; + return GNUNET_SYSERR; } GNUNET_snprintf (port_as_string, sizeof (port_as_string), "%d", h->adv_port); #if DEBUG_NAT @@ -1347,11 +1349,12 @@ GNUNET_NAT_run_client (struct GNUNET_NAT_Handle *h, "gnunet-helper-nat-client", h->internal_address, inet4, port_as_string, NULL); if (NULL == proc) - return; + return GNUNET_SYSERR; /* we know that the gnunet-helper-nat-client will terminate virtually * instantly */ GNUNET_OS_process_wait (proc); GNUNET_OS_process_close (proc); + return GNUNET_OK; } diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c index 2b2d728..1477137 100644 --- a/src/transport/plugin_transport_tcp.c +++ b/src/transport/plugin_transport_tcp.c @@ -1129,8 +1129,21 @@ tcp_plugin_get_session (void *cls, "Created NAT WAIT connection to `%4s' at `%s'\n", GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs)); #endif - GNUNET_NAT_run_client (plugin->nat, &a4); - return session; + if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4)) + return session; + else + { + /* This is necessary for disconnect_session() to work */ + GNUNET_CONTAINER_multihashmap_put(plugin->sessionmap, &address->peer.hashPubKey, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); + session->addr = GNUNET_malloc (addrlen); + memcpy (session->addr, address->address, addrlen); + session->addrlen = addrlen; + + GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove ( + plugin->nat_wait_conns, &address->peer.hashPubKey, session)); + disconnect_session (session); + session = NULL; + } } /* create new outbound session */ @@ -1141,7 +1154,7 @@ tcp_plugin_get_session (void *cls, #if DEBUG_TCP GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "tcp", "Failed to create connection to `%4s' at `%s'\n", - GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs)); + GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs)); #endif return NULL; } -- 1.7.4