summaryrefslogtreecommitdiff
path: root/src/nat-auto
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-01-10 18:50:16 +0100
committerChristian Grothoff <christian@grothoff.org>2017-01-10 18:50:16 +0100
commit07c8655e1ee8aa603def55b018222f3244235d1e (patch)
tree705267efc5a1628532ada0786955ab32dfeff0dc /src/nat-auto
parentea5a38ca2537d0892049dc3ce64576d2a9bafa97 (diff)
fix misc compilation issues in nat-auto
Diffstat (limited to 'src/nat-auto')
-rw-r--r--src/nat-auto/gnunet-nat-auto.c71
-rw-r--r--src/nat-auto/nat_auto_api_test.c120
2 files changed, 72 insertions, 119 deletions
diff --git a/src/nat-auto/gnunet-nat-auto.c b/src/nat-auto/gnunet-nat-auto.c
index d411c6d75..9ba81eb5f 100644
--- a/src/nat-auto/gnunet-nat-auto.c
+++ b/src/nat-auto/gnunet-nat-auto.c
@@ -47,7 +47,7 @@ static int write_cfg;
/**
* Configuration filename.
- */
+ */
static const char *cfg_file;
/**
@@ -56,16 +56,9 @@ static const char *cfg_file;
static const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
- * Address we are bound to (in test), or should bind to
- * (if #do_stun is set).
- */
-static char *bind_addr;
-
-/**
- * External IP address and port to use for the test.
- * If not set, use #bind_addr.
+ * Adapter we are supposed to test.
*/
-static char *extern_addr;
+static char *section_name;
/**
* Should we run autoconfiguration?
@@ -122,7 +115,7 @@ auto_conf_iter (void *cls,
const char *value)
{
struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
-
+
PRINTF ("%s: %s\n",
option,
value);
@@ -192,7 +185,7 @@ auto_config_cb (void *cls,
/* Apply diff to original configuration and show changes
to the user */
new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
-
+
if (NULL != diff)
{
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
@@ -296,12 +289,9 @@ run (void *cls,
const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *c)
{
- struct sockaddr_in bind_sa;
- struct sockaddr_in extern_sa;
-
cfg_file = cfgfile;
cfg = c;
-
+
GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
NULL);
@@ -327,45 +317,11 @@ run (void *cls,
if (use_udp)
proto = IPPROTO_UDP;
- if (NULL != bind_addr)
- {
- if (GNUNET_OK !=
- GNUNET_STRINGS_to_address_ipv4 (bind_addr,
- strlen (bind_addr),
- &bind_sa))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Invalid socket address `%s'\n",
- bind_addr);
- global_ret = 1;
- return;
- }
- }
- if (NULL != extern_addr)
- {
- if (GNUNET_OK !=
- GNUNET_STRINGS_to_address_ipv4 (extern_addr,
- strlen (extern_addr),
- &extern_sa))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
- "Invalid socket address `%s'\n",
- extern_addr);
- global_ret = 1;
- return;
- }
- }
-
- if (NULL != bind_addr)
+ if (NULL != section_name)
{
- if (NULL == extern_addr)
- extern_sa = bind_sa;
nt = GNUNET_NAT_AUTO_test_start (c,
proto,
- bind_sa.sin_addr,
- ntohs (bind_sa.sin_port),
- extern_sa.sin_addr,
- ntohs (extern_sa.sin_port),
+ section_name,
&test_report_cb,
NULL);
}
@@ -374,7 +330,7 @@ run (void *cls,
/**
- * Main function of gnunet-nat
+ * Main function of gnunet-nat-auto
*
* @param argc number of command-line arguments
* @param argv command line
@@ -388,12 +344,9 @@ main (int argc,
{'a', "auto", NULL,
gettext_noop ("run autoconfiguration"),
GNUNET_NO, &GNUNET_GETOPT_set_one, &do_auto },
- {'b', "bind", "ADDRESS",
- gettext_noop ("which IP and port are we bound to"),
- GNUNET_YES, &GNUNET_GETOPT_set_string, &bind_addr },
- {'e', "external", "ADDRESS",
- gettext_noop ("which external IP and port should be used to test"),
- GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
+ {'S', "section", "NAME",
+ gettext_noop ("section name providing the configuration for the adapter"),
+ GNUNET_YES, &GNUNET_GETOPT_set_string, &section_name },
{'t', "tcp", NULL,
gettext_noop ("use TCP"),
GNUNET_NO, &GNUNET_GETOPT_set_one, &use_tcp },
diff --git a/src/nat-auto/nat_auto_api_test.c b/src/nat-auto/nat_auto_api_test.c
index 8359e3cb5..fb2bcd679 100644
--- a/src/nat-auto/nat_auto_api_test.c
+++ b/src/nat-auto/nat_auto_api_test.c
@@ -24,7 +24,8 @@
*/
#include "platform.h"
#include "gnunet_util_lib.h"
-#include "gnunet_nat_lib.h"
+#include "gnunet_nat_service.h"
+#include "gnunet_nat_auto_service.h"
#include "nat-auto.h"
#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
@@ -153,19 +154,19 @@ struct GNUNET_NAT_AUTO_Test
struct GNUNET_SCHEDULER_Task *ttask;
/**
- * #GNUNET_YES if we're testing TCP
+ * Section name of plugin to test.
*/
- int is_tcp;
+ char *section_name;
/**
- * Data that should be transmitted or source-port.
+ * IPPROTO_TCP or IPPROTO_UDP.
*/
- uint16_t data;
+ int proto;
/**
- * Advertised port to the other peer.
+ * Data that should be transmitted or source-port.
*/
- uint16_t adv_port;
+ uint16_t data;
/**
* Status code to be reported to the timeout/status call
@@ -358,12 +359,14 @@ mq_error_handler (void *cls,
* @param cls closure
* @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
* the previous (now invalid) one
+ * @param ac address class the address belongs to
* @param addr either the previous or the new public IP address
* @param addrlen actual length of the @a addr
*/
static void
addr_cb (void *cls,
int add_remove,
+ enum GNUNET_NAT_AddressClass ac,
const struct sockaddr *addr,
socklen_t addrlen)
{
@@ -411,58 +414,50 @@ addr_cb (void *cls,
msg->dst_ipv4 = sa->sin_addr.s_addr;
msg->dport = sa->sin_port;
msg->data = h->data;
- msg->is_tcp = htonl ((uint32_t) h->is_tcp);
+ msg->is_tcp = htonl ((uint32_t) (h->proto == IPPROTO_TCP));
GNUNET_MQ_send (ca->mq,
env);
}
/**
- * Timeout task for a nat test.
- * Calls the report-callback with a timeout return value
+ * Calls the report-callback reporting failure.
*
* Destroys the nat handle after the callback has been processed.
*
* @param cls handle to the timed out NAT test
*/
static void
-do_timeout (void *cls)
+do_fail (void *cls)
{
struct GNUNET_NAT_AUTO_Test *nh = cls;
nh->ttask = NULL;
nh->report (nh->report_cls,
- (GNUNET_NAT_ERROR_SUCCESS == nh->status)
- ? GNUNET_NAT_ERROR_TIMEOUT
- : nh->status);
+ nh->status);
}
/**
- * Start testing if NAT traversal works using the
- * given configuration (IPv4-only).
- *
- * ALL failures are reported directly to the report callback
+ * Start testing if NAT traversal works using the given configuration.
+ * The transport adapters should be down while using this function.
*
* @param cfg configuration for the NAT traversal
- * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
- * @param bnd_port port to bind to, 0 for connection reversal
- * @param adv_port externally advertised port to use
- * @param timeout delay after which the test should be aborted
+ * @param proto protocol to test, i.e. IPPROTO_TCP or IPPROTO_UDP
+ * @param section_name configuration section to use for configuration
* @param report function to call with the result of the test
* @param report_cls closure for @a report
- * @return handle to cancel NAT test or NULL. The error is always indicated via the report callback
+ * @return handle to cancel NAT test
*/
struct GNUNET_NAT_AUTO_Test *
GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
- int is_tcp,
- uint16_t bnd_port,
- uint16_t adv_port,
- struct GNUNET_TIME_Relative timeout,
- GNUNET_NAT_TestCallback report,
- void *report_cls)
+ uint8_t proto,
+ const char *section_name,
+ GNUNET_NAT_TestCallback report,
+ void *report_cls)
{
struct GNUNET_NAT_AUTO_Test *nh;
+ unsigned long long bnd_port;
struct sockaddr_in sa;
const struct sockaddr *addrs[] = {
(const struct sockaddr *) &sa
@@ -471,18 +466,30 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
sizeof (sa)
};
+ if ( (GNUNET_OK !=
+ GNUNET_CONFIGURATION_get_value_number (cfg,
+ section_name,
+ "PORT",
+ &bnd_port)) ||
+ (bnd_port > 65535) )
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Failed to find valid PORT in section `%s'\n"),
+ section_name);
+ return NULL;
+ }
+
memset (&sa, 0, sizeof (sa));
sa.sin_family = AF_INET;
- sa.sin_port = htons (bnd_port);
+ sa.sin_port = htons ((uint16_t) bnd_port);
#if HAVE_SOCKADDR_IN_SIN_LEN
sa.sin_len = sizeof (sa);
#endif
nh = GNUNET_new (struct GNUNET_NAT_AUTO_Test);
nh->cfg = cfg;
- nh->is_tcp = is_tcp;
- nh->data = bnd_port;
- nh->adv_port = adv_port;
+ nh->proto = proto;
+ nh->section_name = GNUNET_strdup (section_name);
nh->report = report;
nh->report_cls = report_cls;
nh->status = GNUNET_NAT_ERROR_SUCCESS;
@@ -490,28 +497,24 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
{
nh->nat
= GNUNET_NAT_register (cfg,
- is_tcp,
- 0,
- 0,
- NULL,
- NULL,
+ section_name,
+ proto,
+ 0, NULL, NULL,
&addr_cb,
&reversal_cb,
- nh,
- NULL);
+ nh);
}
else
{
- nh->lsock =
- GNUNET_NETWORK_socket_create (AF_INET,
- (is_tcp ==
- GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
+ nh->lsock
+ = GNUNET_NETWORK_socket_create (AF_INET,
+ proto,
0);
- if ((nh->lsock == NULL) ||
- (GNUNET_OK !=
- GNUNET_NETWORK_socket_bind (nh->lsock,
- (const struct sockaddr *) &sa,
- sizeof (sa))))
+ if ( (NULL == nh->lsock) ||
+ (GNUNET_OK !=
+ GNUNET_NETWORK_socket_bind (nh->lsock,
+ (const struct sockaddr *) &sa,
+ sizeof (sa))))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
@@ -524,11 +527,11 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
nh->lsock = NULL;
}
nh->status = GNUNET_NAT_ERROR_INTERNAL_NETWORK_ERROR;
- nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout,
+ nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
nh);
return nh;
}
- if (GNUNET_YES == is_tcp)
+ if (IPPROTO_TCP == proto)
{
GNUNET_break (GNUNET_OK ==
GNUNET_NETWORK_socket_listen (nh->lsock,
@@ -550,17 +553,16 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
LOG (GNUNET_ERROR_TYPE_INFO,
"NAT test listens on port %u (%s)\n",
bnd_port,
- (GNUNET_YES == is_tcp) ? "tcp" : "udp");
+ (IPPROTO_TCP == proto) ? "tcp" : "udp");
nh->nat = GNUNET_NAT_register (cfg,
- is_tcp,
- adv_port,
+ section_name,
+ proto,
1,
addrs,
addrlens,
&addr_cb,
NULL,
- nh,
- NULL);
+ nh);
if (NULL == nh->nat)
{
LOG (GNUNET_ERROR_TYPE_INFO,
@@ -576,14 +578,11 @@ GNUNET_NAT_AUTO_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
nh->lsock = NULL;
}
nh->status = GNUNET_NAT_ERROR_NAT_REGISTER_FAILED;
- nh->ttask = GNUNET_SCHEDULER_add_now (&do_timeout,
+ nh->ttask = GNUNET_SCHEDULER_add_now (&do_fail,
nh);
return nh;
}
}
- nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout,
- &do_timeout,
- nh);
return nh;
}
@@ -638,6 +637,7 @@ GNUNET_NAT_AUTO_test_stop (struct GNUNET_NAT_AUTO_Test *tst)
GNUNET_NAT_unregister (tst->nat);
tst->nat = NULL;
}
+ GNUNET_free (tst->section_name);
GNUNET_free (tst);
}