/*
This file is part of GNUnet.
- (C) 2012-2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2012-2013 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
* @file gnunet-gns.c
*/
static struct GNUNET_GNS_Handle *gns;
+/**
+ * Desired timeout for the lookup (default is no timeout).
+ */
+static struct GNUNET_TIME_Relative timeout;
+
/**
* GNS name to lookup. (-u option)
*/
*/
static char *public_key;
+/**
+ * Reverse key
+ */
+static char *reverse_key;
+
+/**
+ * Reverse key
+ */
+static struct GNUNET_CRYPTO_EcdsaPublicKey rkey;
+
+/**
+ * Set to GNUNET_GNS_LO_LOCAL_MASTER if we are looking up in the master zone.
+ */
+static enum GNUNET_GNS_LocalOptions local_options;
+
/**
* raw output
*/
*/
static struct GNUNET_GNS_LookupRequest *lookup_request;
+/**
+ * Handle to reverse lookup request
+ */
+static struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_request;
+
/**
* Lookup an ego with the identity service.
*/
*/
static struct GNUNET_IDENTITY_Operation *id_op;
+/**
+ * Task scheduled to handle timeout.
+ */
+static struct GNUNET_SCHEDULER_Task *tt;
+
/**
* Task run on shutdown. Cleans up everything.
*
* @param cls unused
- * @param tc scheduler context
*/
static void
-do_shutdown (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls)
{
if (NULL != el)
{
GNUNET_GNS_disconnect (gns);
gns = NULL;
}
+ if (NULL != tt)
+ {
+ GNUNET_SCHEDULER_cancel (tt);
+ tt = NULL;
+ }
}
+/**
+ * Task run on timeout. Triggers shutdown.
+ *
+ * @param cls unused
+ */
+static void
+do_timeout (void *cls)
+{
+ tt = NULL;
+ GNUNET_SCHEDULER_shutdown ();
+}
+
+static void
+process_reverse_result (void *cls,
+ const char *name)
+{
+ rev_lookup_request = NULL;
+ if (NULL == name)
+ {
+ printf ("No name found.\n");
+ return;
+ }
+ if (raw)
+ printf ("%s\n", name);
+ else
+ printf ("%s is known as %s\n",
+ reverse_key,
+ name);
+ GNUNET_SCHEDULER_shutdown ();
+}
+
/**
* Function called with the result of a GNS lookup.
*
rtype = GNUNET_GNSRECORD_typename_to_number (lookup_type);
else
rtype = GNUNET_DNSPARSER_TYPE_A;
+ if (UINT32_MAX == rtype)
+ {
+ fprintf (stderr,
+ _("Invalid typename specified, assuming `ANY'\n"));
+ rtype = GNUNET_GNSRECORD_TYPE_ANY;
+ }
if (NULL != lookup_name)
{
lookup_name,
pkey,
rtype,
- GNUNET_NO, /* Use DHT */
+ local_options,
shorten_key,
&process_lookup_result,
lookup_name);
}
+ else if (NULL != reverse_key)
+ {
+ rev_lookup_request = GNUNET_GNS_reverse_lookup (gns,
+ &rkey,
+ pkey,
+ &process_reverse_result,
+ NULL);
+ }
else
{
fprintf (stderr,
const char *name)
{
struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
+ const char *dot;
id_op = NULL;
if (NULL == ego)
return;
}
GNUNET_IDENTITY_ego_get_public_key (ego, &pkey);
+ /* main name is our own master zone, do no look for that in the DHT */
+ local_options = GNUNET_GNS_LO_LOCAL_MASTER;
+
+ /* if the name is of the form 'label.gnu', never go to the DHT */
+ dot = NULL;
+ if (NULL != lookup_name)
+ dot = strchr (lookup_name, '.');
+ if ( (NULL != dot) &&
+ (0 == strcasecmp (dot, ".gnu")) )
+ local_options = GNUNET_GNS_LO_NO_DHT;
lookup_with_public_key (&pkey);
}
* @param c configuration
*/
static void
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+ char *const *args,
+ const char *cfgfile,
const struct GNUNET_CONFIGURATION_Handle *c)
{
struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
_("Failed to connect to GNS\n"));
return;
}
- GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
- &do_shutdown, NULL);
+ tt = GNUNET_SCHEDULER_add_delayed (timeout,
+ &do_timeout, NULL);
+ GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
+ if (NULL != reverse_key)
+ {
+ if (GNUNET_OK !=
+ GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_key,
+ strlen (reverse_key),
+ &rkey))
+ {
+ fprintf (stderr,
+ _("Reverse key `%s' is not well-formed\n"),
+ reverse_key);
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ }
if (NULL != public_key)
{
if (GNUNET_OK !=
- GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
- strlen (public_key),
- &pkey))
+ GNUNET_CRYPTO_ecdsa_public_key_from_string (public_key,
+ strlen (public_key),
+ &pkey))
{
fprintf (stderr,
- _("Public key `%s' is not well-formed\n"),
- public_key);
+ _("Public key `%s' is not well-formed\n"),
+ public_key);
GNUNET_SCHEDULER_shutdown ();
return;
}
lookup_with_public_key (&pkey);
return;
}
+ if (NULL != reverse_key)
+ {
+ if (GNUNET_OK !=
+ GNUNET_CRYPTO_ecdsa_public_key_from_string (reverse_key,
+ strlen (reverse_key),
+ &rkey))
+ {
+ fprintf (stderr,
+ _("Reverse key `%s' is not well-formed\n"),
+ reverse_key);
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ }
if (NULL != zone_ego_name)
{
el = GNUNET_IDENTITY_ego_lookup (cfg,
- zone_ego_name,
- &identity_zone_cb,
- NULL);
+ zone_ego_name,
+ &identity_zone_cb,
+ NULL);
return;
}
if ( (NULL != lookup_name) &&
(strlen (lookup_name) > 4) &&
(0 == strcmp (".zkey",
- &lookup_name[strlen (lookup_name) - 4])) )
+ &lookup_name[strlen (lookup_name) - 4])) )
{
/* no zone required, use 'anonymous' zone */
GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
- &pkey);
+ &pkey);
lookup_with_public_key (&pkey);
}
else
{
GNUNET_break (NULL == id_op);
id_op = GNUNET_IDENTITY_get (identity,
- "gns-master",
- &identity_master_cb,
- NULL);
+ "gns-master",
+ &identity_master_cb,
+ NULL);
GNUNET_assert (NULL != id_op);
}
}
{'t', "type", "TYPE",
gettext_noop ("Specify the type of the record to lookup"), 1,
&GNUNET_GETOPT_set_string, &lookup_type},
+ { 'T', "timeout", "DELAY",
+ gettext_noop ("Specify timeout for the lookup"), 1,
+ &GNUNET_GETOPT_set_relative_time, &timeout },
{'r', "raw", NULL,
gettext_noop ("No unneeded output"), 0,
&GNUNET_GETOPT_set_one, &raw},
{'z', "zone", "NAME",
gettext_noop ("Specify the name of the ego of the zone to lookup the record in"), 1,
&GNUNET_GETOPT_set_string, &zone_ego_name},
+ {'R', "reverse", "PKEY",
+ gettext_noop ("Specify the public key of the zone to reverse lookup a name for"), 1,
+ &GNUNET_GETOPT_set_string, &reverse_key},
GNUNET_GETOPT_OPTION_END
};
int ret;
+ timeout = GNUNET_TIME_UNIT_FOREVER_REL;
if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
return 2;
GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
ret =
- (GNUNET_OK ==
- GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
- _("GNUnet GNS resolver tool"),
- options,
- &run, NULL)) ? 0 : 1;
+ (GNUNET_OK ==
+ GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
+ _("GNUnet GNS resolver tool"),
+ options,
+ &run, NULL)) ? 0 : 1;
GNUNET_free ((void*) argv);
return ret;
}