From: Christian Grothoff Date: Wed, 30 Nov 2016 07:22:59 +0000 (+0100) Subject: moving basic logic for launching nat-client helper to new NAT service X-Git-Tag: taler-0.2.1~568 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=738cbe38dec2f275994c822631bf3aba02a6e6d0;p=oweals%2Fgnunet.git moving basic logic for launching nat-client helper to new NAT service --- diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c index 4867f5675..e476da12d 100644 --- a/src/nat/gnunet-service-nat_helper.c +++ b/src/nat/gnunet-service-nat_helper.c @@ -342,4 +342,69 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h) GNUNET_free (h); } + +/** + * We want to connect to a peer that is behind NAT. Run the + * gnunet-helper-nat-client to send dummy ICMP responses to cause + * that peer to connect to us (connection reversal). + * + * @param internal_address out internal address to use + * @param sa the address of the peer (IPv4-only) + * @return #GNUNET_SYSERR on error, + * #GNUNET_OK otherwise + */ +int +GN_request_connection_reversal (const char *internal_address, + const struct sockaddr_in *sa) +{ + char inet4[INET_ADDRSTRLEN]; + char port_as_string[6]; + struct GNUNET_OS_Process *proc; + char *binary; + + 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 GNUNET_SYSERR; + } + GNUNET_snprintf (port_as_string, + sizeof (port_as_string), + "%d", + ntohs (sa->sin_port)); + LOG (GNUNET_ERROR_TYPE_DEBUG, + _("Running gnunet-helper-nat-client %s %s %u\n"), + internal_address, + inet4, + ntohs (sa->sin_port)); + binary + = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client"); + proc + = GNUNET_OS_start_process (GNUNET_NO, + 0, + NULL, + NULL, + NULL, + binary, + "gnunet-helper-nat-client", + internal_address, + inet4, + port_as_string, + NULL); + GNUNET_free (binary); + if (NULL == proc) + return GNUNET_SYSERR; + /* we know that the gnunet-helper-nat-client will terminate virtually + * instantly */ + GNUNET_OS_process_wait (proc); + GNUNET_OS_process_destroy (proc); + return GNUNET_OK; +} + + /* end of gnunet-service-nat_helper.c */ diff --git a/src/nat/gnunet-service-nat_helper.h b/src/nat/gnunet-service-nat_helper.h index c3074d9ad..861d62c1d 100644 --- a/src/nat/gnunet-service-nat_helper.h +++ b/src/nat/gnunet-service-nat_helper.h @@ -71,4 +71,19 @@ void GN_stop_gnunet_nat_server_ (struct HelperContext *h); +/** + * We want to connect to a peer that is behind NAT. Run the + * gnunet-helper-nat-client to send dummy ICMP responses to cause + * that peer to connect to us (connection reversal). + * + * @param internal_address out internal address to use + * @param sa the address of the peer (IPv4-only) + * @return #GNUNET_SYSERR on error, + * #GNUNET_OK otherwise + */ +int +GN_request_connection_reversal (const char *internal_address, + const struct sockaddr_in *sa); + + /* end of gnunet-service-nat_helper.h */