summaryrefslogtreecommitdiff
path: root/src/nat
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat')
-rw-r--r--src/nat/gnunet-nat.c223
-rw-r--r--src/nat/gnunet-service-nat_helper.c211
-rw-r--r--src/nat/gnunet-service-nat_mini.c263
3 files changed, 293 insertions, 404 deletions
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c
index 275553203..6c533374e 100644
--- a/src/nat/gnunet-nat.c
+++ b/src/nat/gnunet-nat.c
@@ -120,20 +120,19 @@ test_finished ()
*/
static void
address_cb (void *cls,
- void **app_ctx,
- int add_remove,
- enum GNUNET_NAT_AddressClass ac,
- const struct sockaddr *addr,
- socklen_t addrlen)
+ void **app_ctx,
+ int add_remove,
+ enum GNUNET_NAT_AddressClass ac,
+ const struct sockaddr *addr,
+ socklen_t addrlen)
{
(void) cls;
(void) app_ctx;
-
+
fprintf (stdout,
"%s %s (%d)\n",
add_remove ? "+" : "-",
- GNUNET_a2s (addr,
- addrlen),
+ GNUNET_a2s (addr, addrlen),
(int) ac);
}
@@ -149,13 +148,12 @@ address_cb (void *cls,
*/
static void
reversal_cb (void *cls,
- const struct sockaddr *remote_addr,
- socklen_t remote_addrlen)
+ const struct sockaddr *remote_addr,
+ socklen_t remote_addrlen)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Connection reversal requested by %s\n",
- GNUNET_a2s (remote_addr,
- remote_addrlen));
+ "Connection reversal requested by %s\n",
+ GNUNET_a2s (remote_addr, remote_addrlen));
}
@@ -194,9 +192,9 @@ stun_read_task (void *cls)
ssize_t size;
rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- ls,
- &stun_read_task,
- NULL);
+ ls,
+ &stun_read_task,
+ NULL);
size = GNUNET_NETWORK_socket_recvfrom_amount (ls);
if (size > 0)
{
@@ -212,10 +210,10 @@ stun_read_task (void *cls)
ssize_t ret;
ret = GNUNET_NETWORK_socket_recvfrom (ls,
- buf,
- size + 1,
- (struct sockaddr *) &sa,
- &salen);
+ buf,
+ size + 1,
+ (struct sockaddr *) &sa,
+ &salen);
if (ret != size)
{
GNUNET_break (0);
@@ -224,10 +222,10 @@ stun_read_task (void *cls)
return;
}
(void) GNUNET_NAT_stun_handle_packet (nh,
- (const struct sockaddr *) &sa,
- salen,
- buf,
- ret);
+ (const struct sockaddr *) &sa,
+ salen,
+ buf,
+ ret);
}
}
@@ -254,8 +252,7 @@ run (void *cls,
if (use_tcp && use_udp)
{
- GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Cannot use TCP and UDP\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
global_ret = 1;
return;
}
@@ -265,13 +262,11 @@ run (void *cls,
if (use_udp)
proto = IPPROTO_UDP;
- GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
- NULL);
+ GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
if (0 == proto)
{
- GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Must specify either TCP or UDP\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n");
global_ret = 1;
return;
}
@@ -281,28 +276,26 @@ run (void *cls,
remote_sa = NULL;
if (NULL != local_addr)
{
- local_len = (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr,
- &af,
- &local_sa);
+ local_len =
+ (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, &af, &local_sa);
if (0 == local_len)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Invalid socket address `%s'\n",
- local_addr);
+ "Invalid socket address `%s'\n",
+ local_addr);
goto fail_and_shutdown;
}
}
if (NULL != remote_addr)
{
- remote_len = GNUNET_STRINGS_parse_socket_addr (remote_addr,
- &af,
- &remote_sa);
+ remote_len =
+ GNUNET_STRINGS_parse_socket_addr (remote_addr, &af, &remote_sa);
if (0 == remote_len)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Invalid socket address `%s'\n",
- remote_addr);
+ "Invalid socket address `%s'\n",
+ remote_addr);
goto fail_and_shutdown;
}
}
@@ -312,19 +305,19 @@ run (void *cls,
if (NULL == section_name)
section_name = GNUNET_strdup ("undefined");
nh = GNUNET_NAT_register (c,
- section_name,
- proto,
- 1,
- (const struct sockaddr **) &local_sa,
- &local_len,
- &address_cb,
- (listen_reversal) ? &reversal_cb : NULL,
- NULL);
+ section_name,
+ proto,
+ 1,
+ (const struct sockaddr **) &local_sa,
+ &local_len,
+ &address_cb,
+ (listen_reversal) ? &reversal_cb : NULL,
+ NULL);
}
else if (listen_reversal)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Use of `-W` only effective in combination with `-i`\n");
+ "Use of `-W` only effective in combination with `-i`\n");
goto fail_and_shutdown;
}
@@ -332,33 +325,32 @@ run (void *cls,
{
int ret;
- if ( (NULL == nh) ||
- (sizeof (struct sockaddr_in) != local_len) )
+ if ((NULL == nh) || (sizeof (struct sockaddr_in) != local_len))
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Require IPv4 local address to initiate connection reversal\n");
+ "Require IPv4 local address to initiate connection reversal\n");
goto fail_and_shutdown;
}
if (sizeof (struct sockaddr_in) != remote_len)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Require IPv4 reversal target address\n");
+ "Require IPv4 reversal target address\n");
goto fail_and_shutdown;
}
GNUNET_assert (AF_INET == local_sa->sa_family);
GNUNET_assert (AF_INET == remote_sa->sa_family);
ret = GNUNET_NAT_request_reversal (nh,
- (const struct sockaddr_in *) local_sa,
- (const struct sockaddr_in *) remote_sa);
+ (const struct sockaddr_in *) local_sa,
+ (const struct sockaddr_in *) remote_sa);
switch (ret)
{
case GNUNET_SYSERR:
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Connection reversal internal error\n");
+ "Connection reversal internal error\n");
break;
case GNUNET_NO:
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Connection reversal unavailable\n");
+ "Connection reversal unavailable\n");
break;
case GNUNET_OK:
/* operation in progress */
@@ -371,46 +363,38 @@ run (void *cls,
if (NULL == local_addr)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Require local address to support STUN requests\n");
+ "Require local address to support STUN requests\n");
goto fail_and_shutdown;
}
if (IPPROTO_UDP != proto)
{
- GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "STUN only supported over UDP\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n");
goto fail_and_shutdown;
}
- ls = GNUNET_NETWORK_socket_create (af,
- SOCK_DGRAM,
- IPPROTO_UDP);
+ ls = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, IPPROTO_UDP);
if (NULL == ls)
{
- GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Failed to create socket\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n");
goto fail_and_shutdown;
}
- if (GNUNET_OK !=
- GNUNET_NETWORK_socket_bind (ls,
- local_sa,
- local_len))
+ if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, local_sa, local_len))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to bind to %s: %s\n",
- GNUNET_a2s (local_sa,
- local_len),
- STRERROR (errno));
+ "Failed to bind to %s: %s\n",
+ GNUNET_a2s (local_sa, local_len),
+ strerror (errno));
goto fail_and_shutdown;
}
rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- ls,
- &stun_read_task,
- NULL);
+ ls,
+ &stun_read_task,
+ NULL);
}
GNUNET_free_non_null (remote_sa);
GNUNET_free_non_null (local_sa);
test_finished ();
return;
- fail_and_shutdown:
+fail_and_shutdown:
global_ret = 1;
GNUNET_SCHEDULER_shutdown ();
GNUNET_free_non_null (remote_sa);
@@ -426,66 +410,63 @@ run (void *cls,
* @return 0 on success, -1 on error
*/
int
-main (int argc,
- char *const argv[])
+main (int argc, char *const argv[])
{
struct GNUNET_GETOPT_CommandLineOption options[] = {
- GNUNET_GETOPT_option_string ('i',
- "in",
- "ADDRESS",
- gettext_noop ("which IP and port are we locally using to bind/listen to"),
- &local_addr),
-
- GNUNET_GETOPT_option_string ('r',
- "remote",
- "ADDRESS",
- gettext_noop ("which remote IP and port should be asked for connection reversal"),
- &remote_addr),
-
- GNUNET_GETOPT_option_string ('S',
- "section",
- NULL,
- gettext_noop ("name of configuration section to find additional options, such as manual host punching data"),
- &section_name),
+ GNUNET_GETOPT_option_string (
+ 'i',
+ "in",
+ "ADDRESS",
+ gettext_noop ("which IP and port are we locally using to bind/listen to"),
+ &local_addr),
+
+ GNUNET_GETOPT_option_string (
+ 'r',
+ "remote",
+ "ADDRESS",
+ gettext_noop (
+ "which remote IP and port should be asked for connection reversal"),
+ &remote_addr),
+
+ GNUNET_GETOPT_option_string (
+ 'S',
+ "section",
+ NULL,
+ gettext_noop (
+ "name of configuration section to find additional options, such as manual host punching data"),
+ &section_name),
GNUNET_GETOPT_option_flag ('s',
- "stun",
- gettext_noop ("enable STUN processing"),
- &do_stun),
+ "stun",
+ gettext_noop ("enable STUN processing"),
+ &do_stun),
- GNUNET_GETOPT_option_flag ('t',
- "tcp",
- gettext_noop ("use TCP"),
- &use_tcp),
+ GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp),
- GNUNET_GETOPT_option_flag ('u',
- "udp",
- gettext_noop ("use UDP"),
- &use_udp),
+ GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp),
GNUNET_GETOPT_option_flag ('W',
- "watch",
- gettext_noop ("watch for connection reversal requests"),
- &listen_reversal),
- GNUNET_GETOPT_OPTION_END
- };
+ "watch",
+ gettext_noop (
+ "watch for connection reversal requests"),
+ &listen_reversal),
+ GNUNET_GETOPT_OPTION_END};
- if (GNUNET_OK !=
- GNUNET_STRINGS_get_utf8_args (argc, argv,
- &argc, &argv))
+ if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
return 2;
if (GNUNET_OK !=
- GNUNET_PROGRAM_run (argc, argv,
- "gnunet-nat [options]",
- _("GNUnet NAT traversal autoconfigure daemon"),
- options,
+ GNUNET_PROGRAM_run (argc,
+ argv,
+ "gnunet-nat [options]",
+ _ ("GNUnet NAT traversal autoconfigure daemon"),
+ options,
&run,
- NULL))
+ NULL))
{
global_ret = 1;
}
- GNUNET_free ((void*) argv);
+ GNUNET_free ((void *) argv);
return global_ret;
}
diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c
index 43ac54adf..cf5038dd7 100644
--- a/src/nat/gnunet-service-nat_helper.c
+++ b/src/nat/gnunet-service-nat_helper.c
@@ -101,12 +101,10 @@ static void
try_again (struct HelperContext *h)
{
GNUNET_assert (NULL == h->server_read_task);
- h->server_retry_delay
- = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
- h->server_read_task
- = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
- &restart_nat_server,
- h);
+ h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
+ h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
+ &restart_nat_server,
+ h);
}
@@ -128,23 +126,16 @@ nat_server_read (void *cls)
struct sockaddr_in sin_addr;
h->server_read_task = NULL;
- memset (mybuf,
- 0,
- sizeof (mybuf));
- bytes
- = GNUNET_DISK_file_read (h->server_stdout_handle,
- mybuf,
- sizeof (mybuf));
+ memset (mybuf, 0, sizeof (mybuf));
+ bytes =
+ GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf));
if (bytes < 1)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Finished reading from server stdout with code: %d\n",
- (int) bytes);
- if (0 != GNUNET_OS_process_kill (h->server_proc,
- GNUNET_TERM_SIG))
- GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING,
- "nat",
- "kill");
+ "Finished reading from server stdout with code: %d\n",
+ (int) bytes);
+ if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
+ GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
GNUNET_OS_process_wait (h->server_proc);
GNUNET_OS_process_destroy (h->server_proc);
h->server_proc = NULL;
@@ -171,44 +162,37 @@ nat_server_read (void *cls)
}
/* construct socket address of sender */
- memset (&sin_addr,
- 0,
- sizeof (sin_addr));
+ memset (&sin_addr, 0, sizeof (sin_addr));
sin_addr.sin_family = AF_INET;
#if HAVE_SOCKADDR_IN_SIN_LEN
sin_addr.sin_len = sizeof (sin_addr);
#endif
- if ( (NULL == port_start) ||
- (1 != SSCANF (port_start,
- "%d",
- &port)) ||
- (-1 == inet_pton (AF_INET,
- mybuf,
- &sin_addr.sin_addr)))
+ if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
+ (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
{
/* should we restart gnunet-helper-nat-server? */
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- _("gnunet-helper-nat-server generated malformed address `%s'\n"),
- mybuf);
- h->server_read_task
- = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
- h->server_stdout_handle,
- &nat_server_read,
- h);
+ _ (
+ "gnunet-helper-nat-server generated malformed address `%s'\n"),
+ mybuf);
+ h->server_read_task =
+ GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+ h->server_stdout_handle,
+ &nat_server_read,
+ h);
return;
}
sin_addr.sin_port = htons ((uint16_t) port);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "gnunet-helper-nat-server read: %s:%d\n",
- mybuf,
- port);
- h->cb (h->cb_cls,
- &sin_addr);
- h->server_read_task
- = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
- h->server_stdout_handle,
- &nat_server_read,
- h);
+ "gnunet-helper-nat-server read: %s:%d\n",
+ mybuf,
+ port);
+ h->cb (h->cb_cls, &sin_addr);
+ h->server_read_task =
+ GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+ h->server_stdout_handle,
+ &nat_server_read,
+ h);
}
@@ -227,71 +211,59 @@ restart_nat_server (void *cls)
h->server_read_task = NULL;
GNUNET_assert (NULL !=
- inet_ntop (AF_INET,
- &h->internal_address,
- ia,
- sizeof (ia)));
+ inet_ntop (AF_INET, &h->internal_address, ia, sizeof (ia)));
/* Start the server process */
binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server");
- if (GNUNET_YES !=
- GNUNET_OS_check_helper_binary (binary,
- GNUNET_YES,
- ia))
+ if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia))
{
/* move instantly to max delay, as this is unlikely to be fixed */
- h->server_retry_delay
- = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
+ h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
GNUNET_free (binary);
try_again (h);
return;
}
- h->server_stdout
- = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES,
- GNUNET_NO, GNUNET_YES);
+ h->server_stdout =
+ GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if (NULL == h->server_stdout)
{
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
- "pipe");
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
GNUNET_free (binary);
try_again (h);
return;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Starting `%s' at `%s'\n",
- "gnunet-helper-nat-server",
- ia);
- h->server_proc
- = GNUNET_OS_start_process (GNUNET_NO,
- 0,
- NULL,
- h->server_stdout,
- NULL,
- binary,
- "gnunet-helper-nat-server",
- ia,
- NULL);
+ "Starting `%s' at `%s'\n",
+ "gnunet-helper-nat-server",
+ ia);
+ h->server_proc = GNUNET_OS_start_process (GNUNET_NO,
+ 0,
+ NULL,
+ h->server_stdout,
+ NULL,
+ binary,
+ "gnunet-helper-nat-server",
+ ia,
+ NULL);
GNUNET_free (binary);
if (NULL == h->server_proc)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- _("Failed to start %s\n"),
- "gnunet-helper-nat-server");
+ _ ("Failed to start %s\n"),
+ "gnunet-helper-nat-server");
GNUNET_DISK_pipe_close (h->server_stdout);
h->server_stdout = NULL;
try_again (h);
return;
}
/* Close the write end of the read pipe */
- GNUNET_DISK_pipe_close_end (h->server_stdout,
- GNUNET_DISK_PIPE_END_WRITE);
- h->server_stdout_handle
- = GNUNET_DISK_pipe_handle (h->server_stdout,
- GNUNET_DISK_PIPE_END_READ);
- h->server_read_task
- = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
- h->server_stdout_handle,
- &nat_server_read,
- h);
+ GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
+ h->server_stdout_handle =
+ GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ);
+ h->server_read_task =
+ GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+ h->server_stdout_handle,
+ &nat_server_read,
+ h);
}
@@ -307,9 +279,9 @@ restart_nat_server (void *cls)
*/
struct HelperContext *
GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
- GN_ReversalCallback cb,
- void *cb_cls,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
+ GN_ReversalCallback cb,
+ void *cb_cls,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
{
struct HelperContext *h;
@@ -344,10 +316,8 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h)
}
if (NULL != h->server_proc)
{
- if (0 != GNUNET_OS_process_kill (h->server_proc,
- GNUNET_TERM_SIG))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
- "kill");
+ if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
GNUNET_OS_process_wait (h->server_proc);
GNUNET_OS_process_destroy (h->server_proc);
h->server_proc = NULL;
@@ -379,9 +349,9 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h)
*/
int
GN_request_connection_reversal (const struct in_addr *internal_address,
- uint16_t internal_port,
- const struct in_addr *remote_v4,
- const struct GNUNET_CONFIGURATION_Handle *cfg)
+ uint16_t internal_port,
+ const struct in_addr *remote_v4,
+ const struct GNUNET_CONFIGURATION_Handle *cfg)
{
char intv4[INET_ADDRSTRLEN];
char remv4[INET_ADDRSTRLEN];
@@ -389,22 +359,14 @@ GN_request_connection_reversal (const struct in_addr *internal_address,
struct GNUNET_OS_Process *proc;
char *binary;
- if (NULL == inet_ntop (AF_INET,
- internal_address,
- intv4,
- INET_ADDRSTRLEN))
+ if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
{
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
- "inet_ntop");
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
return GNUNET_SYSERR;
}
- if (NULL == inet_ntop (AF_INET,
- remote_v4,
- remv4,
- INET_ADDRSTRLEN))
+ if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
{
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
- "inet_ntop");
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
return GNUNET_SYSERR;
}
GNUNET_snprintf (port_as_string,
@@ -412,23 +374,22 @@ GN_request_connection_reversal (const struct in_addr *internal_address,
"%d",
internal_port);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Running gnunet-helper-nat-client %s %s %u\n",
- intv4,
- remv4,
- internal_port);
+ "Running gnunet-helper-nat-client %s %s %u\n",
+ intv4,
+ remv4,
+ internal_port);
binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client");
- proc
- = GNUNET_OS_start_process (GNUNET_NO,
- 0,
- NULL,
- NULL,
- NULL,
- binary,
- "gnunet-helper-nat-client",
- intv4,
- remv4,
- port_as_string,
- NULL);
+ proc = GNUNET_OS_start_process (GNUNET_NO,
+ 0,
+ NULL,
+ NULL,
+ NULL,
+ binary,
+ "gnunet-helper-nat-client",
+ intv4,
+ remv4,
+ port_as_string,
+ NULL);
GNUNET_free (binary);
if (NULL == proc)
return GNUNET_SYSERR;
diff --git a/src/nat/gnunet-service-nat_mini.c b/src/nat/gnunet-service-nat_mini.c
index c156d0eb0..375188ff2 100644
--- a/src/nat/gnunet-service-nat_mini.c
+++ b/src/nat/gnunet-service-nat_mini.c
@@ -29,7 +29,7 @@
#include "gnunet-service-nat_mini.h"
#include "nat.h"
-#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
+#define LOG(kind, ...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
/**
* How long do we give upnpc to create a mapping?
@@ -39,12 +39,14 @@
/**
* How long do we give upnpc to remove a mapping?
*/
-#define UNMAP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
+#define UNMAP_TIMEOUT \
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
/**
* How often do we check for changes in the mapping?
*/
-#define MAP_REFRESH_FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
+#define MAP_REFRESH_FREQ \
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
/* ************************* external-ip calling ************************ */
@@ -117,30 +119,27 @@ read_external_ipv4 (void *cls)
eh->task = NULL;
ret = GNUNET_DISK_file_read (eh->r,
- &eh->buf[eh->off],
+ &eh->buf[eh->off],
sizeof (eh->buf) - eh->off);
if (ret > 0)
{
/* try to read more */
eh->off += ret;
- eh->task
- = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
- eh->r,
- &read_external_ipv4,
- eh);
+ eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+ eh->r,
+ &read_external_ipv4,
+ eh);
return;
}
eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
- if ( (eh->off > 7) &&
- (eh->buf[eh->off - 1] == '\n') )
+ if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
{
eh->buf[eh->off - 1] = '\0';
- if (1 == inet_pton (AF_INET,
- eh->buf,
- &addr))
+ if (1 == inet_pton (AF_INET, eh->buf, &addr))
{
if (0 == addr.s_addr)
- eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
+ eh->ret =
+ GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID; /* got 0.0.0.0 */
else
eh->ret = GNUNET_NAT_ERROR_SUCCESS;
}
@@ -163,9 +162,7 @@ signal_external_ip_error (void *cls)
struct GNUNET_NAT_ExternalHandle *eh = cls;
eh->task = NULL;
- eh->cb (eh->cb_cls,
- NULL,
- eh->ret);
+ eh->cb (eh->cb_cls, NULL, eh->ret);
GNUNET_free (eh);
}
@@ -178,8 +175,7 @@ signal_external_ip_error (void *cls)
* @return handle for cancellation (can only be used until @a cb is called), never NULL
*/
struct GNUNET_NAT_ExternalHandle *
-GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb,
- void *cb_cls)
+GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb, void *cb_cls)
{
struct GNUNET_NAT_ExternalHandle *eh;
@@ -188,56 +184,43 @@ GNUNET_NAT_mini_get_external_ipv4_ (GNUNET_NAT_IPCallback cb,
eh->cb_cls = cb_cls;
eh->ret = GNUNET_NAT_ERROR_SUCCESS;
if (GNUNET_SYSERR ==
- GNUNET_OS_check_helper_binary ("external-ip",
- GNUNET_NO,
- NULL))
+ GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL))
{
- LOG (GNUNET_ERROR_TYPE_INFO,
- _("`external-ip' command not found\n"));
+ LOG (GNUNET_ERROR_TYPE_INFO, _ ("`external-ip' command not found\n"));
eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
- eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
- eh);
+ eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
return eh;
}
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Running `external-ip' to determine our external IP\n");
- eh->opipe = GNUNET_DISK_pipe (GNUNET_YES,
- GNUNET_YES,
- GNUNET_NO,
- GNUNET_YES);
+ eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
if (NULL == eh->opipe)
{
eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
- eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
- eh);
+ eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
return eh;
}
- eh->eip =
- GNUNET_OS_start_process (GNUNET_NO,
- 0,
- NULL,
- eh->opipe,
- NULL,
- "external-ip",
- "external-ip",
- NULL);
+ eh->eip = GNUNET_OS_start_process (GNUNET_NO,
+ 0,
+ NULL,
+ eh->opipe,
+ NULL,
+ "external-ip",
+ "external-ip",
+ NULL);
if (NULL == eh->eip)
{
GNUNET_DISK_pipe_close (eh->opipe);
eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_FAILED;
- eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
- eh);
+ eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error, eh);
return eh;
}
- GNUNET_DISK_pipe_close_end (eh->opipe,
- GNUNET_DISK_PIPE_END_WRITE);
- eh->r = GNUNET_DISK_pipe_handle (eh->opipe,
- GNUNET_DISK_PIPE_END_READ);
- eh->task
- = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
- eh->r,
- &read_external_ipv4,
- eh);
+ GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
+ eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
+ eh->task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
+ eh->r,
+ &read_external_ipv4,
+ eh);
return eh;
}
@@ -252,10 +235,8 @@ GNUNET_NAT_mini_get_external_ipv4_cancel_ (struct GNUNET_NAT_ExternalHandle *eh)
{
if (NULL != eh->eip)
{
- (void) GNUNET_OS_process_kill (eh->eip,
- SIGKILL);
- GNUNET_break (GNUNET_OK ==
- GNUNET_OS_process_wait (eh->eip));
+ (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
+ GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (eh->eip));
GNUNET_OS_process_destroy (eh->eip);
}
if (NULL != eh->opipe)
@@ -336,7 +317,6 @@ struct GNUNET_NAT_MiniHandle
* Which port are we mapping?
*/
uint16_t port;
-
};
@@ -356,8 +336,7 @@ do_refresh (void *cls);
* @param line line of output, NULL at the end
*/
static void
-process_map_output (void *cls,
- const char *line);
+process_map_output (void *cls, const char *line);
/**
@@ -370,26 +349,22 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
{
char pstr[6];
- GNUNET_snprintf (pstr,
- sizeof (pstr),
- "%u",
- (unsigned int) mini->port);
- mini->map_cmd
- = GNUNET_OS_command_run (&process_map_output,
- mini,
- MAP_TIMEOUT,
- "upnpc",
- "upnpc",
- "-r",
- pstr,
- mini->is_tcp ? "tcp" : "udp",
- NULL);
+ GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port);
+ mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
+ mini,
+ MAP_TIMEOUT,
+ "upnpc",
+ "upnpc",
+ "-r",
+ pstr,
+ mini->is_tcp ? "tcp" : "udp",
+ NULL);
if (NULL == mini->map_cmd)
{
mini->ac (mini->ac_cls,
GNUNET_SYSERR,
NULL,
- 0,
+ 0,
GNUNET_NAT_ERROR_UPNPC_FAILED);
return;
}
@@ -404,8 +379,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
* @param line line of output, NULL at the end
*/
static void
-process_refresh_output (void *cls,
- const char *line)
+process_refresh_output (void *cls, const char *line)
{
struct GNUNET_NAT_MiniHandle *mini = cls;
char pstr[9];
@@ -434,31 +408,28 @@ process_refresh_output (void *cls,
return;
}
if (! mini->did_map)
- return; /* never mapped, won't find our mapping anyway */
+ return; /* never mapped, won't find our mapping anyway */
/* we're looking for output of the form:
* "ExternalIPAddress = 12.134.41.124" */
- s = strstr (line,
- "ExternalIPAddress = ");
+ s = strstr (line, "ExternalIPAddress = ");
if (NULL != s)
{
s += strlen ("ExternalIPAddress = ");
- if (1 != inet_pton (AF_INET,
- s,
- &exip))
- return; /* skip */
+ if (1 != inet_pton (AF_INET, s, &exip))
+ return; /* skip */
if (exip.s_addr == mini->current_addr.sin_addr.s_addr)
- return; /* no change */
+ return; /* no change */
/* update mapping */
mini->ac (mini->ac_cls,
- GNUNET_NO,
+ GNUNET_NO,
(const struct sockaddr *) &mini->current_addr,
sizeof (mini->current_addr),
GNUNET_NAT_ERROR_SUCCESS);
mini->current_addr.sin_addr = exip;
mini->ac (mini->ac_cls,
- GNUNET_YES,
+ GNUNET_YES,
(const struct sockaddr *) &mini->current_addr,
sizeof (mini->current_addr),
GNUNET_NAT_ERROR_SUCCESS);
@@ -475,32 +446,29 @@ process_refresh_output (void *cls,
* "%s TCP PORT->STRING:OURPORT *" or
* "%s UDP PORT->STRING:OURPORT *"
*/
- GNUNET_snprintf (pstr,
- sizeof (pstr),
- ":%u ",
- mini->port);
+ GNUNET_snprintf (pstr, sizeof (pstr), ":%u ", mini->port);
if (NULL == (s = strstr (line, "->")))
- return; /* skip */
+ return; /* skip */
if (NULL == strstr (s, pstr))
- return; /* skip */
- if (1 !=
- SSCANF (line,
- (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s" :
- "%*u UDP %u->%*s:%*u %*s", &nport))
- return; /* skip */
+ return; /* skip */
+ if (1 != sscanf (line,
+ (mini->is_tcp) ? "%*u TCP %u->%*s:%*u %*s"
+ : "%*u UDP %u->%*s:%*u %*s",
+ &nport))
+ return; /* skip */
mini->found = GNUNET_YES;
if (nport == ntohs (mini->current_addr.sin_port))
- return; /* no change */
+ return; /* no change */
/* external port changed, update mapping */
mini->ac (mini->ac_cls,
- GNUNET_NO,
+ GNUNET_NO,
(const struct sockaddr *) &mini->current_addr,
sizeof (mini->current_addr),
GNUNET_NAT_ERROR_SUCCESS);
mini->current_addr.sin_port = htons ((uint16_t) nport);
mini->ac (mini->ac_cls,
- GNUNET_YES,
+ GNUNET_YES,
(const struct sockaddr *) &mini->current_addr,
sizeof (mini->current_addr),
GNUNET_NAT_ERROR_SUCCESS);
@@ -518,10 +486,8 @@ do_refresh (void *cls)
struct GNUNET_NAT_MiniHandle *mini = cls;
int ac;
- mini->refresh_task
- = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
- &do_refresh,
- mini);
+ mini->refresh_task =
+ GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Running `upnpc' to check if our mapping still exists\n");
mini->found = GNUNET_NO;
@@ -540,19 +506,18 @@ do_refresh (void *cls)
mini->refresh_cmd = NULL;
ac = GNUNET_YES;
}
- mini->refresh_cmd
- = GNUNET_OS_command_run (&process_refresh_output,
- mini,
- MAP_TIMEOUT,
- "upnpc",
- "upnpc",
- "-l",
- NULL);
+ mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
+ mini,
+ MAP_TIMEOUT,
+ "upnpc",
+ "upnpc",
+ "-l",
+ NULL);
if (GNUNET_YES == ac)
mini->ac (mini->ac_cls,
GNUNET_SYSERR,
NULL,
- 0,
+ 0,
GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
}
@@ -564,8 +529,7 @@ do_refresh (void *cls)
* @param line line of output, NULL at the end
*/
static void
-process_map_output (void *cls,
- const char *line)
+process_map_output (void *cls, const char *line)
{
struct GNUNET_NAT_MiniHandle *mini = cls;
const char *ipaddr;
@@ -581,13 +545,11 @@ process_map_output (void *cls,
mini->ac (mini->ac_cls,
GNUNET_SYSERR,
NULL,
- 0,
+ 0,
GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
if (NULL == mini->refresh_task)
- mini->refresh_task
- = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
- &do_refresh,
- mini);
+ mini->refresh_task =
+ GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
return;
}
/*
@@ -597,18 +559,16 @@ process_map_output (void *cls,
*/
if ((NULL == (ipaddr = strstr (line, " "))) ||
(NULL == (pstr = strstr (ipaddr, ":"))) ||
- (1 != SSCANF (pstr + 1, "%u", &port)))
+ (1 != sscanf (pstr + 1, "%u", &port)))
{
- return; /* skip line */
+ return; /* skip line */
}
ipa = GNUNET_strdup (ipaddr + 1);
strstr (ipa, ":")[0] = '\0';
- if (1 != inet_pton (AF_INET,
- ipa,
- &mini->current_addr.sin_addr))
+ if (1 != inet_pton (AF_INET, ipa, &mini->current_addr.sin_addr))
{
GNUNET_free (ipa);
- return; /* skip line */
+ return; /* skip line */
}
GNUNET_free (ipa);
@@ -619,7 +579,7 @@ process_map_output (void *cls,
#endif
mini->did_map = GNUNET_YES;
mini->ac (mini->ac_cls,
- GNUNET_YES,
+ GNUNET_YES,
(const struct sockaddr *) &mini->current_addr,
sizeof (mini->current_addr),
GNUNET_NAT_ERROR_SUCCESS);
@@ -647,30 +607,20 @@ GNUNET_NAT_mini_map_start (uint16_t port,
{
struct GNUNET_NAT_MiniHandle *ret;
- if (GNUNET_SYSERR ==
- GNUNET_OS_check_helper_binary ("upnpc",
- GNUNET_NO,
- NULL))
+ if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL))
{
- LOG (GNUNET_ERROR_TYPE_INFO,
- _("`upnpc' command not found\n"));
- ac (ac_cls,
- GNUNET_SYSERR,
- NULL, 0,
- GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
+ LOG (GNUNET_ERROR_TYPE_INFO, _ ("`upnpc' command not found\n"));
+ ac (ac_cls, GNUNET_SYSERR, NULL, 0, GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
return NULL;
}
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Running `upnpc' to install mapping\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Running `upnpc' to install mapping\n");
ret = GNUNET_new (struct GNUNET_NAT_MiniHandle);
ret->ac = ac;
ret->ac_cls = ac_cls;
ret->is_tcp = is_tcp;
ret->port = port;
ret->refresh_task =
- GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
- &do_refresh,
- ret);
+ GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
run_upnpc_r (ret);
return ret;
}
@@ -683,15 +633,13 @@ GNUNET_NAT_mini_map_start (uint16_t port,
* @param line line of output, NULL at the end
*/
static void
-process_unmap_output (void *cls,
- const char *line)
+process_unmap_output (void *cls, const char *line)
{
struct GNUNET_NAT_MiniHandle *mini = cls;
if (NULL == line)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "UPnP unmap done\n");
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n");
GNUNET_OS_command_stop (mini->unmap_cmd);
mini->unmap_cmd = NULL;
GNUNET_free (mini);
@@ -735,7 +683,7 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
return;
}
mini->ac (mini->ac_cls,
- GNUNET_NO,
+ GNUNET_NO,
(const struct sockaddr *) &mini->current_addr,
sizeof (mini->current_addr),
GNUNET_NAT_ERROR_SUCCESS);
@@ -749,16 +697,15 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Unmapping port %u with UPnP\n",
ntohs (mini->current_addr.sin_port));
- mini->unmap_cmd
- = GNUNET_OS_command_run (&process_unmap_output,
- mini,
- UNMAP_TIMEOUT,
- "upnpc",
- "upnpc",
- "-d",
- pstr,
- mini->is_tcp ? "tcp" : "udp",
- NULL);
+ mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
+ mini,
+ UNMAP_TIMEOUT,
+ "upnpc",
+ "upnpc",
+ "-d",
+ pstr,
+ mini->is_tcp ? "tcp" : "udp",
+ NULL);
}