fix uninit variable causing crash on 0 value
[oweals/gnunet.git] / src / gns / gnunet-gns.c
index 7951eb5693434ddf23a3967bda9ba90020bc8e72..17fe4cbdae425b2ac6335df5bf7f6800c4ba6d15 100644 (file)
@@ -1,6 +1,6 @@
 /*
      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
@@ -14,8 +14,8 @@
 
      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
@@ -66,9 +66,19 @@ static char *zone_ego_name;
 static char *public_key;
 
 /**
- * Set to #GNUNET_YES if we must not use the DHT (only local lookup).
+ * Reverse key
  */
-static int only_cached;
+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
@@ -85,6 +95,11 @@ static int rtype;
  */
 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.
  */
@@ -100,16 +115,19 @@ static struct GNUNET_IDENTITY_Handle *identity;
  */
 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)
   {
@@ -136,9 +154,45 @@ do_shutdown (void *cls,
     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.
  *
@@ -209,6 +263,12 @@ lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
     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)
   {
@@ -216,11 +276,19 @@ lookup_with_keys (const struct GNUNET_CRYPTO_EcdsaPublicKey *pkey,
                                        lookup_name,
                                        pkey,
                                        rtype,
-                                       only_cached,
+                                       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,
@@ -349,13 +417,16 @@ identity_master_cb (void *cls,
     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")) )
-    only_cached = GNUNET_YES;
+    local_options = GNUNET_GNS_LO_NO_DHT;
   lookup_with_public_key (&pkey);
 }
 
@@ -369,7 +440,9 @@ identity_master_cb (void *cls,
  * @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;
@@ -383,49 +456,78 @@ run (void *cls, char *const *args, const char *cfgfile,
             _("Failed to connect to GNS\n"));
     return;
   }
-  GNUNET_SCHEDULER_add_delayed (timeout,
-                               &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);
   }
 }
@@ -460,6 +562,9 @@ main (int argc, char *const *argv)
     {'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;
@@ -470,11 +575,11 @@ main (int argc, char *const *argv)
 
   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;
 }