static struct GNUNET_NAT_AutoHandle *ah;
/**
- * Port we advertise.
+ * External hostname and port, if user manually punched
+ * the NAT.
*/
-static unsigned int adv_port;
+static char *hole_external;
/**
* Flag set to 1 if we use IPPROTO_UDP.
*/
static int use_tcp;
+/**
+ * If we do auto-configuration, should we write the result
+ * to a file?
+ */
+static int write_cfg;
+
+/**
+ * Configuration filename.
+ */
+static const char *cfg_file;
+
+/**
+ * Original configuration.
+ */
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
/**
* Protocol to use.
*/
const char *option,
const char *value)
{
+ struct GNUNET_CONFIGURATION_Handle *new_cfg = cls;
+
PRINTF ("%s: %s\n",
option,
value);
+ if (NULL != new_cfg)
+ GNUNET_CONFIGURATION_set_value_string (new_cfg,
+ section,
+ option,
+ value);
}
{
const char *nat_type;
char unknown_type[64];
+ struct GNUNET_CONFIGURATION_Handle *new_cfg;
ah = NULL;
switch (type)
{
- case GNUNET_NAT_TYPE_NO_NAT:
- nat_type = "NO NAT";
- break;
- case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
- nat_type = "NAT but we can traverse";
- break;
- case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
- nat_type = "NAT but STUN is able to identify the correct information";
- break;
- case GNUNET_NAT_TYPE_UPNP_NAT:
- nat_type = "NAT but UPNP opened the ports";
- break;
- default:
- SPRINTF (unknown_type,
- "NAT unknown, type %u",
- type);
- nat_type = unknown_type;
+ case GNUNET_NAT_TYPE_NO_NAT:
+ nat_type = "NO NAT";
+ break;
+ case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
+ nat_type = "NAT but we can traverse";
+ break;
+ case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
+ nat_type = "NAT but STUN is able to identify the correct information";
+ break;
+ case GNUNET_NAT_TYPE_UPNP_NAT:
+ nat_type = "NAT but UPNP opened the ports";
+ break;
+ default:
+ SPRINTF (unknown_type,
+ "NAT unknown, type %u",
+ type);
+ nat_type = unknown_type;
+ break;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+ "NAT status: %s/%s\n",
+ GNUNET_NAT_status2string (result),
+ nat_type);
+
+ /* Shortcut: if there are no changes suggested, bail out early. */
+ if (GNUNET_NO ==
+ GNUNET_CONFIGURATION_is_dirty (diff))
+ {
+ test_finished ();
+ return;
}
- PRINTF ("NAT status: %s/%s\n",
- GNUNET_NAT_status2string (result),
- nat_type);
+ /* Apply diff to original configuration and show changes
+ to the user */
+ new_cfg = write_cfg ? GNUNET_CONFIGURATION_dup (cfg) : NULL;
- PRINTF ("SUGGESTED CHANGES:\n");
- GNUNET_CONFIGURATION_iterate_section_values (diff,
- "nat",
- &auto_conf_iter,
- NULL);
- // FIXME: have option to save config
+ if (NULL != diff)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+ _("Suggested configuration changes:\n"));
+ GNUNET_CONFIGURATION_iterate_section_values (diff,
+ "nat",
+ &auto_conf_iter,
+ new_cfg);
+ }
+
+ /* If desired, write configuration to file; we write only the
+ changes to the defaults to keep things compact. */
+ if ( (write_cfg) &&
+ (NULL != diff) )
+ {
+ struct GNUNET_CONFIGURATION_Handle *def_cfg;
+
+ GNUNET_CONFIGURATION_set_value_string (new_cfg,
+ "ARM",
+ "CONFIG",
+ NULL);
+ def_cfg = GNUNET_CONFIGURATION_create ();
+ GNUNET_break (GNUNET_OK ==
+ GNUNET_CONFIGURATION_load (def_cfg,
+ NULL));
+ if (GNUNET_OK !=
+ GNUNET_CONFIGURATION_write_diffs (def_cfg,
+ new_cfg,
+ cfg_file))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+ _("Failed to write configuration to `%s'\n"),
+ cfg_file);
+ global_ret = 1;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+ _("Wrote updated configuration to `%s'\n"),
+ cfg_file);
+ }
+ GNUNET_CONFIGURATION_destroy (def_cfg);
+ }
+
+ if (NULL != new_cfg)
+ GNUNET_CONFIGURATION_destroy (new_cfg);
test_finished ();
}
* reversal.
*
* @param cls closure, NULL
- * @param local_addr address where we received the request
- * @param local_addrlen actual length of the @a local_addr
* @param remote_addr public IP address of the other peer
* @param remote_addrlen actual length of the @a remote_addr
*/
static void
reversal_cb (void *cls,
- const struct sockaddr *local_addr,
- socklen_t local_addrlen,
const struct sockaddr *remote_addr,
socklen_t remote_addrlen)
{
struct sockaddr_in extern_sa;
struct sockaddr *local_sa;
struct sockaddr *remote_sa;
- size_t local_len;
+ socklen_t local_len;
size_t remote_len;
+
+ cfg_file = cfgfile;
+ cfg = c;
if (use_tcp && use_udp)
{
proto = IPPROTO_TCP;
if (use_udp)
proto = IPPROTO_UDP;
+
+ GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+ NULL);
+
+ if (do_auto)
+ {
+ ah = GNUNET_NAT_autoconfig_start (c,
+ &auto_config_cb,
+ NULL);
+ }
+
if (0 == proto)
{
+ if (do_auto)
+ return; /* all good, we just run auto config */
GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
"Must specify either TCP or UDP\n");
global_ret = 1;
}
if (NULL != local_addr)
{
- local_len = 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,
{
nh = GNUNET_NAT_register (c,
proto,
- (uint16_t) adv_port,
+ hole_external,
1,
(const struct sockaddr **) &local_sa,
&local_len,
(listen_reversal) ? &reversal_cb : NULL,
NULL);
}
-
- GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
- NULL);
+ else if (listen_reversal)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+ "Use of `-W` only effective in combination with `-i`\n");
+ global_ret = 1;
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
if (NULL != remote_addr)
{
}
}
- if (do_auto)
- {
- ah = GNUNET_NAT_autoconfig_start (c,
- &auto_config_cb,
- NULL);
- }
-
if (do_stun)
{
if (NULL == local_addr)
{'e', "external", "ADDRESS",
gettext_noop ("which external IP and port should be used to test"),
GNUNET_YES, &GNUNET_GETOPT_set_string, &extern_addr },
- {'l', "local", "ADDRESS",
- gettext_noop ("which IP and port are we locally using to listen to for connection reversals"),
+ {'i', "in", "ADDRESS",
+ gettext_noop ("which IP and port are we locally using to bind/listen to"),
GNUNET_YES, &GNUNET_GETOPT_set_string, &local_addr },
{'r', "remote", "ADDRESS",
gettext_noop ("which remote IP and port should be asked for connection reversal"),
GNUNET_YES, &GNUNET_GETOPT_set_string, &remote_addr },
- {'L', "listen", NULL,
- gettext_noop ("listen for connection reversal requests"),
- GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
- {'p', "port", NULL,
- gettext_noop ("port to use to advertise"),
- GNUNET_YES, &GNUNET_GETOPT_set_uint, &adv_port },
+ {'p', "punched", NULL,
+ gettext_noop ("external hostname and port of NAT, if punched manually; use AUTO for hostname for automatic determination of the external IP"),
+ GNUNET_YES, &GNUNET_GETOPT_set_string, &hole_external },
{'s', "stun", NULL,
gettext_noop ("enable STUN processing"),
GNUNET_NO, &GNUNET_GETOPT_set_one, &do_stun },
{'u', "udp", NULL,
gettext_noop ("use UDP"),
GNUNET_NO, &GNUNET_GETOPT_set_one, &use_udp },
+ {'w', "write", NULL,
+ gettext_noop ("write configuration file (for autoconfiguration)"),
+ GNUNET_NO, &GNUNET_GETOPT_set_one, &write_cfg },
+ {'W', "watch", NULL,
+ gettext_noop ("watch for connection reversal requests"),
+ GNUNET_NO, &GNUNET_GETOPT_set_one, &listen_reversal },
GNUNET_GETOPT_OPTION_END
};